C/C++实现老鼠走迷宫

老鼠形象可以辨认,可以用上下左右操纵老鼠;正确检测结果,若老鼠在规定的时间内走到粮仓,提示成功,否则提示失败。代码分为3个文件:main.cpp、play.h、play.cpp。

main.cpp:

#include <iostream>

#include <windows.h>

#include "play.h"

#include <stdio.h>

using namespace std;

/* run this program using the console pauser or add your own _getch, system("pause") or input loop */

int main()

{

int Count = 0;

cout << "欢迎使用自制迷宫游戏,Are you ready?";

Sleep(500);

cout << ".";

Sleep(500);

cout << ".";

Sleep(500);

cout << ".";

// system("cls");

// 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*************************************************"<<endl;

Player play_1(11, 11);

Player play_2(13, 13);

Player play_3(15, 15);

int Menu;

while (1)

{

if (Count < 1)

{

system("cls");

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;

}

else if (Count >= 1)

{

system("cls");

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*************************************************" << endl;

}

cin >> Menu;

system("cls");

if (Menu == 1)

{

cout << "*******************************游戏说明*****************************" << endl;

cout << "请使用键盘↑↓←→移动老鼠,在规定时间内用尽量少的步骤帮老鼠找到粮仓" << endl;

system("cls");

cout << "\t\t 第一关 " << endl;

Sleep(500);

play_1.show_Map();

play_1.Move();

play_1.KeepMap();

cout << "\t\t 请进行你的选择 " << endl;

cout << "\t\t 1.继续游戏 " << endl;

cout << "\t\t 2.结束游戏 " << endl;

int choice, choice2;

cin >> choice;

if (choice == 1)

{

cout << "\t\t 第二关 " << endl;

Sleep(500);

play_2.show_Map();

play_2.Move();

play_2.KeepMap();

cout << "\t\t 请进行你的选择 " << endl;

cout << "\t\t 1.继续游戏 " << endl;

cout << "\t\t 2.结束游戏 " << endl;

cin >> choice2;

if (choice2 == 1)

{

cout << "\t\t 第三关 " << endl;

cout << "\t\t 请进行你的选择 " << endl;

cout << "\t\t 1.继续游戏 " << endl;

cout << "\t\t 2.结束游戏 " << endl;

Sleep(500);

play_3.show_Map();

play_3.Move();

play_3.KeepMap();

cout << "您已通关,感谢使用" << endl;

break;

}

else if (choice2 == 2)

{

cout << "游戏结束,感谢使用" << endl;

break;

}

}

else if (choice == 2)

{

cout << "游戏结束,感谢使用" << endl;

break;

}

}

else if (Menu == 2)

{

cout << "\t 请选择想要编辑的关卡 " << endl;

cout << "\t\t 1.第一关 " << endl;

cout << "\t\t 2.第二关 " << endl;

cout << "\t\t 3.第三关 " << endl;

int choice3;

cin >> choice3;

if (choice3 == 1)

{

play_1.EditorMap();

}

else if (choice3 == 2)

{

play_2.EditorMap();

}

else if (choice3 == 3)

{

play_3.EditorMap();

}

system("cls");

}

else if (Menu == 3)

{

cout << "亲,您只有一次查看机会哦╭●★★●╰。。。";

Sleep(2000);

if (Count < 1)

{

Count++;

cout << "\t\t 请输入想要查看的关卡 " << endl;

cout << "\t\t 1.第一关 " << endl;

cout << "\t\t 2.第二关 " << endl;

cout << "\t\t 3.第三关 " << endl;

int Choice;

cin >> Choice;

if (Choice == 1)

{

play_1.Pre_Short();

}

else if (Choice == 2)

{

play_2.Pre_Short();

}

else if (Choice == 3)

{

play_3.Pre_Short();

}

}

}

if (Count < 1)

{

if (Menu == 4)

{

cout << "感谢使用" << endl;

break;

}

}

else if (Count > 1)

{

if (Menu == 3)

{

cout << "感谢小主的使用" << endl;

break;

}

}

}

return 0;

}

play.cpp:

#include <iostream>

#include "play.h"

#include <windows.h>

#include <conio.h>

#include <time.h>

using namespace std;

void Player::Push()

{

front = rear + 1;

rear = 0;

top = -1;

top++;

Mptop = MpQueuefront - 1;

int direc142 = { 1,0,0,1,0,-1,-1,0 }; //定义方向

while (front != rear)

{

front--;

for (int j = 0; j < 4; j++)

{

if (Mptop.x + direc1j0 == MpQueuefront - 1.x && Mptop.y + direc1j1 == MpQueuefront - 1.y)

{

top++;

Mptop = MpQueuefront - 1;

}

}

}

}

