2048

实话说,我玩这个游戏就没到过512

#include<iostream>
#include<cstring>
#include<ctime>
#include<conio.h>
#include<windows.h>
#include<cstdio>
using namespace std;
int n,a[5][5],x,y;
bool f[5][5];
char d;
bool check()
{
	for(int i=1;i<=4;i++)
	for(int j=1;j<=4;j++)
	if(!a[i][j]||a[i-1][j]==a[i][j]||a[i+1][j]==a[i][j]||a[i][j-1]==a[i][j]||a[i][j+1]==a[i][j])
	return true;
	return false;
}
bool full()
{
	for(int i=1;i<=4;i++)
	for(int j=1;j<=4;j++)
	if(!a[i][j]) return false;
	return true;
}
void zyi(int i,int j)
{
	int k=j-1;
	while(k>0)
	{
		if(a[i][k]!=a[i][k+1]&&a[i][k])
		return;
		if(!a[i][k+1])
		return;
		if(a[i][k]==0)
		a[i][k]=a[i][k+1],a[i][k+1]=0;
		if(a[i][k]==a[i][k+1]&&!f[i][k]&&!f[i][k+1])
		{
			a[i][k]=a[i][k+1]*2,a[i][k+1]=0;
			f[i][k]=1;
		}
		k--;
	}
}
void yyi(int i,int j)
{
	int k=j+1;
	while(k<=4)
	{
		if(a[i][k]!=a[i][k-1]&&a[i][k])
		return;
		if(!a[i][k-1])
		return;
		if(a[i][k]==0)
		a[i][k]=a[i][k-1],a[i][k-1]=0;
		if(a[i][k]==a[i][k-1]&&!f[i][k]&&!f[i][k-1])
		{
			a[i][k]=a[i][k-1]*2,a[i][k-1]=0;
			f[i][k]=1;
		}
		k++;
	}
}
void syi(int i,int j)
{
	int k=j-1;
	while(k>0)
	{
		if(a[k][i]!=a[k+1][i]&&a[k][i])
		return;
		if(!a[k+1][i])
		return;
		if(a[k][i]==0)
		a[k][i]=a[k+1][i],a[k+1][i]=0;
		if(a[k][i]==a[k+1][i]&&!f[k][i]&&!f[k+1][i])
		{
			a[k][i]=a[k+1][i]*2,a[k+1][i]=0;
			f[k][i]=1;
		}		
		k--;
	}
}
void xyi(int i,int j)
{
	int k=j+1;
	while(k<=4)
	{
		if(a[k][i]!=a[k-1][i]&&a[k][i])
		return;
		if(!a[k-1][i])
		return;
		if(a[k][i]==0)
		a[k][i]=a[k-1][i],a[k-1][i]=0;
		if(a[k][i]==a[k-1][i]&&!f[k][i]&&!f[k-1][i])
		{
			a[k][i]=a[k-1][i]*2,a[k-1][i]=0;
			f[k][i]=1;
		}
		k++;
	}
}
void zhua()
{
	for(int i=1;i<=4;i++)
	for(int j=2;j<=4;j++)
	zyi(i,j);
}
void yhua()
{
	for(int i=1;i<=4;i++)
	for(int j=3;j>0;j--)
	yyi(i,j);
}
void shua()
{
	for(int i=1;i<=4;i++)
	for(int j=2;j<=4;j++)
	syi(i,j);
}
void xhua()
{
	for(int i=1;i<=4;i++)
	for(int j=3;j>0;j--)
    xyi(i,j);
}
int main()
{
	srand(time(0));
	while(check())
	{
		if(!full())
		{
			do
			{
				x=rand()%4+1;
				y=rand()%4+1;
			}while(a[x][y]);
			int p=rand()%2*2+2;
			a[x][y]=p;
		}
		system("cls");
		for(int i=1;i<=4;i++)
		{
			for(int j=1;j<=4;j++)
			printf("%4d",a[i][j]);
			cout<<endl;
		}
		d=getch();
		if(d=='a')
		zhua();
		if(d=='d')
		yhua();
		if(d=='w')
		shua();
		if(d=='s')
		xhua();
		memset(f,0,sizeof(f));
	}
	system("cls");
	cout<<"GAME OVER";
	return 0;
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页