跳到正文

休闲·娱乐

本人的第一个c++游戏

chenmengyuan约 148 分钟
原文
文章信息与版本

文章信息

发布
2026年2月4日
收录
2026年2月4日
更新
2026年11月17日
篇幅
约 148 分钟
获赞
1

版本

  1. v2当前
  2. v1

啊,说正事,我做了一款地牢c++游戏,游戏原型是One Card Dungeon ,我借鉴了一下,把它做成了电脑游戏,不喜勿喷。

玩法游戏里就有,这里就不讲了。

如果发现了错误,并能改正的话,那就太感谢了,一定会关注的

C++
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <limits>
#include <conio.h>
#include <cctype>
#include <cmath>
#include <unordered_map>
#include <climits>
using namespace std;

constexpr int MAP_SIZE = 5;
constexpr int BASE_HEALTH = 6;
constexpr int MOVE_COST_ORTHO = 2;  // 正交移动成本(上下左右)
constexpr int MOVE_COST_DIAG = 3;   // 斜向移动成本(斜向)
constexpr int INIT_RANGE = 2;       // 玩家初始射程(PDF规则:初始射程2,仅能正交攻击)
constexpr int DICE_MAX = 6;

enum class Difficulty {
    EASY,
    NORMAL,
    HARD
};
Difficulty gameDifficulty;

struct Point {
    int x, y;
    Point(int x = 0, int y = 0) : x(x), y(y) {}
    bool operator==(const Point& other) const {
        return x == other.x && y == other.y;
    }
    bool operator!=(const Point& other) const {
        return !(*this == other);
    }
};

enum class MonsterType {
    NONE,
    SPIDER,
    NORMAL,
    ELITE,
    DRAGON
};

struct MonsterAttr {
    int health;
    int speed;
    int attack;
    int defense;
    int range; // 怪物射程(PDF规则:不同怪物有不同射程,如蜘蛛射程3)
};

struct Monster {
    int fixedId;
    Point pos;
    int health;
    int speed;
    int attack;
    int defense;
    int range;
    Monster(int id, Point p, int h, int s, int a, int d, int r)
        : fixedId(id), pos(p), health(h), speed(s), attack(a), defense(d), range(r) {}
};

class OneCardDungeon {
private:
    int currentLevel;
    int adventurerHealth;
    int baseSpeed, baseAttack, baseDefense, baseRange; // 玩家基础属性(射程为单值)
    int tempSpeed, tempAttack, tempDefense;            // 本回合临时属性(射程固定,不临时加成)
    Point adventurerPos;
    vector<Monster> monsters;
    vector<string> dungeonMap;
    bool firstRun = true;
    int nextMonsterId;

    // PDF原版怪物属性(射程按PDF配置,如蜘蛛射程3)
    const unordered_map<MonsterType, MonsterAttr> MONSTER_ATTRS = {
        {MonsterType::SPIDER, {2, 5, 4, 4, 3}},    // 蜘蛛:射程3
        {MonsterType::NORMAL, {3, 4, 5, 4, 4}},    // 普通怪物:射程4
        {MonsterType::ELITE, {5, 3, 7, 7, 2}},     // 精英怪物:射程2
        {MonsterType::DRAGON, {5, 5, 5, 5, 5}}     // 龙:射程5
    };

    const unordered_map<int, vector<string>> DUNGEON_MAPS = {
        {1, {".....", "...#.", ".....", ".#.#.", "....."}},
        {2, {".....", ".....", "#..#.", "...#.", "....."}},
        {3, {".....", ".#.#.", ".....", ".#...", "....."}},
        {4, {".....", ".#...", ".#..#", ".....", "....."}}
    };

    struct LevelConfig {
        vector<string> map_data;
        Point player_pos;
        MonsterType monster_type;
        vector<Point> monster_pos;
    };
    vector<LevelConfig> levelConfigs;

    void drawAttributePanel() {
        system("cls");
        cout << "========================================" << endl;
        cout << "           冒险者状态面板               " << endl;
        cout << "========================================" << endl;
        cout << "当前层数:" << currentLevel << "  游戏难度:" << ((gameDifficulty==Difficulty::EASY)?"简单":(gameDifficulty==Difficulty::NORMAL)?"普通":"困难") << endl;
        cout << "当前生命:" << adventurerHealth << "/" << BASE_HEALTH << endl;
        cout << "----------------------------------------" << endl;
        // PDF规则:射程固定,不随骰子分配加成,仅通过通关升级提升
        cout << "基础属性:速度" << baseSpeed << " 攻击" << baseAttack << " 防御" << baseDefense << " 射程" << baseRange << "(永久)" << endl;
        cout << "临时属性:速度" << tempSpeed << " 攻击" << tempAttack << " 防御" << tempDefense << "(本回合,射程固定)" << endl;
        cout << "当前怪物:" << monsters.size() << "只" << endl;
        cout << "========================================" << endl;
    }

    void drawGameMap(const string& phase, int spd = -1, int atk = -1) {
        drawAttributePanel();
        cout << "\n第" << currentLevel << "层 - " << phase << endl;
        if (spd != -1 && atk != -1)
            cout << "剩余速度:" << spd << "  剩余攻击:" << atk << endl;
        // PDF规则说明:射程=到达目标的最小移动点数(正交2/斜向3)
        cout << "图例:@=玩家  数字=怪物编号  #=墙  .=地板 | 射程规则:=最小移动点数(正交2/斜向3)" << endl;
        cout << "----------------------------------------" << endl;

        vector<string> mp = dungeonMap;
        mp[adventurerPos.y][adventurerPos.x] = '@';
        for (auto& monster : monsters) {
            int x = monster.pos.x;
            int y = monster.pos.y;
            mp[y][x] = '0' + monster.fixedId;
        }

        for (auto& row : mp) {
            for (char c : row) cout << c << "  ";
            cout << endl;
        }

        cout << "----------------------------------------" << endl;
        for (auto& monster : monsters) {
            // 显示怪物射程(PDF原版配置)
            cout << "怪物" << monster.fixedId << ":生命" << monster.health << " 速度" << monster.speed << " 攻击" << monster.attack 
                 << " 防御" << monster.defense << " 射程" << monster.range << " 位置(" << monster.pos.x << "," << monster.pos.y << ")" << endl;
        }
    }

    void showGuide() {
        drawGameMap("新手引导");
        cout << "\n操作指引(完全遵循PDF规则):" << endl;
        cout << "1. WASD 上下左右移动,消耗" << MOVE_COST_ORTHO << "点速度(正交移动)" << endl;
        cout << "2. QEZC 斜向移动,消耗" << MOVE_COST_DIAG << "点速度(斜向移动)" << endl;
        cout << "3. 数字键攻击对应编号怪物,0键结束回合" << endl;
        // PDF核心规则:射程=最小移动点数,需满足射程≤自身射程+视线无阻挡+攻击力≥怪物防御
        cout << "4. 攻击需满足:射程≤自身射程(最小移动点数)+视线无阻挡+攻击力≥怪物防御" << endl;
        cout << "5. 骰子需全部分配给速度/攻击/防御,射程固定不分配(PDF规则)" << endl;
        cout << "\n按任意键开始冒险..." << endl;
        _getch();
        firstRun = false;
    }

    // PDF核心规则:计算两点间的最小移动点数(即射程)
    int calculateRange(Point a, Point b) {
        int dx = abs(a.x - b.x);
        int dy = abs(a.y - b.y);
        // 最小移动点数=正交移动点数+斜向移动点数(斜向1格=3点,正交1格=2点)
        return (dx + dy - min(dx, dy)) * MOVE_COST_ORTHO + min(dx, dy) * MOVE_COST_DIAG;
    }

    // 曼哈顿距离(仅用于寻路优先级排序,不参与射程计算)
    int manhattanDistance(Point a, Point b) {
        return abs(a.x - b.x) + abs(a.y - b.y);
    }

    // PDF规则:视线判断(从任意角到任意角,不穿过墙或其他怪物)
    bool hasLineOfSight(Point a, Point b, const vector<Monster>& currentMonsters) {
        if (a == b) return true;
        vector<Point> aCorners = { {a.x, a.y}, {a.x+1, a.y}, {a.x, a.y+1}, {a.x+1, a.y+1} };
        vector<Point> bCorners = { {b.x, b.y}, {b.x+1, b.y}, {b.x, b.y+1}, {b.x+1, b.y+1} };

        for (auto& p1 : aCorners) {
            for (auto& p2 : bCorners) {
                double x0 = p1.x, y0 = p1.y;
                double x1 = p2.x, y1 = p2.y;
                double dx = fabs(x1 - x0), dy = fabs(y1 - y0);
                int sx = (x0 < x1) ? 1 : -1;
                int sy = (y0 < y1) ? 1 : -1;
                double err = dx - dy;
                bool lineClear = true;

                while (true) {
                    int gridX = floor(x0);
                    int gridY = floor(y0);
                    
                    if (gridX >= 0 && gridX < MAP_SIZE && gridY >= 0 && gridY < MAP_SIZE) {
                        // 穿过墙=视线阻挡
                        if (dungeonMap[gridY][gridX] == '#') {
                            lineClear = false;
                            break;
                        }
                        // 穿过其他怪物=视线阻挡(PDF规则)
                        Point gridP(gridX, gridY);
                        for (auto& m : currentMonsters) {
                            if (m.pos == gridP && m.pos != a && m.pos != b) {
                                lineClear = false;
                                break;
                            }
                        }
                        if (!lineClear) break;
                    }

                    if (fabs(x0 - x1) < 1e-6 && fabs(y0 - y1) < 1e-6) break;
                    
                    double e2 = 2 * err;
                    if (e2 > -dy) { err -= dy; x0 += sx; }
                    if (e2 < dx) { err += dx; y0 += sy; }
                }

                if (lineClear) return true;
            }
        }
        return false;
    }

