问题标题: 走迷宫1.0.1

0
0
已解决
马博闻
马博闻
新手守护
新手守护

本代码为AI编写,大幅度优化了地图生成逻辑

代码如下,复制到Dev-C++运行

#include <iostream>
#include <conio.h>
#include <windows.h>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <stack>
using namespace std;

// 控制台颜色定义
enum Color {
    GRAY = 8, GREEN = 2, RED = 4, YELLOW = 6, WHITE = 15
};

// 设置控制台颜色
void setColor(Color c) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

// 迷宫尺寸
const int WIDTH = 21;
const int HEIGHT = 21;

// 方向枚举
enum Direction { UP, DOWN, LEFT, RIGHT };

struct Point {
    int x, y;
    Point(int _x, int _y) : x(_x), y(_y) {}
};

class Maze {
private:
    string map[HEIGHT];
    Point player;
    Point exit;
    int stamina;  // 体力
    int coins;    // 金币

    // 检查是否在边界内
    bool inBounds(int x, int y) {
        return x >= 0 && x < HEIGHT && y >= 0 && y < WIDTH;
    }

    // 生成迷宫算法(递归回溯)
    void generateMaze() {
        // 初始化全为墙
        for (int i = 0; i < HEIGHT; i++)
            map[i] = string(WIDTH, '#');

        stack<Point> path;
        Point start(1, 1);
        map[start.x][start.y] = ' ';
        path.push(start);

        while (!path.empty()) {
            Point current = path.top();
            vector<Direction> dirs;

            // 检查可能的方向
            if (current.x > 1 && map[current.x - 2][current.y] == '#')
                dirs.push_back(UP);
            if (current.x < HEIGHT - 2 && map[current.x + 2][current.y] == '#')
                dirs.push_back(DOWN);
            if (current.y > 1 && map[current.x][current.y - 2] == '#')
                dirs.push_back(LEFT);
            if (current.y < WIDTH - 2 && map[current.x][current.y + 2] == '#')
                dirs.push_back(RIGHT);

            if (!dirs.empty()) {
                Direction dir = dirs[rand() % dirs.size()];
                switch (dir) {
                case UP:
                    map[current.x - 1][current.y] = ' ';
                    map[current.x - 2][current.y] = ' ';
                    path.push(Point(current.x - 2, current.y));
                    break;
                case DOWN:
                    map[current.x + 1][current.y] = ' ';
                    map[current.x + 2][current.y] = ' ';
                    path.push(Point(current.x + 2, current.y));
                    break;
                case LEFT:
                    map[current.x][current.y - 1] = ' ';
                    map[current.x][current.y - 2] = ' ';
                    path.push(Point(current.x, current.y - 2));
                    break;
                case RIGHT:
                    map[current.x][current.y + 1] = ' ';
                    map[current.x][current.y + 2] = ' ';
                    path.push(Point(current.x, current.y + 2));
                    break;
                }
            } else {
                path.pop();
            }
        }

        // 创建出口和初始金币
        exit = Point(HEIGHT - 2, WIDTH - 2);
        map[exit.x][exit.y] = 'E';
        generateCoins(5);  // 初始生成5个金币
    }

    // 生成金币(确保位置有效)
    void generateCoins(int num) {
        for (int i = 0; i < num; i++) {
            int x, y;
            do {
                x = rand() % HEIGHT;
                y = rand() % WIDTH;
            } while (map[x][y] != ' ' ||  // 必须是空地
                    (x == player.x && y == player.y) ||  // 不能与玩家重叠
                    (x == exit.x && y == exit.y));        // 不能与出口重叠
            map[x][y] = 'G';
        }
    }

    // 将单个字符转换为双字符
    string toDoubleChar(char c) {
        if (c == '#') return "##";
        if (c == 'P') return "Pl";
        if (c == 'E') return "M ";
        if (c == 'G') return "Go";
        return "  ";
    }

