简单的推箱子实现

简单的推箱子实现

只有一个地图,写了菜单,算是赘余吧。

// An highlighted block
#include <iostream>
#include<Windows.h>
#include <conio.h>//获取键盘输入
using namespace std;
void Map(int nMapArray[10][10]);
bool bOver(int nMapArray[10][10]);
void Move(int &x, int &y, int nMapArray[10][10], int cx, int cy);
void main(){
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息
	CursorInfo.bVisible = false; //隐藏控制台光标
	SetConsoleCursorInfo(handle, &CursorInfo);
	int nState = 1;
	char StartControl;
	while (true)
	{
		system("cls");
		if (nState == 1) cout << " >开始游戏< " << endl;
		else
		{
			cout << "  开始游戏  " << endl;
		}
		if (nState == 2) cout << " >退出游戏< " << endl;
		else
		{
			cout << "  退出游戏 " << endl;
		}
		cout << "使用wasd四键控制。" << endl;
		StartControl = _getch();
		if (StartControl == 'w')
		{
			nState--;
			if (nState<1)
			{
				nState = 2;
			}
		}
		else if (StartControl == 's'){
			nState++;
			if (nState>2)
			{
				nState = 1;
			}
		}
		else if (nState == 1 && StartControl == 13)
		{
			system("cls");
			int nMapArray[10][10] = {//用数值来表示不同的状态,0:障碍1:空地2:人 3:箱子 4:占据位置的人 5;人
				{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
				{ 0, 1, 1, 1, 0, 1, 1, 1, 1, 0 },
				{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
				{ 0, 1, 1, 1, 0, 1, 1, 5, 1, 0 },
				{ 0, 1, 1, 0, 0, 1, 1, 0, 1, 0 },
				{ 0, 1, 1, 0, 1, 1, 1, 1, 1, 0 },
				{ 0, 1, 1, 0, 1, 1, 2, 1, 1, 0 },
				{ 0, 1, 1, 5, 1, 1, 2, 1, 1, 0 },
				{ 0, 1, 0, 1, 1, 1, 1, 1, 1, 0 },
				{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
			};
			char Control;
			int x = 3;//当前位置x值
			int y = 3;//当前位置y值
			nMapArray[x][y] = 3;
			Map(nMapArray);
			while (bOver(nMapArray)){//判定不存在位置或占位人,存在返回true,不存在返回false
				system("cls"); 
				Map(nMapArray);
				Control = _getch();
				if (Control == 27)
				{
					break;
				}
				switch (Control){
				case 'w': Move(x, y, nMapArray, -1, 0); break;
				case 'a': Move(x, y, nMapArray, 0, -1); break;
				case 's': Move(x, y, nMapArray, 1, 0);break;
				case 'd': Move(x, y, nMapArray, 0, 1);break;
				default:break;
				}

			}
		}
		else if (nState == 2 && StartControl == 13){
			exit(0);
		}
		else if (StartControl == 27){
			exit(0);
		}
	}



	//cout << "游戏结束!" << endl;
	//system("pause");
}
//			case 0:cout << "□■●★×☒○¤"; break;

void Map(int nMapArray[10][10]){
	for (int i = 0; i<10; i++){
		for (int j = 0; j<10; j++){
			switch (nMapArray[i][j])
			{
			case 0:cout << "■"; break;//0:障碍  
			case 1:cout << "  "; break;//1:空地
			case 2:cout << "×"; break;//2:位置
			case 3:cout << "○"; break;//3:人
			case 4:cout << "¤"; break;//4:占据位置的人
			case 5:cout << "□"; break;//5:箱子
			case 6:cout << "★"; break;//6:占位置的箱子
			default:
				break;
			}
		}
		cout << endl;
	}
}
bool bOver(int nMapArray[10][10]){
	for (int i = 0; i<10; i++){
		for (int j = 0; j<10; j++){
			if (nMapArray[i][j] == 2 || nMapArray[i][j] == 4)
			{
				return true;
			}
		}
	}
	return false;
}
void Move(int &x, int &y, int nMapArray[10][10], int cx, int cy){
	//if (){}无障碍直接走
	//if (){}有障碍不能走
	//是位置
	//有箱子可以推
	//有箱子不能推
	if (nMapArray[x + cx][y + cy] == 1 || nMapArray[x + cx][y + cy] == 2){
		//空地移进
		nMapArray[x][y] -= 2;//原地图变化
		x += cx;
		y += cy;//人物坐标变化
		nMapArray[x][y] += 2;//新地图变化
	}
	else if (nMapArray[x + cx][y + cy] == 5 || nMapArray[x + cx][y + cy] == 6){
		//普通箱子或者占位箱子
		if (nMapArray[x + cx * 2][y + cy * 2] == 1 || nMapArray[x + cx * 2][y + cy * 2] == 2){//箱子可以推
			nMapArray[x][y] -= 2;//原地图变化
			x += cx;
			y += cy;//人物坐标变化
			nMapArray[x][y] -= 2;
			nMapArray[x + cx][y + cy] += 4;
		}
	}
	else
	{//无法移动

	}
}

PS:欢迎交流想法。

´问题描述: 码头仓库是划分为n×m个格子的矩形阵列。有公共边的格子是相邻格子。当前仓库中 有的格子是空闲的;有的格子则已经堆放了沉重的货物。由于堆放的货物很重,单凭仓库管 理员的力量是无法移动的。仓库管理员有一项任务,要将一个小箱子推到指定的格子上去。 管理员可以在仓库中移动,但不能跨过已经堆放了货物的格子。管理员站在与箱子相对的空 闲格子上时,可以一次推动,把箱子推到另一相邻的空闲格子。推箱时只能向管理员的对 面方向推。由于要推动的箱子很重,仓库管理员想尽量减少推箱子的次数。 ´编程任务: 对于给定的仓库布局,以及仓库管理员在仓库中的位置和箱子的开始位置和目标位置, 设计一个解推箱子问题的分支限界法, 计算出仓库管理员将箱子从开始位置推到目标位置所 需的最少推动次数。 ´数据输入: 由文件input.txt提供输入数据。输入文件第 1 行有 2个正整数 n和 m(1<=n,m<=100) , 表示仓库是n×m个格子的矩形阵列。接下来有 n行,每行有 m个字符,表示格子的状态。 S 表示格子上放了不可移动的沉重货物; w 表示格子空闲; M 表示仓库管理员的初始位置; P 表示箱子的初始位置; K 表示箱子的目标位置。 ´结果输出: 将计算出的最少推动次数输出到文件 output.txt。如果仓库管理员无法将箱子从开始位 置推到目标位置则输出“No solution!” 。 输入文件示例 输出文件示例 input.txt output.txt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值