【游戏】2048

前言

蒟蒻原创
2048

#include <windows.h>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<conio.h>
#include<cstdio>
#include<ctime>
#include<stack> 
#define random(x) ((x>0)?(1+rand()%x):0)
using namespace std;
int F[6][6],Flag[6][6];
int x,y,z,tot;
bool flag,bol;
char c;
void way(int x,int y)
{
	int m=F[x][y],t=0; 
	while(m)m/=10,++t;
	if(!F[x][y] && x!=0 && x!=5 && y!=0 && y!=5)printf("     ");
	else if(t==1)printf("  %d  ",F[x][y]);
	else if(t==2)printf("  %d ",F[x][y]);
	else if(t==3)printf(" %d ",F[x][y]);
	else if(t==4)printf(" %d",F[x][y]);
	else if(F[0][0]==0 && x==0 && y==0)printf("  0  ");
	else if(F[5][5]==0 && x==5 && y==5)printf("  0  ");
	return;
} 
void write()
{
	printf("|-----------------------|----------------|\n");
	cout<<"|";way(1,1);cout<<"|";way(1,2);cout<<"|";
	way(1,3);cout<<"|";way(1,4);cout<<"|  历史高分      |"<<endl;
	printf("|-----+-----+-----+-----|     ");way(0,0);printf("      |\n");
	cout<<"|";way(2,1);cout<<"|";way(2,2);cout<<"|";
	way(2,3);cout<<"|";way(2,4);cout<<"|                |"<<endl;
	printf("|-----+-----+-----+-----|  当前分数      |\n");
	cout<<"|";way(3,1);cout<<"|";way(3,2);cout<<"|";
	way(3,3);cout<<"|";way(3,4);cout<<"|     ";way(5,5);cout<<"      |"<<endl;
	printf("|-----+-----+-----+-----|                |\n");
	cout<<"|";way(4,1);cout<<"|";way(4,2);cout<<"|";
	way(4,3);cout<<"|";way(4,4);cout<<"|                |"<<endl;
	printf("|-----------------------|----------------|\n");
	printf("←:a	→:d	↑:w	↓:s\n");
	return;
}
void Copy()
{
	for(int i=1;i<=4;++i)
		for(int j=1;j<=4;++j)
			Flag[i][j]=F[i][j];
	return;
}
bool check()
{
	for(int i=1;i<=4;++i)
		for(int j=1;j<=4;++j)
			if(Flag[i][j]!=F[i][j])return 0;
	return 1;
}
bool Up()
{
	Copy();
	for(int i=1;i<=4;++i)
	{
		stack<int>up;
		tot=-1;
		for(int j=1;j<=4;++j)
			if(F[j][i])
			{
				if(F[j][i]==tot)
				{
					F[5][5]+=tot*2;
					up.pop();
					up.push(tot*2);
					F[j][i]=0;
					tot=-1; 
				}
				else up.push(F[j][i]),tot=up.top(),F[j][i]=0;
			}
		for(int j=up.size();j>=1;--j)
			F[j][i]=up.top(),up.pop();
	}
	if(check())return 0;
	else return 1;
}
bool Down()
{
	Copy();
	for(int i=1;i<=4;++i)
	{
		stack<int>down;
		tot=-1;
		for(int j=4;j>=1;--j)
			if(F[j][i])
			{
				if(F[j][i]==tot)
				{
					F[5][5]+=tot*2;
					down.pop();
					down.push(tot*2);
					F[j][i]=0;
					tot=-1; 
				}
				else down.push(F[j][i]),tot=down.top(),F[j][i]=0;
			}
		for(int j=4-down.size()+1;j<=4;++j)
			F[j][i]=down.top(),down.pop();
	}
	if(check())return 0;
	else return 1;
}
bool Left()
{
	Copy();
	for(int i=1;i<=4;++i)
	{
		stack<int>left;
		tot=-1;
		for(int j=1;j<=4;++j)
			if(F[i][j])
			{
				if(F[i][j]==tot)
				{
					F[5][5]+=tot*2;
					left.pop();
					left.push(tot*2);
					F[i][j]=0;
					tot=-1; 
				}
				else left.push(F[i][j]),tot=left.top(),F[i][j]=0;
			}
		for(int j=left.size();j>=1;--j)
			F[i][j]=left.top(),left.pop();
	}
	if(check())return 0;
	else return 1;
}
bool Right()
{
	Copy();
	for(int i=1;i<=4;++i)
	{
		stack<int>right;
		tot=-1;
		for(int j=4;j>=1;--j)
			if(F[i][j])
			{
				if(F[i][j]==tot)
				{
					F[5][5]+=tot*2;
					right.pop();
					right.push(tot*2);
					F[i][j]=0;
					tot=-1; 
				}
				else right.push(F[i][j]),tot=right.top(),F[i][j]=0;
			}
		for(int j=4-right.size()+1;j<=4;++j)
			F[i][j]=right.top(),right.pop();
	}
	if(check())return 0;
	else return 1;
}
bool Flag_()
{
	flag=0;
	for(int i=1;i<=4;++i)
	{
		for(int j=1;j<=4;++j)
			if(!F[i][j]){flag=1;break;}
		if(flag)break;
	}
	for(int i=1;i<=4;++i)
	{
		tot=-1; 
		for(int j=1;j<=4;++j)
		{
			if(F[i][j]==tot){flag=1;break;}
			else tot=F[i][j];
		} 
		if(flag)break;
	}
	for(int i=1;i<=4;++i)
	{
		tot=-1; 
		for(int j=1;j<=4;++j)
		{
			if(F[j][i]==tot){flag=1;break;}
			else tot=F[j][i];
		} 
		if(flag)break;
	}
	return flag;
}
int main()
{
	freopen("Sum.txt","r",stdin);
	scanf("%d",&F[0][0]);
	fclose(stdin);
	srand(time(0));
	x=random(4);
	y=random(4);
	while(F[x][y])
	{x=random(4);y=random(4);}
	z=(random(100)<=80);
	if(z)z=2;else z=4;
	F[x][y]=z;
	system("cls");
	write();
	while(Flag_()) 
	{
		c=getch();
		while(!(c=='w' || c=='s' || c=='d' || c=='a'))c=getch();
		bol=0;
		switch(c)
		{
			case 'w':{if(!Up())bol=1;break;}
			case 's':{if(!Down())bol=1;break;}
			case 'a':{if(!Left())bol=1;break;}
			case 'd':{if(!Right())bol=1;break;}
		}
		if(bol)continue;
		F[0][0]=max(F[0][0],F[5][5]);
		x=random(4);
		y=random(4);
		while(F[x][y])
		{x=random(4);y=random(4);}
		z=(random(100)<=80);
		if(z)z=2;else z=4;
		F[x][y]=z;
		system("cls");
		write();
	}
	printf("Game over!\n");
	printf("按ESC键退出游戏并保存纪录");
	while(1)
	if(GetAsyncKeyState(VK_ESCAPE))
	{
		freopen("Sum.txt","w",stdout);
		printf("%d",F[0][0]);
		fclose(stdout);
		break;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/SSL_wujiajie/article/details/88763515