2048简单模拟

2、控制层
#include <iostream>
#include <iomanip>
using namespace std;
const int m=4;
int score=0;
inline void swapTwoInt(int &x,int &y)
{
	if (x==y)
	{
		return;
	}
	x^=y;
	y^=x;
	x^=y;
}
void printArray(int arr[][4])
{
	for (int i=0;i<m;++i)
	{
		for (int j=0;j<m;++j)
		{
			cout<<setw(4)<<arr[i][j];
		}
		cout<<endl;
	}
}
int RowAlignLeft(int a[])
{
	int count=0;
	int i=0,j=0;
	for (;i<m;++i)
	{
		if (a[i]==0)
		{
			for (j=i+1;j<m;++j)
			{
				if (a[j]!=0)
				{
					a[i]=a[j];
					a[j]=0;
					break;
				}
			}
		}
		//else
		{
			for (j=i+1;j<m;++j)
			{
				if(a[i]==a[j])
				{
					a[i]+=a[j];
					count+=a[i];
					a[j]=0;
					break;
				}
				else
				{
					if (a[j]==0)
					{
						continue;
					}
					else break;
				}
					
			}
		}
	}	
	return count;
}


void reverseArray(int a[])
{
	int i=0,j=0;
	int count=0;
	for (int i=0;i< m/2;++i)
	{
		swapTwoInt(a[i],a[m-i-1]);
	}

}
void symmetricArray(int a[4][4])
{
	for (int i=0;i<m;++i)
	{
		for(int j=i;j<m;++j)
		{
			swapTwoInt(a[i][j],a[j][i]);
		}
		
	}
}
void toLeft(int ar[][4])
{
	for (int i=0;i<m;++i)
	{
		score+=RowAlignLeft(ar[i]);
	}
	
}
void toRight(int ar[][4])
{
	for (int i=0;i<m;++i)
	{
		reverseArray(ar[i]);
		score+=RowAlignLeft(ar[i]);
		reverseArray(ar[i]);
	}

}
void toTop(int ar[][4])
{
	symmetricArray(ar);
	toLeft(ar);
	symmetricArray(ar);

}
void toBottom(int ar[][4])
{
	symmetricArray(ar);
	toRight(ar);
	symmetricArray(ar);
}
/*int main(int argr,int agrc[])
{

	int TwoArray[][m]={{0,2,2,2},
						{2,2,4,0},
						{8,2,0,4},
						{0,0,2,4}};
	printArray(TwoArray);
	toBottom(TwoArray);
	printArray(TwoArray);
	cout<<"score is "<<score<<endl;
	system("pause");
	return 0;
}*/

7-7 简易连连看 (15分) 本题要求实现一个简易连连看游戏模拟程序。 给定一个2N×2N的方阵网格游戏盘面,每个格子中放置一些符号。这些符号一定是成对出现的,同一个符号可能不止一对。程序读入玩家给出的一对位置(x ​1 ​​ ,y ​1 ​​ )、(x ​2 ​​ ,y ​2 ​​ ),判断这两个位置上的符号是否匹配。如果匹配成功,则将两个符号消为“*”并输出消去后的盘面;否则输出“Uh-oh”。若匹配错误达到3次,则输出“Game Over”并结束游戏。或者当全部符号匹配成功,则输出“Congratulations!”,然后结束游戏。 输入格式: 输入在一行中给一个正整数N(<5)。随后2N行,每行2N个大写英文字母(其间以1个空格分隔),表示游戏盘面。盘面之后给出一个正整数K,随后K行,每行按照格式“x ​1 ​​ y ​1 ​​ x ​2 ​​ y ​2 ​​ ”给出一个玩家的输入。注意格子的行、列编号是从1到2N。 输出格式: 根据玩家的每一步输入,输出相应的结果。输出盘面时注意,每行字符间以1个空格分隔,行末不得有多余空格。 输入样例1: 2 I T I T Y T I A T A T Y I K K T 11 1 1 1 3 4 2 4 3 3 1 4 2 2 2 1 2 3 1 2 4 4 4 3 1 2 1 3 4 3 3 1 4 4 1 2 3 2 4 3 2 1 1 2 2 输出样例1: * T * T Y T I A T A T Y I K K T * T * T Y T I A T A T Y I * * T Uh-oh * * * T Y * I A T A T Y I * * T Uh-oh * * * T Y * I A * A T Y I * * * * * * T * * I A * A T * I * * * * * * * * * I A * A * * I * * * * * * * * * * A * A * * * * * * Congratulations! 输入样例2: 2 I T I T Y T I A T A T Y I K K T 5 1 1 4 4 1 1 2 3 1 1 2 3 2 2 4 1 2 2 3 3 输出样例2: Uh-oh * T I T Y T * A T A T Y I K K T Uh-oh Uh-oh Game Over
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值