C语言写的2048小游戏
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include "stdio.h"
#include "time.h"
#include "conio.h"
#include "stdlib.h"
int death();//结束,结束返还0、否则返还1。
void initial();//初始化
void newnum();//新数字。
void print ();//输出。
void up();
void down();
void left();
void right();
void holle();
void again();
int data[5][5],dx,score;
int main(void)
{
holle ();
while (death())
{
if (kbhit())
{
switch (getch())
{
case 72 :up();break;
case 75 :left();break;
case 77 :right();break;
case 80 :down();break;
case 27 :
printf ("你已经游戏了没多久,休息一下吧@_@\n");
printf ("ESC退出,任意键继续。\n");
if (getch() == 27)
exit(0);
else
break;
}
print();
}
}
again();
return 0;
}
void holle()
{
int n = 1;
printf ("\t\t\t^^^^^2048^^^^^\n\n\n");
printf ("\n\n\t\t\t\tESC暂停游戏。")
printf ("\t\t设置:\n\t\t\t选择界面大小(4/5):\n");
while (n)
switch (getch())
{
case '4':
dx = 4;n--;break;
case '5':
dx = 5;n--;break;
default:
printf ("\t\t\t输入有误,请重新输入(4/5):\n");
break;
}
initial();
newnum();
}
void again()
{
printf ("\t\t\t\t游戏结束!\n\t\t\t分数:%d\n",score);
printf ("\t\t\tESC退出!\n\t\t\t任意键再来一局!");
switch (getch())
{
case 27:
return;break;
default :
holle();
break;
}
}
void initial()
{
int i, j;
for (i = 0;i < dx;i++)
for (j = 0;j < dx;j++)
data[i][j] = 0;
if (dx == 4)
for (i = 0;i < dx;i++)
{
data[i][4] = 1;
data[4][i] = 1;
}
score = 0;
}
int death ()
{
int i, j;
for (i = 0;i < dx;i++)
for (j = 0;j < dx;j++)
if (data[i][j] == 0)
return 1;
for (i = 0;i < dx;i++)
for (j = 0;j < dx-1;j++)
{
if (data[i][j] == data[i][j+1])
return 1;
}
for (i = 0;i < dx-1;i++)
for (j = 0;j < dx;j++)
{
if (data[i][j] == data[i+1][j])
return 1;
}
return 0;
}
void print ()
{
int i, j;
system("cls");
if (dx == 4)
for (i = 0;i < dx;i++)
{
if (i == 0)
printf ("\t┏━━━┳━━━┳━━━┳━━━┓\n");
printf ("\t┃ ┃ ┃ ┃ ┃\n");
printf ("\t┃");
for (j = 0;j < dx;j++)
if (data[i][j]==0)
printf (" ┃");
else
printf ("%4d ┃",data[i][j]);
printf ("\n");
printf ("\t┃ ┃ ┃ ┃ ┃\n");
if (i != 3)
printf ("\t┣━━━╋━━━╋━━━╋━━━┫\n");
if (i == 3)
printf ("\t┗━━━┻━━━┻━━━┻━━━┛\n");
}
else
for(i = 0;i < dx;i++)
{
if (i == 0)
printf ("\t┏━━━┳━━━┳━━━┳━━━┳━━━┓\n");
printf ("\t┃ ┃ ┃ ┃ ┃
┃\n");
printf ("\t┃");
for (j = 0;j < dx;j++)
if (data[i][j] == 0)
printf (" ┃");
else
printf ("%5d ┃",data[i][j]);
printf ("\n");
printf ("\t┃ ┃ ┃ ┃ ┃ ┃\n");
if (i != 4)
printf ("\t┣━━━╋━━━╋━━━╋━━━╋━━━┫\n");
if (i == 4)
printf ("\t┗━━━┻━━━┻━━━┻━━━┻━━━┛\n");
}
printf ("\t\t\t\t\t分数:%d\n",score);
}
void newnum()
{
int i, j, n = 0;
int *p[25];
srand((unsigned)time(NULL));
for (i = 0;i < 5;i++)
for (j = 0;j < 5;j++)
if (data[i][j] == 0)
{
n++;
p[n-1] = &data[i][j];
}
if((rand()%3) == 0)
*p[rand()%n] = 2;
else
*p[rand()%n] = 4;
}
void up()
{
int i, j, k, t, m = 0, n = 0;
for (j = 0;j < dx;j++)
for (k = 0;k < dx-1;k++)
for (i = 0;i < dx-k-1;i++)
if (data[i][j] == 0)
if (data[i][j] != data[i+1][j])
{
data[i][j] = data[i+1][j];
data[i+1][j] = 0;
m = 1;
}
for (j = 0;j < dx;j++)
for (i = 0;i < dx-1;i++)
if (data[i][j] != 0)
if (data[i][j] == data[i+1][j])
{
data[i][j] *= 2;
data[i+1][j] = 0;
score += data[i][j];
m = 1;n = 1;
}
if (n)
for (j = 0;j < dx;j++)
for (k = 0;k < dx;k++)
for (i = 0;i < dx-k-1;i++)
if (data[i][j] == 0)
{
data[i][j] = data[i+1][j];
data[i+1][j] = 0;
}
if(m)
newnum();
}
void down ()
{
int i,j,k,m=0,n=0;
for (j = 0;j < dx;j++)
for (k = 0;k < dx;k++)
for (i = dx-1;i > k;i--)
if (data[i][j] == 0)
if (data[i][j] != data[i-1][j])
{
data[i][j] = data[i-1][j];
data[i-1][j] = 0;
m = 1;
}
for (j = 0;j < dx;j++)
for (i = dx-1;i > 0;i--)
if (data[i][j] != 0)
if (data[i][j] == data[i-1][j])
{
data[i][j] *= 2;
data[i-1][j] = 0;
score += data[i][j];
m = 1;n = 1;
}
if (n)
for (j = 0;j < dx;j++)
for (k = 0;k < dx;k++)
for (i = dx-1;i > k;i--)
if (data[i][j] == 0)
{
data[i][j] = data[i-1][j];
data[i-1][j] = 0;
}
if (m)
newnum();
}
void left()
{
int i, j, k, m = 0, n = 0;
for (i = 0;i < dx;i++)
for (k = 0;k < dx;k++)
for (j = 0;j < dx-k-1;j++)
if (data[i][j] == 0)
if (data[i][j] != data[i][j+1])
{
data[i][j] = data[i][j+1];
data[i][j+1] = 0;
m = 1;
}
for (i = 0;i < dx;i++)
for (j = 0;j < dx-1;j++)
if (data[i][j] != 0)
if (data[i][j] ==
data[i][j+1])
{
data[i][j] *= 2;
data[i][j+1] = 0;
score += data[i][j];
m = 1;n = 1;
}
if (n)
for (i = 0;i < dx;i++)
for (k = 0;k < dx;k++)
for (j = 0;j < dx-k-1;j++)
if (data[i][j] == 0)
{
data[i][j] = data[i][j+1];
data[i][j+1] = 0;
}
if (m)
newnum();
}
void right()
{
int i, j, k, m = 0,n = 0;
for (i = 0;i < dx;i++)
for (k = 0;k < dx;k++)
for (j = dx-1;j > k;j--)
if (data[i][j] == 0)
if (data[i][j] != data[i][j-1])
{
data[i][j] = data[i][j-1];
data[i][j-1] = 0;
m = 1;
}
for (i = 0;i < dx;i++)
for (j = dx-1;j > 0;j--)
if (data[i][j] != 0)
if (data[i][j] == data[i][j-1])
{
data[i][j] *= 2;
data[i][j-1] = 0;
score += data[i][j];
m = 1;n = 1;
}
if (n)
for (i = 0;i < dx;i++)
for (k = 0;k < dx;k++)
for (j = dx-1;j > k;j--)
if (data[i][j] == 0)
{
data[i][j] = data[i][j-1];
data[i][j-1] = 0;
}
if (m)
newnum();
}