#include <bits/stdc++.h>  // 包含所有常用的标准库
using namespace std;

const char wall = '@';  // 墙的标识符
const char startpoint = 'S';  // 起点的标识符
const char endpoint = 'E';  // 终点的标识符
const char route = ' ';  // 路径的标识符
const char player = 'e';  // 玩家的标识符
const int limit = 100;  // 最大得分

class labyrinth
{
private:
    void labyrinth_()
    {
        // 输出迷宫
        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j < size; j++)
            {
                cout << maze[i][j] << " ";
            }
            cout << endl;
        }
        cout << endl;
    }
    
    int size;  // 迷宫大小
    int diff;  // 迷宫难度
    vector<vector<char>>maze;  // 迷宫二维数组
    int beginR,beginC;  // 起点的行列坐标
    int endR,endC;  // 终点的行列坐标
    
public:
    labyrinth(int size, int diff) : size(size), diff(diff)
    {
        // 初始化迷宫,将所有格子都设置为墙
        maze = vector<vector<char>>(size, vector<char>(size, wall));
        
        // 将内部的格子设置为可通行
        for (int i = 1; i < size - 1; i++)
        {
            for (int j = 1; j < size - 1; j++)
            {
                maze[i][j] = route;
            }
        }
        
        srand(time(nullptr));  // 随机数种子
        
        // 随机生成起点和终点的行列坐标
        beginR = rand() % (size - 2) + 1;
        beginC = rand() % (size - 2) + 1;
        endR = rand() % (size - 2) + 1;
        endC = rand() % (size - 2) + 1;
        
        // 设置起点和终点的位置
        maze[beginR][beginC] = startpoint;
        maze[endR][endC] = endpoint;
        
        int fork = size * diff / 10;  // 计算需要生成的墙的数量
        
        // 随机生成墙的位置
        for (int i = 0; i < fork; i++)
        {
            int row = rand() % (size - 2) + 1;
            int col = rand() % (size - 2) + 1;
            maze[row][col] = wall;
        }
    }
    
    void play()
    {
        int score = 0;  // 得分
        int steps = 0;  // 步数
        vector<pair<int, int>> path;  // 记录路径的行列坐标
        
        int curRow = beginR;  // 当前行坐标初始化为起点的行坐标
        int curCol = beginC;  // 当前列坐标初始化为起点的列坐标
        
        while (curRow != endR || curCol != endC)
        {
            maze[curRow][curCol] = player;  // 在当前位置设置玩家的标识符
            path.push_back(make_pair(curRow, curCol));  // 将当前位置添加到路径中
            
            cout << "当前位置:(" << curRow << ", " << curCol << ")" << endl;  // 输出当前位置
            labyrinth_();  // 输出迷宫
            
            char move;  // 存储玩家移动方向的变量
            cout << "请输入移动方向(1上,2下,3左,4右):";
            cin >> move;  // 获取玩家输入的移动方向
            
            int nextRow = curRow;  // 下一步的行坐标初始化为当前行坐标
            int nextCol = curCol;  // 下一步的列坐标初始化为当前列坐标
            
            switch (move)
            {
            case '1':  // 上
                nextRow--;
                break;
            case '2':  // 下
                nextRow++;
                break;
            case '3':  // 左
                nextCol--;
                break;
            case '4':  // 右
                nextCol++;
                break;
            default:
                cout << "移动方向无效!" << endl;
                continue;
            }
            
            if (maze[nextRow][nextCol] == wall)
            {
                cout << "该方向不通,已撞墙!" << endl;
                continue;
            }
            
            curRow = nextRow;  // 更新当前行坐标
            curCol = nextCol;  // 更新当前列坐标
            steps++;  // 步数加一
        }
        
        score = limit - steps * diff;  // 计算得分
        cout << "恭喜你成功走出迷宫!得分:" << score << endl;  // 输出得分
        
        for (const auto& p : path)
        {
            maze[p.first][p.second] = route;  // 将路径上的格子恢复为可通行
            labyrinth_();  // 输出迷宫
        }
    }
};

int main()
{
    int size, diff;
    cout << "迷宫大小(3至10):";
    cin >> size;  // 获取迷宫大小
    cout << "迷宫难度(1至10):";
    cin >> diff;  // 获取迷宫难度
    
    labyrinth game(size, diff);  // 创建迷宫游戏对象
    game.play();  // 开始游戏
    
    return 0;
}
``
#include bitsstdc++husing namespace std;const char wall = ;const char startpoint = S;const char endpoint = E;const char route = ;const char player = e;const int limit = 100;class labyrinthprivate

原文地址: https://www.cveoy.top/t/topic/iTzX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录