c语言猜数字游戏源代码

2048年奥运会:

# include & ltstdio.h & gt

# include & ltstdlib.h & gt

# include & ltconio.h & gt

# include & lttime.h & gt

# include & ltwindows.h & gt

int?jsk();?//计算空格数

void rsgm();?//重置游戏

void inkey();?//按键输入

void left();?//向左移动

void right();?//向右移动

void up();?//上移

void down();?//向下移动

void show();?//输出接口

void adnum();//添加一个随机数

void yes();//游戏结束了吗(1是0和否)

void gtxy(int x,int y);//控制光标位置的函数

int a[4][4];//将数字存储在16网格中。

int得分= 0;?//每场比赛得分

int best = 0;?//最高分

int ifnum?//是否需要加一个数字(1是0,否)

int over?//游戏结束标志(1为0,否)

int i,j,k;

int main()

{ rsgm();?//重置游戏

?inkey();?//按键输入

?返回0;

}

空色(int a)?//设置字符颜色的函数(A应该是1-15)

{?SetConsoleTextAttribute(GetStdHandle(STD _ OUTPUT _ HANDLE),a);?} ?

void rsgm()?//重置游戏

{ score = 0;ifnum = 1;over = 0;srand((无符号)time(0));?//启动随机数生成器

int n = rand()% 16;?//随机函数生成一个0-15的数。

for(I = 0;我& lt4;i++)

{ for(j = 0;j & lt4;j++)

{ if(n = = 0){ int k = rand()% 3;if(k = = 0 | | k = = 1){ a[I][j]= 2;}

?else { a[I][j]= 4;} n-;}

?else { a[I][j]= 0;n-;}

}

}

adnum();

系统(“cls”);

CONSOLE_CURSOR_INFO gb={1,0}?//下面两行是隐藏光标的设置,gb指的是光标。

SetConsoleCursorInfo(GetStdHandle(STD _ OUTPUT _ HANDLE),& ampGB);

颜色(14);?//将字体设置为浅黄色

printf("\n\n\t\t?2048场”);颜色(7);?//恢复白色字符和黑色背景

printf("\n\t┌──────┬──────┬──────┬──────┐”);

printf("\n\t│?│?│?│?│");

printf("\n\t├──────┼──────┼──────┼──────┤”);

printf("\n\t│?│?│?│?│");

printf("\n\t├──────┼──────┼──────┼──────┤”);

printf("\n\t│?│?│?│?│");

printf("\n\t├──────┼──────┼──────┼──────┤”);

printf("\n\t│?│?│?│?│");

printf("\n\t└──────┴──────┴──────┴──────┘”);

show();

}

void show()?//输出接口

{ for(I = 0;我& lt4;i++)

for(j = 0;j & lt4;j++)

{ gtxy(7*j+9,2 * I+4);?//gtxy(7*j+9,2*i+4)是光标到指定位置输出的数字。

?if(a[i][j]==0){printf("?);颜色(7);printf(" │");}

?else if(a[I][j]& lt;10){ if(a[I][j]= = 2){ Color(14);}

?else if(a[I][j]= = 4){ Color(13);}

?else if(a[I][j]= = 8){ Color(12);}

?printf("?%d?”,a[I][j]);颜色(7);printf(" │");

}

?else if(a[I][j]& lt;100){ if(a[I][j]= = 16){ Color(12);}

else if(a[I][j]= = 32){ Color(10);}

else if(a[I][j]= = 64){ Color(2);}

printf("?%d?”,a[I][j]);颜色(7);printf(" │");

?}

else if(a[I][j]& lt;1000){ if(a[I][j]= = 128){ Color(9);}

else if(a[I][j]= = 256){ Color(1);}

else if(a[I][j]= = 512){ Color(13);}

printf("?%d”,a[I][j]);颜色(7);printf(" │");

?}

?else if(a[I][j]& lt;10000){ if(a[I][j]= = 1024){ Color(5);}

?else { Color(15);}

?printf(" %d ",a[I][j]);颜色(7);printf(" │");

}

}

if (jsk( ) == 0)

{?是();?if (over) { gtxy(9,12);颜色(10);

?printf("\n\n?游戏结束!你想继续吗?[是/否]:);}

}

}

void inkey()?//按键输入

{ int key

while (1)

?{ key = getch();

if(over){ if(key = = 89 | | key = = 121){ rsgm();继续;}

不然呢?if(key = = 78 | | key = = 110){ return;}

不然呢?继续;}

ifnum = 0;

?if(key = = 224)key = getch();

?开关(钥匙)

?{案例75:left();打破;

案例77:右();打破;

案例72:up();打破;

案例80:down();打破;

}

if(score & gt;最好){最好=分数;}

if(if num){ adnum();show();}

}

}

int jsk()?//计算空格数

{ int n = 0;

for(I = 0;我& lt4;i++)

{ for(j = 0;j & lt4;j++){ if(a[I][j]= = 0){ n++;}?} ?}

返回n;

}

void左()?//向左移动

{ for(I = 0;我& lt4;i++)

{for (j = 1,k = 0;j & lt4;j++)

?{ if(a[I][j]& gt;0)

{ if ( a[i][k] == a[i][j])

?{ a[I][k]* = 2;?k++;

?得分=得分+2 * a[I][j];

?a[I][j]= 0;?ifnum = 1;}

else if(a[I][k]= = 0){ a[I][k]= a[I][j];a[I][j]= 0;ifnum = 1;}

else { a[I][k+1]= a[I][j];if ((k + 1)!= j){ a[I][j]= 0;ifnum = 1;}

k++;}

}

?}

}

}

void right()?//向右移动

{ for(I = 0;我& lt4;i++)

{for (j = 2,k = 3;j & gt= 0;j -)

?{ if(a[I][j]& gt;0)

?{ if (a[i][k] == a[i][j])

{ a[I][k]* = 2;k-;得分=得分+2 * a[I][j];a[I][j]= 0;ifnum = 1;}

else if(a[I][k]= = 0){ a[I][k]= a[I][j];a[I][j]= 0;ifnum = 1;}

else { a[I][k-1]= a[I][j];if ((k - 1)!= j){ a[I][j]= 0;ifnum = 1;} k-;}

}

?}

}

}

void up()?//上移

{ for(I = 0;我& lt4;i++)

{for (j = 1,k = 0;j & lt4;j++)

?{ if(a[j][I]& gt;0)

?{ if(a[k][I]= = a[j][I]){ a[k][I]* = 2;k++;得分=得分+2 * a[j][I];

a[j][I]= 0;ifnum = 1;}

else if(a[k][I]= = 0){ a[k][I]= a[j][I];a[j][I]= 0;ifnum = 1;}

else { a[k+1][I]= a[j][I];if ((k + 1)!= j){ a[j][I]= 0;ifnum = 1;}

k++;}

}

?}

}

}

void down()。//向下移动

{ for(I = 0;我& lt4;i++)

{for (j = 2,k = 3;j & gt= 0;j -)

{ if(a[j][I]& gt;0)

?{if (a[k][i] == a[j][i])

{ a[k][I]* = 2;k-;得分=得分+2 * a[j][I];a[j][I]= 0;ifnum = 1;}

else if(a[k][I]= = 0){ a[k][I]= a[j][I];a[j][I]= 0;ifnum = 1;}

else { a[k-1][I]= a[j][I];

?if ((k - 1)!= j){ a[j][I]= 0;ifnum = 1;} k-;}

}

}

}

}

void adnum()?//添加一个随机数

{ srand(time(0));int n = rand()% jsk();

for(int I = 0;我& lt4;i++)

{ for(int j = 0;j & lt4;j++)

{ if (a[i][j] == 0) {if (n!= 0){ n-;}

?else { int k = rand()% 3;

?if(k = = 0 | | k = = 1){ a[I][j]= 2;返回;}

?else { a[I][j]= 4;返回;} }

}

}

}

}

void yes()?//游戏结束了吗?

{ for(int I = 0;我& lt4;i++)

{ for(int j = 0;j & lt3;j++)

?{ if(a[I][j]= = a[I][j+1]| | a[j][I]= = a[j+1][I]){ over = 0;返回;}}

}

over = 1;

}

void gtxy(int x,int y)?//控制光标位置的函数

{ COORD zb?//zb是指坐标

zb。X = x

zb。Y = y

SetConsoleCursorPosition(GetStdHandle(STD _ OUTPUT _ HANDLE),zb);

}