// test2048.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<conio.h>
int code[4][4] = { {0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}};//游戏的十六个格子
int temp[5];//中间变量
int socre=0;//得分
int move=0;//移动次数
void print();//显示游戏界面
int add();//数据处理
void print()
{
int i,j;
system("cls");
printf("**********2048**********\n");
printf("a->left s->down w->up d->righr\n");
printf("Socre:%d\nMove:%d\n",socre,move);
printf("|-------------------|\n");
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
if(code[i][j] == 0)
{
printf("| ");
}
else
{
printf("|%4d",code[i][j]);
}
}
printf("|\n|-------------------|\n");
}
}
int add()
{
int i = 0,t = 0;
int change = 0;//数值是否发生变化,0--无变化 1--有变化
while(t<4)
{
for(i=0; i<4; i++)
{
if(temp[i] == 0)
{
if(temp[i+1] != 0)
change = 1;
temp[i] = temp[i+1];
temp[i+1] = 0;
}
}
t++;
}
t=0;
for(i=0;i<4;i++)
{
if( temp[i] == temp[i+1])
{
if(temp[i] != 0 )
{
change = 1;
socre = socre+temp[i];
}
temp[i] = temp[i]*2;
temp[i+1] = 0;
}
}
while(t<4)
{
for(i=0; i<4; i++)
{
if(temp[i] == 0)
{
temp[i] = temp[i+1];
temp[i+1] = 0;
}
}
t++;
}
return change;
}
int main(int argc, char* argv[])
{
/* int i;
//srand((unsigned)time(NULL));
for(i=0;i<10;i++)
{
printf("%d\n",rand());
}*/
int gameover = 0;//判断游戏结束,1-结束,0-继续
int i=0,j=0;
int change=1;
char input;
srand((unsigned)time(NULL));
while(gameover == 0)
{
if(change >= 1)
{
do
{
i = ((unsigned)rand())%4;
j = ((unsigned)rand())%4;
}while(code[i][j] != 0);
if(((unsigned)rand())%4 ==0)
{
code[i][j] = 4;
}
else
{
code[i][j] = 2;
}
move++;
}
print();
input = getch();
change=0;
switch(input)
{
case '0':
printf("Are you sure to exit!(y/n)");
input = getchar();
if(input == 'y'||input == 'Y')
exit(0);
break;
case 'W':
case 'w':
for(j=0;j<4;j++)
{
for(i=0;i<4;i++)
{
temp[i] = code[i][j];
}
temp[4] = 0;
change=change+add();
for(i=0;i<4;i++)
{
code[i][j]=temp[i];
}
}
break;
case 'A':
case 'a':
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
temp[j] = code[i][j];
}
temp[4] = 0;
change=change+add();
for(j=0;j<4;j++)
{
code[i][j]=temp[j];
}
}
break;
case 'S':
case 's':
for(j=0;j<4;j++)
{
for(i=0;i<4;i++)
{
temp[i] = code[3-i][j];
}
temp[4] = 0;
change=change+add();
for(i=0;i<4;i++)
{
code[3-i][j]=temp[i];
}
}
break;
case 'D':
case 'd':
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
temp[j] = code[i][3-j];
}
temp[4] = 0;
change=change+add();
for(j=0;j<4;j++)
{
code[i][3-j]=temp[j];
}
}
break;
}
gameover =1;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(code[i][j]==0)
gameover=0;
}
printf("game over!");
return 0;
}
//
#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<conio.h>
int code[4][4] = { {0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}};//游戏的十六个格子
int temp[5];//中间变量
int socre=0;//得分
int move=0;//移动次数
void print();//显示游戏界面
int add();//数据处理
void print()
{
int i,j;
system("cls");
printf("**********2048**********\n");
printf("a->left s->down w->up d->righr\n");
printf("Socre:%d\nMove:%d\n",socre,move);
printf("|-------------------|\n");
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
if(code[i][j] == 0)
{
printf("| ");
}
else
{
printf("|%4d",code[i][j]);
}
}
printf("|\n|-------------------|\n");
}
}
int add()
{
int i = 0,t = 0;
int change = 0;//数值是否发生变化,0--无变化 1--有变化
while(t<4)
{
for(i=0; i<4; i++)
{
if(temp[i] == 0)
{
if(temp[i+1] != 0)
change = 1;
temp[i] = temp[i+1];
temp[i+1] = 0;
}
}
t++;
}
t=0;
for(i=0;i<4;i++)
{
if( temp[i] == temp[i+1])
{
if(temp[i] != 0 )
{
change = 1;
socre = socre+temp[i];
}
temp[i] = temp[i]*2;
temp[i+1] = 0;
}
}
while(t<4)
{
for(i=0; i<4; i++)
{
if(temp[i] == 0)
{
temp[i] = temp[i+1];
temp[i+1] = 0;
}
}
t++;
}
return change;
}
int main(int argc, char* argv[])
{
/* int i;
//srand((unsigned)time(NULL));
for(i=0;i<10;i++)
{
printf("%d\n",rand());
}*/
int gameover = 0;//判断游戏结束,1-结束,0-继续
int i=0,j=0;
int change=1;
char input;
srand((unsigned)time(NULL));
while(gameover == 0)
{
if(change >= 1)
{
do
{
i = ((unsigned)rand())%4;
j = ((unsigned)rand())%4;
}while(code[i][j] != 0);
if(((unsigned)rand())%4 ==0)
{
code[i][j] = 4;
}
else
{
code[i][j] = 2;
}
move++;
}
print();
input = getch();
change=0;
switch(input)
{
case '0':
printf("Are you sure to exit!(y/n)");
input = getchar();
if(input == 'y'||input == 'Y')
exit(0);
break;
case 'W':
case 'w':
for(j=0;j<4;j++)
{
for(i=0;i<4;i++)
{
temp[i] = code[i][j];
}
temp[4] = 0;
change=change+add();
for(i=0;i<4;i++)
{
code[i][j]=temp[i];
}
}
break;
case 'A':
case 'a':
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
temp[j] = code[i][j];
}
temp[4] = 0;
change=change+add();
for(j=0;j<4;j++)
{
code[i][j]=temp[j];
}
}
break;
case 'S':
case 's':
for(j=0;j<4;j++)
{
for(i=0;i<4;i++)
{
temp[i] = code[3-i][j];
}
temp[4] = 0;
change=change+add();
for(i=0;i<4;i++)
{
code[3-i][j]=temp[i];
}
}
break;
case 'D':
case 'd':
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
temp[j] = code[i][3-j];
}
temp[4] = 0;
change=change+add();
for(j=0;j<4;j++)
{
code[i][3-j]=temp[j];
}
}
break;
}
gameover =1;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(code[i][j]==0)
gameover=0;
}
printf("game over!");
return 0;
}