C语言实现2048源程序如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <termios.h>
#define N 4
int a[N][N];
int tmp[N];
int change = 1;
struct termios old;
struct termios new;
void ShowMenu(void)
{
int i = 0;
int j = 0;
system(&quot;clear&quot;);
printf(&quot;\n\t**********************************\n&quot;);
printf(&quot;\t\t欢迎**进入2048!\n&quot;);
printf(&quot;\ta:left d:right w:up s:down q:quit\n&quot;);
printf(&quot;\t**********************************\n\n&quot;);
printf(&quot;\t ┌────┬────┬────┬────┐\n&quot;);
for (j = 0; j < N; j++)
{
printf(&quot;\t │&quot;);
for(i = 0; i < N; i++)
{
if(0 == a[j])
{
printf(&quot; │&quot;);
}
else
{
printf(&quot;%4d│&quot;, a[j]);
}
}
if (j < 3)
{
printf(&quot;\n\t ├────┼────┼────┼────┤\n&quot;);
}
}
printf(&quot;\n\t └────┴────┴────┴────┘\n&quot;);
printf(&quot;\n\t---------------------------------\n&quot;);
return;
}
void GameBegin(void)
{
int x = 0;
int y = 0;
while(1)
{
x = rand() % N;
y = rand() % N;
if (a[x][y] == 0)
{
a[x][y] = rand() % 5 ? 2 : 4;
break;
}
}
ShowMenu();
return;
}
void AddNum(void)
{
int i = 0;
int j = 0;
for (j = 0; j < N-1; j++)
{
for (i = 0; i < N-1; i++)
{
if (0 == tmp && tmp[i+1] != 0)
{
tmp = tmp[i+1];
tmp[i+1] = 0;
change = 1;
}
}
}
for (i = 0; i < N-1; i++)
{
if (tmp == tmp[i+1])
{
tmp *= 2;
tmp[i+1] = 0;
change = 1;
}
}
for (j = 0; j < N-1; j++)
{
for (i = 0; i < N-1; i++)
{
if (0 == tmp && tmp[i+1] != 0)
{
tmp = tmp[i+1];
tmp[i+1] = 0;
}
}
}
return;
}
void LeftKey(void)
{
int j = 0;
int i = 0;
for (j = 0; j < N; j++)
{
for (i = 0; i < N; i++)
{
tmp = a[j];
}
AddNum();
for (i = 0; i < N; i++)
{
a[j] = tmp;
}
}
return;
}
void RightKey(void)
{
int j = 0;
int i = 0;
for (j = 0; j < N; j++)
{
for (i = 0; i < N; i++)
{
tmp = a[j][N-1-i];
}
AddNum();
for (i = 0; i < N; i++)
{
a[j][N-1-i] = tmp;
}
}
return;
}
void DownKey(void)
{
int i = 0;
int j = 0;
for (j = 0; j < N; j++)
{
for (i = 0; i < N; i++)
{
tmp = a[N-1-i][j];
}
AddNum();
for (i = 0; i < N; i++)
{
a[N-1-i][j] = tmp;
}
}
return;
}
void UpKey(void)
{
int i = 0;
int j = 0;
for (j = 0; j < N; j++)
{
for (i = 0; i < N; i++)
{
tmp = a[j];
}
AddNum();
for (i = 0; i < N; i++)
{
a[j] = tmp;
}
}
return;
}
void GetKey(void)
{
char ch = 0;
while (1)
{
ch = getchar();
if('a' == ch || 'A' == ch || 'w' == ch || 'W' == ch ||
's' == ch || 'S' == ch || 'd' == ch || 'D' == ch
|| 'q' == ch || 'Q' == ch)
{
break;
}
}
switch (ch)
{
case 'a':
case 'A': LeftKey();break;
case 'D':
case 'd': RightKey();break;
case 's':
case 'S': DownKey();break;
case 'w':
case 'W': UpKey();break;
case 'q':
case 'Q': exit(0);break;
default:
break;
}
return;
}
void Succeed(void)
{
int i = 0;
int j = 0;
for (j = 0; j < N; j++)
{
for (i = 0; i < N; i++)
{
if (256 == a[j])
{
printf(&quot;\t\tCongratulation!\n&quot;);
}
}
}
return;
}
void GameOver(void)
{
int i = 0;
int j = 0;
int cnt = 0;
for (j = 0; j < N; j++)
{
for (i = 0; i < N; i++)
{
if (0 == a[j])
{
cnt++;
}
}
}
if (cnt != 0)
{
return;
}
for (j = 0; j < N-1; j++)
{
for (i = 0; i < N-1; i++)
{
if (a[j] == a[j][i+1] || a[j] == a[j+1])
{
return;
}
}
}
printf(&quot;\t\t GAMEOVER!\n&quot;);
return;
}
void ChangTerminalState(void)
{
tcgetattr(0, &old);
tcgetattr(0, &new);
new.c_lflag = new.c_lflag & ~(ICANON | ECHO);
new.c_cc[VTIME] = 0;
new.c_cc[VMIN] = 1;
tcsetattr(0, TCSANOW, &new);
return;
}
int main(int argc, const char *argv[])
{
ChangTerminalState();
srand(time(NULL));
while(1)
{
if (change)
{
GameBegin();
}
Succeed();
GameOver();
change = 0;
GetKey();
}
return 0;
}
复制代码
所有资料51hei提供下载:
2048.zip
(1.28 KB, 下载次数: 20)
2019-2-28 22:17 上传
点击文件名下载附件 |