新手自己写的第一个控制台游戏“2048”
程序代码:#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define ESC 27
int a_ival[16]; //an array of 16 elements
int interface(); //an interface contains a menu including the key operations
void Display(); //display the results
int FindIdleIndex(); // find an idle index in the array
int CreateNewValue(); // the element assigned to the new value
int Change(int,int=0); // how to change the array
int main()
{
srand((unsigned)time(NULL));
interface();
system("cls");
printf("Game Over!\n");
printf("The final result:\n");
Display();
getchar();
return 0;
}
int interface()
{
int key, end=0;
CreateNewValue();
CreateNewValue();
while(1)
{
system("cls");
printf("Now,you can use the keys \n<<<<<\tLeft Arrow \n \tRight Arrow \n \tUp Arrow \n \tDown Arrow \t>>>>>\nto change the value of elements in the array.\n\n");
printf("Press ESC to exit game.\n");
Display();
key=getch();
if(key==ESC)
break;
if(Change(key)!=0)
CreateNewValue();
if(Change(UP,1)!=0)
end=1;
if(Change(DOWN,1)!=0)
end=1;
if(Change(LEFT,1)!=0)
end=1;
if(Change(RIGHT,1)!=0)
end=1;
if(end==0)
break;
end=0;
}
return 0;
}
void Display()
{
int i;
printf("\n\n==========================================\n");
for(i=0;i<16;++i)
{
if(i%4==0)
printf("||");
printf("\t%d",a_ival[i]);
if(i%4==3)
printf("\t||\n");
}
printf("==========================================\n");
}
int FindIdleIndex()
{
int index,i;
for(i=index=rand()%16;index<16;++index)
if(a_ival[index]==0)
return index;
for(index=i-1;index>=0;--index)
if(a_ival[index]==0)
return index;
return -1;
}
int CreateNewValue()
{
int index;
if((index=FindIdleIndex())!=-1)
if(rand()%4<=2)
a_ival[index]=2;
else
a_ival[index]=4;
return 0;
}
int Change(int key,int style)
{
int i,j,IsChange=0,a_stop[4]={-1,-1,-1,-1};
switch(key)
{
case UP:
for(j=0;j<3;++j)
{
for(i=4;i<16-j*4;++i)
{
if(i!=a_stop[i%4] && i!=a_stop[i%4]+4)
{
if(a_ival[i-4]==0 && a_ival[i]!=0)
{
if (style==0)
{
a_ival[i-4]=a_ival[i];
a_ival[i]=0;
}
IsChange=1;
}else if(a_ival[i-4]!=0 && a_ival[i-4]==a_ival[i])
{
if (style==0)
{
a_ival[i-4]+=a_ival[i];
a_ival[i]=0;
a_stop[i%4]=i-4;
}
IsChange=1;
}
}
}
}
break;
case DOWN:
for(j=0;j<3;++j)
{
for(i=11;i>=0+j*4;--i)
{
if(i!=a_stop[i%4] && i!=a_stop[i%4]-4)
{
if(a_ival[i+4]==0 && a_ival[i]!=0)
{
if (style==0)
{
a_ival[i+4]=a_ival[i];
a_ival[i]=0;
}
IsChange=1;
}else if(a_ival[i+4]!=0 && a_ival[i+4]==a_ival[i])
{
if (style==0)
{
a_ival[i+4]+=a_ival[i];
a_ival[i]=0;
a_stop[i%4]=i+4;
}
IsChange=1;
}
}
}
}
break;
case LEFT:
for(j=0;j<3;++j)
{
for(i=0;i<16;++i)
{
if(j==0 && i%4==0)
continue;
else if(j==1 && (i%4==0 || i%4==3))
continue;
else if(j==2 && (i%4==0 || i%4==3 ||i%4==2))
continue;
if(i!=a_stop[i/4] && i!=a_stop[i/4]+1)
{
if(a_ival[i-1]==0 && a_ival[i]!=0)
{
if (style==0)
{
a_ival[i-1]=a_ival[i];
a_ival[i]=0;
}
IsChange=1;
}else if(a_ival[i-1]!=0 && a_ival[i-1]==a_ival[i])
{
if (style==0)
{
a_ival[i-1]+=a_ival[i];
a_ival[i]=0;
a_stop[i/4]=i-1;
}
IsChange=1;
}
}
}
}
break;
case RIGHT:
for(j=0;j<3;++j)
{
for(i=15;i>=0;--i)
{
if(j==0 && i%4==3)
continue;
else if(j==1 && (i%4==3 || i%4==0))
continue;
else if(j==2 && (i%4==3 || i%4==0 ||i%4==1))
continue;
if(i!=a_stop[i/4] && i!=a_stop[i/4]-1)
{
if(a_ival[i+1]==0 && a_ival[i]!=0)
{
if (style==0)
{
a_ival[i+1]=a_ival[i];
a_ival[i]=0;
}
IsChange=1;
}else if(a_ival[i+1]!=0 && a_ival[i+1]==a_ival[i])
{
if (style==0)
{
a_ival[i+1]+=a_ival[i];
a_ival[i]=0;
a_stop[i/4]=i+1;
}
IsChange=1;
}
}
}
}
}
return IsChange;
}









