学生党数组实现C语言

代码如下
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <conio.h>
typedef struct gamer//输入玩家的信息
{
char name[300];
int score;
}GAMER;
char begin[100] = “开始游戏”;//后面将“开始游戏”在dao_ji_shi()函数中写入文件
void menu(GAMER *players);//主菜单
void dao_ji_shi();//玩家有5秒倒计时及开始提示音
int xuan_shu();//保证4出现的概率是2的十分之一
void new_game(int a[4][4]);//创建新的游戏
void player_input(int a[4][4],GAMER *players);//玩家输入指令
void print(int a[4][4]);//显示游戏当前界面的函数
int if_stop(int a[4][4]);//判断游戏是否结束
void move_up(int a[4][4],GAMER *players);//上,并用指针传参的方法计算每步得分
void move_down(int a[4][4],GAMER *players);//下,并用指针传参的方法计算每步得分
void move_left(int a[4][4],GAMER *players);//左,并用指针传参的方法计算每步得分
void move_right(int a[4][4],GAMER *players);//右,并用指针传参的方法计算每步得分
void new_num(int a[4][4]);//每一步后出现新的数字
int if_empty(int a[4][4]);//判断场上是否有空位
void color(const unsigned short textColor);//使每个不同的数字呈现不同的颜色
int main()
{
GAMER players;
players.score=0;
int a[4][4]={0};
system(“mode con cols=38 lines=20”);
menu(&players);
dao_ji_shi();
new_game(a);
print(a);
while(if_stop(a))
{
player_input(a,&players);
print(a);
printf(“\n”);
printf(" 当前得分:%d \n",players.score);
}
if(players.score>=2048)
{
printf(" you win!\n");
printf(" your score:%d",players.score);
}
else
{
printf(" your lost!\n");
printf(" your score:%d",players.score);
}
return 0;
}
void menu(GAMER *players)//主菜单
{
char ch;
printf(“2048游戏规则:\n”);
printf(“首先输入玩家姓名:\n”);
printf(“w or W:上\n”);
printf(“s or S:下\n”);
printf(“a or A:左\n”);
printf(“d or D:右\n”);
printf(“0:退出游戏\n”);
printf(“请输入你的昵称并按回车键输入:”);
gets((*players).name);
if((*players).name[0]‘0’&&(*players).name[1]‘\0’)
exit(0);
printf(“请按下除0外的任意键开始游戏!\n”);
ch=getch(); //不用按回车直接进入下一步
if(ch==‘0’)
exit(0);
system(“CLS”);
}
void dao_ji_shi()//玩家有5秒倒计时及“开始游戏”提示音
{
int i;
printf(“你有五秒准备时间!\n”);
for(i=1;i<=5;i++)
{
printf(“%d “,i);
Sleep(1000);
}
FILE *fq=fopen(“voice.vbs”,“w”);
fprintf(fq,“CreateObject(“SAPI.Spvoice”).Speak”%s””,begin);
fclose(fq);
system(“voice.vbs”);
system(“del voice.vbs”);
fclose(fq);
}
int xuan_shu()//保证4出现的概率是2的十分之一
{
int b[11]={4,2,2,2,2,2,2,2,2,2,2};
int num,xuan_shu;
num=rand()%11;
xuan_shu=b[num];
return xuan_shu;
}
void new_game(int a[4][4])//创建新的游戏
{
int x,y,i=0;
do
{
x=rand()%4;
y=rand()%4;
if(a[x][y]==0)
{
a[x][y]=xuan_shu();
i++;
}
}while(i<2);
}
void print(int a[4][4])
{
int x,y;
system(“CLS”);
printf(“\n”);
printf(“w:上;s:下;a:左;d:右\n”);
printf(“\n”);
printf(“-------------------------------------\n”);
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
color(7);printf(“|”);
if(a[x][y]==0){color(0);printf(“%5d “,a[x][y]);}
if(a[x][y]==2){color(14);printf(”%5d “,a[x][y]);}
if(a[x][y]==4){color(13);printf(”%5d “,a[x][y]);}
if(a[x][y]==8){color(12);printf(”%5d “,a[x][y]);}
if(a[x][y]==16){color(11);printf(”%5d “,a[x][y]);}
if(a[x][y]==32){color(10);printf(”%5d “,a[x][y]);}
if(a[x][y]==64){color(9);printf(”%5d “,a[x][y]);}
if(a[x][y]==128){color(2);printf(”%5d “,a[x][y]);}
if(a[x][y]==256){color(3);printf(”%5d “,a[x][y]);}
if(a[x][y]==512){color(6);printf(”%5d “,a[x][y]);}
if(a[x][y]==1024){color(5);printf(”%5d “,a[x][y]);}
if(a[x][y]==2048){color(4);printf(”%5d “,a[x][y]);}
}
color(7);printf(”|”);
printf(“\n”);
color(7); printf(“-------------------------------------\n”);
}
}
void player_input(int a[4][4],GAMER *players)
{
char ch,ch1;
int flag=0;
do{
flag=0;
ch=getch();
if(ch==‘w’||ch==‘W’)
move_up(a,players);
else if(ch==‘s’||ch==‘S’)
move_down(a,players);
else if(ch==‘a’||ch==‘A’)
move_left(a,players);
else if(ch==‘d’||ch==‘D’)
move_right(a,players);
else if(ch==‘0’)
{
printf(“是否中止游戏?(Y:是;N:否)”);
ch1=getch();
if(ch1==‘n’||ch1==‘N’)
{
}
if(ch1=='y'||ch1=='Y')
{
exit(0);
}
}
else
{
printf(" 非法输入!\n");
flag=1;
}
}while(flag);
}
int if_stop(int a[4][4])//返回值为1则游戏未结束
{
int x,y;
for(x=0;x<4;x++)
{
for(y=0;y<3;y++)
{
if(a[x][y]==a[x][y+1])
{
return 1;
}
}
}
for(y=0;y<4;y++)
{
for(x=0;x<3;x++)
{
if(a[x][y]==a[x+1][y])
{
return 1;
}
}
}
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
if(a[x][y]==0)
{
return 1;
}
}
}
return 0;
}
void move_up(int a[4][4],GAMER *players)//上,并用指针传参的方法计算每步得分
{
int x,y,i,flag=0;
for(i=0;i<3;i++)
{
for(y=0;y<4;y++)
{
for(x=0;x<3;x++)
{
if(a[x][y]==0&&a[x+1][y]!=0)
{
a[x][y]=a[x+1][y];
a[x+1][y]=0;
flag=1;
}
}
}
}
for(y=0;y<4;y++)
{
for(x=0;x<3;x++)
{
if(a[x][y]==a[x+1][y]&&a[x][y]!=0)
{
a[x][y]+=a[x+1][y];
a[x+1][y]=0;
(*players).score=(*players).score+a[x][y];
flag=1;
}
}
}
for(i=0;i<3;i++)
{
for(y=0;y<4;y++)
{
for(x=0;x<3;x++)
{
if(a[x][y]==0&&a[x+1][y]!=0)
{
a[x][y]=a[x+1][y];
a[x+1][y]=0;
}
}
}
}
if(flag)
{
if(if_empty(a))
{
if(if_stop(a))
{
new_num(a);
}
}
}
}
void move_down(int a[4][4],GAMER *players)//下,并用指针传参的方法计算每步得分
{
int x,y,i,flag=0;
for(i=0;i<3;i++)
{
for(y=0;y<4;y++)
{
for(x=3;x>0;x–)
{
if(a[x][y]==0&&a[x-1][y]!=0)
{
a[x][y]=a[x-1][y];
a[x-1][y]=0;
flag=1;
}
}
}
}
for(y=0;y<4;y++)
{
for(x=3;x>0;x–)
{
if(a[x][y]==a[x-1][y]&&a[x][y]!=0)
{
a[x][y]+=a[x-1][y];
a[x-1][y]=0;
(*players).score=(*players).score+a[x][y];
flag=1;
}
}
}
for(i=0;i<3;i++)
{
for(y=0;y<4;y++)
{
for(x=3;x>0;x–)
{
if(a[x][y]==0&&a[x-1][y]!=0)
{
a[x][y]=a[x-1][y];
a[x-1][y]=0;
}
}
}
}
if(flag)
{
if(if_empty(a))
{
if(if_stop(a))
{
new_num(a);
}
}
}
}
void move_left(int a[4][4],GAMER *players)//左,并用指针传参的方法计算每步得分
{
int x,y,i,flag=0;
for(i=0;i<3;i++)
{
for(x=0;x<4;x++)
{
for(y=0;y<3;y++)
{
if(a[x][y]==0&&a[x][y+1]!=0)
{
a[x][y]=a[x][y+1];
a[x][y+1]=0;
flag=1;
}
}
}
}
for(x=0;x<4;x++)
{
for(y=0;y<3;y++)
{
if(a[x][y]==a[x][y+1]&&a[x][y]!=0)
{
a[x][y]+=a[x][y+1];
a[x][y+1]=0;
(*players).score=(*players).score+a[x][y];
flag=1;
}
}
}
for(i=0;i<3;i++)
{
for(x=0;x<4;x++)
{
for(y=0;y<3;y++)
{
if(a[x][y]==0&&a[x][y+1]!=0)
{
a[x][y]=a[x][y+1];
a[x][y+1]=0;
}
}
}
}
if(flag)
{
if(if_empty(a))
{
if(if_stop(a))
{
new_num(a);
}
}
}
}
void move_right(int a[4][4],GAMER *players)//右,并用指针传参的方法计算每步得分
{
int x,y,i,flag=0;
for(i=0;i<3;i++)
{
for(x=0;x<4;x++)
{
for(y=3;y>0;y–)
{
if(a[x][y]==0&&a[x][y-1]!=0)
{
a[x][y]=a[x][y-1];
a[x][y-1]=0;
flag=1;
}
}
}
}
for(x=0;x<4;x++)
{
for(y=3;y>0;y–)
{
if(a[x][y]==a[x][y-1]&&a[x][y]!=0)
{
a[x][y]+=a[x][y-1];
a[x][y-1]=0;
(*players).score=(*players).score+a[x][y];
flag=1;
}
}
}
for(i=0;i<3;i++)
{
for(x=0;x<4;x++)
{
for(y=3;y>0;y–)
{
if(a[x][y]==0&&a[x][y-1]!=0)
{
a[x][y]=a[x][y-1];
a[x][y-1]=0;
}
}
}
}
if(flag)
{
if(if_empty(a))
{
if(if_stop(a))
{
new_num(a);
}
}
}
}
void new_num(int a[4][4])//每一步后出现新的数字
{
int x,y,flag=1;
do{
x=rand()%4;
y=rand()%4;
if(a[x][y]==0)
{
a[x][y]=xuan_shu();
flag=0;
}
}while(flag);
}
int if_empty(int a[4][4])//判断场上是否有空位
{
int x,y;
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
if(a[x][y]==0)
return 1;
}
}
return 0;
}
void color(const unsigned short textColor)//根据参数改变字体颜色
{
if(textColor>=0&&textColor<=15)//参数在0-15的范围颜色
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),textColor);
else SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);
//默认的字体颜色是白色
}

该代码实现了一个基于C语言的2048游戏,包括玩家信息结构体、主菜单、倒计时、随机数生成、游戏移动和合并规则等功能。玩家可以输入方向键进行游戏操作,当达到2048分或无空位时,游戏结束。

1935

被折叠的 条评论
为什么被折叠?



