数据结构与算法课程设计——C++迷宫游戏

问题描述

程序开始运行时显示一个迷宫地图,迷宫中央有一只老鼠,迷宫的右下方有一个粮仓。游戏的任务是使用键盘上的方向健操纵老鼠在规定的时间内走到粮仓处。

基本要求

(1) 老鼠形象可以辨认,可用键盘操纵老鼠上下左右移动
(2) 迷宫的墙足够结实,老鼠不能穿墙而过
(3) 正确检测结果,若老鼠在规定时间内走到粮仓处,提示成功,并给出一条路径,否则提示失败
(4) 添加编辑迷宫功能,可修改当前迷宫,修改内容:墙变路、路变墙

提高要求

(1) 增加闯关和计分功能
(2) 找出走出迷宫的所有路径及最短路径

源代码

注:所有源代码分3个文件存放
1.main.cpp

// 老鼠走迷宫.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <windows.h>
#include <conio.h>
#include "PlayGame.h"

void GameInterface();//开始界面
void ChooseLevel();//选择关卡
void StartGame(Player* player, bool* flag);//开始游戏
void EditorGame(Player* player, bool* flag);//编辑游戏
void ShortestPath(Player player[], bool* flag);//最短路径
void GameDescription();//游戏说明
void ExitGame();//退出游戏
void Hidden();//隐藏光标
void ShowAll(Player player[], bool* flag);//所有路径

int main()
{
	Player* player;
	for (int i = 0; i < NumberOfCheckpoints; i++)
	{
		player = new Player[NumberOfCheckpoints];
	}

	//Player player[NumberOfCheckpoints];

	char myChoice;
	char yes_no;

	bool flag[NumberOfCheckpoints];
	for (int i = 0; i < NumberOfCheckpoints; i++)
	{
		flag[i] = false;//默认每一关都闯关失败
	}

	Hidden();
	do
	{
		GameInterface();

		cin >> myChoice;
		system("cls");

		switch (myChoice)
		{
		case '1':
			StartGame(player, flag);//开始游戏
			break;
		case '2':
			EditorGame(player, flag);//编辑游戏
			break;
		case '3':
			ShortestPath(player, flag);//展示最短路径
			break;
		case '4':
			ShowAll(player, flag);//展示所有路径
			break;
		case '5':
			GameDescription();//游戏说明
			break;
		case '0':
			ExitGame();//退出游戏
			break;
		default:
			cout << "│────★输入非法!★────│" << endl;
			Sleep(1000);
			break;
		}

		if (myChoice == '0')
		{
			break;
		}

		do
		{
			system("cls");
			cout << "│────★是否回到主菜单?(Y/N)★────│" << endl;
			cin >> yes_no;
			if (yes_no != 'Y' && yes_no != 'y' && yes_no != 'N' && yes_no != 'n')
			{
				system("cls");
				cout << "│────★输入非法!请重新输入!★────│" << endl;
				Sleep(1000);
			}
		} while (yes_no != 'Y' && yes_no != 'y' && yes_no != 'N' && yes_no != 'n');

		if (yes_no == 'N' || yes_no == 'n')
		{
			ExitGame();
			break;
		}

	} while (yes_no == 'Y' || yes_no == 'y');

	delete[] player;
	return 0;
}

void GameInterface()//开始界面
{
	system("cls");
	system("@color 0a");//淡绿色前景色
	cout << "┌──────────────────────────────────────────────┐" << endl;
	cout << "│                                              │" << endl;
	cout << "│                ★1.开始游戏★                │" << endl;
	cout << "│                                              │" << endl;
	cout << "│                ★2.编辑游戏★                │" << endl;
	cout << "│                                              │" << endl;
	cout << "│              ★3.查看最短路径★              │" << endl;
	cout << "│                                              │" << endl;
	cout << "│              ★4.查看所有路径★              │" << endl;
	cout << "│                                              │" << endl;
	cout << "│         ★5.游戏说明(游戏前必读)★         │" << endl;
	cout << "│                                              │" << endl;
	cout << "│                ★0.退出游戏★                │" << endl;
	cout << "│                                              │" << endl;
	cout << "└──────────────────────────────────────────────┘" << endl;
}