    // 合法格子判断(移动/停留)
    bool isLegalTile(Point p, const vector<Monster>& currentMonsters, const vector<Point>& occupiedTiles, bool isMovement = false) {
        if (p.x < 0 || p.x >= MAP_SIZE || p.y < 0 || p.y >= MAP_SIZE) return false;
        if (dungeonMap[p.y][p.x] == '#') return false;
        // 不能停留在怪物格子
        for (const auto& m : currentMonsters) {
            if (m.pos == p) return false;
        }
        // 移动时可穿过怪物,但不能停留(PDF规则)
        if (!isMovement) {
            for (const auto& occ : occupiedTiles) {
                if (p == occ) return false;
            }
        }
        return true;
    }

    // 获取单步移动成本(正交2/斜向3)
    int getMoveCost(Point from, Point to) {
        int dx = abs(from.x - to.x);
        int dy = abs(from.y - to.y);
        if (dx + dy == 1) return MOVE_COST_ORTHO;  // 正交
        if (dx == 1 && dy == 1) return MOVE_COST_DIAG; // 斜向
        return INT_MAX; // 不可移动
    }

    // PDF规则:怪物寻路目标优先级(优先进入射程+视线,再靠近)
    Point findBestTargetTile(Monster& monster, const vector<Monster>& currentMonsters, const vector<Point>& occupiedTiles) {
        Point bestTile = monster.pos;
        int bestPriority = -1;
        int minRangeCost = calculateRange(monster.pos, adventurerPos); // 当前到玩家的射程成本

        // 当前位置已满足射程+视线,直接停留
        bool curInRange = (minRangeCost <= monster.range);
        bool curLoS = hasLineOfSight(monster.pos, adventurerPos, currentMonsters);
        if (curInRange && curLoS) {
            return monster.pos;
        }

        // 遍历所有可达格子,选择最优目标
        for (int y = 0; y < MAP_SIZE; ++y) {
            for (int x = 0; x < MAP_SIZE; ++x) {
                Point tile(x, y);
                if (!isLegalTile(tile, currentMonsters, occupiedTiles, false)) {
                    continue;
                }
                // 怪物移动到该格子的成本必须≤自身速度
                int moveCostToTile = calculateRange(monster.pos, tile);
                if (moveCostToTile == INT_MAX || moveCostToTile > monster.speed) {
                    continue;
                }

                // 计算目标格子到玩家的射程和视线
                int tileToPlayerRange = calculateRange(tile, adventurerPos);
                bool tileLoS = hasLineOfSight(tile, adventurerPos, currentMonsters);
                int currentPriority = 0;

                // PDF怪物行为优先级:1. 进入射程+视线 2. 有视线 3. 更靠近 4. 无优先级
                if (tileToPlayerRange <= monster.range && tileLoS) currentPriority = 3;
                else if (tileLoS) currentPriority = 2;
                else if (tileToPlayerRange < minRangeCost) currentPriority = 1;
                else currentPriority = 0;

                // 困难难度加成优先级
                if (gameDifficulty == Difficulty::HARD && currentPriority > 0) {
                    currentPriority += 1;
                }

                // 更新最优格子
                if (currentPriority > bestPriority) {
                    bestPriority = currentPriority;
                    minRangeCost = tileToPlayerRange;
                    bestTile = tile;
                } else if (currentPriority == bestPriority) {
                    // 优先级相同,选射程成本更低的
                    if (tileToPlayerRange < minRangeCost) {
                        minRangeCost = tileToPlayerRange;
                        bestTile = tile;
                    }
                }
            }
        }

        return bestTile;
    }

    // PDF规则:怪物移动(正交2/斜向3成本,可穿过怪物但不停留)
    void moveMonsterToTarget(Monster& monster, Point targetTile) {
        int remainingSpeed = monster.speed;
        Point currentPos = monster.pos;
        if (currentPos == targetTile || remainingSpeed <= 0) {
            return;
        }

        const vector<Point> directions = {
            {0, -1}, {0, 1}, {-1, 0}, {1, 0},  // 正交
            {-1, -1}, {-1, 1}, {1, -1}, {1, 1} // 斜向
        };

        while (remainingSpeed > 0 && currentPos != targetTile) {
            Point bestNextPos = currentPos;
            int minMoveCost = INT_MAX;
            int currentRangeCost = calculateRange(currentPos, targetTile);

            // 选择单步成本最低且更靠近目标的方向
            for (const auto& dir : directions) {
                Point nextPos(currentPos.x + dir.x, currentPos.y + dir.y);
                int cost = getMoveCost(currentPos, nextPos);
                if (cost != INT_MAX && cost <= remainingSpeed &&
                    isLegalTile(nextPos, monsters, {}, true) && // 移动时允许穿过怪物
                    calculateRange(nextPos, targetTile) < currentRangeCost) {
                    if (cost < minMoveCost) {
                        minMoveCost = cost;
                        bestNextPos = nextPos;
                    }
                }
            }

            // 无法靠近目标,停止移动
            if (bestNextPos == currentPos) {
                break;
            }

            // 消耗速度移动
            remainingSpeed -= minMoveCost;
            currentPos = bestNextPos;
        }

        // 输出移动信息
        if (currentPos != monster.pos) {
            cout << "怪物" << monster.fixedId << " 从(" << monster.pos.x << "," << monster.pos.y 
                 << ")移动到(" << currentPos.x << "," << currentPos.y << "),剩余速度:" << remainingSpeed << endl;
            monster.pos = currentPos;
        } else {
            cout << "怪物" << monster.fixedId << " 被阻挡,无法移动" << endl;
        }
    }

    // 初始化关卡配置(按PDF 12层设计)
    void initLevelData() {
        auto getMap = [&](int mapId) {
            return DUNGEON_MAPS.at(mapId);
        };

        // 12层关卡配置(怪物类型、位置按PDF逻辑)
        levelConfigs.push_back({getMap(1), Point(0,4), MonsterType::SPIDER, {{4,2},{3,0}}});    // 层1:蜘蛛
        levelConfigs.push_back({getMap(2), Point(4,4), MonsterType::NORMAL, {{0,1},{2,0}}});   // 层2:普通怪物
        levelConfigs.push_back({getMap(3), Point(0,4), MonsterType::ELITE, {{4,2}}});          // 层3:精英怪物
        levelConfigs.push_back({getMap(4), Point(4,4), MonsterType::DRAGON, {{1,0}}});         // 层4:龙
        levelConfigs.push_back({getMap(1), Point(0,4), MonsterType::SPIDER, {{1,0},{4,1},{4,4}}}); // 层5:多蜘蛛
        levelConfigs.push_back({getMap(2), Point(4,4), MonsterType::NORMAL, {{0,4},{1,1},{1,0}}});// 层6:多普通怪物
        levelConfigs.push_back({getMap(3), Point(0,4), MonsterType::ELITE, {{2,1},{4,3}}});     // 层7:多精英
        levelConfigs.push_back({getMap(4), Point(4,4), MonsterType::DRAGON, {{3,0},{1,4}}});    // 层8:多龙
        levelConfigs.push_back({getMap(1), Point(0,4), MonsterType::SPIDER, {{0,0},{2,2},{4,3},{2,4}}});// 层9:4只蜘蛛
        levelConfigs.push_back({getMap(2), Point(4,4), MonsterType::NORMAL, {{4,0},{2,1},{1,2},{0,3}}});// 层10:4只普通
        levelConfigs.push_back({getMap(3), Point(0,4), MonsterType::ELITE, {{1,0},{3,0},{4,2}}});// 层11:3只精英
        levelConfigs.push_back({getMap(4), Point(4,4), MonsterType::DRAGON, {{0,1},{2,1},{0,3}}});// 层12:3只龙(PDF通关目标)
    }

    // 验证骰子分配是否有效(必须用完3颗骰子)
    bool isAllocationValid(int s, int a, int d, vector<int> dice) {
        vector<int> allocated = {s, a, d};
        sort(allocated.begin(), allocated.end());
        sort(dice.begin(), dice.end());
        return allocated == dice;
    }

    // 获取小写输入字符
    char getLowerCharInput() {
        char c = tolower(_getch());
        cout << endl;
        return c;
    }

    // 按难度调整怪物属性(PDF基础上调整)
    MonsterAttr adjustMonsterAttrByDifficulty(MonsterAttr monsterBaseAttr) {
        MonsterAttr adjusted = monsterBaseAttr;
        switch (gameDifficulty) {
            case Difficulty::EASY:
                adjusted.health = max(1, (int)floor(monsterBaseAttr.health * 0.8));
                adjusted.speed = max(1, (int)floor(monsterBaseAttr.speed * 0.8));
                adjusted.attack = max(1, (int)floor(monsterBaseAttr.attack * 0.8));
                adjusted.defense = max(1, (int)floor(monsterBaseAttr.defense * 0.8));
                adjusted.range = max(1, (int)floor(monsterBaseAttr.range * 0.8)); // 射程也按难度调整
                break;
            case Difficulty::HARD:
                adjusted.health = (int)ceil(monsterBaseAttr.health * 1.2);
                adjusted.speed = (int)ceil(monsterBaseAttr.speed * 1.2);
                adjusted.attack = (int)ceil(monsterBaseAttr.attack * 1.2);
                adjusted.defense = (int)ceil(monsterBaseAttr.defense * 1.2);
                adjusted.range = (int)ceil(monsterBaseAttr.range * 1.2); // 射程也按难度调整
                break;
            default:
                break;
        }
        return adjusted;
    }

    // 按难度初始化玩家属性(PDF原版初始属性:普通/困难1/1/1/2,简单2/2/2/2)
    void initPlayerAttrByDifficulty() {
        switch (gameDifficulty) {
            case Difficulty::EASY:
                baseSpeed = 2;
                baseAttack = 2;
                baseDefense = 2;
                baseRange = INIT_RANGE; // 初始射程2(PDF规则)
                break;
            case Difficulty::NORMAL:
                baseSpeed = 1;
                baseAttack = 1;
                baseDefense = 1;
                baseRange = INIT_RANGE; // 初始射程2(PDF规则)
                break;
            case Difficulty::HARD:
                baseSpeed = 1;
                baseAttack = 1;
                baseDefense = 1;
                baseRange = INIT_RANGE; // 初始射程2(PDF规则)
                break;
        }
    }