    // 打开商城
    void openShop() {
        system("cls");
        cout << "========== 商城 ==========" << endl;
        cout << "1. 面包 (3金币, +5体力)" << endl;
        cout << "2. 牛肉 (5金币, +10体力)" << endl;
        cout << "按 1 或 2 购买,按其他键返回游戏。" << endl;

        char input = _getch();
        switch (input) {
        case '1':
            if (coins >= 3) {
                coins -= 3;
                stamina += 5;
                cout << "购买成功!体力 +5" << endl;
            } else {
                cout << "金币不足!" << endl;
            }
            break;
        case '2':
            if (coins >= 5) {
                coins -= 5;
                stamina += 10;
                cout << "购买成功!体力 +10" << endl;
            } else {
                cout << "金币不足!" << endl;
            }
            break;
        default:
            cout << "返回游戏..." << endl;
            break;
        }
        Sleep(1000);  // 暂停1秒
    }

public:
    Maze() : player(1, 1), exit(HEIGHT - 2, WIDTH - 2), stamina(100), coins(0) {
        srand(time(0));
        generateMaze();
        map[player.x][player.y] = 'P';
    }

    // 绘制迷宫
    void draw() {
        system("cls");
        for (int i = 0; i < HEIGHT; i++) {
            for (int j = 0; j < WIDTH; j++) {
                char c = map[i][j];
                if (c == 'P') {
                    setColor(RED);
                    cout << toDoubleChar(c);
                } else if (c == 'E') {
                    setColor(GREEN);
                    cout << toDoubleChar(c);
                } else if (c == '#') {
                    setColor(GRAY);
                    cout << toDoubleChar(c);
                } else if (c == 'G') {
                    setColor(YELLOW);
                    cout << toDoubleChar(c);
                } else {
                    setColor(WHITE);
                    cout << toDoubleChar(c);
                }
            }
            cout << endl;
        }
        setColor(WHITE);
        cout << "体力: " << stamina << "  金币: " << coins << endl;
    }

    // 移动玩家
    bool move(Direction dir) {
        int dx = 0, dy = 0;
        switch (dir) {
        case UP: dx = -1; break;
        case DOWN: dx = 1; break;
        case LEFT: dy = -1; break;
        case RIGHT: dy = 1; break;
        }

        int newX = player.x + dx;
        int newY = player.y + dy;

        if (inBounds(newX, newY) && map[newX][newY] != '#') {
            // 拾取金币
            if (map[newX][newY] == 'G') coins++;

            map[player.x][player.y] = ' ';
            player.x = newX;
            player.y = newY;
            map[player.x][player.y] = 'P';
            stamina--;

            // 降低金币生成概率为30%
            if (rand() % 100 < 30) {
                generateCoins(1);  // 每次移动有30%概率生成1个新金币
            }
            return true;
        }
        return false;
    }

    bool isWin() {
        return player.x == exit.x && player.y == exit.y;
    }

    bool isGameOver() {
        return stamina <= 0;
    }

    // 处理商城逻辑
    void handleShop() {
        openShop();
    }
};

int main() {
    Maze maze;
    while (true) {
        maze.draw();
        if (maze.isWin()) {
            cout << "\nCongratulations! You won!" << endl;
            break;
        }
        if (maze.isGameOver()) {
            cout << "\nGame Over! You ran out of stamina." << endl;
            break;
        }

        char input = _getch();
        switch (input) {
        case 'w': maze.move(UP); break;
        case 's': maze.move(DOWN); break;
        case 'a': maze.move(LEFT); break;
        case 'd': maze.move(RIGHT); break;
        case 'e': maze.handleShop(); break;  // 按E键打开商城
        case 27: return 0; // ESC退出
        }
    }
    system("pause");
    return 0;
}

 


0
已采纳
孟津臣
孟津臣
新手守护
新手守护

上下左右是啥键???

0
我要回答