void ChooseLevel()//选择关卡
{
	system("cls");
	system("@color 09");//淡蓝色前景色
	cout << "┌────★请选择关卡★────┐" << endl;
	for (int i = 0; i < NumberOfCheckpoints; i++)
	{
		cout << "│       " << i + 1 << "." << "第" << i + 1 << "关        │" << endl;
	}
	cout << "└──────────────────────┘" << endl;
}

void StartGame(Player* player, bool* flag)//开始游戏
{
	int i = 0;
	//bool flag = false;//每一关是否闯关成功
	char yes_no;

	system("@color 06");//黄色前景色
	cout << "┌──────────────────────★游戏说明★───────────────────────┐" << endl;
	cout << "│  使用键盘方向键移动老鼠(※),在规定的时间内走到粮仓(★) │" << endl;
	cout << "└─────────────────────────────────────────────────────────┘" << endl;
	Sleep(1500);



	do
	{
		system("cls");
		cout << "│────★第" << i + 1 << "关★────│" << endl;
		Sleep(1000);
		system("cls");

		player[i].ShowMap();
		flag[i] = player[i].Move();

		if (flag[i] == true)//若当前关卡闯关成功
		{
			system("cls");
			player[i].SaveMap();//保存并展示老鼠的路径
			system("pause");
			if (i == NumberOfCheckpoints - 1)
			{
				cout << "│────★您已通关所有关卡!祝贺您!★────│" << endl;
				system("pause");
				break;
			}

			do
			{
				cout << "┌────★路径生成完毕,请进行你的选择★────┐" << endl;
				cout << "│             ★Y.继续游戏★             │" << endl;
				cout << "│             ★N.结束游戏★             │" << endl;
				cout << "└────────────────────────────────────────┘" << endl;

				cin >> yes_no;
				if (yes_no != 'Y' && yes_no != 'y' && yes_no != 'N' && yes_no != 'n')
				{
					cout << "│────★输入非法!请重新输入!★────│" << endl;
					Sleep(1000);
				}
			} while (yes_no != 'Y' && yes_no != 'y' && yes_no != 'N' && yes_no != 'n');

			if (yes_no == 'Y' || yes_no == 'y')
			{
				i++;//地图下标+1,下一关
			}
			else
			{
				break;
			}

		}

	} while (flag[i - 1] == true && i - 1 < NumberOfCheckpoints);//成功通关且在关卡最大数量限制内


}

void EditorGame(Player* player, bool* flag)//编辑游戏
{
	int myChoice;

	do
	{
		ChooseLevel();

		cin >> myChoice;

		if (myChoice < 1 || myChoice > NumberOfCheckpoints)
		{
			cout << "│────★输入非法!请重新输入!★────│" << endl;
			Sleep(1000);
		}
	} while (myChoice < 1 || myChoice > NumberOfCheckpoints);

	system("@color 0d");//淡紫色前景色
	player[myChoice - 1].EditorMap();//编辑相应的地图
	flag[myChoice - 1] = false;
	system("cls");

	Sleep(500);
}

void ShortestPath(Player player[], bool* flag)//最短路径
{
	int myChoice;

	do
	{
		ChooseLevel();

		cin >> myChoice;

		if (myChoice < 1 || myChoice > NumberOfCheckpoints)
		{
			cout << "│────★输入非法!请重新输入!★────│" << endl;
		}
	} while (myChoice < 1 || myChoice > NumberOfCheckpoints);

	if (flag[myChoice - 1] == true)
	{
		player[myChoice - 1].PreShort(player[myChoice - 1]);//调用该对象的前置最短路径方法
		system("cls");
	}
	else
	{
		cout << "│────★请先通关本关卡再看最短路径!★────│" << endl;
		Sleep(1000);
	}


}