    // PDF规则:计算玩家受到的伤害(总攻击÷总防御,向下取整)
    int calculatePlayerDamage(int totalMonsterAttack, int playerDefense) {
        int damage = 0;
        switch (gameDifficulty) {
            case Difficulty::EASY:
                damage = max(0, (int)floor((double)totalMonsterAttack / max(1, playerDefense)));
                break;
            case Difficulty::NORMAL:
                damage = (playerDefense > 0) ? (totalMonsterAttack / playerDefense) : totalMonsterAttack;
                if (playerDefense > totalMonsterAttack) damage = 0;
                break;
            case Difficulty::HARD:
                if (totalMonsterAttack > 0) {
                    damage = max(1, (int)ceil((double)totalMonsterAttack / max(1, playerDefense)));
                }
                break;
        }
        return damage;
    }

    // 选择游戏难度
    void selectDifficulty() {
        system("cls");
        cout << "========================================" << endl;
        cout << "           选择游戏难度                 " << endl;
        cout << "========================================" << endl;
        cout << "1. 简单难度" << endl;
        cout << "   玩家初始属性2/2/2/2,怪物属性×0.8,伤害向下取整" << endl;
        cout << "2. 普通难度(默认)" << endl;
        cout << "   玩家初始属性1/1/1/2,无属性修正,标准伤害计算(PDF原版)" << endl;
        cout << "3. 困难难度" << endl;
        cout << "   玩家初始属性1/1/1/2,怪物属性×1.2,伤害向上取整" << endl;
        cout << "========================================" << endl;
        cout << "请选择难度(1/2/3):";
        
        while (true) {
            char c = tolower(_getch());
            if (c == '1' || c == '2' || c == '3') {
                gameDifficulty = (c == '1') ? Difficulty::EASY : (c == '2') ? Difficulty::NORMAL : Difficulty::HARD;
                break;
            } else {
                cout << "\n选择无效!请输入1、2或3:";
            }
        }
        
        string diffStr = (gameDifficulty == Difficulty::EASY) ? "简单" : (gameDifficulty == Difficulty::NORMAL) ? "普通" : "困难";
        cout << "\n已选择【" << diffStr << "】难度,按任意键进入游戏..." << endl;
        _getch();
        initPlayerAttrByDifficulty();
    }

    // 显示PDF原版游戏规则
    void showRule() {
        system("cls");
        cout << "========================================" << endl;
        cout << "               游戏规则(PDF原版)       " << endl;
        cout << "========================================" << endl;
        cout << "【核心目标】" << endl;
        cout << "挑战12层地牢,击败每层怪物,最终战胜3只恶龙获得胜利(PDF目标)" << endl;
        cout << "----------------------------------------" << endl;
        cout << "【回合流程】" << endl;
        cout << "1. 能量分配:掷出3颗骰子,全部分配给速度/攻击/防御(射程固定)" << endl;
        cout << "2. 玩家行动:消耗速度移动,消耗攻击攻击怪物,0键结束" << endl;
        cout << "3. 怪物移动:按优先级移动,优先进入射程+视线" << endl;
        cout << "4. 怪物攻击:所有在射程+视线内的怪物发起攻击" << endl;
        cout << "----------------------------------------" << endl;
        cout << "【核心规则】" << endl;
        cout << "1. 移动:正交2点速度/斜向3点速度,不可穿墙/停留怪物格子" << endl;
        cout << "2. 射程:=最小移动点数(正交2/斜向3),攻击需满足:" << endl;
        cout << "   射程≤自身射程 + 视线无阻挡(墙/其他怪物) + 攻击力≥怪物防御" << endl;
        cout << "3. 攻击:消耗攻击点数=怪物防御值,每次攻击造成1点伤害(PDF规则)" << endl;
        cout << "4. 通关奖励:选择属性+1(速度/攻击/防御/射程)或生命回满(不可兼得)" << endl;
        cout << "----------------------------------------" << endl;
        cout << "【难度差异】" << endl;
        cout << "简单:玩家属性强化,怪物弱化,伤害计算有利" << endl;
        cout << "普通:标准数值,完全遵循PDF原版规则" << endl;
        cout << "困难:怪物强化,伤害计算惩罚,按攻击力优先行动" << endl;
        cout << "========================================" << endl;
        cout << "按任意键返回主菜单..." << endl;
        _getch();
        showMainMenu();
    }

    // 显示制作团队信息
    void showTeam() {
        system("cls");
        cout << "========================================" << endl;
        cout << "             制作团队信息               " << endl;
        cout << "========================================" << endl;
        cout << endl;
        cout << "         制作:CMY与他的好朋友们               " << endl;
        cout << "         规则依据:One Card Dungeon PDF v1.1" << endl;
        cout << endl;
        cout << "========================================" << endl;
        cout << "按任意键返回主菜单..." << endl;
        _getch();
        showMainMenu();
    }

public:
    OneCardDungeon() {
        srand((unsigned int)time(NULL));
        initLevelData();
    }

    // 主菜单(立体艺术字)
    void showMainMenu() {
        int select = 1;
        while (true) {
            system("cls");
            cout << "##########################################" << endl;
            cout << "#  ######  #     #  #####  #     #  ###  #" << endl;
            cout << "#  #     # #     # #     # ##   ##  #    #" << endl;
            cout << "#  #     # #     # #     # # # # #  #    #" << endl;
            cout << "#  ######  #     #  #####  #  #  #  ###  #" << endl;
            cout << "#  #   #   #     # #     # #     #     # #" << endl;
            cout << "#  #    #  #     # #     # #     # #   # #" << endl;
            cout << "#  #     #  #####   #####  #     #  ###  #" << endl;
            cout << "##########################################" << endl;
            cout << endl;
            cout << endl;
            cout << "                " << (select == 1 ? "【>】开始游戏" : "    开始游戏") << endl;
            cout << "                " << (select == 2 ? "【>】规则介绍" : "    规则介绍") << endl;
            cout << "                " << (select == 3 ? "【>】制作团队" : "    制作团队") << endl;
            cout << endl;
            cout << endl;
            cout << "                                          操作指引:WS选择 Enter确认" << endl;
            cout << "                                          版本信息:V1.0(PDF原版规则)" << endl;
            cout << "                                          游戏类型:文字地牢冒险" << endl;
            cout << "========================================" << endl;

            char key = _getch();
            if (key == 'w' || key == 'W') {
                select--;
                if (select < 1) select = 3;
            } else if (key == 's' || key == 'S') {
                select++;
                if (select > 3) select = 1;
            } else if (key == 13) {
                if (select == 1) {
                    selectDifficulty();
                    initGame();
                    gameLoop();
                    break;
                } else if (select == 2) {
                    showRule();
                } else if (select == 3) {
                    showTeam();
                }
            }
        }
    }

    // 初始化游戏
    void initGame() {
        currentLevel = 1;
        adventurerHealth = BASE_HEALTH; // 初始生命6(PDF规则)
        tempSpeed = baseSpeed;
        tempAttack = baseAttack;
        tempDefense = baseDefense;
        firstRun = true;
        loadLevel(currentLevel);
    }

    // 重置游戏
    void resetGame() {
        currentLevel = 1;
        adventurerHealth = BASE_HEALTH;
        tempSpeed = baseSpeed;
        tempAttack = baseAttack;
        tempDefense = baseDefense;
        firstRun = true;
        loadLevel(currentLevel);
    }

    // 加载关卡(按PDF规则初始化玩家和怪物位置、属性)
    void loadLevel(int lv) {
        if (lv < 1 || lv > 12) return;
        nextMonsterId = 1;
        auto& cfg = levelConfigs[lv-1];
        dungeonMap = cfg.map_data;
        adventurerPos = cfg.player_pos;
        monsters.clear();
        
        // 加载怪物基础属性并按难度调整
        MonsterAttr originalAttr = MONSTER_ATTRS.at(cfg.monster_type);
        MonsterAttr adjustedAttr = adjustMonsterAttrByDifficulty(originalAttr);
        
        // 生成怪物(位置按关卡配置,属性按调整后的值)
        for (auto& p : cfg.monster_pos) {
            monsters.emplace_back(
                nextMonsterId++,
                p, 
                adjustedAttr.health, 
                adjustedAttr.speed, 
                adjustedAttr.attack, 
                adjustedAttr.defense, 
                adjustedAttr.range // 怪物射程(PDF原版+难度调整)
            );
        }
        
        // 重置本回合临时属性
        tempSpeed = baseSpeed;
        tempAttack = baseAttack;
        tempDefense = baseDefense;
        if (firstRun) showGuide();
    }

    // 能量阶段(PDF规则:掷3颗骰子,分配给速度/攻击/防御,射程不分配)
    void energyPhase() {
        drawGameMap("能量分配");
        int d1 = rand()%DICE_MAX+1, d2=rand()%DICE_MAX+1, d3=rand()%DICE_MAX+1;
        vector<int> dice = {d1, d2, d3};
        cout << "\n掷出能量骰子:" << d1 << "、" << d2 << "、" << d3 << endl;
        cout << "请将骰子全部分配给速度/攻击/防御(空格分隔):";

        int s=0, a=0, d=0;
        while (true) {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            if (cin >> s >> a >> d) {
                if (isAllocationValid(s, a, d, dice)) {
                    break;
                } else {
                    cout << "分配无效!请使用全部点数:";
                }
            } else {
                cout << "输入无效!请输入3个整数(如3 5 2):";
            }
        }

        // 临时属性=基础属性+骰子分配值(射程固定,不加成)
        tempSpeed = baseSpeed + s;
        tempAttack = baseAttack + a;
        tempDefense = baseDefense + d;
        cout << "\n分配完成!本回合临时属性:速度" << tempSpeed << " 攻击" << tempAttack << " 防御" << tempDefense << endl;
        cout << "按任意键进入玩家行动阶段..." << endl;
        _getch();
    }

