問題描述
程式開始運行時顯示一個迷宮地圖,迷宮中央有一只老鼠,迷宮的右下方有一個糧倉,游戲的任務是使用鍵盤上的方向健操縱老鼠在規定的時間內走到糧倉處,
基本要求
(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
標籤:其他
