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;
}*/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章