C++迷宫游戏

问题描述

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

基本要求

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

提高要求

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

源代码

1.文件main.cpp

#include<iostream>
#include"maze.h"//自定义的头文件,存放类声明和常量
#include<cstdlib>//各种函数头文件,如rand函数
#include<time.h>//日期和时间的头文件
#include<stdio.h>//标准输入输出
#include<Windows.h>//图形界面设计头文件
#include<conio.h>//获取方向键的头文件

using namespace std;

void MainMenu();//显示游戏菜单函数
void MeunGame(int num);//显示不同关卡函数
void Introduce();//游戏介绍

int main()
{
	Maze maze_1(11, 11), maze_2(13, 13), maze_3(15, 15), maze_4(17, 17);//定义四个关卡
	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);//显示关卡菜单
					system("cls");
					maze_1.Show_Map();
					judge[0] = maze_1.Move();
					if (judge[0] == true)
					{
						cout << "憨憨鼠所走过的路径";
						maze_1.KeepMap();
						cout << "是否继续闯关:(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 << "是否继续闯关:(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 << "是否继续闯关:(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★太棒了,恭喜你通过全部关卡★" << 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 << "请你输入要编辑的关卡:" << endl;
			cout << "1.第一关" << endl;
			cout << "2.第二关" << endl;
			cout << "3.第三关" << endl;
			cout << "4.第四关" << 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 << "此关卡未通过,不能编译此关!" << endl;
				system("pause");
			}
			break;
		case 3:
			system("cls");
			cout << "请你输入要查看的关卡" << endl;
			cout << "1.第一关" << endl;
			cout << "2.第二关" << endl;
			cout << "3.第三关" << endl;
			cout << "4.第四关" << 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 << "此关卡未通过,不能查看此关!" << 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()//游戏菜单函数
{
	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)//关卡菜单
{
	system("cls");
	cout << "\n\t\t**************************************************************" << endl;
	cout << "\t\t*                                                            *" << endl;
	cout << "\t\t*                       ◤ 第" << num << "关 ◢                          *" << 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.在选择破解地图时,会给出坐标路线,原点为迷宫左上角  *" << endl;
	cout << "\t\t*                                                                  *" << endl;
	cout << "\t\t*            4.在规定时间内走到粮仓算过关,时间越短所获积分越多    *" << endl;
	cout << "\t\t*                                                                  *" << endl;
	cout << "\t\t********************************************************************" << endl;
	system("pause");
}

2.maze.cpp文件

#include<iostream>
#include"maze.h"//自定义的头文件,存放类声明和常量
#include<cstdlib>//各种函数头文件,如rand函数
#include<time.h>//日期和时间的头文件
#include<stdio.h>//标准输入输出
#include<Windows.h>//图形界面设计头文件
#include<conio.h>//获取方向键的头文件

using namespace std;

Maze::Maze(int l, int w)//构造函数初始化地图和成员变量
{
	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;//默认地图中都是墙
			}
		}
	}

	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)//创建地图
{
	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)//任意两点之间有路
		{
			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));//种随机粒子
	CreateMap(2 * (rand() % (Map_Length / 2 + 1)), 2 * (rand() % (Map_Width / 2 + 1)));//随机选取x,y坐标
	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);//获取系统时间
	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 << "闯关失败" << endl;
					temp = false;
				}
				t = time(NULL);//获取当前时间
			}
		}
		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 << "★恭喜你闯关成功★" << endl;
				Map[pos_x][pos_y] = visited;
				cout << "★获得" << count << "点积分★" << 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;//确定老鼠坐标
	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坐标的队列
	int brr[100] = { 0 };//存放y坐标的队列
	int record_x[100] = { 0 };//存放x坐标的栈
	int record_y[100] = { 0 };//存放y坐标的栈
	front = rear = -1;
	rear++;
	arr[rear] = x;//当前x坐标入队
	brr[rear] = y;//当前y坐标入队
	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;//终点
const int visited = 4;//被访问的路径
const int MaxSmall = 5;//最短路径

class Maze
{
private:
	int pos_x, pos_y;//主角老鼠的坐标
	int Map_Length, Map_Width;//地图的长宽
	int Visited[MaxSize][MaxSize];//是否被访问数组
	int rear, front;
	int top;
public:
	Maze(int l, int w);//构造函数
	int Map[MaxSize][MaxSize];//地图数组
	void CreateMap(int, int);//创建地图
	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

版本提示

所用开发版本为vs2019版,版本不同编译可能存在错误。

上一篇:第2节 讲解flex布局容器器六⼤属性之flex-direction


下一篇:设计模式之解释器模式(C++实现)