    // 玩家行动阶段(PDF规则:移动+攻击)
    void playerPhase() {
        int remSpd = tempSpeed;
        int remAtk = tempAttack;
        while (true) {
            drawGameMap("玩家回合", remSpd, remAtk);
            cout << "\n请进行操作:";
            char k = getLowerCharInput();
            
            if (k == '0') {
                cout << "玩家回合结束,进入怪物行动阶段..." << endl;
                _getch();
                break;
            }

            // 移动处理
            Point new_pos = adventurerPos;
            int cost = 0;
            bool is_move = false;

            switch(k) {
                case 'w': new_pos.y--; cost=MOVE_COST_ORTHO; is_move=true; break;
                case 's': new_pos.y++; cost=MOVE_COST_ORTHO; is_move=true; break;
                case 'a': new_pos.x--; cost=MOVE_COST_ORTHO; is_move=true; break;
                case 'd': new_pos.x++; cost=MOVE_COST_ORTHO; is_move=true; break;
                case 'q': new_pos.x--; new_pos.y--; cost=MOVE_COST_DIAG; is_move=true; break;
                case 'e': new_pos.x++; new_pos.y--; cost=MOVE_COST_DIAG; is_move=true; break;
                case 'z': new_pos.x--; new_pos.y++; cost=MOVE_COST_DIAG; is_move=true; break;
                case 'c': new_pos.x++; new_pos.y++; cost=MOVE_COST_DIAG; is_move=true; break;
            }

            if (is_move) {
                bool outOfBound = (new_pos.x < 0 || new_pos.x >= MAP_SIZE || new_pos.y < 0 || new_pos.y >= MAP_SIZE);
                bool hitWall = !outOfBound && (dungeonMap[new_pos.y][new_pos.x] == '#');
                bool hitMonster = false;
                if (!outOfBound && !hitWall) {
                    for (auto& m : monsters) {
                        if (m.pos == new_pos) {
                            hitMonster = true;
                            break;
                        }
                    }
                }

                if (outOfBound) {
                    cout << "无法移动:超出地图边界!" << endl;
                } else if (hitWall) {
                    cout << "无法移动:撞到墙壁!" << endl;
                } else if (hitMonster) {
                    cout << "无法移动:前方有怪物!" << endl;
                } else if (remSpd < cost) {
                    cout << "无法移动:速度不足!" << endl;
                } else {
                    adventurerPos = new_pos;
                    remSpd -= cost;
                    cout << "移动成功!剩余速度:" << remSpd << endl;
                }
                _getch();
                continue;
            }

            // 攻击处理(完全遵循PDF规则)
            if (isdigit(k)) {
                int targetId = k - '0';
                if (targetId == 0) continue;

                // 查找目标怪物
                auto targetIt = find_if(monsters.begin(), monsters.end(), [&](const Monster& m) {
                    return m.fixedId == targetId;
                });

                if (targetIt == monsters.end()) {
                    cout << "攻击失败:无此编号怪物!" << endl;
                    _getch();
                    continue;
                }
                
                Monster& target = *targetIt;
                // PDF规则1:射程判断(最小移动点数≤玩家射程)
                int targetRange = calculateRange(adventurerPos, target.pos);
                if (targetRange > baseRange) {
                    cout << "攻击失败:距离超出射程(当前射程" << targetRange << ",最大射程" << baseRange << ")!" << endl;
                    _getch();
                    continue;
                }
                
                // PDF规则2:视线判断
                if (!hasLineOfSight(adventurerPos, target.pos, monsters)) {
                    cout << "攻击失败:视线被阻挡!" << endl;
                    _getch();
                    continue;
                }
                
                // PDF规则3:攻击力≥怪物防御
                if (remAtk < target.defense) {
                    cout << "攻击失败:攻击力不足(需" << target.defense << "点,剩余" << remAtk << "点)!" << endl;
                    _getch();
                    continue;
                }
                
                // 攻击成功:消耗攻击点数,怪物生命-1
                remAtk -= target.defense;
                target.health--;
                cout << "攻击命中!怪物" << targetId << "剩余生命:" << target.health << endl;
                cout << "剩余攻击点:" << remAtk << endl;
                
                // 怪物死亡:移除
                if (target.health <= 0) {
                    cout << "怪物" << targetId << "已被击败!" << endl;
                    monsters.erase(targetIt);
                    if (monsters.empty()) {
                        _getch();
                        return;
                    }
                }
                
                _getch();
            } else {
                cout << "操作无效!请按提示操作!" << endl;
                _getch();
            }
        }
    }

    // 怪物行动阶段(移动+攻击,完全遵循PDF规则)
    void monsterPhase() {
        if (monsters.empty()) return;
        drawGameMap("怪物行动");
        cout << "怪物开始行动..." << endl;
        _getch();

        // 排序怪物:普通/简单难度按距离玩家远近,困难难度按攻击力优先
        vector<Monster*> sortedMonsters;
        for (auto& m : monsters) {
            sortedMonsters.push_back(&m);
        }
        
        if (gameDifficulty == Difficulty::HARD) {
            sort(sortedMonsters.begin(), sortedMonsters.end(), [&](Monster* a, Monster* b) {
                if (a->attack != b->attack) {
                    return a->attack > b->attack;
                }
                return calculateRange(a->pos, adventurerPos) < calculateRange(b->pos, adventurerPos);
            });
        } else {
            sort(sortedMonsters.begin(), sortedMonsters.end(), [&](Monster* a, Monster* b) {
                return calculateRange(a->pos, adventurerPos) < calculateRange(b->pos, adventurerPos);
            });
        }

        vector<Point> occupiedTiles; // 已被其他怪物占据的格子
        vector<Monster*> attackMonsters; // 能攻击的怪物
        for (auto* monster : sortedMonsters) {
            cout << "\n【怪物" << monster->fixedId << "】生命:" << monster->health << " 速度:" << monster->speed << " 射程:" << monster->range << ":";
            
            // 查找最优移动目标
            Point targetTile = findBestTargetTile(*monster, monsters, occupiedTiles);
            if (targetTile != monster->pos) {
                cout << "移动至(" << targetTile.x << "," << targetTile.y << ") → ";
                moveMonsterToTarget(*monster, targetTile);
            } else {
                cout << "原地攻击";
            }

            // 判断是否能攻击(射程+视线)
            int finalRange = calculateRange(monster->pos, adventurerPos);
            bool finalLoS = hasLineOfSight(monster->pos, adventurerPos, monsters);
            if (finalRange <= monster->range && finalLoS) {
                attackMonsters.push_back(monster);
                cout << " → 发起攻击";
            } else {
                cout << " → 无法攻击(射程/视线不满足)";
            }

            // 标记当前怪物位置为已占据
            occupiedTiles.push_back(monster->pos);
            _getch();
        }

        // 怪物攻击阶段
        drawGameMap("怪物攻击阶段");
        if (attackMonsters.empty()) {
            cout << "所有怪物均无法攻击,你躲过一劫!" << endl;
            _getch();
            return;
        }

        // 计算总攻击
        int totalAtk = 0;
        cout << "共" << attackMonsters.size() << "只怪物发起攻击:" << endl;
        for (auto* m : attackMonsters) {
            cout << "怪物" << m->fixedId << " 攻击值:" << m->attack << endl;
            totalAtk += m->attack;
        }

        // 计算伤害并扣除生命
        int damage = calculatePlayerDamage(totalAtk, tempDefense);
        adventurerHealth = max(0, adventurerHealth - damage);

        cout << "----------------------------------------" << endl;
        cout << "怪物总攻击:" << totalAtk << "  你的防御:" << tempDefense << endl;
        if (damage > 0) {
            cout << "你受到" << damage << "点伤害!剩余生命:" << adventurerHealth << "/" << BASE_HEALTH << endl;
        } else {
            cout << "你的防御抵消了所有伤害!" << endl;
        }
        cout << "按任意键结束怪物回合..." << endl;
        _getch();
    }

    // 关卡结束(PDF规则:选择属性+1或生命回满)
    void levelEnd() {
        drawGameMap("关卡通关");
        cout << "\n========================================" << endl;
        cout << "           恭喜通关第" << currentLevel << "层!           " << endl;
        cout << "========================================" << endl;
        cout << "通关奖励(五选一,不可兼得,必须输入1-5):" << endl;
        cout << "1. 速度+1(永久)  2. 攻击+1(永久)" << endl;
        cout << "3. 防御+1(永久)  4. 射程+1(永久,PDF规则)" << endl;
        cout << "5. 生命回满(恢复至" << BASE_HEALTH << "/" << BASE_HEALTH << ")" << endl;
        cout << "========================================" << endl;

        // 选择奖励(强制输入1-5)
        char c = '0';
        while (true) {
            cout << "请选择奖励(1-5):";
            c = getLowerCharInput();
            if (c >= '1' && c <= '5') {
                break;
            } else {
                cout << "选择无效!请输入1、2、3、4、5中的一个有效数字!" << endl;
                cout << "按任意键重新选择..." << endl;
                _getch();
                drawGameMap("关卡通关");
                cout << "\n========================================" << endl;
                cout << "           恭喜通关第" << currentLevel << "层!           " << endl;
                cout << "========================================" << endl;
                cout << "通关奖励(五选一,不可兼得,必须输入1-5):" << endl;
                cout << "1. 速度+1(永久)  2. 攻击+1(永久)" << endl;
                cout << "3. 防御+1(永久)  4. 射程+1(永久,PDF规则)" << endl;
                cout << "5. 生命回满(恢复至" << BASE_HEALTH << "/" << BASE_HEALTH << ")" << endl;
                cout << "========================================" << endl;
            }
        }

        // 应用奖励
        switch(c) {
            case '1': baseSpeed++; cout << "速度永久提升至 " << baseSpeed << endl; break;
            case '2': baseAttack++; cout << "攻击永久提升至 " << baseAttack << endl; break;
            case '3': baseDefense++; cout << "防御永久提升至 " << baseDefense << endl; break;
            case '4': baseRange++; cout << "射程永久提升至 " << baseRange << endl; break; // 射程+1(PDF规则)
            case '5': adventurerHealth = BASE_HEALTH; cout << "生命值已回满!" << endl; break;
        }
        cout << "奖励已生效!" << endl;
        cout << "按任意键进入下一层..." << endl;
        _getch();
        
        // 通关12层(击败3只龙)= 游戏胜利(PDF目标)
        currentLevel++;
        if (currentLevel > 12) {
            system("cls");
            cout << "========================================" << endl;
            cout << "             恭喜!终极胜利             " << endl;
            cout << "========================================" << endl;
            cout << "你成功击败12层所有怪物,战胜最终3只恶龙!" << endl;
            cout << "获得了M'Guf-yn权杖,成为地牢最强冒险者!(PDF原版胜利)" << endl;
            cout << "========================================" << endl;
            cout << "按任意键退出游戏..." << endl;
            _getch();
            exit(0);
        }
        loadLevel(currentLevel);
        cout << "\n进入第" << currentLevel << "层,新的挑战开始!" << endl;
        _getch();
    }