void GameDescription()//游戏说明
{
	system("cls");
	cout << "│────★游戏说明★────│" << endl;
	cout << "│────★1.本游戏提供了随机生成迷宫、编辑迷宫、查看最短路径、查看所有路径等功能★────│" << endl;
	cout << "│────★2.玩家需先通过关卡才能够查看最短路径和所有路径,编辑地图后也需要先通关再查看最短路径和所有路径★────│" << endl;
	cout << "│────★3.通过键盘的方向键进行老鼠(※)的移动,到达粮仓(★)即为闯关成功且进入下一关,关卡耗时越少分数越高;规定时间内未能到达粮仓则闯关失败★────│" << endl;
	cout << "│────★4.编辑地图时通过WASD键控制老鼠的上下左右方向是否生产墙或通路★────│" << endl;
	cout << "│────★5.编辑地图时应保证至少有一条通路,编辑地图时可以编辑多条通路,若为死路则无法通关★────│" << endl;
	system("pause");
}

void ExitGame()//退出游戏
{
	system("@color 0c");//淡红色前景色
	cout << "│────★游戏结束,感谢体验★────│" << endl;
	system("pause");
}

void Hidden()//隐藏光标
{
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO cci;
	GetConsoleCursorInfo(hOut, &cci);
	cci.bVisible = 0;
	SetConsoleCursorInfo(hOut, &cci);
}

