C++實(shí)現(xiàn)簡單迷宮游戲
本文實(shí)例為大家分享了C++實(shí)現(xiàn)簡單迷宮游戲的具體代碼,供大家參考,具體內(nèi)容如下
問題描述
程序開始運(yùn)行時顯示一個迷宮地圖,迷宮中央有一只老鼠,迷宮的右下方有一個糧倉。游戲的任務(wù)是使用鍵盤上的方向健操縱老鼠在規(guī)定的時間內(nèi)走到糧倉處。
基本要求
(1)老鼠形象可以辨認(rèn),可用鍵盤操縱老鼠上下左右移動;
(2)迷宮的墻足夠結(jié)實(shí),老鼠不能穿墻而過;
(3)正確檢測結(jié)果,若老鼠在規(guī)定時間內(nèi)走到糧倉處,提示 成功,并給出一條路徑,否則提示失?。?br />
(4)添加編輯迷宮功能,可修改當(dāng)前迷宮,修改內(nèi)容:墻變路、路變墻;
提高要求
(1)增加闖關(guān)和計分功能;
(2)找出走出迷宮的所有路徑及最短路徑。
源代碼
1.文件main.cpp
#include<iostream>
#include"maze.h"http://自定義的頭文件,存放類聲明和常量
#include<cstdlib>//各種函數(shù)頭文件,如rand函數(shù)
#include<time.h>//日期和時間的頭文件
#include<stdio.h>//標(biāo)準(zhǔn)輸入輸出
#include<Windows.h>//圖形界面設(shè)計頭文件
#include<conio.h>//獲取方向鍵的頭文件
using namespace std;
void MainMenu();//顯示游戲菜單函數(shù)
void MeunGame(int num);//顯示不同關(guān)卡函數(shù)
void Introduce();//游戲介紹
int main()
{
Maze maze_1(11, 11), maze_2(13, 13), maze_3(15, 15), maze_4(17, 17);//定義四個關(guān)卡
int i = 0, choice = 0, choice_2, choice_3;
char isexit = ' ', choice_1 = ' ';
bool Go_on = false, judge[4] = { false };
cout << "\n\t歡迎試玩憨憨老鼠走迷宮游戲";
for (i = 0;i < 4;i++)
{
Sleep(500);
cout << "·";
}
system("cls");
Introduce();//游戲介紹
system("cls");
MainMenu();//顯示游戲菜單
cout << "\n\t請輸入選擇:(1~4)";
cin >> choice;
while (1)
{
switch (choice)
{
case 1:
for (i = 1;i <= 4;i++)
{
if (i == 1)
{
MeunGame(i);//顯示關(guān)卡菜單
system("cls");
maze_1.Show_Map();
judge[0] = maze_1.Move();
if (judge[0] == true)
{
cout << "憨憨鼠所走過的路徑";
maze_1.KeepMap();
cout << "是否繼續(xù)闖關(guān):(Y|N)" << endl;
cin >> choice_1;
if (choice_1 == 'y' || choice_1 == 'Y')
{
Go_on = true;
}
else if (choice_1 == 'n' || choice_1 == 'N')
{
Go_on = false;
}
}
else
{
system("pause");
break;
}
}
else if (i == 2)
{
MeunGame(i);
system("cls");
maze_2.Show_Map();
judge[1] = maze_2.Move();
if (judge[1] == true)
{
cout << "憨憨鼠所走過的路徑";
maze_2.KeepMap();
cout << "是否繼續(xù)闖關(guān):(Y|N)" << endl;
cin >> choice_1;
if (choice_1 == 'y' || choice_1 == 'Y')
{
Go_on = true;
}
else if (choice_1 == 'n' || choice_1 == 'N')
{
Go_on = false;
}
}
else
{
system("pause");
break;
}
}
else if (i == 3)
{
MeunGame(i);
system("cls");
maze_3.Show_Map();
judge[2] = maze_3.Move();
if (judge[2] == true)
{
cout << "憨憨鼠所走過的路徑";
maze_3.KeepMap();
cout << "是否繼續(xù)闖關(guān):(Y|N)" << endl;
cin >> choice_1;
if (choice_1 == 'y' || choice_1 == 'Y')
{
Go_on = true;
}
else if (choice_1 == 'n' || choice_1 == 'N')
{
Go_on = false;
}
}
else
{
system("pause");
break;
}
}
else if (i == 4)
{
MeunGame(i);
system("cls");
maze_4.Show_Map();
judge[3] = maze_4.Move();
if (judge[3] == true)
{
cout << "憨憨鼠所走過的路徑";
maze_4.KeepMap();
system("pause");
system("cls");
cout << "\t★太棒了,恭喜你通過全部關(guān)卡★" << endl;
cout << "\t 是否退出游戲?(Y|N)" << endl;
cin >> choice_1;
if (choice_1 == 'y' || choice_1 == 'Y')
{
return 0;
}
else if (choice_1 == 'n' || choice_1 == 'N')
{
Go_on = false;
}
}
else
{
system("pause");
break;
}
}
if (Go_on == false)
{
break;
}
}
break;
case 2:
system("cls");
cout << "請你輸入要編輯的關(guān)卡:" << endl;
cout << "1.第一關(guān)" << endl;
cout << "2.第二關(guān)" << endl;
cout << "3.第三關(guān)" << endl;
cout << "4.第四關(guān)" << endl;
cin >> choice_2;
if (choice_2 == 1 && judge[0] == true)
{
maze_1.EdidMap();
}
else if (choice_2 == 2 && judge[1] == true)
{
maze_2.EdidMap();
}
else if (choice_2 == 3 && judge[2] == true)
{
maze_3.EdidMap();
}
else if (choice_2 == 4 && judge[3] == true)
{
maze_4.EdidMap();
}
else
{
cout << "此關(guān)卡未通過,不能編譯此關(guān)!" << endl;
system("pause");
}
break;
case 3:
system("cls");
cout << "請你輸入要查看的關(guān)卡" << endl;
cout << "1.第一關(guān)" << endl;
cout << "2.第二關(guān)" << endl;
cout << "3.第三關(guān)" << endl;
cout << "4.第四關(guān)" << endl;
cin >> choice_3;
if (choice_3 == 1 && judge[0] == true)
{
maze_1.Short();
}
else if (choice_3 == 2 && judge[1] == true)
{
maze_2.Short();
}
else if (choice_3 == 3 && judge[2] == true)
{
maze_3.Short();
}
else if (choice_3 == 4 && judge[3] == true)
{
maze_4.Short();
}
else
{
cout << "此關(guān)卡未通過,不能查看此關(guān)!" << endl;
system("pause");
}
break;
case 4:
system("cls");
cout << "\n\n\t\t是否確定退出游戲?(Y|N)";
cin >> isexit;
if (isexit == 'Y' || isexit == 'y')
{
return 0;
}
else if (isexit == 'N' || isexit == 'n')
{
break;
}
default:
cout << "\n\t輸入選擇選擇無效,請從新輸入:";
Sleep(500);
break;
}
system("cls");
MainMenu();
cout << "\n\t請輸入輸入選擇:(1~4)";
cin >> choice;
}
}
void MainMenu()//游戲菜單函數(shù)
{
cout << "\n\t\t**************************************************************" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t* *老鼠走迷宮游戲* *" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t* 1.開始游戲 *" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t* 2.編輯地圖 *" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t* 3.破解地圖 *" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t* 4.退出游戲 *" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t**************************************************************" << endl;
}
void MeunGame(int num)//關(guān)卡菜單
{
system("cls");
cout << "\n\t\t**************************************************************" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t* ◤ 第" << num << "關(guān) ◢ *" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t**************************************************************" << endl;
system("pause");
}
void Introduce()//游戲介紹
{
cout << "\n\t\t********************************************************************" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t* *老鼠走迷宮游戲介紹* *" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t* 1.玩家可通過方向鍵↑↓←→控制老鼠移動 *" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t* 2.在選擇編輯地圖時,玩家通過WASD編輯 *" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t* 3.在選擇破解地圖時,會給出坐標(biāo)路線,原點(diǎn)為迷宮左上角 *" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t* 4.在規(guī)定時間內(nèi)走到糧倉算過關(guān),時間越短所獲積分越多 *" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t********************************************************************" << endl;
system("pause");
}
2.maze.cpp文件
#include<iostream>
#include"maze.h"http://自定義的頭文件,存放類聲明和常量
#include<cstdlib>//各種函數(shù)頭文件,如rand函數(shù)
#include<time.h>//日期和時間的頭文件
#include<stdio.h>//標(biāo)準(zhǔn)輸入輸出
#include<Windows.h>//圖形界面設(shè)計頭文件
#include<conio.h>//獲取方向鍵的頭文件
using namespace std;
Maze::Maze(int l, int w)//構(gòu)造函數(shù)初始化地圖和成員變量
{
int i, j;
Map_Length = l, Map_Width = w;
for (i = 0;i <= Map_Length;i++)
{
for (j = 0;j <= Map_Width;j++)
{
if (i == 0 || j == 0)
{
Map[i][j] = road;
}
else
{
Map[i][j] = wall;//默認(rèn)地圖中都是墻
}
}
}
for (i = 0;i < Map_Length;i++)
{
for (j = 0;j < Map_Width;j++)
{
Visited[i][j] = 0;
}
}
front = rear = -1;
top = -1;
}
void Maze::CreateMap(int x, int y)//創(chuàng)建地圖
{
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;
}
Map[x][y] = road;//選取[x][y]為路
for (i = 0;i < 4;i++)
{
if (Map[x + 2 * Direction[i][0]][y + 2 * Direction[i][1]] == wall)//任意兩點(diǎn)之間有路
{
Map[x + Direction[i][0]][y + Direction[i][1]] = road;
CreateMap(x + 2 * Direction[i][0], y + 2 * Direction[i][1]);
}
}
}
void Maze::Show_Map()//顯示地圖
{
//srand((unsigned)time(NULL));//種隨機(jī)粒子
CreateMap(2 * (rand() % (Map_Length / 2 + 1)), 2 * (rand() % (Map_Width / 2 + 1)));//隨機(jī)選取x,y坐標(biāo)
Map[Map_Length / 2][Map_Width / 2] = Mouse;//定義老鼠的位置
Map[Map_Length - 1][Map_Width - 1] = End;//定義糧倉的位置
Display();
}
void Maze::Display()//查看地圖
{
int i, j;
for (i = 0;i <= Map_Length;i++)
{
for (j = 0;j <= Map_Width;j++)
{
if (Map[i][j] == road)
{
cout << " ";
}
else if (Map[i][j] == wall)
{
cout << "■";
}
else if (Map[i][j] == Mouse)
{
cout << "♂";
}
else if (Map[i][j] == End)
{
cout << "★";
}
else if (Map[i][j] == visited)
{
cout << " ";
}
}
cout << endl;
}
}
void Maze::KeepMap()//顯示老鼠走過的路徑
{
int i, j;
for (i = 0;i <= Map_Length;i++)
{
for (j = 0;j <= Map_Width;j++)
{
if (Map[i][j] == road)
{
cout << " ";
}
else if (Map[i][j] == wall)
{
cout << "■";
}
else if (Map[i][j] == Mouse)
{
cout << "♂";
}
else if (Map[i][j] == End)
{
cout << "★";
}
else if (Map[i][j] == visited)
{
cout << "×";
}
}
cout << endl;
}
}
bool Maze::Move()//老鼠移動
{
int count = 30, m = 0, n = 0;
bool temp = false;
char Enter = ' ';
int t = time(NULL);//獲取系統(tǒng)時間
pos_x = Map_Length / 2, pos_y = Map_Width / 2;//老鼠的初始位置
while (count >= 0)
{
if (_kbhit() == 0)
{
if (t != time(NULL))
{
system("cls");
Display();
count--;
cout << "|---剩余時間:" << count << "---|";
if (count == 0)
{
system("cls");
cout << "闖關(guān)失敗" << endl;
temp = false;
}
t = time(NULL);//獲取當(dāng)前時間
}
}
if (_kbhit() != 0)
{
system("cls");
Enter = _getch();
if (Enter == -32)//*****鍵盤事件*****
{
switch (_getch())
{
case 72://up
Up();
Display();
break;
case 80://down
Dowm();
Display();
break;
case 75://left
Left();
Display();
break;
case 77://right
Right();
Display();
break;
default:
break;
}
}
if (Map[Map_Length - 1][Map_Width - 1] != End)
{
system("cls");
cout << "★恭喜你闖關(guān)成功★" << endl;
Map[pos_x][pos_y] = visited;
cout << "★獲得" << count << "點(diǎn)積分★" << endl;
temp = true;
break;
}
}
}
return temp;
}
void Maze::Up()//老鼠向上移動
{
if (pos_y <= 0)
{
return;
}
else if (Map[pos_x - 1][pos_y] != wall)
{
Map[pos_x][pos_y] = visited;
pos_x--;
Map[pos_x][pos_y] = Mouse;
}
}
void Maze::Dowm()//老鼠向下移動
{
if (pos_y > Map_Width - 1)
{
return;
}
else if (Map[pos_x + 1][pos_y] != wall)
{
Map[pos_x][pos_y] = visited;
pos_x++;
Map[pos_x][pos_y] = Mouse;
}
}
void Maze::Left()//老鼠向左移動
{
if (pos_x <= 0)
{
return;
}
else if (Map[pos_x][pos_y - 1] != wall)
{
Map[pos_x][pos_y] = visited;
pos_y--;
Map[pos_x][pos_y] = Mouse;
}
}
void Maze::Right()//老鼠向右移動
{
if (pos_x > Map_Width - 1)
{
return;
}
else if (Map[pos_x][pos_y + 1] != wall)
{
Map[pos_x][pos_y] = visited;
pos_y++;
Map[pos_x][pos_y] = Mouse;
}
}
void Maze::EdidMap()//編輯地圖
{
system("cls");
char Enter = ' ';
bool isKeep = false;
pos_x = Map_Length / 2, pos_y = Map_Width / 2;//確定老鼠坐標(biāo)
Map[pos_x][pos_y] = Mouse;
while (1)
{
Display();
Enter = _getch();
if (Enter == -32)//*****鍵盤事件*****
{
switch (_getch())
{
case 72://up
Up();
break;
case 80://down
Dowm();
break;
case 75://left
Left();
break;
case 77://right
Right();
break;
default:
break;
}
}
switch (Enter)
{
case 119://W鍵
if (Map[pos_x - 1][pos_y] == wall)
{
Map[pos_x - 1][pos_y] = road;
}
else if (Map[pos_x - 1][pos_y] == road || Map[pos_x - 1][pos_y] == visited)
{
Map[pos_x - 1][pos_y] = wall;
}
break;
case 97://A鍵
if (Map[pos_x][pos_y - 1] == wall)
{
Map[pos_x][pos_y - 1] = road;
}
else if (Map[pos_x][pos_y - 1] == road || Map[pos_x][pos_y - 1] == visited)
{
Map[pos_x][pos_y - 1] = wall;
}
break;
case 115://S鍵
if (Map[pos_x + 1][pos_y] == wall)
{
Map[pos_x + 1][pos_y] = road;
}
else if (Map[pos_x + 1][pos_y] == road || Map[pos_x + 1][pos_y] == visited)
{
Map[pos_x + 1][pos_y] = wall;
}
break;
case 100://D鍵
if (Map[pos_x][pos_y + 1] == wall)
{
Map[pos_x][pos_y + 1] = road;
}
else if (Map[pos_x][pos_y + 1] == road || Map[pos_x][pos_y + 1] == visited)
{
Map[pos_x][pos_y + 1] = wall;
}
break;
case 0x0D://回車
system("cls");
Map[pos_x][pos_y] = road;
cout << "*****保存成功*****" << endl;
isKeep = true;
system("pause");
break;
default:
break;
}
if (isKeep == true)
{
for (int i = 0;i < Map_Length;i++)
{
for (int j = 0;j < Map_Width;j++)
{
if (Map[i][j] == visited)
{
Map[i][j] = road;
}
}
}
break;
}
system("cls");
}
}
void Maze::Short()
{
rear = front = -1;
int i, j;
for (i = 1;i <= Map_Length;i++)
{
for (j = 1;j <= Map_Width;j++)
{
if (Map[i][j] == visited)
{
Map[i][j] = road;//被訪問的變成路
}
}
}
for (i = 0;i <= Map_Length;i++)
{
for (j = 0;j <= Map_Width;j++)
{
Visited[i][j] = 0;
}
}
Show_Map();//顯示地圖
system("cls");
int m = Map_Length - 1, n = Map_Width - 1;
SmallRoadDisplay(m, n);//找最短路徑
while (top != -1)
{
top--;
}
}
void Maze::SmallRoadDisplay(int x, int y)//最短路徑
{
bool flag = false;
Visited[x - 1][y - 1] = 1;
Map[x][y] = End;
int i = 0, j = 0, k = 0;
int Direction[4][2] = { {1,0}, {0,1}, {0,-1}, {-1,0} };//定義四個方向
int arr[100] = { 0 };//存放x坐標(biāo)的隊(duì)列
int brr[100] = { 0 };//存放y坐標(biāo)的隊(duì)列
int record_x[100] = { 0 };//存放x坐標(biāo)的棧
int record_y[100] = { 0 };//存放y坐標(biāo)的棧
front = rear = -1;
rear++;
arr[rear] = x;//當(dāng)前x坐標(biāo)入隊(duì)
brr[rear] = y;//當(dāng)前y坐標(biāo)入隊(duì)
while (front != rear)
{
front++;
for (i = 0;i < 4;i++)
{
if ((Map[arr[front] + Direction[i][0]][brr[front] + Direction[i][1]] == road || Map[arr[front] + Direction[i][0]][brr[front] + Direction[i][1]] == Mouse || Map[arr[front] + Direction[i][0]][brr[front] + Direction[i][1]] == visited) && Visited[arr[front] + Direction[i][0]][brr[front] + Direction[i][1]] == 0 && arr[front] < Map_Width && arr[front] >= 1 && brr[front] < Map_Length && brr[front] >= 1)
{
rear++;
arr[rear] = arr[front] + Direction[i][0];
brr[rear] = brr[front] + Direction[i][1];
Visited[arr[front] + Direction[i][0]][brr[front] + Direction[i][1]] = 1;
if (arr[rear] == (Map_Length / 2) && brr[rear] == (Map_Width / 2))
{
flag = true;
break;
}
}
}
if (flag == true)
{
break;
}
}
front = rear + 1;
rear = 0;
top = -1;
top++;
record_x[top] = arr[front - 1];
record_y[top] = brr[front - 1];
while (rear != front)
{
front--;
for (j = 0;j < 4;j++)
{
if (record_x[top] + Direction[j][0] == arr[front - 1] && record_y[top] + Direction[j][1] == brr[front - 1])
{
top++;
record_x[top] = arr[front - 1];
record_y[top] = brr[front - 1];
}
}
}
Display();
cout << "最短路徑如下:" << endl;
cout << "鼠" << "->";
for (i = 0;i <= top;i++)
{
cout << "(" << record_x[i] << "," << record_y[i] << ")" << "->";
}
cout << "倉" << endl;
system("pause");
}
3.maze.h文件
#ifndef MAZE_H
#define MAZE_H
const int MaxSize = 100;
const int road = 0;//路
const int wall = 1;//墻
const int Mouse = 2;//老鼠
const int End = 3;//終點(diǎn)
const int visited = 4;//被訪問的路徑
const int MaxSmall = 5;//最短路徑
class Maze
{
private:
int pos_x, pos_y;//主角老鼠的坐標(biāo)
int Map_Length, Map_Width;//地圖的長寬
int Visited[MaxSize][MaxSize];//是否被訪問數(shù)組
int rear, front;
int top;
public:
Maze(int l, int w);//構(gòu)造函數(shù)
int Map[MaxSize][MaxSize];//地圖數(shù)組
void CreateMap(int, int);//創(chuàng)建地圖
void Show_Map();//顯示地圖
void Display();//查看地圖
void KeepMap();//顯示老鼠走過的路徑
bool Move();//老鼠移動
void Up();//老鼠向上移動
void Dowm();//老鼠向下移動
void Right();//老鼠向右移動
void Left();//老鼠向左移動
void EdidMap();//編輯地圖
void Short();
void SmallRoadDisplay(int x, int y);//最短路徑
};
#endif
#pragma once
版本提示
所用開發(fā)版本為vs2019版,版本不同編譯可能存在錯誤。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

