主頁 >  其他 > 資料結構與演算法課程設計——C++迷宮游戲

資料結構與演算法課程設計——C++迷宮游戲

2021-01-07 12:02:20 其他

問題描述

程式開始運行時顯示一個迷宮地圖,迷宮中央有一只老鼠,迷宮的右下方有一個糧倉,游戲的任務是使用鍵盤上的方向健操縱老鼠在規定的時間內走到糧倉處,

基本要求

(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();//隱藏游標

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':
			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 << "│                ★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;
	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);
}

// 運行程式: 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;//初始化地圖的寬度

	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;//初始化地圖的寬度

	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::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);//移動判斷

	MapPoint Map[MaxSize][MaxSize];//地圖陣列
};

#endif

注意事項

博主使用的是VS2019開發環境,編譯器不同可能會有編譯錯誤

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/245757.html

標籤:其他

上一篇:NEUQ-ACM實驗班訓練001題解

下一篇:一文教你用java實作俄羅斯方塊專案

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布
  • 2023年最新微信小程式抓包教程

    01 開門見山 隔一個月發一篇文章,不過分。 首先回顧一下《微信系結手機號資料庫被脫庫事件》,我也是第一時間得知了這個訊息,然后跟蹤了整件事情的經過。下面是這起事件的相關截圖以及近日流出的一萬條資料樣本: 個人認為這件事也沒什么,還不如關注一下之前45億快遞資料查詢渠道疑似在近日復活的訊息。 訊息是 ......

    uj5u.com 2023-04-20 08:48:24 more
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

    Metamask錢包是一種基于區塊鏈技術的數字貨幣錢包,它允許用戶在安全、便捷的環境下管理自己的加密資產。Metamask錢包是以太坊生態系統中最流行的錢包之一,它具有易于使用、安全性高和功能強大等優點。 本文將詳細介紹Metamask錢包的功能和使用方法。 一、 Metamask錢包的功能 數字資 ......

    uj5u.com 2023-04-20 08:47:46 more
  • vulnhub_Earth

    前言 靶機地址->>>vulnhub_Earth 攻擊機ip:192.168.20.121 靶機ip:192.168.20.122 參考文章 https://www.cnblogs.com/Jing-X/archive/2022/04/03/16097695.html https://www.cnb ......

    uj5u.com 2023-04-20 07:46:20 more
  • 從4k到42k,軟體測驗工程師的漲薪史,給我看哭了

    清明節一過,盲猜大家已經無心上班,在數著日子準備過五一,但一想到銀行卡里的余額……瞬間心情就不美麗了。最近,2023年高校畢業生就業調查顯示,本科畢業月平均起薪為5825元。調查一出,便有很多同學表示自己又被平均了。看著這一資料,不免讓人想到前不久中國青年報的一項調查:近六成大學生認為畢業10年內會 ......

    uj5u.com 2023-04-20 07:44:00 more
  • 最新版本 Stable Diffusion 開源 AI 繪畫工具之中文自動提詞篇

    🎈 標簽生成器 由于輸入正向提示詞 prompt 和反向提示詞 negative prompt 都是使用英文,所以對學習母語的我們非常不友好 使用網址:https://tinygeeker.github.io/p/ai-prompt-generator 這個網址是為了讓大家在使用 AI 繪畫的時候 ......

    uj5u.com 2023-04-20 07:43:36 more
  • 漫談前端自動化測驗演進之路及測驗工具分析

    隨著前端技術的不斷發展和應用程式的日益復雜,前端自動化測驗也在不斷演進。隨著 Web 應用程式變得越來越復雜,自動化測驗的需求也越來越高。如今,自動化測驗已經成為 Web 應用程式開發程序中不可或缺的一部分,它們可以幫助開發人員更快地發現和修復錯誤,提高應用程式的性能和可靠性。 ......

    uj5u.com 2023-04-20 07:43:16 more
  • CANN開發實踐:4個DVPP記憶體問題的典型案例解讀

    摘要:由于DVPP媒體資料處理功能對存放輸入、輸出資料的記憶體有更高的要求(例如,記憶體首地址128位元組對齊),因此需呼叫專用的記憶體申請介面,那么本期就分享幾個關于DVPP記憶體問題的典型案例,并給出原因分析及解決方法。 本文分享自華為云社區《FAQ_DVPP記憶體問題案例》,作者:昇騰CANN。 DVPP ......

    uj5u.com 2023-04-20 07:43:03 more
  • msf學習

    msf學習 以kali自帶的msf為例 一、msf核心模塊與功能 msf模塊都放在/usr/share/metasploit-framework/modules目錄下 1、auxiliary 輔助模塊,輔助滲透(埠掃描、登錄密碼爆破、漏洞驗證等) 2、encoders 編碼器模塊,主要包含各種編碼 ......

    uj5u.com 2023-04-20 07:42:59 more
  • Halcon軟體安裝與界面簡介

    1. 下載Halcon17版本到到本地 2. 雙擊安裝包后 3. 步驟如下 1.2 Halcon軟體安裝 界面分為四大塊 1. Halcon的五個助手 1) 影像采集助手:與相機連接,設定相機引數,采集影像 2) 標定助手:九點標定或是其它的標定,生成標定檔案及內參外參,可以將像素單位轉換為長度單位 ......

    uj5u.com 2023-04-20 07:42:17 more
  • 在MacOS下使用Unity3D開發游戲

    第一次發博客,先發一下我的游戲開發環境吧。 去年2月份買了一臺MacBookPro2021 M1pro(以下簡稱mbp),這一年來一直在用mbp開發游戲。我大致分享一下我的開發工具以及使用體驗。 1、Unity 官網鏈接: https://unity.cn/releases 我一般使用的Apple ......

    uj5u.com 2023-04-20 07:40:19 more