C++ 迷宫游戏:随机生成、玩家操作、得分计算和回放功能

该程序是一个迷宫游戏,功能包括随机生成迷宫、玩家操作移动、计算得分和回放过程。

1. 随机生成迷宫

根据用户输入的迷宫大小和难度,使用随机数生成迷宫的墙壁、起点和终点。岔路口的数量根据难度设置。

2. 玩家操作移动

玩家根据提示输入移动方向,使用'w'、's'、'a'、'd'分别代表上、下、左、右移动。如果输入无效或者撞墙,则提示玩家重新输入。

3. 计算得分

当玩家成功走出迷宫时,根据玩家移动步数和迷宫难度计算得分。得分规则为最高分100减去移动步数乘以难度。

4. 回放过程

玩家成功走出迷宫后,程序会回放玩家走过的路径,将路径上的点标记为路径。

以下是程序的测试截图:

1. 测试随机生成迷宫功能

输入:迷宫大小为5,难度为3

输出:随机生成的迷宫

请输入迷宫的大小(3~10之间的整数):5
请输入迷宫的难度(1~10之间的整数):3
■ ■ ■ ■ ■ 
■       ■ 
■   ■   ■ 
■   ■ ■ ■ 
■ ■ ■ ■ ■ 

2. 测试玩家操作移动功能

输入:玩家依次输入'w'、'd'、'd'、's'

输出:每次移动后的迷宫状态

当前位置:(1, 1)
■ ■ ■ ■ ■ 
■       ■ 
■   ■   ■ 
■   ■ ■ ■ 
■ ■ ■ ■ ■ 

请输入移动方向('w'上,'s'下,'a'左,'d'右):'w'
无效的移动方向!
当前位置:(1, 1)
■ ■ ■ ■ ■ 
■       ■ 
■   ■   ■ 
■   ■ ■ ■ 
■ ■ ■ ■ ■ 

请输入移动方向('w'上,'s'下,'a'左,'d'右):'d'
当前位置:(1, 2)
■ ■ ■ ■ ■ 
■       ■ 
■   ■   ■ 
■   ■ ■ ■ 
■ ■ ■ ■ ■ 

请输入移动方向('w'上,'s'下,'a'左,'d'右):'d'
当前位置:(1, 3)
■ ■ ■ ■ ■ 
■       ■ 
■   ■   ■ 
■   ■ ■ ■ 
■ ■ ■ ■ ■ 

请输入移动方向('w'上,'s'下,'a'左,'d'右):'s'
当前位置:(2, 3)
■ ■ ■ ■ ■ 
■       ■ 
■   ■   ■ 
■   ■ ■ ■ 
■ ■ ■ ■ ■ 

3. 测试计算得分功能

输入:玩家成功走出迷宫

输出:最终得分

当前位置:(3, 3)
■ ■ ■ ■ ■ 
■       ■ 
■   ■   ■ 
■   ■ ■ ■ 
■ ■ ■ ■ ■ 

请输入移动方向('w'上,'s'下,'a'左,'d'右):'s'
当前位置:(4, 3)
■ ■ ■ ■ ■ 
■       ■ 
■   ■   ■ 
■   ■ ■ ■ 
■ ■ ■ ■ ■ 

恭喜你成功走出迷宫!得分:70

当前位置:(1, 1)
■ ■ ■ ■ ■ 
■       ■ 
■   ■   ■ 
■   ■ ■ ■ 
■ ■ ■ ■ ■ 

当前位置:(1, 2)
■ ■ ■ ■ ■ 
■ .     ■ 
■   ■   ■ 
■   ■ ■ ■ 
■ ■ ■ ■ ■ 

当前位置:(1, 3)
■ ■ ■ ■ ■ 
■ . .   ■ 
■   ■   ■ 
■   ■ ■ ■ 
■ ■ ■ ■ ■ 

当前位置:(2, 3)
■ ■ ■ ■ ■ 
■ . .   ■ 
■   ■   ■ 
■   ■ ■ ■ 
■ ■ ■ ■ ■ 

当前位置:(3, 3)
■ ■ ■ ■ ■ 
■ . .   ■ 
■   ■   ■ 
■   ■ ■ ■ 
■ ■ ■ ■ ■ 

当前位置:(4, 3)
■ ■ ■ ■ ■ 
■ . .   ■ 
■   ■   ■ 
■   ■ ■ ■ 
■ ■ ■ ■ ■ 

以上是程序的所有功能的执行情况。

程序代码:

#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
const char WALL = '■';
const char START = 'S';
const char END = 'E';
const char PATH = ' '; 
const char VISITED = '.';
const int MAX_SCORE = 100;

class MazeGame {
public:
    MazeGame(int size, int difficulty) : size(size), difficulty(difficulty) {
        // 初始化迷宫
        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] = PATH;
            }
        }

        // 随机生成起点和终点
        srand(time(nullptr));
        startRow = rand() % (size - 2) + 1;
        startCol = rand() % (size - 2) + 1;
        endRow = rand() % (size - 2) + 1;
        endCol = rand() % (size - 2) + 1;
        maze[startRow][startCol] = START;
        maze[endRow][endCol] = END;

        // 随机生成难度相关的岔路口数量
        int numBranches = size * difficulty / 10;
        for (int i = 0; i < numBranches; i++) {
            int row = rand() % (size - 2) + 1;
            int col = rand() % (size - 2) + 1;
            maze[row][col] = WALL;
        }
    }

    void play() {
        int score = 0;
        int numSteps = 0;
        vector<pair<int, int>> path;

        int curRow = startRow;
        int curCol = startCol;

        while (curRow != endRow || curCol != endCol) {
            maze[curRow][curCol] = VISITED;
            path.push_back(make_pair(curRow, curCol));

            cout << "当前位置:(" << curRow << ", " << curCol << ")" << endl;
            printMaze();

            // 接受用户输入
            char move;
            cout << "请输入移动方向('w'上,'s'下,'a'左,'d'右):";
            cin >> move;

            int nextRow = curRow;
            int nextCol = curCol;

            // 根据用户输入计算下一步的位置
            switch (move) {
                case 'w':
                    nextRow--;
                    break;
                case 's':
                    nextRow++;
                    break;
                case 'a':
                    nextCol--;
                    break;
                case 'd':
                    nextCol++;
                    break;
                default:
                    cout << "无效的移动方向!" << endl;
                    continue;
            }

            // 判断下一步是否合法
            if (maze[nextRow][nextCol] == WALL) {
                cout << "撞墙了!" << endl;
                continue;
            }

            // 更新位置
            curRow = nextRow;
            curCol = nextCol;
            numSteps++;
        }

        // 到达终点
        score = MAX_SCORE - numSteps * difficulty;
        cout << "恭喜你成功走出迷宫!得分:" << score << endl;

        // 回放过程
        for (const auto& p : path) {
            maze[p.first][p.second] = PATH;
            printMaze();
        }
    }

private:
    void printMaze() {
        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 difficulty;
    vector<vector<char>> maze;
    int startRow, startCol;
    int endRow, endCol;
};

int main() {
    int size, difficulty;
    cout << "请输入迷宫的大小(3~10之间的整数):";
    cin >> size;
    cout << "请输入迷宫的难度(1~10之间的整数):";
    cin >> difficulty;

    MazeGame game(size, difficulty);
    game.play();

    return 0;
}
C++ 迷宫游戏:随机生成、玩家操作、得分计算和回放功能

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

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