void Player::show()

{

cout << "鼠";

for (int i = 0; i <= top; i++)

{

cout << "(" << Mpi.x << "," << Mpi.y << ")"

<< "->";

}

cout << "仓";

system("pause>nul");

}

void Player::Move()

{

time_t Start;

time_t Over;

int Count = 100;

char Enter;

int Time = 30;

int a, b = 0, c = Map_Length / 2, d = Map_Width / 2, i, j;

Start = time(NULL);

while (Time >= 0)

{

Over = time(NULL);

a = Over - Start;

if (_kbhit() == 0)

{

if (b != a)

{

system("cls");

for (i = 1; i <= Map_Length; i++)

{

for (j = 1; j <= Map_Length; j++)

{

if (Mapij.data == 1)

{

cout << "■";

}

else if (Mapij.data == 0)

{

cout << " ";

}

else if (Mapij.data == 2)

{

cout << "鼠";

}

else if (Mapij.data == 3)

{

cout << "仓";

}

else if (Mapij.data == 4)

{

cout << " ";

}

}

cout << endl;

}

cout << "剩余时间" << Time-- << "秒" << endl;

b = a;

if (Time == -1)

{

system("cls");

cout << "闯关失败" << endl;

exit(1);

break;

}

}

}

if (_kbhit() != 0)

{

Enter = _getch();

system("cls");

if (Enter == -32)

{

Enter = _getch();

if (Enter == 75)

{

if (Mapcd - 1.data == 1)

{

cout << "老鼠不能穿墙" << endl;

}

else

{

Mapcd - 1.data = 2;

Mapcd.data = 4;

d = d - 1;

Count--;

}

}

else if (Enter == 77)

{

if (Mapcd + 1.data == 1)

{

cout << "老鼠不能穿墙" << endl;

}

else

{

Mapcd + 1.data = 2;

Mapcd.data = 4;

d = d + 1;

Count--;

}

}

else if (Enter == 72)

{

if (Mapc - 1d.data == 1)

{

cout << "老鼠不能穿墙" << endl;

}

else

{

Mapc - 1d.data = 2;

Mapcd.data = 4;

c = c - 1;

Count--;

}

}

else if (Enter == 80)

{

if (Mapc + 1d.data == 1)

{

cout << "老鼠不能穿墙" << endl;

}

else

{

Mapc + 1d.data = 2;

Mapcd.data = 4;

c = c + 1;

Count--;

}

}

}

for (i = 1; i <= Map_Length; i++)

{

for (j = 1; j <= Map_Length; j++)

{

if (Mapij.data == 1)

{

cout << "■";

}

else if (Mapij.data == 0)

{

cout << " ";

}

else if (Mapij.data == 2)

{

cout << "鼠";

}

else if (Mapij.data == 3)

{

cout << "仓";

}

else if (Mapij.data == 4)

{

cout << " ";

}

}

cout << endl;

}

if (MapMap_Length - 1Map_Length - 1.data != 3)

{

system("cls");

cout << "闯关成功" << endl;

cout << "您的积分为" << Count << endl;

break;

}

}

}

}

void Player::KeepMap() //保存老鼠走过的路径

{

for (int i = 1; i <= Map_Length; i++)

{

for (int j = 1; j <= Map_Width; j++)

{

if (Mapij.data == 0)

{

cout << " ";

}

else if (Mapij.data == 1)

{

cout << "■";

}

else if (Mapij.data == 2)

{

cout << "鼠";

}

else if (Mapij.data == 3)

{

cout << "仓";

}

else if (Mapij.data == 4)

{

cout << "◇";

}

}

cout << endl;

}

}

void Player::show_Map() //编辑地图

{

int i, j;

//srand((unsigned)time(NULL)); //如果不适用随机数种子,那么每次程序启动生成的随机数(rand)都是一样的

GenerateMap(2 * (rand() % (Map_Length / 2 + 1)), 2 * (rand() % (Map_Width / 2 + 1)));

MapMap_Length / 2Map_Width / 2.data = 2; //初始化鼠 当二维数组的值为2时,代表鼠

MapMap_Length - 1Map_Width - 1.data = 3; //初始化仓 当二维数组的值为3时,代表仓

for (i = 1; i <= Map_Length; i++)

{

for (j = 1; j <= Map_Width; j++)

{

if (Mapij.data == 1)

{

cout << "■";

}

else if (Mapij.data == 0)

{

cout << " ";

}

else if (Mapij.data == 2)

{

cout << "鼠";

}

else if (Mapij.data == 3)

{

cout << "仓";

}

}

cout << endl;

}

}

