C++三维数组写推箱子

C++三维数组写推箱子

#include 
#include 
using namespace std;
int main()
{
	int Level = 0,PosX,PosY;
	int oldMap[3][10][10], oldMap1[10][10],oldMap2[10][10],PosX1, PosY1;
	int GameMap[3][10][10] = {//地图
		{
			{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
			{ 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 },
			{ 0, 0, 0, 0, 1, 4, 1, 0, 0, 0 },
			{ 0, 1, 1, 1, 1, 3, 1, 0, 0, 0 },
			{ 0, 1, 4, 0, 3, 2, 1, 1, 1, 0 },
			{ 0, 1, 1, 1, 3, 3, 0, 4, 1, 0 },
			{ 0, 0, 0, 1, 0, 1, 1, 1, 1, 0 },
			{ 0, 0, 0, 1, 4, 1, 0, 0, 0, 0 },
			{ 0, 0, 0, 1, 1, 1, 0, 0, 0, 0 },
			{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
		},
		{ 
			{ 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
			{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
			{ 1, 0, 3, 0, 1, 0, 1, 1, 1, 0 },
			{ 1, 0, 3, 0, 1, 0, 1, 4, 1, 0 },
			{ 1, 1, 1, 2, 1, 1, 1, 4, 1, 0 },
			{ 0, 1, 1, 3, 0, 0, 0, 4, 1, 0 },
			{ 0, 1, 0, 0, 0, 1, 0, 0, 1, 0 },
			{ 0, 1, 0, 0, 0, 1, 1, 1, 1, 0 },
			{ 0, 1, 1, 1, 1, 1, 0, 0, 0, 0 },
			{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
		},
		{
			{ 0, 0, 0, 1, 1, 1, 1, 1, 1, 0 },
			{ 0, 1, 1, 1, 0, 0, 0, 0, 1, 0 },
			{ 1, 1, 4, 0, 3, 1, 1, 0, 1, 1 },
			{ 1, 4, 4, 3, 0, 3, 0, 0, 2, 1 },
			{ 1, 4, 4, 0, 3, 0, 3, 0, 1, 1 },
			{ 1, 1, 1, 1, 1, 1, 0, 0, 1, 0 },
			{ 0, 0, 0, 0, 0, 1, 1, 1, 1, 0 },
			{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
			{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
			{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
		}
	};
		
	for (int i = 0; i < 10; i++)//保存第一关的地图
	{
		for (int j = 0; j < 10; j++)
		{
			oldMap2[i][j] = GameMap[Level][i][j];
		}
	}
	while (true)
	{
		
		for (int i = 0; i < 10; i++)
		{
			for (int j = 0; j < 10; j++)
			{
				if (GameMap[Level][i][j] == 2)//获取人的坐标
				{
					PosX = i; PosY = j;
				}
				switch (GameMap[Level][i][j])
				{
				case 0:
					cout << "  ";
					break;
				case 1:
					cout << "■";
					break;
				case 2:
				case 6:
					cout << "♂";
					break;
				case 3:
				case 7:
					cout << "★";
					break;
				case 4:
					cout << "☆";
					break;
				}

			}
			cout << endl;
		}
		

		int Input = getch();
		system("CLS");
		int OffSetX = 0, OffSetY = 0;//行列偏移量
		switch (Input)
		{
		case 's':
		case 'S':
			OffSetX = 1;
			break;
		case 'D':
		case 'd':
			OffSetY = 1;
			break;
		case 'a':
		case 'A':
			OffSetY = -1;
			break;
		case 'w':
		case 'W':
			OffSetX = -1;
			break;
		case 'r':
		case 'R'://撤销
			PosX = PosX1; PosY =PosY1;
			for (int i = 0; i < 10; i++)
			{
				for (int j = 0; j < 10; j++)
				{
					GameMap[Level][i][j] = oldMap[Level][i][j];
				}
			}
			break;
		case 'q':
		case 'Q'://重置地图,把旧地图的元素复制给游戏地图
			if (0==Level)
			for (int i = 0; i < 10; i++)
			{
				for (int j = 0; j < 10; j++)
				{
					GameMap[Level][i][j] = oldMap2[i][j];
				}
			}
		   else 
			for (int i = 0; i < 10; i++)
			{
				for (int j = 0; j < 10; j++)
				{
					GameMap[Level][i][j] = oldMap1[i][j];
				}
			}
			break;
		}
		if (0 == GameMap[Level][PosX + OffSetX][PosY + OffSetY] || 4 == GameMap[Level][PosX + OffSetX][PosY + OffSetY])
		{
			//保存上一步的人物坐标和地图
			for (int i = 0; i < 10; i++)
			{
				for (int j = 0; j < 10; j++)
				{
					oldMap[Level][i][j] = GameMap[Level][i][j];
					if (GameMap[Level][i][j] == 2)
					{
						PosX1 = i; PosY1 = j;
					}

				}
			}
			GameMap[Level][PosX][PosY] -= 2;//人离开
			GameMap[Level][PosX + OffSetX][PosY + OffSetY] += 2;//人来了
			PosX += OffSetX;
			PosY += OffSetY;
		}else if (3 == GameMap[Level][PosX + OffSetX][PosY + OffSetY] || 7 == GameMap[Level][PosX + OffSetX][PosY + OffSetY])
		{
			if (0 == GameMap[Level][PosX + OffSetX*2][PosY + OffSetY*2] || 4 == GameMap[Level][PosX + OffSetX*2][PosY + OffSetY*2])
			{
				for (int i = 0; i < 10; i++)
				{
					for (int j = 0; j < 10; j++)
					{
						oldMap[Level][i][j] = GameMap[Level][i][j];
						if (GameMap[Level][i][j] == 2)
						{
							PosX1 = i; PosY1 = j;
						}

					}
				}
				GameMap[Level][PosX][PosY] -= 2;//人离开
				GameMap[Level][PosX + OffSetX][PosY + OffSetY] += 2-3;//人来了
				GameMap[Level][PosX + OffSetX * 2][PosY + OffSetY * 2] += 3;
				PosX += OffSetX;
				PosY += OffSetY;
			}
		
		}
		int cnum = 0;//遍历,如果箱子(3)的个数为0,则进入下一关
		for (int i = 0; i < 10; i++)
		{
			for (int j = 0; j < 10; j++)
			{
				if (3 == GameMap[Level][i][j])
				{
					++cnum;
					
				}
			}
		}
		if (0 == cnum)
	        	  {
			           Level++;
	    for (int i = 0; i < 10; i++)//保存第二关和之后关卡的地图
		{
			for (int j = 0; j < 10; j++)
			{
				oldMap1[i][j] = GameMap[Level][i][j];
			}
		}
		          }
		
		
		if (Level >= 3)//如果到三了,说明过关了
		{
			cout << "you win!" << endl;
			break;
		}
	}
	
} 

你可能感兴趣的:(C++三维数组写推箱子)