```c
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>
int j,table[5][5],i,num,t1[5][5];
bool ck=1;
inline bool check1()
{
int x,y;
for(x=1;x<=4;x++)
{
for(y=1;y<=4;y++)
if(table[x][y]== 2048) return 1;
}
return 0;
}
inline void display()
{
for(i=1;i<=4;i++)
{
printf(" ");
for(j=1;j<=4;j++)
printf("%10d",table[i][j]);
puts("\n\n\n");
}
puts("");
}
inline void menu()
{
srand(time(0));
int y,x,num1=0;
for(y=1;y<=4;y++)
{
for(x=1;x<=4;x++)
if(table[y][x]) num1++;
}
if(num1==16) return ;
while(1)
{
int h=rand()%4+1,l=rand()%4+1;
if(!table[h][l])
{
table[h][l]=2;
break;
}
}
}
inline bool ck1(int h)
{
int k=table[h][1],num=0,p=2,b[5];
while(p<=4)
{
if(p==4)
{
if(table[h][p]==k)
b[++num]=2*k;
else b[++num]=k,b[++num]=table[h][p];
break;
}
if(table[h][p]==0)
{
p++;
continue;
}
if(table[h][p]==k)
{
b[++num]=2*k;
if(p==3)
{
b[++num]=table[h][4];
break;
}
else
{
k=table[h][p+1];
p+=2;
continue;
}
}
if(table[h][p]!=k)
{
b[++num]=k;
k=table[h][p];
p++;
}
}
int kk=0;
for(i=1;i<=num;i++)
{
if(b[i]==0) continue;
table[h][++kk]=b[i];
}
for(i=kk+1;i<=4;i++)
table[h][i]=0;
}
inline bool ck2(int h)
{
int k=table[h][4],num=0,p=3,b[5];
while(p>=1)
{
if(p==1)
{
if(table[h][p]==k)
b[++num]=2*k;
else b[++num]=k,b[++num]=table[h][p];
break;
}
if(table[h][p]==0)
{
p--;
continue;
}
if(table[h][p]==k)
{
b[++num]=2*k;
if(p==2)
{
b[++num]=table[h][1];
break;
}
else
{
k=table[h][p-1];
p-=2;
continue;
}
}
if(table[h][p]!=k)
{
b[++num]=k;
k=table[h][p];
p--;
}
}
int kk=5;
for(i=1;i<=num;i++)
{
if(b[i]==0) continue;
table[h][--kk]=b[i];
}
for(i=kk-1;i>=1;i--)
table[h][i]=0;
}
inline bool ck3(int h)
{
int k=table[1][h],num=0,p=2,b[5];
while(p<=4)
{
if(p== 4)
{
if(table[p][h]== k)
b[++num]=2*k;
else b[++num]=k,b[++num]=table[p][h];
break;
}
if(table[p][h]== 0)
{
p++;
continue;
}
if(table[p][h]== k)
{
b[++num]=2*k;
if(p== 3)
{
b[++num]=table[4][h];
break;
}
else
{
k=table[p+1][h];
p+=2;
continue;
}
}
if(table[p][h]!=k)
{
b[++num]=k;
k=table[p][h];
p++;
}
}
int kk=0;
for(i=1;i<=num;i++)
{
if(b[i]== 0) continue;
table[++kk][h]=b[i];
}
for(i=kk+1;i<=4;i++)
table[i][h]=0;
}
inline bool ck4(int h)
{
int k=table[4][h],num=0,p=3,b[5];
while(p>=1)
{
if(p== 1)
{
if(table[p][h]== k)
b[++num]=2*k;
else b[++num]=k,b[++num]=table[p][h];
break;
}
if(table[p][h]== 0)
{
p--;
continue;
}
if(table[p][h]==k)
{
b[++num]=2*k;
if(p==2)
{
b[++num]=table[1][h];
break;
}
else
{
k=table[p-1][h];
p-=2;
continue;
}
}
if(table[p][h]!=k)
{
b[++num]=k;
k=table[p][h];
p--;
}
}
int kk=5;
for(i=1;i<=num;i++)
{
if(b[i]==0) continue;
table[--kk][h]=b[i];
}
for(i=kk-1;i>=1;i--)
table[i][h]=0;
}
inline bool check()
{
int y,x,num1=0;
for(y=1;y<=4;y++)
{
for(x=1;x<=4;x++)
if(table[y][x]) num1++;
}
if(num1<16) return 1;
for(y=2;y<=3;y++)
{
for(x=2;x<=3;x++)
if((table[y][x]==table[y][x-1])||(table[y][x]==table[y][x+1])||(table[y][x]==table[y+1][x])||(table[y][x]==table[y-1][x])) return 1;
}
return 0;
}
inline void work1()
{
int i;
for(i=1;i<=4;i++)
{
ck1(i);
}
}
inline void work2()
{
int i;
for(i=1;i<=4;i++)
{
ck2(i);
}
}
inline void work3()
{
int i;
for(i=1;i<=4;i++)
{
ck3(i);
}
}
inline void work4()
{
int i;
for(i=1;i<=4;i++)
{
ck4(i);
}
}
int main()
{
//puts("\n\n\n\n Welcome to the game 2048!");
system("color Bf");
srand(time(0));
int h=rand()%4+1,l=rand()%4+1;
table[h][l]=2;
while(check())
{
system("cls");
menu();
display();
//printf("Please input direction:");
char dir;dir=_getch();
if(dir=='4') work1();
else if(dir=='6') work2();
else if(dir=='8') work3();
else if(dir=='2') work4();
if(check1())
{
printf("\n\nYou win!!\n");
return 0;
}
}
printf("\n\nYou are lose!!\n");
return 0;
}
简陋的2048小游戏
最新推荐文章于 2024-07-25 14:45:01 发布