void Player::Pre_Short()

{

rear = front = -1;

for (int i = 1; i <= Map_Length + 1; i++) //1-Map_Length才是想要的

{

for (int j = 1; j <= Map_Width + 1; j++)

{

if (i == 0 || i == Map_Length + 1 || j == 0 || j == Map_Width + 1)

{

Mapij.data = 0;

}

else

{

Mapij.data = 1;

}

}

}

for (int i = 0; i <= Map_Length; i++)

{

for (int j = 0; j < Map_Width; j++)

{

Mapij.visited = 0;

}

}

show_Map();

system("cls");

int m = Map_Length - 1, n = Map_Width - 1;

MapPoint p;

p.x = m, p.y = n, p.visited = 1;

p.data = 3;

ShortMap(p);

show();

while (top != -1)

{

top--;

}

}

void Player::EditorMap()

{

int c = Map_Length / 2;

int d = Map_Width / 2;

show_Map();

system("cls");

char Enter;

while (1)

{

for (int i = 1; i <= Map_Length; i++)

{

for (int j = 1; j <= Map_Length; j++)

{

if (Mapij.data == 1)

{

cout << "■";

}

else if (Mapij.data == 0)

{

cout << " ";

}

else if (Mapij.data == 2)

{

cout << "鼠";

}

else if (Mapij.data == 3)

{

cout << "仓";

}

else if (Mapij.data == 4)

{

cout << " ";

}

}

cout << endl;

}

cout << "输入回车键保存修改" << endl;

Enter = _getch();

system("cls");

if (Enter == -32)

{

Enter = _getch();

if (Enter == 75)

{

if (Mapcd - 1.data == 1)

{

cout << "老鼠不能穿墙" << endl;

}

else

{

Mapcd - 1.data = 2;

Mapcd.data = 4;

d = d - 1;

}

}

else if (Enter == 77)

{

if (Mapcd + 1.data == 1)

{

cout << "老鼠不能穿墙" << endl;

}

else

{

Mapcd + 1.data = 2;

Mapcd.data = 4;

d = d + 1;

}

}

else if (Enter == 72)

{

if (Mapc - 1d.data == 1)

{

cout << "老鼠不能穿墙" << endl;

}

else

{

Mapc - 1d.data = 2;

Mapcd.data = 4;

c = c - 1;

}

}

else if (Enter == 80)

{

if (Mapc + 1d.data == 1)

{

cout << "老鼠不能穿墙" << endl;

}

else

{

Mapc + 1d.data = 2;

Mapcd.data = 4;

c = c + 1;

}

}

}

if (Enter == 97)

{

if (Mapcd - 1.data == 1)

{

Mapcd - 1.data = 0;

}

else if (Mapcd - 1.data == 0 || Mapcd - 1.data == 4)

{

Mapcd - 1.data = 1;

}

}

else if (Enter == 119)

{

if (Mapc - 1d.data == 1)

{

Mapc - 1d.data = 0;

}

else if (Mapc - 1d.data == 0 || Mapc - 1d.data == 4)

{

Mapc - 1d.data = 1;

}

}

else if (Enter == 100)

{

if (Mapcd + 1.data == 1)

{

Mapcd + 1.data = 0;

}

else if (Mapcd + 1.data == 0 || Mapcd + 1.data == 4)

{

Mapcd + 1.data = 1;

}

}

else if (Enter == 115)

{

if (Mapc + 1d.data == 1)

{

Mapc + 1d.data = 0;

}

else if (Mapc + 1d.data == 0 || Mapc + 1d.data == 4)

{

Mapc + 1d.data = 1;

}

}

else if (Enter == 0x0D)

{

Mapcd.data = 0;

break;

}

}

}

void Player::ShortMap(MapPoint& M)