    // 游戏主循环
    void gameLoop() {
        while (adventurerHealth > 0) {
            if (monsters.empty()) {
                levelEnd();
                continue;
            }
            energyPhase();    // 能量分配
            playerPhase();    // 玩家行动
            if (monsters.empty()) continue;
            if (adventurerHealth > 0) monsterPhase(); // 怪物行动
        }
        
        // 游戏失败
        system("cls");
        cout << "========================================" << endl;
        cout << "               冒险失败                 " << endl;
        cout << "========================================" << endl;
        cout << "你在第" << currentLevel << "层被怪物击败..." << endl;
        cout << "调整策略,重新挑战一定会成功!" << endl;
        cout << "========================================" << endl;
        cout << "是否重新开始?(Y/N):";
        char restart = tolower(_getch());
        if (restart == 'y') {
            selectDifficulty();
            resetGame();
            gameLoop();
        } else {
            cout << "\n感谢游玩,再见!" << endl;
            _getch();
            exit(0);
        }
    }
};

int main() {
    OneCardDungeon game;
    game.showMainMenu();
    return 0;
}
C++
// OneCardDungeon_Fixed.cpp
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <limits>
#include <conio.h>
#include <cctype>
#include <cmath>
#include <unordered_map>
#include <climits>
#include <queue>
#include <unordered_set>
#include <sstream>
#include <string>
using namespace std;

constexpr int MAP_SIZE = 5;
constexpr int BASE_HEALTH = 6;
constexpr int MOVE_COST_ORTHO = 2;  // 正交移动成本
constexpr int MOVE_COST_DIAG = 3;   // 斜向移动成本
constexpr int INIT_RANGE = 2;       // 初始射程
constexpr int DICE_MAX = 6;

enum class Difficulty {
    EASY,
    NORMAL,
    HARD
};
Difficulty gameDifficulty;

struct Point {
    int x, y;
    Point(int x = 0, int y = 0) : x(x), y(y) {}
    bool operator==(const Point& other) const {
        return x == other.x && y == other.y;
    }
    bool operator!=(const Point& other) const {
        return !(*this == other);
    }
};

enum class MonsterType {
    NONE,
    SPIDER,
    NORMAL,
    ELITE,
    DRAGON
};

struct MonsterAttr {
    int health;
    int speed;
    int attack;
    int defense;
    int range;
};

struct Monster {
    int fixedId;
    Point pos;
    int health;
    int speed;
    int attack;
    int defense;
    int range;
    Monster(int id, Point p, int h, int s, int a, int d, int r)
        : fixedId(id), pos(p), health(h), speed(s), attack(a), defense(d), range(r) {}
};

class OneCardDungeon {
private:
    int currentLevel;
    int adventurerHealth;
    int baseSpeed, baseAttack, baseDefense, baseRange;
    int tempSpeed, tempAttack, tempDefense;
    Point adventurerPos;
    vector<Monster> monsters;
    vector<string> dungeonMap;
    bool firstRun = true;
    int nextMonsterId;

    const unordered_map<MonsterType, MonsterAttr> MONSTER_ATTRS = {
        {MonsterType::SPIDER, {2, 5, 4, 4, 3}},
        {MonsterType::NORMAL, {3, 4, 5, 4, 4}},
        {MonsterType::ELITE, {5, 3, 7, 7, 2}},
        {MonsterType::DRAGON, {5, 5, 5, 5, 5}}
    };

    const unordered_map<int, vector<string>> DUNGEON_MAPS = {
        {1, {".....", "...#.", ".....", ".#.#.", "....."}},
        {2, {".....", ".....", "#..#.", "...#.", "....."}},
        {3, {".....", ".#.#.", ".....", ".#...", "....."}},
        {4, {".....", ".#...", ".#..#", ".....", "....."}}
    };

    struct LevelConfig {
        vector<string> map_data;
        Point player_pos;
        MonsterType monster_type;
        vector<Point> monster_pos;
    };
    vector<LevelConfig> levelConfigs;

    void drawAttributePanel() {
        system("cls");
        cout << "========================================" << endl;
        cout << "           冒险者状态面板               " << endl;
        cout << "========================================" << endl;
        cout << "当前层数:" << currentLevel << "  游戏难度:" << ((gameDifficulty==Difficulty::EASY)?"简单":(gameDifficulty==Difficulty::NORMAL)?"普通":"困难") << endl;
        cout << "当前生命:" << adventurerHealth << "/" << BASE_HEALTH << endl;
        cout << "----------------------------------------" << endl;
        cout << "基础属性:速度" << baseSpeed << " 攻击" << baseAttack << " 防御" << baseDefense << " 射程" << baseRange << "(永久)" << endl;
        cout << "临时属性:速度" << tempSpeed << " 攻击" << tempAttack << " 防御" << tempDefense << "(本回合,射程固定)" << endl;
        cout << "当前怪物:" << monsters.size() << "只" << endl;
        cout << "========================================" << endl;
    }

    void drawGameMap(const string& phase, int spd = -1, int atk = -1) {
        drawAttributePanel();
        cout << "\n第" << currentLevel << "层 - " << phase << endl;
        if (spd != -1 && atk != -1)
            cout << "剩余速度:" << spd << "  剩余攻击:" << atk << endl;
        cout << "图例:@=玩家  数字=怪物编号  #=墙  .=地板 | 射程规则:=最小移动点数(正交2/斜向3)" << endl;
        cout << "----------------------------------------" << endl;

        vector<string> mp = dungeonMap;
        mp[adventurerPos.y][adventurerPos.x] = '@';
        for (auto& monster : monsters) {
            int x = monster.pos.x;
            int y = monster.pos.y;
            mp[y][x] = '0' + monster.fixedId;
        }

        for (auto& row : mp) {
            for (char c : row) cout << c << "  ";
            cout << endl;
        }

        cout << "----------------------------------------" << endl;
        for (auto& monster : monsters) {
            cout << "怪物" << monster.fixedId << ":生命" << monster.health << " 速度" << monster.speed << " 攻击" << monster.attack 
                 << " 防御" << monster.defense << " 射程" << monster.range << " 位置(" << monster.pos.x << "," << monster.pos.y << ")" << endl;
        }
    }

    void showGuide() {
        drawGameMap("新手引导");
        cout << "\n操作指引(完全遵循PDF规则):" << endl;
        cout << "1. WASD 上下左右移动,消耗" << MOVE_COST_ORTHO << "点速度(正交移动)" << endl;
        cout << "2. QEZC 斜向移动,消耗" << MOVE_COST_DIAG << "点速度(斜向移动)" << endl;
        cout << "3. 数字键攻击对应编号怪物,0键结束回合" << endl;
        cout << "4. 攻击需满足:射程≤自身射程 + 视线无阻挡 + 剩余攻击点 ≥ 怪物防御" << endl;
        cout << "5. 每次攻击消耗 = 怪物防御值,造成1点伤害" << endl;
        cout << "\n按任意键开始冒险..." << endl;
        _getch();
        firstRun = false;
    }

    int calculateRange(Point a, Point b) {
        int dx = abs(a.x - b.x);
        int dy = abs(a.y - b.y);
        return (dx + dy - min(dx, dy)) * MOVE_COST_ORTHO + min(dx, dy) * MOVE_COST_DIAG;
    }

    // ✅ 修复:使用整数 Bresenham 直线算法判断视线
    bool hasLineOfSight(Point a, Point b, const vector<Monster>& currentMonsters) {
        if (a == b) return true;

        int dx = abs(b.x - a.x);
        int dy = abs(b.y - a.y);
        int sx = (a.x < b.x) ? 1 : -1;
        int sy = (a.y < b.y) ? 1 : -1;
        int err = dx - dy;
        int x = a.x, y = a.y;

        // 构建障碍集合(排除起点和终点)
        unordered_set<int> blocked;
        for (const auto& m : currentMonsters) {
            if (m.pos != a && m.pos != b) {
                blocked.insert(m.pos.y * MAP_SIZE + m.pos.x);
            }
        }

        while (!(x == b.x && y == b.y)) {
            int e2 = 2 * err;
            if (e2 > -dy) {
                err -= dy;
                x += sx;
            }
            if (e2 < dx) {
                err += dx;
                y += sy;
            }

            // 检查 (x, y) 是否合法
            if (x < 0 || x >= MAP_SIZE || y < 0 || y >= MAP_SIZE) continue;
            if (dungeonMap[y][x] == '#') return false;
            if (blocked.count(y * MAP_SIZE + x)) return false;
        }
        return true;
    }

