本人写的2048的C语言小程序,可是就是编译不了,求赐教

C语言 码拜 8年前 (2016-06-05) 1087次浏览
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
int code[4][4]={0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0};/*游戏中的16个格子*/
int temp[5];/*中间变量*/
int move=0;/*移动次数*/
int score=0;/*分数*/
void print(void)/*显示游戏界面*/
{
int i,j;
printf(“2048\n”);
printf(“W–UP  A–LEFT  S–DOWN  D–RIGHT  0–EXIT\n”);
printf(“Score:%d Move:%d\n”,score,move);
printf(“Made by Yanjisheng\n”);
printf(“|–|\n”);/*显示横向分隔线*/
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
if(code[i][j]==0)
{
printf(“|    “);/*0显示空格*/
}
else
{
printf(“|%4d”,code[i][j]);/*显示数字和分隔线*/
}
}
printf(“|\n|–|\n”);/*显示横向分隔线*/
}
}
int add(void)/*对中间变量数组进行处理*/
{
int i;
int t=0;
int change=0;/*判断数组能否有改变,0不变,1变化*/
do
{
for(i=0;i<=3;i++)
{
if(temp[i]==0)
{
if(temp[i]!=temp[i+1])
change=1;/*当一个0后面不是0时数组改变*/
temp[i]=temp[i+1];
temp[i+1]=0;
}
}/*去掉中间的0*/
t++;
}while(t<=3);/*重复多次*/
for(i=1;i<=3;i++)
{
if(temp[i]==temp[i-1])
{
if(temp[i]!=0)
{
change=1;/*当两个非零相同的数相加时数组改变*/
score=score+temp[i];/*加分*/
}
temp[i-1]=temp[i-1]*2;
temp[i]=0;
}
}/*把两个相邻的相同的数加起来*/
do
{
for(i=0;i<=3;i++)
{
if(temp[i]==0)
{
temp[i]=temp[i+1];
temp[i+1]=0;
}
else
continue;
}/*去掉中间的0*/
t++;
}while(t<=3);/*重复多次*/
return change;
for(i=1;i<=3;i++)
{
if(temp[i]==temp[i-1])
{
if(temp[i]!=0)
{
change=1;/*当两个非零相同的数相加时数组改变*/
score=score+temp[i];/*加分*/
}
temp[i-1]=temp[i-1]*2;
temp[i]=0;
}
}/*把两个相邻的相同的数加起来*/
do
{
for(i=0;i<=3;i++)
{
if(temp[i]==0)
{
temp[i]=temp[i+1];
temp[i+1]=0;
}
else
continue;
}/*去掉中间的0*/
t++;
}while(t<=3);/*重复多次*/
return change;
int main(void){
int gameover=0;/*判断游戏能否结束,1结束,0继续*/
int i,j;
int change=1;/*判断格子中的数能否改变,0不变*/
char input;
srand((unsigned)time(NULL));/*设置随机数的起点*/
while(gameover==0)
{
if(change>=1)/*仅当数发生改变时添加新数*/
{
do
{
i=((unsigned)rand())%4;
j=((unsigned)rand())%4;
}while(code[i][j]!=0);
if(((unsigned)rand())%4==0)
{
code[i][j]=4;
}
else
{
code[i][j]=2;/*随机选一个空格填上2或4*/
}
move++;/*增加次数*/
}
print();/*显示*/
input=getch();/*输入方向*/
change=0;
switch(input)
{
case “0”:/*退出*/
printf(“Are you sure to exit?(y/n)”);
input=getchar();
exit(0);
break;
case “W”:
case “w”:/*上*/
for(j=0;j<=3;j++)
{
for(i=0;i<=3;i++)
{
temp[i]=code[i][j];/*把一列数移到中间变量*/
}
temp[4]=0;
change=change+add();
for(i=0;i<=3;i++)
{
code[i][j]=temp[i];/*把处理好的中间变量移回来*/
}
}
break;
case “A”:
case “a”:/*左*/
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
temp[j]=code[i][j];/*把一行数移到中间变量*/
}
temp[4]=0;
change=change+add();
for(j=0;j<=3;j++)
{
code[i][j]=temp[j];/*把处理好的中间变量移回来*/
}
}
break;
case “S”:
case “s”:/*下*/
for(j=0;j<=3;j++)
{
for(i=0;i<=3;i++)
{
temp[i]=code[3-i][j];
temp[4]=0;/*把一列数移到中间变量*/
}
temp[4]=0;
change=change+add();
for(i=0;i<=3;i++)
{
code[3-i][j]=temp[i];/*把处理好的中间变量移回来*/
}
}
break;
case “D”:
case “d”:/*右*/
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
temp[j]=code[i][3-j];/*把一行数移到中间变量*/
}
change=change+add();
for(j=0;j<=3;j++)
{
code[i][3-j]=temp[j];/*把处理好的中间变量移回来*/
}
}
break;
}
gameover=1;
for(i=0;i<=3;i++)
for(j=0;j<=3;j++)
if(code[i][j]==0)
gameover=0;/*全部格子都填满则游戏结束*/
}
printf(“Game over!\n”);
getch();
return 0;
}
解决方案

50

   }while(t<=3);/*重复多次*/
return change;           <–缺了个”}”
int main(void){
int gameover=0;/*判断游戏能否结束,1结束,0继续*/

50

本人试了试  copy了你的代码 本人在 return change; 后面加上了个}      后花括号  然后直接运行
没啥问题!连你那些编译错误都没有!如图
本人写的2048的C语言小程序,可是就是编译不了,求赐教

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明本人写的2048的C语言小程序,可是就是编译不了,求赐教
喜欢 (0)
[1034331897@qq.com]
分享 (0)