谁能帮忙写一个c语言连连看游戏,要4x4的,功能最简单的就可以!但是要实现连连看应该有的功能
3个回答
展开全部
刚写的,新鲜出炉:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define MAPSIZE 4
#define MAXLINESIZE 60
//typedef enum{false,true} bool;
typedef struct{
int x,y;
}Point;
const char pictureTable[]={" ABCEDFGHI"};
bool judgeLine(char **MP,Point *start,Point *end){
int i;
if(start->x==end->x){
if(start->y > end->y){
for(i = start->y - 1 ; i > end->y ; i--)
if(MP[start->x][i]!=0)
return false;
return true;
}
else{
for(i = start->y + 1 ; i < end->y ; i++)
if(MP[start->x][i]!=0)
return false;
return true;
}
}
else if(start->y==end->y){
if(start->x > end->x ){
for(i = start->x - 1 ; i > end->x ; i--)
if(MP[i][start->y]!=0)
return false;
return true;
}
else{
for(i = start->x + 1 ; i < end->x ; i++)
if(MP[i][start->y]!=0)
return false;
return true;
}
}
return false;
}
bool judgeTwoLines(char **MP,Point *start,Point *end,Point *mid){
Point p1,p2;
mid->x=-1;
mid->y=-1;
if(judgeLine(MP,start,end)==true) return true;
p1.x=start->x;
p1.y=end->y;
p2.x=end->x;
p2.y=start->y;
mid->x=p1.x;
mid->y=p1.y;
if(MP[p1.x][p1.y]==0 && judgeLine(MP,start,&p1) && judgeLine(MP,end,&p1)) return true;
mid->x=p2.x;
mid->y=p2.y;
if(MP[p2.x][p2.y]==0 && judgeLine(MP,start,&p2) && judgeLine(MP,end,&p2)) return true;
return false;
}
bool judgeTreeLines(char **MP,Point *start,Point *end,Point *mid1,Point *mid2,int n){
int i;
mid1->x=-1;mid1->y=-1;
mid2->x=-1;mid2->y=-1;
if(judgeTwoLines(MP,start,end,mid1)) return true;
for( i=start->x - 1;i>=0;i--){
if(MP[i][start->y]!=0) break;
mid1->x=i;
mid1->y=start->y;
if(judgeTwoLines(MP,mid1,end,mid2)) return true;
}
for( i=start->x + 1;i<=n+1;i++){
if(MP[i][start->y]!=0) break;
mid1->x=i;
mid1->y=start->y;
if(judgeTwoLines(MP,mid1,end,mid2)) return true;
}
for( i=start->y - 1;i>=0;i--){
if(MP[start->x][i]!=0) break;
mid1->x=start->x;
mid1->y=i;
if(judgeTwoLines(MP,mid1,end,mid2)) return true;
}
for( i=start->y + 1;i<=n+1;i++){
if(MP[start->x][i]!=0) break;
mid1->x=start->x;
mid1->y=i;
if(judgeTwoLines(MP,mid1,end,mid2)) return true;
}
return false;
}
void ptMap(char **MP,int n){
int space=(MAXLINESIZE-n*2)/2;
int i,j;
for(i=0;i<(MAXLINESIZE-10)/2;i++)
printf(" ");
printf("《连连看》\n");
for(i=2;i<space;i++) printf(" ");
printf("x\n");
for(i=1;i<=n;i++){
for(j=2;j<space;j++)
printf(" ");
printf("%d ",i);
for(j=1;j<=n;j++)
printf("%c ",pictureTable[MP[i][j]]);
printf("\n");
}
for(i=0;i<space;i++)
printf("*");
for(i=0;i<n;i++)
printf("%d*",i+1);
for(i=1;i<space;i++)
printf("*");
printf("\n");
}
char **createMap(int n){
char **ret;
int i;
ret=(char**)malloc(sizeof(char*)*(n+2));
for(i=0;i<n+2;i++)
ret[i]=(char*)malloc(sizeof(char)*(n+2));
return ret;
}
void ranMap(char **MP,int n){
int *all=(int*)malloc(sizeof(int)*n*n);
int i,tmpi,tmp;
for(i=0;i<n*n;i++)
all[i]=i/4+1;
for(i=0;i<n*n;i++){
tmpi=rand()%(n*n-i);
tmp=all[tmpi];
all[tmpi]=all[n*n-i-1];
all[n*n-i-1]=tmp;
}
for(i=0;i<n+2;i++){
MP[0][i]=0;
MP[n+1][i]=0;
MP[i][0]=0;
MP[i][n+1]=0;
}
tmpi=0;
for(i=1;i<=n;i++)
for(tmp=1;tmp<=n;tmp++)
MP[i][tmp]=all[tmpi++];
}
void deletePoints(char **MP,Point *p1,Point *p2){
MP[p1->x][p1->y]=0;
MP[p2->x][p2->y]=0;
}
int playTurns(int n){
int rest=n*n;
char **mp=createMap(n),c;
ranMap(mp,n);
Point mid1,mid2,pt1,pt2;
while(1){
ptMap(mp,n);
printf("请输入消去的坐标1(x1 y1):\n");
scanf("%d%d",&pt1.x,&pt1.y);
printf("请输入消去的坐标2(x2 y2):\n");
scanf("%d%d",&pt2.x,&pt2.y);
if((pt1.x==pt2.x && pt1.y==pt2.y) || (pt1.x<1 || pt1.x>n || pt2.x < 1 || pt2.x > n || pt1.y<1 || pt1.y>n || pt2.y < 1 || pt2.y > n)){
printf("无法消除这两图案,请再次检查。");
}
else if(mp[pt1.x][pt1.y]!=0 && mp[pt1.x][pt1.y]==mp[pt2.x][pt2.y] && judgeTreeLines(mp,&pt1,&pt2,&mid1,&mid2,n)){
if(mid1.x==-1){
printf("Direct\n");
}
else if(mid2.x==-1){
printf("TwoLines :(%d,%d)\n",mid1.x,mid1.y);
}
else{
printf("TreeLines:(%d,%d)(%d,%d)\n",mid1.x,mid1.y,mid2.x,mid2.y);
}
deletePoints(mp,&pt1,&pt2);
printf("消去成功!\n");
rest-=2;
if(rest==0){
printf("恭喜!你已消去所有图案!\n");
break;
}
}
else{
printf("无法消除这两图案,请再次检查。");
}
printf("继续游戏(N/n不继续)?");
scanf(" %c",&c);
if(c=='N' || c=='n') break;
}
printf("是否重新开局(Y/y继续)?");
scanf(" %c",&c);
if(c=='y' || c=='Y') return 1;
return 0;
}
int main(){
srand(time(0));
while(playTurns(4));
return 0;
}
展开全部
////
// 简单连连看游戏
//
//
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROW 4 //行数目
#define COL 4
#define ICO_NUM 5
//游戏图标
const char ico[ICO_NUM] = "@#$& ";
char games[ROW][COL] = {0};
//初始化数据
void init_game(void)
{
int i, j;
for(i = 0; i < ROW; i++)
{
for(j = 0; j < COL; j++)
{
games[i][j] = rand() % (ICO_NUM - 1); //不能为空白
if()
}
}
}
//绘制游戏
void draw_game(void)
{
int i, j;
printf("(连连看)\n");
printf("=x= =0==1==2==3==4==5==6==7==8==9=\n");
for(i = 0; i < ROW; i++)
{
printf("=%d= ", i);
for(j = 0; j < COL; j++)
{
printf(" %c ", ico[games[i][j]]);
}
printf("\n");
}
printf("== y=0==1==2==3==4==5==6==7==8==9=\n");
}
//运行游戏
void run_game(void)
{
//坐标从0开始
int x1 = -1, y1 = -1,
x2 = -1, y2 = -1;
int num = 0;
char c = 0;
while(1)
{
//初始化数据
init_game();
while(1)
{
//绘制游戏
draw_game();
while(1)
{
printf("请输入要消除的坐标1(x1 y1):\n");
scanf("%d%d", &x1, &y1);
if(x1 < 0 || x1 >= ROW)
{
printf("x1输入有误,请从新输入:(0 <= x1 <= %d)\n",
ROW);
}
else if(y1 < 0 || y1 >= COL)
{
printf("y1输入有误,请从新输入:(0 <= y1 <= %d)\n",
COL);
}
else
{
printf(" (%d, %d):%c \n",x1, y1, ico[games[x1][y1]]);
break;
}
}
while(1)
{
printf("请输入要消除的坐标2(x2 y2):\n");
scanf("%d%d", &x2, &y2);
if(x2 < 0 || x2 >= ROW)
{
printf("x2输入有误,请从新输入:(0 <= x2 <= %d)\n",
ROW);
}
else if(y2 < 0 || y2 >= COL)
{
printf("y2输入有误,请从新输入:(0 <= y2 <= %d)\n",
COL);
}
else
{
printf(" (%d, %d):%c \n",x2, y2, ico[games[x2][y2]]);
break;
}
}
if(games[x1][y1] == games[x2][y2])
{
printf("%c <---消除成功--->%c\n", ico[games[x1][y1]],
ico[games[x2][y2]]);
games[x1][y1] = ICO_NUM - 1;
games[x2][y2] = ICO_NUM - 1;
num++;
if(num >= (ROW * COL)/2)
{
printf("恭喜你获得胜利~\n");
break;
}
}
else
{
printf("%c <---消除失败!--->%c\n", ico[games[x1][y1]],
ico[games[x2][y2]]);
}
}
printf("是否再来一局?(Y/N)\n");
scanf("%c", &c);
if(c == 'N' || c == 'n')
break;
}
}
int main(void)
{
//运行游戏
run_game();
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-01-05
展开全部
// Check if the write concern could not be fulfilled
if ($writeConcernError = $result->getWriteConcernError()) {
printf("%s (%d): %s\n",
$writeConcernError->getMessage(),
$writeConcernError->getCode(),
var_export($writeConcernError->getInfo(), true)
);
}
// Check if any write operations did not complete at all
foreach ($result->getWriteErrors() as $writeError) {
printf("Operation#%d: %s (%d)\n",
$writeError->getIndex(),
$writeError->getMessage(),
$writeError->getCode()
);
}
} catch (MongoDB\Driver\Exception\Exception $e) {
printf("Other error: %s\n", $e->getMessage());
exit;
}
if ($writeConcernError = $result->getWriteConcernError()) {
printf("%s (%d): %s\n",
$writeConcernError->getMessage(),
$writeConcernError->getCode(),
var_export($writeConcernError->getInfo(), true)
);
}
// Check if any write operations did not complete at all
foreach ($result->getWriteErrors() as $writeError) {
printf("Operation#%d: %s (%d)\n",
$writeError->getIndex(),
$writeError->getMessage(),
$writeError->getCode()
);
}
} catch (MongoDB\Driver\Exception\Exception $e) {
printf("Other error: %s\n", $e->getMessage());
exit;
}
追问
要c语言
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询