    // ✅ 新增:带障碍的通行判断
    bool isPassableForMonster(Point p, const unordered_set<int>& occupied) {
        if (p.x < 0 || p.x >= MAP_SIZE || p.y < 0 || p.y >= MAP_SIZE) return false;
        if (dungeonMap[p.y][p.x] == '#') return false;
        if (occupied.count(p.y * MAP_SIZE + p.x)) return false;
        if (p == adventurerPos) return false; // 不能进玩家格
        return true;
    }

    int getMoveCost(Point from, Point to) {
        int dx = abs(from.x - to.x);
        int dy = abs(from.y - to.y);
        if (dx + dy == 1) return MOVE_COST_ORTHO;
        if (dx == 1 && dy == 1) return MOVE_COST_DIAG;
        return INT_MAX;
    }

    // ✅ 修复:使用 Dijkstra 找最佳可达目标格
    Point findBestTargetTile(Monster& monster, const unordered_set<int>& occupied) {
        Point bestTile = monster.pos;
        int bestPriority = -1;
        int minRangeToPlayer = calculateRange(monster.pos, adventurerPos);

        // 检查当前位置是否可攻击
        if (minRangeToPlayer <= monster.range && hasLineOfSight(monster.pos, adventurerPos, monsters)) {
            return monster.pos;
        }

        // Dijkstra 找所有可达格
        priority_queue<pair<int, Point>, vector<pair<int, Point>>, greater<pair<int, Point>>> pq;
        unordered_map<int, int> dist;
        pq.push({0, monster.pos});
        dist[monster.pos.y * MAP_SIZE + monster.pos.x] = 0;

        const vector<Point> dirs = {{0,-1},{0,1},{-1,0},{1,0},{-1,-1},{-1,1},{1,-1},{1,1}};

        while (!pq.empty()) {
            auto [cost, cur] = pq.top(); pq.pop();
            int key = cur.y * MAP_SIZE + cur.x;
            if (dist.count(key) && dist[key] < cost) continue;

            // 评估此格
            if (cost <= monster.speed) {
                int rangeToPlayer = calculateRange(cur, adventurerPos);
                bool los = hasLineOfSight(cur, adventurerPos, monsters);
                int priority = 0;
                if (rangeToPlayer <= monster.range && los) priority = 3;
                else if (los) priority = 2;
                else if (rangeToPlayer < minRangeToPlayer) priority = 1;

                if (gameDifficulty == Difficulty::HARD && priority > 0) priority++;

                if (priority > bestPriority || 
                    (priority == bestPriority && rangeToPlayer < minRangeToPlayer)) {
                    bestPriority = priority;
                    minRangeToPlayer = rangeToPlayer;
                    bestTile = cur;
                }
            }

            if (cost >= monster.speed) continue;

            for (const auto& d : dirs) {
                Point next(cur.x + d.x, cur.y + d.y);
                if (!isPassableForMonster(next, occupied)) continue;
                int moveCost = getMoveCost(cur, next);
                if (moveCost == INT_MAX) continue;
                int newCost = cost + moveCost;
                if (newCost > monster.speed) continue;
                int nkey = next.y * MAP_SIZE + next.x;
                if (!dist.count(nkey) || newCost < dist[nkey]) {
                    dist[nkey] = newCost;
                    pq.push({newCost, next});
                }
            }
        }

        return bestTile;
    }

    // ✅ 修复:移动时考虑实时障碍
    void moveMonsterToTarget(Monster& monster, Point targetTile, const unordered_set<int>& occupied) {
        if (monster.pos == targetTile || monster.speed <= 0) {
            return;
        }

        // 使用 Dijkstra 找精确路径
        priority_queue<pair<int, Point>, vector<pair<int, Point>>, greater<pair<int, Point>>> pq;
        unordered_map<int, pair<int, Point>> parent; // cost, from
        unordered_map<int, int> dist;
        pq.push({0, monster.pos});
        dist[monster.pos.y * MAP_SIZE + monster.pos.x] = 0;
        parent[monster.pos.y * MAP_SIZE + monster.pos.x] = {0, {-1,-1}};

        const vector<Point> dirs = {{0,-1},{0,1},{-1,0},{1,0},{-1,-1},{-1,1},{1,-1},{1,1}};
        Point finalPos = monster.pos;

        while (!pq.empty()) {
            auto [cost, cur] = pq.top(); pq.pop();
            int key = cur.y * MAP_SIZE + cur.x;
            if (dist.count(key) && dist[key] < cost) continue;
            if (cur == targetTile) {
                finalPos = cur;
                break;
            }
            if (cost >= monster.speed) continue;

            for (const auto& d : dirs) {
                Point next(cur.x + d.x, cur.y + d.y);
                if (!isPassableForMonster(next, occupied)) continue;
                int moveCost = getMoveCost(cur, next);
                if (moveCost == INT_MAX) continue;
                int newCost = cost + moveCost;
                if (newCost > monster.speed) continue;
                int nkey = next.y * MAP_SIZE + next.x;
                if (!dist.count(nkey) || newCost < dist[nkey]) {
                    dist[nkey] = newCost;
                    parent[nkey] = {key, cur};
                    pq.push({newCost, next});
                }
            }
        }

        // 回溯路径,找到最远可达点
        if (finalPos != monster.pos) {
            // 实际上我们只需要最终位置,Dijkstra 已保证可达
            monster.pos = finalPos;
        }
    }

    void initLevelData() {
        auto getMap = [&](int mapId) {
            return DUNGEON_MAPS.at(mapId);
        };

        levelConfigs.push_back({getMap(1), Point(0,4), MonsterType::SPIDER, {{4,2},{3,0}}});
        levelConfigs.push_back({getMap(2), Point(4,4), MonsterType::NORMAL, {{0,1},{2,0}}});
        levelConfigs.push_back({getMap(3), Point(0,4), MonsterType::ELITE, {{4,2}}});
        levelConfigs.push_back({getMap(4), Point(4,4), MonsterType::DRAGON, {{1,0}}});
        levelConfigs.push_back({getMap(1), Point(0,4), MonsterType::SPIDER, {{1,0},{4,1},{4,4}}});
        levelConfigs.push_back({getMap(2), Point(4,4), MonsterType::NORMAL, {{0,4},{1,1},{1,0}}});
        levelConfigs.push_back({getMap(3), Point(0,4), MonsterType::ELITE, {{2,1},{4,3}}});
        levelConfigs.push_back({getMap(4), Point(4,4), MonsterType::DRAGON, {{3,0},{1,4}}});
        levelConfigs.push_back({getMap(1), Point(0,4), MonsterType::SPIDER, {{0,0},{2,2},{4,3},{2,4}}});
        levelConfigs.push_back({getMap(2), Point(4,4), MonsterType::NORMAL, {{4,0},{2,1},{1,2},{0,3}}});
        levelConfigs.push_back({getMap(3), Point(0,4), MonsterType::ELITE, {{1,0},{3,0},{4,2}}});
        levelConfigs.push_back({getMap(4), Point(4,4), MonsterType::DRAGON, {{0,1},{2,1},{0,3}}});
    }

    bool isAllocationValid(int s, int a, int d, vector<int> dice) {
        vector<int> allocated = {s, a, d};
        sort(allocated.begin(), allocated.end());
        sort(dice.begin(), dice.end());
        return allocated == dice;
    }

    char getLowerCharInput() {
        char c = tolower(_getch());
        cout << endl;
        return c;
    }

    MonsterAttr adjustMonsterAttrByDifficulty(MonsterAttr monsterBaseAttr) {
        MonsterAttr adjusted = monsterBaseAttr;
        switch (gameDifficulty) {
            case Difficulty::EASY:
                adjusted.health = max(1, (int)floor(monsterBaseAttr.health * 0.8));
                adjusted.speed = max(1, (int)floor(monsterBaseAttr.speed * 0.8));
                adjusted.attack = max(1, (int)floor(monsterBaseAttr.attack * 0.8));
                adjusted.defense = max(1, (int)floor(monsterBaseAttr.defense * 0.8));
                adjusted.range = max(1, (int)floor(monsterBaseAttr.range * 0.8));
                break;
            case Difficulty::HARD:
                adjusted.health = (int)ceil(monsterBaseAttr.health * 1.2);
                adjusted.speed = (int)ceil(monsterBaseAttr.speed * 1.2);
                adjusted.attack = (int)ceil(monsterBaseAttr.attack * 1.2);
                adjusted.defense = (int)ceil(monsterBaseAttr.defense * 1.2);
                adjusted.range = (int)ceil(monsterBaseAttr.range * 1.2);
                break;
            default:
                break;
        }
        return adjusted;
    }

    void initPlayerAttrByDifficulty() {
        switch (gameDifficulty) {
            case Difficulty::EASY:
                baseSpeed = 2;
                baseAttack = 2;
                baseDefense = 2;
                baseRange = INIT_RANGE;
                break;
            case Difficulty::NORMAL:
            case Difficulty::HARD:
                baseSpeed = 1;
                baseAttack = 1;
                baseDefense = 1;
                baseRange = INIT_RANGE;
                break;
        }
    }

    int calculatePlayerDamage(int totalMonsterAttack, int playerDefense) {
        int damage = 0;
        switch (gameDifficulty) {
            case Difficulty::EASY:
                damage = max(0, (int)floor((double)totalMonsterAttack / max(1, playerDefense)));
                break;
            case Difficulty::NORMAL:
                damage = (playerDefense > 0) ? (totalMonsterAttack / playerDefense) : totalMonsterAttack;
                if (playerDefense > totalMonsterAttack) damage = 0;
                break;
            case Difficulty::HARD:
                if (totalMonsterAttack > 0) {
                    damage = max(1, (int)ceil((double)totalMonsterAttack / max(1, playerDefense)));
                }
                break;
        }
        return damage;
    }