void ShowAll(Player player[], bool* flag)//展示所有路径
{
	system("cls");

	int myChoice;

	do
	{
		ChooseLevel();

		cin >> myChoice;

		if (myChoice < 1 || myChoice > NumberOfCheckpoints)
		{
			cout << "│────★输入非法!请重新输入!★────│" << endl;
		}

	} while (myChoice < 1 || myChoice > NumberOfCheckpoints);

	if (flag[myChoice - 1] == true)
	{
		for (int i = 1; i <= DefaultSize; i++)
		{
			for (int j = 1; j <= DefaultSize; j++)
			{
				if (player[myChoice - 1].Map[i][j].data == 4)
				{
					player[myChoice - 1].Map[i][j].data = 0;
				}
			}
		}

		player[myChoice - 1].pathCount = 0;
		player[myChoice - 1].ShowAllPath(DefaultSize / 2 + 1, DefaultSize / 2 + 1);//调用该对象的所有短路径方法
		system("pause");
	}
	else
	{
		cout << "│────★请先通关本关卡再看所有路径!★────│" << endl;
		Sleep(1000);
	}
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件

2.PlayGameFunction.cpp

#include <iostream>
#include <windows.h>
#include <conio.h>
#include <time.h>
#include "PlayGame.h"

Player::Player()//无参构造函数
{
	int i, j;
	this->mapLength = DefaultSize;//初始化地图的高度
	this->mapWidth = DefaultSize;//初始化地图的宽度
	this->pathCount = 0;

	for (i = 1; i <= this->mapLength; i++)//1~mapLength是需要的范围
	{
		for (j = 1; j <= this->mapWidth; j++)
		{
			this->Map[i][j].data = 1;//墙
		}
	}

	for (int i = 0; i < MaxSize; i++)
	{
		for (int j = 0; j < MaxSize; j++)
		{
			this->Map[i][j].visited = 0;//最大限度内,地图的所有点都默认未被访问过
		}
	}

	this->front = -1;
	this->rear = -1;
	this->top = -1;
}

Player::Player(int m, int n)//有参构造函数
{
	int i, j;
	this->mapLength = m;//初始化地图的高度
	this->mapWidth = n;//初始化地图的宽度
	this->pathCount = 0;

	for (i = 1; i <= this->mapLength + 1; i++)//1~mapLength是需要的范围
	{
		for (j = 1; j <= this->mapWidth + 1; j++)
		{
			if (i == 0 || i == this->mapLength + 1 || j == 0 || j == this->mapWidth + 1)
			{
				this->Map[i][j].data = 0;//通路
			}
			else
			{
				this->Map[i][j].data = 1;//墙
			}
		}
	}

	for (int i = 0; i < MaxSize; i++)
	{
		for (int j = 0; j < MaxSize; j++)
		{
			this->Map[i][j].visited = 0;//最大限度内,地图的所有点都默认未被访问过
		}
	}

	this->front = -1;
	this->rear = -1;
	this->top = -1;
}

void Player::Push()//入栈
{
	this->front = this->rear + 1;
	this->rear = 0;
	this->top = -1;

	this->mapStack[++this->top] = this->mapQueue[front - 1];//??

	int direction[4][2] = { 1, 0, 0, 1, 0, -1, -1, 0 };//定义方向向量

	while (this->front != this->rear)//队列不为空时
	{
		this->front--;
		for (int i = 0; i < 4; i++)
		{
			//当栈顶的x坐标+方向向量第1列 == 队列队首的x坐标 且 栈顶的y坐标+方向向量第2列 == 队列队首的y坐标
			if (this->mapStack[top].positionX + direction[i][0] == this->mapQueue[this->front - 1].positionX && this->mapStack[top].positionY + direction[i][1] == this->mapQueue[this->front - 1].positionY)
			{
				this->mapStack[++top] = this->mapQueue[this->front - 1];//将队列的队首入栈
			}
		}
	}
}

void Player::Show()//展示最短路径
{
	system("cls");

	for (int i = 0; i <= this->top; i++)
	{
		if (this->Map[this->mapStack[i].positionX][this->mapStack[i].positionY].data != 2 && this->Map[this->mapStack[i].positionX][this->mapStack[i].positionY].data != 3)
		{
			this->Map[this->mapStack[i].positionX][this->mapStack[i].positionY].data = 4;
		}
	}
	this->SaveMap();

	system("pause");
}

bool Player::Move()//老鼠的移动
{
	bool flag = true;

	time_t startTime, endTime;

	int score = 100;//关卡初始分数
	char enter;
	int countDownTime = 20;//倒计时
	int timeDifference;//时间差
	int b = 0;
	int c = this->mapLength / 2 + 1;//地图高度的中点
	int d = this->mapWidth / 2 + 1;//地图宽度的中点
	int i, j;

	startTime = time(NULL);//获取系统当前时间并且startTime不变

	for (int i = 1; i <= this->mapLength; i++)
	{
		for (int j = 1; j <= this->mapWidth; j++)
		{
			if (this->Map[i][j].data == 4)
			{
				this->Map[i][j].data = 0;
			}
		}
	}

	while (countDownTime >= 0)
	{
		endTime = time(NULL);//同样的,获取系统当前时间
		timeDifference = endTime - startTime;//时间差(递增)

		if (_kbhit() == 0)//键盘输入事件未发生时
		{
			if (b != timeDifference)
			{
				system("cls");

				this->PrintMap();

				cout << endl << "│────★倒计时:" << countDownTime-- << "秒★────│" << endl;
				b = timeDifference;

				if (countDownTime == -1)
				{
					system("cls");
					cout << "│────★闯关失败!★────│" << endl;
					system("pause");
					flag = false;
					break;
				}
			}
		}

		if (_kbhit() != 0)//键盘输入事件发生时
		{
			enter = _getch();
			system("cls");
			if (enter == -32)//当键盘事件为方向键或功能键时,必须调用两次_getch()函数,第一次调用返回0或0xe0(转换为-32),第二次返回实际的键代码
			{
				enter = _getch();//实际键盘代码
				if (enter == 75)//左方向键
				{
					if (this->Map[c][d - 1].data != 1 && this->MoveJudge(c, d - 1) == true)//左边不是墙
					{
						this->Map[c][d - 1].data = 2;//即将走的路标记为老鼠
						this->Map[c][d].data = 4;//之前的路标记为路径
						d -= 1;//移动成功,列标-1
					}
				}
				else if (enter == 77)//右方向键
				{
					if (this->Map[c][d + 1].data != 1 && this->MoveJudge(c, d + 1) == true)//右边不是墙
					{
						this->Map[c][d + 1].data = 2;
						this->Map[c][d].data = 4;
						d += 1;
					}
				}
				else if (enter == 72)//上方向键
				{
					if (this->Map[c - 1][d].data != 1 && this->MoveJudge(c - 1, d) == true)//上边不是墙
					{
						this->Map[c - 1][d].data = 2;
						this->Map[c][d].data = 4;
						c -= 1;
					}
				}
				else if (enter == 80)//下方向键
				{
					if (Map[c + 1][d].data != 1 && this->MoveJudge(c + 1, d) == true)//下边不是墙
					{
						this->Map[c + 1][d].data = 2;
						this->Map[c][d].data = 4;
						c += 1;
					}
				}
			}

			this->PrintMap();

			if (this->Map[this->mapLength - 1][this->mapLength - 1].data != 3)//当原本粮仓的位置变成老鼠
			{
				system("cls");
				cout << "│────★恭喜闯关成功!★────│" << endl;
				score -= timeDifference;
				cout << "│────★本关卡耗时:" << timeDifference << "秒" << endl;
				cout << "│────★本关卡得分:" << score << endl;
				flag = true;
				Sleep(1000);
				cout << "│────★正在生成路径.";
				Sleep(500);
				cout << ".";
				Sleep(500);
				cout << ".";
				Sleep(500);
				break;
			}
		}
	}

	return flag;
}

void Player::SaveMap()//保存老鼠的移动路径
{
	for (int i = 1; i <= this->mapLength; i++)
	{
		for (int j = 1; j <= this->mapWidth; j++)
		{
			switch (this->Map[i][j].data)
			{
			case 1:
				cout << "■";
				break;
			case 2:
				cout << "※";
				break;
			case 3:
				cout << "★";
				break;
			case 4:
				cout << "×";//经过的路径标记为×
				break;
			case 0:
				cout << "  ";
				break;
			default:
				break;
			}
		}
		cout << endl;
	}
}

void Player::ShowMap()//展示初始地图
{
	//srand((unsigned)time(NULL));//使用随机数种子,确保每次程序启动生成的随机数(rand)都是不一样的

	this->Generate(2 * (rand() % (this->mapLength / 2 + 1)), 2 * (rand() % (this->mapWidth / 2 + 1)));//调用生成地图函数,参数为一个随机的点
	this->Map[this->mapLength / 2 + 1][this->mapWidth / 2 + 1].data = 2;//老鼠的初始位置
	this->Map[this->mapLength - 1][this->mapWidth - 1].data = 3;//粮仓的初始位置

	this->PrintMap();
}

void Player::PreShort(Player player)//最短路径的前置函数
{
	player.front = -1;
	player.rear = -1;

	for (int i = 1; i <= player.mapLength; i++)//将走过的路径置为0,否则最短路径将会显示为走过的路径
	{
		for (int j = 1; j <= player.mapWidth; j++)
		{
			if (player.Map[i][j].data == 4)
			{
				player.Map[i][j].data = 0;
			}
		}
	}

	player.ShowMap();//展示初始地图

	system("cls");

	int m = player.mapLength - 1, n = player.mapWidth - 1;

	MapPoint p;//定义一个点的结构(终点)
	p.positionX = m;
	p.positionY = n;
	p.visited = 1;
	p.data = 3;
	player.ShortMap(p);//将终点传入最短路径函数
	player.Show();
}

void Player::EditorMap()//编辑地图
{
	int c = this->mapLength / 2 + 1;
	int d = this->mapWidth / 2 + 1;
	this->ShowMap();//展示初始地图
	system("cls");
	char enter;
	while (true)
	{
		this->Map[this->mapLength - 1][this->mapWidth - 1].data = 3;//避免走到终点时覆盖终点
		this->PrintMap();
		cout << "│────★按下回车键保存修改★────│" << endl;

		enter = _getch();
		system("cls");
		if (enter == -32)//当键盘事件为方向键或功能键时,必须调用两次_getch()函数,第一次调用返回0或0xe0(转换为 - 32),第二次返回实际的键代码
		{
			enter = _getch();//实际键盘代码
			if (enter == 75)//左方向键
			{
				if (this->Map[c][d - 1].data != 1)
				{
					this->Map[c][d - 1].data = 2;
					this->Map[c][d].data = 0;
					d -= 1;
				}
			}
			else if (enter == 77)//右方向键
			{
				if (this->Map[c][d + 1].data != 1)
				{
					this->Map[c][d + 1].data = 2;
					this->Map[c][d].data = 0;
					d += 1;
				}
			}
			else if (enter == 72)//上方向键
			{
				if (this->Map[c - 1][d].data != 1)
				{
					this->Map[c - 1][d].data = 2;
					this->Map[c][d].data = 0;
					c -= 1;
				}
			}
			else if (enter == 80)//下方向键
			{
				if (this->Map[c + 1][d].data != 1)
				{
					this->Map[c + 1][d].data = 2;
					this->Map[c][d].data = 0;
					c += 1;
				}
			}
		}

		if (enter == 97)//A键
		{
			if (this->Map[c][d - 1].data == 1)//墙变路
			{
				this->Map[c][d - 1].data = 0;
			}
			else if (this->Map[c][d - 1].data == 0 || this->Map[c][d - 1].data == 4)//路变墙(包括走过的路)
			{
				this->Map[c][d - 1].data = 1;
			}
		}
		else if (enter == 119)//W键
		{
			if (this->Map[c - 1][d].data == 1)//墙变路
			{
				this->Map[c - 1][d].data = 0;
			}
			else if (this->Map[c - 1][d].data == 0 || this->Map[c - 1][d].data == 4)//路变墙(包括走过的路)
			{
				this->Map[c - 1][d].data = 1;
			}
		}
		else if (enter == 100)//D键
		{
			if (this->Map[c][d + 1].data == 1)//墙变路
			{
				this->Map[c][d + 1].data = 0;
			}
			else if (this->Map[c][d + 1].data == 0 || this->Map[c][d + 1].data == 4)//路变墙(包括走过的路)
			{
				this->Map[c][d + 1].data = 1;
			}
		}
		else if (enter == 115)//S键
		{
			if (this->Map[c + 1][d].data == 1)//墙变路
			{
				this->Map[c + 1][d].data = 0;
			}
			else if (this->Map[c + 1][d].data == 0 || this->Map[c + 1][d].data == 4)//路变墙(包括走过的路)
			{
				this->Map[c + 1][d].data = 1;
			}
		}
		else if (enter == 0x0D)//按下回车键
		{
			this->Map[c][d].data = 0;//老鼠所在位置变成路
			break;
		}
	}
}

void Player::ShortMap(MapPoint& Map)//最短路径,参数为终点结构的引用
{
	/*
		核心思想:图的广度优先算法寻找最短路径
		1.首先将终点Map入队
		2.从终点出发,所有与终点邻接的非墙的点与队首一起入队
		3.将其标记为1
	*/
	bool flag = false;

	Map.visited = 1;//终点标记为1

	this->PrintMap();//首先打印地图

	this->front = -1;
	this->rear = -1;

	this->mapQueue[++this->rear] = Map;

	int direction[4][2] = { 1, 0, 0, 1, 0, -1, -1, 0 };//定义方向向量
	while (this->rear != this->front)//当队列不为空时
	{
		this->front++;

		for (int j = 0; j < 4; j++)
		{
			bool flag1 = false, flag2 = false, flag3 = false, flag4 = false, flag5 = false, flag6 = false;
			if (this->Map[this->mapQueue[this->front].positionX + direction[j][0]][this->mapQueue[this->front].positionY + direction[j][1]].data == 0)
			{
				flag1 = true;
			}
			if (this->Map[this->mapQueue[this->front].positionX + direction[j][0]][this->mapQueue[this->front].positionY + direction[j][1]].data == 2)
			{
				flag2 = true;
			}
			if (this->Map[this->mapQueue[this->front].positionX + direction[j][0]][this->mapQueue[this->front].positionY + direction[j][1]].data == 4)
			{
				flag3 = true;
			}
			if (this->Map[this->mapQueue[this->front].positionX + direction[j][0]][this->mapQueue[this->front].positionY + direction[j][1]].visited == 0)
			{
				flag4 = true;
			}
			if (this->mapQueue[this->front].positionX < this->mapWidth && this->mapQueue[this->front].positionX >= 1)
			{
				flag5 = true;
			}
			if (this->mapQueue[this->front].positionY < this->mapLength && this->mapQueue[this->front].positionY >= 1)
			{
				flag6 = true;
			}
			if ((flag1 == true || flag2 == true || flag3 == true) && flag4 == true && flag5 == true && flag6 == true)
			{
				this->rear++;

				this->mapQueue[this->rear].positionX = this->mapQueue[this->front].positionX + direction[j][0];
				this->mapQueue[this->rear].positionY = this->mapQueue[this->front].positionY + direction[j][1];

				this->Map[this->mapQueue[this->front].positionX + direction[j][0]][this->mapQueue[this->front].positionY + direction[j][1]].visited = 1;

				if (this->mapQueue[this->rear].positionX == (this->mapLength / 2 + 1) && this->mapQueue[this->rear].positionY == (this->mapWidth / 2 + 1))//当地图队列队尾的坐标等于老鼠的坐标时
				{
					flag = true;
					break;
				}
			}
		}

		if (flag == true)
		{
			break;
		}
	}
	this->Push();
}

void Player::ShowAllPath(int x, int y)//展示所有路径
{
	int i, j;

	this->Map[x][y].data = 4;

	if (x == DefaultSize - 1 && y == DefaultSize - 1)
	{
		cout << "发现第" << ++this->pathCount << "条路径" << endl;
		this->Map[this->mapLength / 2 + 1][this->mapWidth / 2 + 1].data = 2;
		this->Map[x][y].data = 3;
		for (i = 1; i <= this->mapLength; i++)
		{
			for (j = 1; j <= this->mapWidth; j++)
			{
				switch (this->Map[i][j].data)
				{
				case 1:
					cout << "■";
					break;
				case 2:
					cout << "※";
					break;
				case 3:
					cout << "★";
					break;
				case 4:
					cout << "×";
					break;
				case 0:
					cout << "  ";
					break;
				default:
					break;
				}
			}
			cout << endl;
		}

	}

	this->Map[this->mapLength / 2 + 1][this->mapWidth / 2 + 1].data = 4;
	this->Map[x][y].data = 4;

	if (this->Map[x][y + 1].data == 0 || this->Map[x][y + 1].data == 2)//右
	{
		this->ShowAllPath(x, y + 1);
	}
	if (this->Map[x + 1][y].data == 0 || this->Map[x + 1][y].data == 2)//下
	{
		this->ShowAllPath(x + 1, y);
	}
	if (this->Map[x][y - 1].data == 0 || this->Map[x][y - 1].data == 2)//左
	{
		this->ShowAllPath(x, y - 1);
	}
	if (this->Map[x - 1][y].data == 0 || this->Map[x - 1][y].data == 2)//上
	{
		this->ShowAllPath(x - 1, y);
	}
	this->Map[x][y].data = 0;
}

void Player::Generate(int m, int n)//地图的初始化过程,参数为一个随机的点
{
	int direction[4][2] = { 1, 0, 0, 1, 0, -1, -1, 0 };//定义方向向量
	int i, j, temp;
	for (i = 0; i < 4; i++)//打乱方向向量
	{
		j = rand() % 4;//随机生成方向向量
		temp = direction[i][0];
		direction[i][0] = direction[j][0];
		direction[j][0] = temp;

		temp = direction[i][1];
		direction[i][1] = direction[j][1];
		direction[j][1] = temp;
	}

	this->Map[m][n].data = 0;//传入的点设为通路

	for (i = 0; i < 4; i++)//任何两个空的地方都会有路可走
	{
		if (this->Map[m + 2 * direction[i][0]][n + 2 * direction[i][1]].data == 1)//当传入的点+随机方向向量为墙时
		{
			this->Map[m + direction[i][0]][n + direction[i][1]].data = 0;//打通墙,使墙变通路
			this->Generate(m + 2 * direction[i][0], n + 2 * direction[i][1]);//递归调用
		}
	}
}

void Player::PrintMap()//打印地图
{
	for (int i = 1; i <= this->mapWidth + 1; i++)//确保迷宫的上面都是墙,没有缺口
	{
		if (this->Map[1][i].data != 1)
		{
			this->Map[1][i].data = 1;
		}
	}
	for (int i = 1; i <= this->mapWidth + 1; i++)//确保迷宫的下面都是墙,没有缺口
	{
		if (this->Map[this->mapLength][i].data != 1)
		{
			this->Map[this->mapLength][i].data = 1;
		}
	}
	for (int i = 1; i <= this->mapLength + 1; i++)//确保迷宫的左面都是墙,没有缺口
	{
		if (this->Map[i][1].data != 1)
		{
			this->Map[i][1].data = 1;
		}
	}
	for (int i = 1; i <= this->mapLength + 1; i++)//确保迷宫的右面都是墙,没有缺口
	{
		if (this->Map[i][this->mapWidth].data != 1)
		{
			this->Map[i][this->mapWidth].data = 1;
		}
	}

	for (int i = 1; i <= this->mapLength; i++)
	{
		for (int j = 1; j <= this->mapLength; j++)
		{
			switch (this->Map[i][j].data)
			{
			case 1:
				cout << "■";
				break;
			case 2:
				cout << "※";
				break;
			case 3:
				cout << "★";
				break;
			case 4:
				cout << "  ";
				break;
			case 0:
				cout << "  ";
				break;
			default:
				break;
			}
		}
		cout << endl;
	}
}

bool Player::MoveJudge(int x, int y)//移动判断,是否走出边界
{
	bool flag = true;
	if ((x < 1 || x > DefaultSize) || (y < 1 || y > DefaultSize))
	{
		flag = false;
	}
	return flag;
}

3.PlayGame.h

#pragma once
#ifndef PLAYGAME_H
#define PLAYGAME_H

using namespace std;

const int MaxSize = 100;
const int NumberOfCheckpoints = 5;//最大关卡数
const int DefaultSize = 11;//默认地图尺寸

struct MapPoint
{
	int data;
	int positionX;
	int positionY;//路径的坐标
	int visited;//是否被访问过
};

class Player
{
private:
	int top;
	int rear, front;
	int mapLength, mapWidth;//地图的尺寸

	MapPoint mapStack[MaxSize];//地图栈
	MapPoint mapQueue[MaxSize];//地图队列
public:
	Player();//无参构造函数
	Player(int m, int n);//有参构造函数,手动设置地图的尺寸
	void Push();//入栈
	void Show();//展示最短路径
	bool Move();//移动
	void SaveMap();//保存老鼠的移动路径
	void ShowMap();//显示地图
	void PreShort(Player player);//最短路径的前置方法
	void EditorMap();//编辑地图
	void ShortMap(MapPoint& Map);//求最短路径
	void Generate(int x, int y);//生成地图
	void PrintMap();//打印地图
	bool MoveJudge(int x, int y);//移动判断

	void ShowAllPath(int x, int y);//展示所有路径
	int pathCount;//所有路径的数量

	MapPoint Map[MaxSize][MaxSize];//地图数组
};

#endif

注意事项

迷宫的生成默认只有一条通路,所有路径和最短路径是一样的。若要体现所有路径,可以先手动编辑地图使其有多条通路,然后通过此关卡再查看所有路径。编辑地图的具体操作可以查看游戏说明获取帮助。
博主使用的是VS2019开发环境,编译器不同可能会有编译错误

  • 39
    点赞
  • 248
    收藏
    觉得还不错? 一键收藏
  • 24
    评论
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值