{

M.visited = 1;

for (int i = 1; i <= Map_Length; i++)

{

for (int j = 1; j <= Map_Length; j++)

{

if (Mapij.data == 1)

{

cout << "■";

}

else if (Mapij.data == 0)

{

cout << " ";

}

else if (Mapij.data == 2)

{

cout << "鼠";

}

else if (Mapij.data == 3)

{

cout << "仓";

}

else if (Mapij.data == 4)

{

cout << " ";

}

}

cout << endl;

}

front = rear = -1;

rear++;

MpQueuerear = M;

int direc142 = { 1, 0, 0, 1, 0, -1, -1, 0 }; //d定义四个方向

while (front != rear)

{

front++;

for (int j = 0; j < 4; j++)

{

if ((MapMpQueue\[front.x + direc1j0]MpQueue\[front.y + direc1j1].data == 0 || MapMpQueue\[front.x + direc1j0]MpQueue\[front.y + direc1j1].data == 2 || MapMpQueue\[front.x + direc1j0]MpQueue\[front.y + direc1j1].data == 4) && MapMpQueue\[front.x + direc1j0]MpQueue\[front.y + direc1j1].visited == 0 && MpQueuefront.x < Map_Width && MpQueuefront.x >= 1 && MpQueuefront.y < Map_Length && MpQueuefront.y >= 1)

{

rear++;

MpQueuerear.x = MpQueuefront.x + direc1j0;

MpQueuerear.y = MpQueuefront.y + direc1j1;

MapMpQueue\[front.x + direc1j0]MpQueue\[front.y + direc1j1].visited = 1;

if (MpQueuerear.x == (Map_Length / 2) && MpQueuerear.y == (Map_Width / 2))

{

flag = 1;

break;

}

}

}

if (flag == 1)

{

break;

}

}

Push();

}

void Player::GenerateMap(int x, int y)

{

int direction42 = { 1,0,0,1,0,-1,-1,0 }; //定义方向

int i, j, temp;

for (i = 0; i < 4; i++) //打乱方向

{

j = rand() % 4; //随机选取方向

temp = directioni0;

directioni0 = directionj0;

directionj0 = temp;

temp = directioni1;

directioni1 = directionj1;

directionj1 = temp;

}

Mapxy.data = 0;

for (i = 0; i < 4; i++) //任何两个空的地方都有路可走

{

if (Mapx + 2 \* direction\[i0]y + 2 \* direction\[i1].data == 1)

{

Mapx + direction\[i0]y + direction\[i1].data = 0; //打通墙

GenerateMap(x + 2 * directioni0, y + 2 * directioni1);

}

}

}

Player::Player(int m, int n)

{

int i, j;

Map_Length = m, Map_Width = n;

for (i = 1; i <= Map_Length + 1; i++) //1-Map_Length才是想要的

{

for (j = 1; j <= Map_Width + 1; j++)

{

if (i == 0 || i == Map_Length + 1 || j == 0 || j == Map_Width + 1)

{

Mapij.data = 0;

}

else

{

Mapij.data = 1;

}

}

}

for (int i = 0; i < Size; i++)

{

for (int j = 0; j < Size; j++)

{

Mapij.visited = 0;

}

}

flag = 0;

front = rear = -1;

top = -1;

}

play.h:

#ifndef PLAY_H

#define PLAY_H

const int Size = 100;

struct MapPoint

{

int data;

int x, y; //保存路径的x与y坐标

int visited; //是否访问过的标签

};

class Player

{

private:

int top;

int flag;

int x, y;

int rear;

int front;

int Mouse_x, Mouse1_y; //老鼠的位置

int Map_Length, Map_Width;

MapPoint MpSize; //栈

MapPoint MpQueue230; //队列

public:

Player(int m, int n);

void Push(); //入栈操作

void show();

void Move(); //老鼠移动

void KeepMap(); //保存路径

void PlayGame(); //开始游戏

void show_Map(); //显示地图

void Pre_Short();

void EditorMap(); //编辑地图

void ShortMap(MapPoint& M); //计算最短路径

void GenerateMap(int x, int y); //生成地图

MapPoint MapSizeSize; //地图数组

};

#endif

相关推荐
吃好睡好便好1 小时前
提取矩阵某一行或某一列元素
开发语言·人工智能·线性代数·算法·matlab·矩阵
deepin_sir4 小时前
10 - 函数
开发语言·python
z落落5 小时前
C#String字符串
开发语言·c#·php
wljy15 小时前
二、进制状态转换
linux·运维·服务器·c语言·c++
猫头虎-前端技术5 小时前
JS 作用域与闭包:从变量提升到闭包陷阱的超详细解析
开发语言·javascript·云计算·bootstrap·ecmascript·openstack·perl
云泽8085 小时前
笔试算法 -位运算篇(二):从唯一字符到消失数字
c++·算法·位运算
枫叶林FYL5 小时前
项目十:事件溯源仓储管理系统(WMS)仿真实现
开发语言·python
繁华落尽,倾城殇?5 小时前
[C++11] : atomic,nullptr,default/delete,enum class
开发语言·c++·c++11·nullptr·atomic·enum class·default/delete
01_ice6 小时前
C语言数据在内存中的存储
c语言·开发语言
代码村新手6 小时前
C++-二叉搜索树
开发语言·c++