    void selectDifficulty() {
        system("cls");
        cout << "========================================" << endl;
        cout << "           选择游戏难度                 " << endl;
        cout << "========================================" << endl;
        cout << "1. 简单难度" << endl;
        cout << "   玩家初始属性2/2/2/2,怪物属性×0.8,伤害向下取整" << endl;
        cout << "2. 普通难度(默认)" << endl;
        cout << "   玩家初始属性1/1/1/2,无属性修正,标准伤害计算(PDF原版)" << endl;
        cout << "3. 困难难度" << endl;
        cout << "   玩家初始属性1/1/1/2,怪物属性×1.2,伤害向上取整" << endl;
        cout << "========================================" << endl;
        cout << "请选择难度(1/2/3):";
        
        while (true) {
            char c = tolower(_getch());
            if (c == '1' || c == '2' || c == '3') {
                gameDifficulty = (c == '1') ? Difficulty::EASY : (c == '2') ? Difficulty::NORMAL : Difficulty::HARD;
                break;
            } else {
                cout << "\n选择无效!请输入1、2或3:";
            }
        }
        
        string diffStr = (gameDifficulty == Difficulty::EASY) ? "简单" : (gameDifficulty == Difficulty::NORMAL) ? "普通" : "困难";
        cout << "\n已选择【" << diffStr << "】难度,按任意键进入游戏..." << endl;
        _getch();
        initPlayerAttrByDifficulty();
    }

    void showRule() {
        system("cls");
        cout << "========================================" << endl;
        cout << "               游戏规则(PDF原版)       " << endl;
        cout << "========================================" << endl;
        cout << "【核心目标】" << endl;
        cout << "挑战12层地牢,击败每层怪物,最终战胜3只恶龙获得胜利(PDF目标)" << endl;
        cout << "----------------------------------------" << endl;
        cout << "【回合流程】" << endl;
        cout << "1. 能量分配:掷出3颗骰子,全部分配给速度/攻击/防御(射程固定)" << endl;
        cout << "2. 玩家行动:消耗速度移动,消耗攻击攻击怪物,0键结束" << endl;
        cout << "3. 怪物移动:按优先级移动,优先进入射程+视线" << endl;
        cout << "4. 怪物攻击:所有在射程+视线内的怪物发起攻击" << endl;
        cout << "----------------------------------------" << endl;
        cout << "【核心规则】" << endl;
        cout << "1. 移动:正交2点速度/斜向3点速度,不可穿墙/停留怪物格子" << endl;
        cout << "2. 射程:=最小移动点数(正交2/斜向3),攻击需满足:" << endl;
        cout << "   射程≤自身射程 + 视线无阻挡(墙/其他怪物) + 剩余攻击点 ≥ 怪物防御" << endl;
        cout << "3. 攻击:消耗攻击点数=怪物防御值,每次攻击造成1点伤害(PDF规则)" << endl;
        cout << "4. 通关奖励:选择属性+1(速度/攻击/防御/射程)或生命回满(不可兼得)" << endl;
        cout << "----------------------------------------" << endl;
        cout << "【难度差异】" << endl;
        cout << "简单:玩家属性强化,怪物弱化,伤害计算有利" << endl;
        cout << "普通:标准数值,完全遵循PDF原版规则" << endl;
        cout << "困难:怪物强化,伤害计算惩罚,按攻击力优先行动" << endl;
        cout << "========================================" << endl;
        cout << "按任意键返回主菜单..." << endl;
        _getch();
        showMainMenu();
    }

    void showTeam() {
        system("cls");
        cout << "========================================" << endl;
        cout << "             制作团队信息               " << endl;
        cout << "========================================" << endl;
        cout << endl;
        cout << "         制作:CMY与他的好朋友们               " << endl;
        cout << "         规则依据:One Card Dungeon PDF v1.1" << endl;
        cout << endl;
        cout << "========================================" << endl;
        cout << "按任意键返回主菜单..." << endl;
        _getch();
        showMainMenu();
    }

public:
    OneCardDungeon() {
        srand((unsigned int)time(NULL));
        initLevelData();
    }

    void showMainMenu() {
        int select = 1;
        while (true) {
            system("cls");
            cout << "##########################################" << endl;
            cout << "#  ######  #     #  #####  #     #  ###  #" << endl;
            cout << "#  #     # #     # #     # ##   ##  #    #" << endl;
            cout << "#  #     # #     # #     # # # # #  #    #" << endl;
            cout << "#  ######  #     #  #####  #  #  #  ###  #" << endl;
            cout << "#  #   #   #     # #     # #     #     # #" << endl;
            cout << "#  #    #  #     # #     # #     # #   # #" << endl;
            cout << "#  #     #  #####   #####  #     #  ###  #" << endl;
            cout << "##########################################" << endl;
            cout << endl;
            cout << endl;
            cout << "                " << (select == 1 ? "【>】开始游戏" : "    开始游戏") << endl;
            cout << "                " << (select == 2 ? "【>】规则介绍" : "    规则介绍") << endl;
            cout << "                " << (select == 3 ? "【>】制作团队" : "    制作团队") << endl;
            cout << endl;
            cout << endl;
            cout << "                                          操作指引:WS选择 Enter确认" << endl;
            cout << "                                          版本信息:V1.0(PDF原版规则)" << endl;
            cout << "                                          游戏类型:文字地牢冒险" << endl;
            cout << "========================================" << endl;

            char key = _getch();
            if (key == 'w' || key == 'W') {
                select--;
                if (select < 1) select = 3;
            } else if (key == 's' || key == 'S') {
                select++;
                if (select > 3) select = 1;
            } else if (key == 13) {
                if (select == 1) {
                    selectDifficulty();
                    initGame();
                    gameLoop();
                    break;
                } else if (select == 2) {
                    showRule();
                } else if (select == 3) {
                    showTeam();
                }
            }
        }
    }

    void initGame() {
        currentLevel = 1;
        adventurerHealth = BASE_HEALTH;
        tempSpeed = baseSpeed;
        tempAttack = baseAttack;
        tempDefense = baseDefense;
        firstRun = true;
        loadLevel(currentLevel);
    }

    void resetGame() {
        currentLevel = 1;
        adventurerHealth = BASE_HEALTH;
        tempSpeed = baseSpeed;
        tempAttack = baseAttack;
        tempDefense = baseDefense;
        firstRun = true;
        loadLevel(currentLevel);
    }

    void loadLevel(int lv) {
        if (lv < 1 || lv > 12) return;
        nextMonsterId = 1;
        auto& cfg = levelConfigs[lv-1];
        dungeonMap = cfg.map_data;
        adventurerPos = cfg.player_pos;
        monsters.clear();
        
        MonsterAttr originalAttr = MONSTER_ATTRS.at(cfg.monster_type);
        MonsterAttr adjustedAttr = adjustMonsterAttrByDifficulty(originalAttr);
        
        for (auto& p : cfg.monster_pos) {
            monsters.emplace_back(
                nextMonsterId++,
                p, 
                adjustedAttr.health, 
                adjustedAttr.speed, 
                adjustedAttr.attack, 
                adjustedAttr.defense, 
                adjustedAttr.range
            );
        }
        
        tempSpeed = baseSpeed;
        tempAttack = baseAttack;
        tempDefense = baseDefense;
        if (firstRun) showGuide();
    }

    void energyPhase() {
        drawGameMap("能量分配");
        int d1 = rand()%DICE_MAX+1, d2=rand()%DICE_MAX+1, d3=rand()%DICE_MAX+1;
        vector<int> dice = {d1, d2, d3};
        sort(dice.begin(), dice.end());
        cout << "\n掷出能量骰子:" << d1 << "、" << d2 << "、" << d3 << endl;
        cout << "请将骰子全部分配给速度/攻击/防御(空格分隔三个数字):";

        string line;
        vector<int> input;
        while (true) {
            getline(cin, line);
            stringstream ss(line);
            int val;
            input.clear();
            while (ss >> val) {
                input.push_back(val);
            }
            if (input.size() != 3) {
                cout << "请输入三个整数(如 3 5 2):";
                continue;
            }
            vector<int> alloc = input;
            sort(alloc.begin(), alloc.end());
            if (alloc == dice) {
                break;
            } else {
                cout << "分配无效!必须使用全部点数 " << d1 << "/" << d2 << "/" << d3 << ":";
            }
        }

        int s = input[0], a = input[1], d = input[2];
        tempSpeed = baseSpeed + s;
        tempAttack = baseAttack + a;
        tempDefense = baseDefense + d;
        cout << "\n分配完成!本回合临时属性:速度" << tempSpeed << " 攻击" << tempAttack << " 防御" << tempDefense << endl;
        cout << "按任意键进入玩家行动阶段..." << endl;
        _getch();
    }

