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

news/2024/6/2 14:47:15 标签: 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++;
    Mp[top] = MpQueue[front - 1];
    int direc1[4][2] = { 1,0,0,1,0,-1,-1,0 };  //定义方向
    while (front != rear)
    {
        front--;

        for (int j = 0; j < 4; j++)
        {
            if (Mp[top].x + direc1[j][0] == MpQueue[front - 1].x && Mp[top].y + direc1[j][1] == MpQueue[front - 1].y)
            {
                top++;
                Mp[top] = MpQueue[front - 1];
            }
        }
    }
}

void Player::show()
{
    cout << "鼠";
    for (int i = 0; i <= top; i++)
    {
        cout << "(" << Mp[i].x << "," << Mp[i].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 (Map[i][j].data == 1)
                        {
                            cout << "■";
                        }

                        else if (Map[i][j].data == 0)
                        {
                            cout << "  ";
                        }

                        else if (Map[i][j].data == 2)
                        {
                            cout << "鼠";
                        }

                        else if (Map[i][j].data == 3)
                        {
                            cout << "仓";
                        }

                        else if (Map[i][j].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 (Map[c][d - 1].data == 1)
                    {
                        cout << "老鼠不能穿墙" << endl;
                    }

                    else
                    {

                        Map[c][d - 1].data = 2;
                        Map[c][d].data = 4;
                        d = d - 1;
                        Count--;
                    }
                }
                else if (Enter == 77)
                {
                    if (Map[c][d + 1].data == 1)
                    {
                        cout << "老鼠不能穿墙" << endl;
                    }

                    else
                    {
                        Map[c][d + 1].data = 2;
                        Map[c][d].data = 4;
                        d = d + 1;
                        Count--;
                    }

                }
                else if (Enter == 72)
                {
                    if (Map[c - 1][d].data == 1)
                    {
                        cout << "老鼠不能穿墙" << endl;
                    }

                    else
                    {
                        Map[c - 1][d].data = 2;
                        Map[c][d].data = 4;
                        c = c - 1;
                        Count--;
                    }
                }
                else if (Enter == 80)
                {
                    if (Map[c + 1][d].data == 1)
                    {
                        cout << "老鼠不能穿墙" << endl;
                    }

                    else
                    {
                        Map[c + 1][d].data = 2;
                        Map[c][d].data = 4;
                        c = c + 1;
                        Count--;
                    }
                }
            }

            for (i = 1; i <= Map_Length; i++)
            {
                for (j = 1; j <= Map_Length; j++)
                {
                    if (Map[i][j].data == 1)
                    {
                        cout << "■";
                    }

                    else if (Map[i][j].data == 0)
                    {
                        cout << "  ";
                    }

                    else if (Map[i][j].data == 2)
                    {
                        cout << "鼠";
                    }

                    else if (Map[i][j].data == 3)
                    {
                        cout << "仓";
                    }

                    else if (Map[i][j].data == 4)
                    {
                        cout << "  ";
                    }
                }
                cout << endl;
            }

            if (Map[Map_Length - 1][Map_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 (Map[i][j].data == 0)
            {
                cout << "  ";
            }
            else if (Map[i][j].data == 1)
            {
                cout << "■";
            }
            else if (Map[i][j].data == 2)
            {
                cout << "鼠";
            }
            else if (Map[i][j].data == 3)
            {
                cout << "仓";
            }
            else if (Map[i][j].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)));
    Map[Map_Length / 2][Map_Width / 2].data = 2;                //初始化鼠 当二维数组的值为2时,代表鼠 
    Map[Map_Length - 1][Map_Width - 1].data = 3;                //初始化仓 当二维数组的值为3时,代表仓 
    for (i = 1; i <= Map_Length; i++)
    {
        for (j = 1; j <= Map_Width; j++)
        {
            if (Map[i][j].data == 1)
            {
                cout << "■";
            }
            else if (Map[i][j].data == 0)
            {
                cout << "  ";
            }
            else if (Map[i][j].data == 2)
            {
                cout << "鼠";
            }
            else if (Map[i][j].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)
            {
                Map[i][j].data = 0;
            }
            else
            {
                Map[i][j].data = 1;
            }
        }
    }
    for (int i = 0; i <= Map_Length; i++)
    {
        for (int j = 0; j < Map_Width; j++)
        {
            Map[i][j].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 (Map[i][j].data == 1)
                {
                    cout << "■";
                }

                else if (Map[i][j].data == 0)
                {
                    cout << "  ";
                }

                else if (Map[i][j].data == 2)
                {
                    cout << "鼠";
                }

                else if (Map[i][j].data == 3)
                {
                    cout << "仓";
                }

                else if (Map[i][j].data == 4)
                {
                    cout << "  ";
                }
            }
            cout << endl;
        }
        cout << "输入回车键保存修改" << endl;

        Enter = _getch();
        system("cls");
        if (Enter == -32)
        {
            Enter = _getch();

            if (Enter == 75)
            {
                if (Map[c][d - 1].data == 1)
                {
                    cout << "老鼠不能穿墙" << endl;
                }

                else
                {
                    Map[c][d - 1].data = 2;
                    Map[c][d].data = 4;
                    d = d - 1;
                }
            }
            else if (Enter == 77)
            {
                if (Map[c][d + 1].data == 1)
                {
                    cout << "老鼠不能穿墙" << endl;
                }

                else
                {
                    Map[c][d + 1].data = 2;
                    Map[c][d].data = 4;
                    d = d + 1;
                }

            }
            else if (Enter == 72)
            {
                if (Map[c - 1][d].data == 1)
                {
                    cout << "老鼠不能穿墙" << endl;
                }

                else
                {
                    Map[c - 1][d].data = 2;
                    Map[c][d].data = 4;
                    c = c - 1;
                }
            }
            else if (Enter == 80)
            {
                if (Map[c + 1][d].data == 1)
                {
                    cout << "老鼠不能穿墙" << endl;
                }

                else
                {
                    Map[c + 1][d].data = 2;
                    Map[c][d].data = 4;
                    c = c + 1;
                }
            }
        }

        if (Enter == 97)
        {
            if (Map[c][d - 1].data == 1)
            {
                Map[c][d - 1].data = 0;
            }
            else if (Map[c][d - 1].data == 0 || Map[c][d - 1].data == 4)
            {
                Map[c][d - 1].data = 1;
            }
        }
        else if (Enter == 119)
        {
            if (Map[c - 1][d].data == 1)
            {
                Map[c - 1][d].data = 0;
            }
            else if (Map[c - 1][d].data == 0 || Map[c - 1][d].data == 4)
            {
                Map[c - 1][d].data = 1;
            }
        }
        else if (Enter == 100)
        {
            if (Map[c][d + 1].data == 1)
            {
                Map[c][d + 1].data = 0;
            }
            else if (Map[c][d + 1].data == 0 || Map[c][d + 1].data == 4)
            {
                Map[c][d + 1].data = 1;
            }
        }
        else if (Enter == 115)
        {
            if (Map[c + 1][d].data == 1)
            {
                Map[c + 1][d].data = 0;
            }
            else if (Map[c + 1][d].data == 0 || Map[c + 1][d].data == 4)
            {
                Map[c + 1][d].data = 1;
            }
        }
        else if (Enter == 0x0D)
        {
            Map[c][d].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 (Map[i][j].data == 1)
            {
                cout << "■";
            }

            else if (Map[i][j].data == 0)
            {
                cout << "  ";
            }

            else if (Map[i][j].data == 2)
            {
                cout << "鼠";
            }

            else if (Map[i][j].data == 3)
            {
                cout << "仓";
            }

            else if (Map[i][j].data == 4)
            {
                cout << "  ";
            }
        }
        cout << endl;
    }

    front = rear = -1;
    rear++;
    MpQueue[rear] = M;

    int direc1[4][2] = { 1, 0, 0, 1, 0, -1, -1, 0 };                //d定义四个方向 
    while (front != rear)
    {
        front++;

        for (int j = 0; j < 4; j++)
        {
            if ((Map[MpQueue[front].x + direc1[j][0]][MpQueue[front].y + direc1[j][1]].data == 0 || Map[MpQueue[front].x + direc1[j][0]][MpQueue[front].y + direc1[j][1]].data == 2 || Map[MpQueue[front].x + direc1[j][0]][MpQueue[front].y + direc1[j][1]].data == 4) && Map[MpQueue[front].x + direc1[j][0]][MpQueue[front].y + direc1[j][1]].visited == 0 && MpQueue[front].x < Map_Width && MpQueue[front].x >= 1 && MpQueue[front].y < Map_Length && MpQueue[front].y >= 1)
            {
                rear++;
                MpQueue[rear].x = MpQueue[front].x + direc1[j][0];
                MpQueue[rear].y = MpQueue[front].y + direc1[j][1];
                Map[MpQueue[front].x + direc1[j][0]][MpQueue[front].y + direc1[j][1]].visited = 1;

                if (MpQueue[rear].x == (Map_Length / 2) && MpQueue[rear].y == (Map_Width / 2))
                {
                    flag = 1;
                    break;
                }
            }
        }

        if (flag == 1)
        {
            break;
        }
    }
    Push();
}

