利用数组实现2048,

#include
#include
#include<conio.h>
#include<windows.h>
#include<time.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
int a[4][4],aa[4];
int _max;
void gotoxy(int x,int y)
{
COORD pos = {x,y};//(坐标位置);
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); //得到标准处理(标准输出处理);
SetConsoleCursorPosition(hOut, pos);//设置控制台光标位置;
}
void hidden()//隐藏光标
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut,&cci);
cci.bVisible=0;//赋1为显示,赋0为隐藏
SetConsoleCursorInfo(hOut,&cci);
}
void set()
{
int i,j,m,n;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
a[i][j]=0;
}
srand((unsigned)time(NULL));
do
{
i=rand()%4;j=rand()%4;m=rand()%4;n=rand()%4;
}while(im&&jn);
a[i][j]=a[m][n]=2;
}
void show()
{
int i,j,M=3;
char p=32;
gotoxy(8,3);
cout<<setw(25)<<""<<“5 重新开始 6 退出游戏”<<endl;
cout<<setw(30)<<""<<" ________________________"<<endl;
for(i=0;i<4;i++)
{
cout<<setw(30)<<"";
for(j=0;j<4;j++)
{
if(a[i][j]==0)
{
cout<<setw(2)<<"|"<<setw(4)<<(char)p;
}
else
cout<<setw(2)<<"|"<<setw(4)<<a[i][j];
}
cout<<" |"<<endl<<setw(30)<<""<<" |-----------------------|"<<endl;
}
}
void upmove()
{
int i,j,k,i1,j1,k1,t;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++) //上衣
{
aa[j]=a[j][i];
}
for(i1=0;i1<3;i1++)
{
for(j1=0;j1<3-i1;j1++)
{
if(aa[j1]==0)
{
t=aa[j1];
aa[j1]=aa[j1+1];
aa[j1+1]=t;
}
}
} //上衣结束
for(k=0;k<4;k++)
{
a[k][i]=aa[k];
}
for(k1=0;k1<4;k1++)
{
if(aa[0]==aa[1])
{
aa[0]+=aa[1];aa[1]=aa[2];aa[2]=aa[3];aa[3]=0;
break;
}
if(aa[1]==aa[2])
{
aa[1]+=aa[2];
aa[2]=aa[3];aa[3]=0;
break;
}
if(aa[2]==aa[3])
{
aa[2]+=aa[3];
aa[3]=0; break;
}
}
for(k=0;k<4;k++)
{
a[k][i]=aa[k];
}

}	

}
void chong() //添加新数
{
int i,j,m,n;
srand((unsigned)time(NULL));
do
{
m=rand()%4;
n=rand()%4;
}while(a[m][n]!=0);
a[m][n]=2;
}
void left()//左
{
int i,j,t;
for(i=0;i<4;i++)
{
for(j=0;j<i;j++)
{
t=a[i][j];
a[i][j]=a[j][i];
a[j][i]=t;
}
}
for(i=0;i<2;i++)
{
for(j=0;j<4;j++)
{
t=a[i][j];
a[i][j]=a[3-i][j];
a[3-i][j]=t;
}
}
}
void right()
{
int i,j,t;
for(i=0;i<4;i++)
{
for(j=0;j<i;j++)
{
t=a[i][j];
a[i][j]=a[j][i];
a[j][i]=t;
}
}
for(i=0;i<4;i++)
{
for(j=0;j<2;j++)
{
t=a[i][j];
a[i][j]=a[i][3-j];
a[i][3-j]=t;
}
}
}
void drop()
{
int i,j,t;
for(i=0;i<2;i++)
{
for(j=0;j<4;j++)
{
t=a[i][j];
a[i][j]=a[3-i][j];
a[3-i][j]=t;
}
}
}
int pan()
{
int i,j,ret=1,k=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(a[i][j]2048)
ret=2;
if(a[i][j]0)
k++;
}
}
if(k
0)
ret=2;
return ret;
}
int main()
{
int ch,ret1,sec=0,minute=0,hour=0,i,j,_max=0;
system(“color f0”);
hidden();
set();
show();
while(1)
{
ret1=pan();
if(ret1
2)
{
cout<<“come over”<<endl;
return 0;
}
/*s1 :
ch = _getch(); */
if(ch72||ch75||ch77||ch80)
{
s1 :
while(!kbhit())
{
sec++;
if(sec>500)
{
sec=sec-500;
minute++;
}
if(minute>59)
{
minute-=60;
hour++;
}
gotoxy(5,2);
cout<<“时间 “;
cout<<hour/10<<hour%10<<”:”<<minute/10<<minute%10<<endl;
Sleep(1);
}
}
ch = _getch();
//if(ch72||ch75||ch77||ch80)
switch(ch)
{
case 72:
upmove();chong();system(“cls”);break;
case 75:
right();upmove();left();chong();system(“cls”);break;
case 77:
left();upmove();right();chong();system(“cls”);break;
case 80:
drop();upmove();drop();chong();system(“cls”);break;
case 53:
set();hour=minute=sec=0;system(“cls”);break;
case 54:
return 0;
case 55:
system(“cls”);
gotoxy(5,2);
cout<<“时间 “;
cout<<hour/10<<hour%10<<”:”<<minute/10<<minute%10<<endl;
Sleep(1);break;
default:
goto s1;
}
show();
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(a[i][j]>_max)
_max=a[i][j];
}
}
cout<<setw(45)<<"得分 "<<_max<<endl;
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值