    void playerPhase() {
        int remSpd = tempSpeed;
        int remAtk = tempAttack;
        while (true) {
            drawGameMap("玩家回合", remSpd, remAtk);
            cout << "\n请进行操作:";
            char k = getLowerCharInput();
            
            if (k == '0') {
                cout << "玩家回合结束,进入怪物行动阶段..." << endl;
                _getch();
                break;
            }

            Point new_pos = adventurerPos;
            int cost = 0;
            bool is_move = false;

            switch(k) {
                case 'w': new_pos.y--; cost=MOVE_COST_ORTHO; is_move=true; break;
                case 's': new_pos.y++; cost=MOVE_COST_ORTHO; is_move=true; break;
                case 'a': new_pos.x--; cost=MOVE_COST_ORTHO; is_move=true; break;
                case 'd': new_pos.x++; cost=MOVE_COST_ORTHO; is_move=true; break;
                case 'q': new_pos.x--; new_pos.y--; cost=MOVE_COST_DIAG; is_move=true; break;
                case 'e': new_pos.x++; new_pos.y--; cost=MOVE_COST_DIAG; is_move=true; break;
                case 'z': new_pos.x--; new_pos.y++; cost=MOVE_COST_DIAG; is_move=true; break;
                case 'c': new_pos.x++; new_pos.y++; cost=MOVE_COST_DIAG; is_move=true; break;
            }

            if (is_move) {
                bool outOfBound = (new_pos.x < 0 || new_pos.x >= MAP_SIZE || new_pos.y < 0 || new_pos.y >= MAP_SIZE);
                bool hitWall = !outOfBound && (dungeonMap[new_pos.y][new_pos.x] == '#');
                bool hitMonster = false;
                if (!outOfBound && !hitWall) {
                    for (auto& m : monsters) {
                        if (m.pos == new_pos) {
                            hitMonster = true;
                            break;
                        }
                    }
                }

                if (outOfBound) {
                    cout << "无法移动:超出地图边界!" << endl;
                } else if (hitWall) {
                    cout << "无法移动:撞到墙壁!" << endl;
                } else if (hitMonster) {
                    cout << "无法移动:前方有怪物!" << endl;
                } else if (remSpd < cost) {
                    cout << "无法移动:速度不足!" << endl;
                } else {
                    adventurerPos = new_pos;
                    remSpd -= cost;
                    cout << "移动成功!剩余速度:" << remSpd << endl;
                }
                _getch();
                continue;
            }

            if (isdigit(k)) {
                int targetId = k - '0';
                if (targetId == 0) continue;

                auto targetIt = find_if(monsters.begin(), monsters.end(), [&](const Monster& m) {
                    return m.fixedId == targetId;
                });

                if (targetIt == monsters.end()) {
                    cout << "攻击失败:无此编号怪物!" << endl;
                    _getch();
                    continue;
                }
                
                Monster& target = *targetIt;
                int targetRange = calculateRange(adventurerPos, target.pos);
                if (targetRange > baseRange) {
                    cout << "攻击失败:距离超出射程(当前射程" << targetRange << ",最大射程" << baseRange << ")!" << endl;
                    _getch();
                    continue;
                }
                
                if (!hasLineOfSight(adventurerPos, target.pos, monsters)) {
                    cout << "攻击失败:视线被阻挡!" << endl;
                    _getch();
                    continue;
                }
                
                if (remAtk < target.defense) {
                    cout << "攻击失败:攻击点不足(需" << target.defense << "点,剩余" << remAtk << "点)!" << endl;
                    _getch();
                    continue;
                }
                
                remAtk -= target.defense;
                target.health--;
                cout << "攻击命中!消耗" << target.defense << "点攻击,怪物" << targetId << "生命-1(剩余" << target.health << ")" << endl;
                cout << "剩余攻击点:" << remAtk << endl;
                
                if (target.health <= 0) {
                    cout << "怪物" << targetId << "已被击败!" << endl;
                    monsters.erase(targetIt);
                    if (monsters.empty()) {
                        _getch();
                        return;
                    }
                }
                
                _getch();
            } else {
                cout << "操作无效!请按提示操作!" << endl;
                _getch();
            }
        }
    }

    // ✅ 修复:怪物行动时互斥
    void monsterPhase() {
        if (monsters.empty()) return;
        drawGameMap("怪物行动");
        cout << "怪物开始行动..." << endl;
        _getch();

        vector<Monster*> sortedMonsters;
        for (auto& m : monsters) {
            sortedMonsters.push_back(&m);
        }
        
        if (gameDifficulty == Difficulty::HARD) {
            sort(sortedMonsters.begin(), sortedMonsters.end(), [&](Monster* a, Monster* b) {
                if (a->attack != b->attack) {
                    return a->attack > b->attack;
                }
                return calculateRange(a->pos, adventurerPos) < calculateRange(b->pos, adventurerPos);
            });
        } else {
            sort(sortedMonsters.begin(), sortedMonsters.end(), [&](Monster* a, Monster* b) {
                return calculateRange(a->pos, adventurerPos) < calculateRange(b->pos, adventurerPos);
            });
        }

        vector<Monster*> attackMonsters;

        // 逐个处理怪物,实时构建障碍集
        for (size_t i = 0; i < sortedMonsters.size(); ++i) {
            Monster* monster = sortedMonsters[i];
            cout << "\n【怪物" << monster->fixedId << "】生命:" << monster->health 
                 << " 速度:" << monster->speed << " 射程:" << monster->range << ":";

            // 构建当前障碍:玩家 + 其他所有怪物(包括已移动的)
            unordered_set<int> occupied;
            occupied.insert(adventurerPos.y * MAP_SIZE + adventurerPos.x);
            for (size_t j = 0; j < monsters.size(); ++j) {
                if (&monsters[j] != monster) {
                    occupied.insert(monsters[j].pos.y * MAP_SIZE + monsters[j].pos.x);
                }
            }

            Point targetTile = findBestTargetTile(*monster, occupied);
            if (targetTile != monster->pos) {
                cout << "移动至(" << targetTile.x << "," << targetTile.y << ") → ";
                moveMonsterToTarget(*monster, targetTile, occupied);
            } else {
                cout << "原地";
            }

            int finalRange = calculateRange(monster->pos, adventurerPos);
            bool finalLoS = hasLineOfSight(monster->pos, adventurerPos, monsters);
            if (finalRange <= monster->range && finalLoS) {
                attackMonsters.push_back(monster);
                cout << " → 发起攻击";
            } else {
                cout << " → 无法攻击(射程/视线不满足)";
            }
            _getch();
        }

        drawGameMap("怪物攻击阶段");
        if (attackMonsters.empty()) {
            cout << "所有怪物均无法攻击,你躲过一劫!" << endl;
            _getch();
            return;
        }

        int totalAtk = 0;
        cout << "共" << attackMonsters.size() << "只怪物发起攻击:" << endl;
        for (auto* m : attackMonsters) {
            cout << "怪物" << m->fixedId << " 攻击值:" << m->attack << endl;
            totalAtk += m->attack;
        }

        int damage = calculatePlayerDamage(totalAtk, tempDefense);
        adventurerHealth = max(0, adventurerHealth - damage);

        cout << "----------------------------------------" << endl;
        cout << "怪物总攻击:" << totalAtk << "  你的防御:" << tempDefense << endl;
        if (damage > 0) {
            cout << "你受到" << damage << "点伤害!剩余生命:" << adventurerHealth << "/" << BASE_HEALTH << endl;
        } else {
            cout << "你的防御抵消了所有伤害!" << endl;
        }
        cout << "按任意键结束怪物回合..." << endl;
        _getch();
    }

    void levelEnd() {
        drawGameMap("关卡通关");
        cout << "\n========================================" << endl;
        cout << "           恭喜通关第" << currentLevel << "层!           " << endl;
        cout << "========================================" << endl;
        cout << "通关奖励(五选一,不可兼得,必须输入1-5):" << endl;
        cout << "1. 速度+1(永久)  2. 攻击+1(永久)" << endl;
        cout << "3. 防御+1(永久)  4. 射程+1(永久,PDF规则)" << endl;
        cout << "5. 生命回满(恢复至" << BASE_HEALTH << "/" << BASE_HEALTH << ")" << endl;
        cout << "========================================" << endl;

        char c = '0';
        while (true) {
            cout << "请选择奖励(1-5):";
            c = getLowerCharInput();
            if (c >= '1' && c <= '5') {
                break;
            } else {
                cout << "选择无效!请输入1、2、3、4、5中的一个有效数字!" << endl;
                cout << "按任意键重新选择..." << endl;
                _getch();
                drawGameMap("关卡通关");
                cout << "\n========================================" << endl;
                cout << "           恭喜通关第" << currentLevel << "层!           " << endl;
                cout << "========================================" << endl;
                cout << "通关奖励(五选一,不可兼得,必须输入1-5):" << endl;
                cout << "1. 速度+1(永久)  2. 攻击+1(永久)" << endl;
                cout << "3. 防御+1(永久)  4. 射程+1(永久,PDF规则)" << endl;
                cout << "5. 生命回满(恢复至" << BASE_HEALTH << "/" << BASE_HEALTH << ")" << endl;
                cout << "========================================" << endl;
            }
        }

        switch(c) {
            case '1': baseSpeed++; cout << "速度永久提升至 " << baseSpeed << endl; break;
            case '2': baseAttack++; cout << "攻击永久提升至 " << baseAttack << endl; break;
            case '3': baseDefense++; cout << "防御永久提升至 " << baseDefense << endl; break;
            case '4': baseRange++; cout << "射程永久提升至 " << baseRange << endl; break;
            case '5': adventurerHealth = BASE_HEALTH; cout << "生命值已回满!" << endl; break;
        }
        cout << "奖励已生效!" << endl;
        cout << "按任意键进入下一层..." << endl;
        _getch();
        
        currentLevel++;
        if (currentLevel > 12) {
            system("cls");
            cout << "========================================" << endl;
            cout << "             恭喜!终极胜利             " << endl;
            cout << "========================================" << endl;
            cout << "你成功击败12层所有怪物,战胜最终3只恶龙!" << endl;
            cout << "获得了M'Guf-yn权杖,成为地牢最强冒险者!(PDF原版胜利)" << endl;
            cout << "========================================" << endl;
            cout << "按任意键退出游戏..." << endl;
            _getch();
            exit(0);
        }
        loadLevel(currentLevel);
        cout << "\n进入第" << currentLevel << "层,新的挑战开始!" << endl;
        _getch();
    }

    void gameLoop() {
        while (adventurerHealth > 0) {
            if (monsters.empty()) {
                levelEnd();
                continue;
            }
            energyPhase();
            playerPhase();
            if (monsters.empty()) continue;
            if (adventurerHealth > 0) monsterPhase();
        }
        
        system("cls");
        cout << "========================================" << endl;
        cout << "               冒险失败                 " << endl;
        cout << "========================================" << endl;
        cout << "你在第" << currentLevel << "层被怪物击败..." << endl;
        cout << "调整策略,重新挑战一定会成功!" << endl;
        cout << "========================================" << endl;
        cout << "是否重新开始?(Y/N):";
        char restart = tolower(_getch());
        if (restart == 'y') {
            selectDifficulty();
            resetGame();
            gameLoop();
        } else {
            cout << "\n感谢游玩,再见!" << endl;
            _getch();
            exit(0);
        }
    }
};

int main() {
    OneCardDungeon game;
    game.showMainMenu();
    return 0;
}

都看完了这么好的游戏了,就点一个赞,评论一下吧QAQ

原文