版本比较
本人的第一个c++游戏
正文
增加 951 行删除 0 行
1,004 行未改动全部展开100510061007100810091,004 行未改动全部展开game.showMainMenu();return 0;}```都看完了这么好的游戏了,就点一个赞,评论一下吧QAQ1,004 行未改动全部展开100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601,004 行未改动全部展开game.showMainMenu();return 0;}``````cpp// 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, fromunordered_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