void Player::GenerateMap(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].data = 0;
    for (i = 0; i < 4; i++)                                            //任何两个空的地方都有路可走 
    {
        if (Map[x + 2 * direction[i][0]][y + 2 * direction[i][1]].data == 1)
        {
            Map[x + direction[i][0]][y + direction[i][1]].data = 0;        //打通墙 
            GenerateMap(x + 2 * direction[i][0], y + 2 * direction[i][1]);
        }
    }
}

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)
            {
                Map[i][j].data = 0;
            }
            else
            {
                Map[i][j].data = 1;
            }
        }
    }

    for (int i = 0; i < Size; i++)
    {
        for (int j = 0; j < Size; j++)
        {
            Map[i][j].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 Mp[Size];     //栈
    MapPoint MpQueue[230]; //队列

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 Map[Size][Size];            //地图数组 
};

#endif


http://www.niftyadmin.cn/n/5391327.html

相关文章

【Docker】前端基于dockerfiel构建镜像部署,实现在容器启动时传递环境变量, 请求不同服务地址

前端部署采用 docker 的方式&#xff0c; 实现在容器启动时传递环境变量&#xff0c; 请求不同服务地址 实现思路&#xff1a; 定义.env.xxx 文件&#xff08;环境变量赋值&#xff09;&#xff0c;在compose.yml中引入.env.xxx 文件&#xff0c;环境变量通过nginx的sub_filte…

Singularity基本命令学习

Singularity是一个为科学计算和数据密集型任务设计的容器平台,它提供了一组强大的命令来帮助用户创建、管理和运行容器。以下是一些Singularity的基本命令,以及每个命令的简单实例,以帮助你入门和理解如何使用Singularity。 1. 安装Singularity 首先,你需要在你的系统上安…

一个简单的mysql绕过

一、环境代码 上一个环境的demo文件 二、ctf技巧 代码先进入&#xff0c;到chr转换为英文&#xff0c;之后连接到hehe后面&#xff0c;假设我连接了一个&#xff0c;接下来就回去demo中查找hehe&#xff0c;如果name是hehe就输出&#xff0c;意思就是只认前面的hehe后面的会被…

台式电脑电源功率越大越费电吗?装机选购多少W电源

要组装一台电脑&#xff0c;我们首先需要选择硬件。 硬件搭配最关键的一点就是CPU和主板的兼容性。 硬件、电源等之间的平衡都需要仔细考虑。 那么台式电脑电源多大功率合适呢&#xff1f; 下面分享组装电脑电源瓦数选购指南&#xff0c;教您正确选择合适的电源瓦数。 让我们来…

unity学习(40)——创建(create)角色脚本(panel)——UI

1.点击不同的头像按钮&#xff0c;分别选择职业1和职业2&#xff0c;create脚本中对应的函数。 2.调取inputfield中所输入的角色名&#xff08;限制用户名长度为7字符&#xff09;&#xff0c;但愿逆向的服务器可以查重名&#xff1a; 3.点击头衔&#xff0c;显示选择的职业&a…

leetcode单调栈

739. 每日温度 请根据每日 气温 列表&#xff0c;重新生成一个列表。对应位置的输出为&#xff1a;要想观测到更高的气温&#xff0c;至少需要等待的天数。如果气温在这之后都不会升高&#xff0c;请在该位置用 0 来代替。 例如&#xff0c;给定一个列表 temperatures [73, …

linux之JAVA环境配置Tomcat离线安装与启动

文章目录 一、jdk安装具体步骤二、tomcat安装具体步骤三、MySql具体步骤修改密码登录 四、部署单价项目具体步骤 一、jdk安装具体步骤 1、查询是否有jdk java -version 2、进入opt目录 cd /opt 3.连接服务器工具 进入opt目录&#xff0c;把压缩文件上传 4.等待传好之后&am…

CSP-J 2023 复赛第4题:旅游巴士

【题目来源】https://www.luogu.com.cn/problem/P9751https://www.acwing.com/problem/content/description/5313/【题目描述】 小 Z 打算在国庆假期期间搭乘旅游巴士去一处他向往已久的景点旅游。 旅游景点的地图共有 n 处地点&#xff0c;在这些地点之间连有 m 条道路。 其中…