版本比较
【AI生成】C++游戏——狼人杀
正文
增加 64 行删除 64 行
22 行未改动2324252627282930313233343536373839404142434445464748495051525354555618 行未改动757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391407 行未改动1481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362375 行未改动2432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433444 行未改动3493503513523533543553563572 行未改动3603613623633643653663673683693703713723735 行未改动3793803813823833843853863875 行未改动3933943953963973983994004015 行未改动40740840941041141241341441541641741841942042142242342442528 行未改动22 行未改动bool isProtectedFlag;bool isLover;int loverId;bool isExiled;public:Player(int id, Role role) : id(id), role(role), isAlive(true),isProtectedFlag(false), isLover(false),loverId(-1), isExiled(false) {}int getId() const { return id; }Role getRole() const { return role; }bool alive() const { return isAlive; }bool isProtected() const { return isProtectedFlag; }bool getIsLover() const { return isLover; }int getLoverId() const { return loverId; }bool getIsExiled() const { return isExiled; }void kill() {if (!isProtectedFlag) {isAlive = false;}isProtectedFlag = false;}void protect() { isProtectedFlag = true; }void setLover(int lover) { isLover = true; loverId = lover; }void clearProtection() { isProtectedFlag = false; }void exile() { isExiled = true; }string getRoleName() const {switch(role) {case WEREWOLF: return "狼人";case VILLAGER: return "村民";18 行未改动int hunterShot;int guardProtect;bool cupidDone;vector<int> nightDeaths;public:WerewolfGame(int count) :playerCount(count),witchHasPotion(true),witchHasPoison(true),hunterShot(-1),guardProtect(-1),cupidDone(false) {wolfCount = max(2, count / 4);vector<Role> roles;for (int i = 0; i < wolfCount; ++i) roles.push_back(WEREWOLF);roles.push_back(SEER);roles.push_back(WITCH);roles.push_back(HUNTER);roles.push_back(GUARD);roles.push_back(CUPID);roles.push_back(IDIOT);int villagerCount = count - wolfCount - 6;for (int i = 0; i < villagerCount; ++i) roles.push_back(VILLAGER);srand(time(0));random_shuffle(roles.begin(), roles.end());for (int i = 0; i < count; ++i) {players.emplace_back(i + 1, roles[i]);}}void debugPrintRoles() const {cout << "\n===== 角色分配(调试用) =====" << endl;for (const auto& p : players) {cout << "玩家" << p.getId() << ": " << p.getRoleName() << endl;}cout << "===========================\n" << endl;}bool isGameOver() const {int aliveWolves = 0, aliveVillagers = 0;for (const auto& p : players) {if (p.alive() && !p.getIsExiled()) {if (p.getRole() == WEREWOLF) aliveWolves++;else aliveVillagers++;}}if (aliveWolves == 0) {cout << "\n村民阵营胜利!" << endl;return true;}if (aliveVillagers <= aliveWolves) {cout << "\n狼人阵营胜利!" << endl;return true;}set<int> lovers;for (const auto& p : players) {if (p.getIsLover() && p.alive()) {lovers.insert(p.getId());7 行未改动cout << "\n情侣胜利!" << endl;return true;}}return false;}void nightPhase() {cout << "\n===== 夜晚阶段 =====" << endl;cout << "所有玩家闭眼..." << endl;system("pause");system("cls");nightDeaths.clear();hunterShot = -1;guardProtect = -1;if (!cupidDone && hasAliveRole(CUPID)) {cupidAction();cupidDone = true;}if (hasAliveRole(GUARD)) guardAction();if (hasAliveRole(WEREWOLF)) wolfAction();if (hasAliveRole(SEER)) seerAction();if (hasAliveRole(WITCH)) witchAction();processNightDeaths();processLoverDeath();}void dayPhase() {cout << "\n===== 白天阶段 =====" << endl;announceNightDeaths();if (isGameOver()) return;int vote = getValidPlayerInput("请所有玩家讨论后投票(输入玩家ID):");executePlayer(vote);if (hunterShot != -1) {cout << "猎人发动技能,开枪射杀玩家" << hunterShot << endl;executePlayer(hunterShot);processLoverDeath();}}void start() {cout << "欢迎来到狼人杀游戏!" << endl;cout << "玩家数量:" << playerCount << endl;cout << "狼人数量:" << wolfCount << endl;cout << "特殊角色:预言家、女巫、猎人、守卫、丘比特、白痴" << endl;for (const auto& p : players) {system("cls");cout << "\n玩家" << p.getId() << "请查看自己的身份(其他人请回避)..." << endl;system("pause");cout << "\n玩家" << p.getId() << ",你的身份是:" << p.getRoleName() << endl;system("pause");system("cls");}int day = 1;while (!isGameOver()) {cout << "\n========= 第" << day << "天 =========" << endl;nightPhase();if (isGameOver()) break;dayPhase();day++;}cout << "\n===== 游戏结束,所有身份 =====" << endl;for (const auto& p : players) {cout << "玩家" << p.getId() << ": " << p.getRoleName()<< (p.alive() ? (p.getIsExiled() ? "(被放逐)" : "(存活)") : "(已死亡)") << endl;}}private:bool hasAliveRole(Role role) const {for (const auto& p : players) {if (p.alive() && p.getRole() == role) return true;}return false;}int getValidPlayerInput(const string& prompt) {int input;while (true) {cout << prompt;5 行未改动}cout << "输入无效,请重新输入!" << endl;}}void cupidAction() {system("cls");cout << "===== 丘比特行动 =====" << endl;cout << "请丘比特睁眼(其他玩家请保持闭眼)" << endl;system("pause");int p1, p2;while (true) {cout << "请选择两名玩家成为情侣(输入两个玩家ID,用空格分隔):";cin >> p1 >> p2;bool valid1 = false, valid2 = false;for (const auto& p : players) {if (p.getId() == p1 && p.alive()) valid1 = true;if (p.getId() == p2 && p.alive()) valid2 = true;}if (valid1 && valid2 && p1 != p2) break;cout << "输入无效,请重新选择!" << endl;}for (auto& p : players) {if (p.getId() == p1) p.setLover(p2);if (p.getId() == p2) p.setLover(p1);}cout << "情侣已确定,丘比特请闭眼。" << endl;system("pause");system("cls");}void guardAction() {system("cls");cout << "===== 守卫行动 =====" << endl;cout << "请守卫睁眼(其他玩家请保持闭眼)" << endl;system("pause");int target = getValidPlayerInput("请选择要保护的玩家(输入玩家ID):");guardProtect = target;for (auto& p : players) {if (p.getId() == target) {p.protect();break;}}cout << "守卫请闭眼。" << endl;system("pause");system("cls");}void wolfAction() {system("cls");cout << "===== 狼人行动 =====" << endl;cout << "请所有狼人睁眼并讨论(其他玩家请保持闭眼)" << endl;system("pause");int target = getValidPlayerInput("请选择要袭击的玩家(输入玩家ID):");nightDeaths.push_back(target);cout << "狼人请闭眼。" << endl;system("pause");system("cls");}void seerAction() {system("cls");cout << "===== 预言家行动 =====" << endl;cout << "请预言家睁眼(其他玩家请保持闭眼)" << endl;system("pause");int target = getValidPlayerInput("请选择要查验的玩家(输入玩家ID):");for (const auto& p : players) {if (p.getId() == target) {cout << "该玩家是" << (p.getRole() == WEREWOLF ? "狼人" : "好人") << endl;break;}}cout << "预言家请闭眼。" << endl;system("pause");system("cls");}void witchAction() {system("cls");cout << "===== 女巫行动 =====" << endl;cout << "请女巫睁眼(其他玩家请保持闭眼)" << endl;system("pause");cout << "女巫当前状态:" << (witchHasPotion ? "有解药 " : "") << (witchHasPoison ? "有毒药" : "") << endl;bool hasAttack = !nightDeaths.empty();if (hasAttack && witchHasPotion) {cout << "今晚有玩家被袭击,是否使用解药?(1-使用,0-不使用):";int usePotion;cin >> usePotion;4 行未改动}} else if (hasAttack) {cout << "今晚有玩家被袭击,但你已没有解药。" << endl;}if (witchHasPoison) {cout << "是否使用毒药?(1-使用,0-不使用):";int usePoison;cin >> usePoison;2 行未改动nightDeaths.push_back(target);witchHasPoison = false;}}cout << "女巫请闭眼。" << endl;system("pause");system("cls");}void processNightDeaths() {for (int target : nightDeaths) {for (auto& p : players) {if (p.getId() == target && p.alive()) {5 行未改动}}}}void processLoverDeath() {vector<int> loversToKill;for (const auto& p : players) {if (!p.alive() && p.getIsLover()) {5 行未改动}}}}for (int id : loversToKill) {for (auto& p : players) {if (p.getId() == id) {p.kill();5 行未改动}}}}void announceNightDeaths() {if (nightDeaths.empty()) {cout << "昨晚是平安夜,没有人死亡。" << endl;return;}cout << "昨晚死亡的玩家是:";for (int id : nightDeaths) cout << "玩家" << id << " ";cout << endl;}void executePlayer(int id) {for (auto& p : players) {if (p.getId() == id && p.alive()) {if (p.getRole() == IDIOT) {28 行未改动22 行未改动2324252627282930313233343536373839404142434445464748495051525354555618 行未改动757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391407 行未改动1481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362375 行未改动2432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433444 行未改动3493503513523533543553563572 行未改动3603613623633643653663673683693703713723735 行未改动3793803813823833843853863875 行未改动3933943953963973983994004015 行未改动40740840941041141241341441541641741841942042142242342442528 行未改动22 行未改动bool isProtectedFlag;bool isLover;int loverId;bool isExiled;public:Player(int id, Role role) : id(id), role(role), isAlive(true),isProtectedFlag(false), isLover(false),loverId(-1), isExiled(false) {}int getId() const { return id; }Role getRole() const { return role; }bool alive() const { return isAlive; }bool isProtected() const { return isProtectedFlag; }bool getIsLover() const { return isLover; }int getLoverId() const { return loverId; }bool getIsExiled() const { return isExiled; }void kill() {if (!isProtectedFlag) {isAlive = false;}isProtectedFlag = false;}void protect() { isProtectedFlag = true; }void setLover(int lover) { isLover = true; loverId = lover; }void clearProtection() { isProtectedFlag = false; }void exile() { isExiled = true; }string getRoleName() const {switch(role) {case WEREWOLF: return "狼人";case VILLAGER: return "村民";18 行未改动int hunterShot;int guardProtect;bool cupidDone;vector<int> nightDeaths;public:WerewolfGame(int count) :playerCount(count),witchHasPotion(true),witchHasPoison(true),hunterShot(-1),guardProtect(-1),cupidDone(false) {wolfCount = max(2, count / 4);vector<Role> roles;for (int i = 0; i < wolfCount; ++i) roles.push_back(WEREWOLF);roles.push_back(SEER);roles.push_back(WITCH);roles.push_back(HUNTER);roles.push_back(GUARD);roles.push_back(CUPID);roles.push_back(IDIOT);int villagerCount = count - wolfCount - 6;for (int i = 0; i < villagerCount; ++i) roles.push_back(VILLAGER);srand(time(0));random_shuffle(roles.begin(), roles.end());for (int i = 0; i < count; ++i) {players.emplace_back(i + 1, roles[i]);}}void debugPrintRoles() const {cout << "\n===== 角色分配(调试用) =====" << endl;for (const auto& p : players) {cout << "玩家" << p.getId() << ": " << p.getRoleName() << endl;}cout << "===========================\n" << endl;}bool isGameOver() const {int aliveWolves = 0, aliveVillagers = 0;for (const auto& p : players) {if (p.alive() && !p.getIsExiled()) {if (p.getRole() == WEREWOLF) aliveWolves++;else aliveVillagers++;}}if (aliveWolves == 0) {cout << "\n村民阵营胜利!" << endl;return true;}if (aliveVillagers <= aliveWolves) {cout << "\n狼人阵营胜利!" << endl;return true;}set<int> lovers;for (const auto& p : players) {if (p.getIsLover() && p.alive()) {lovers.insert(p.getId());7 行未改动cout << "\n情侣胜利!" << endl;return true;}}return false;}void nightPhase() {cout << "\n===== 夜晚阶段 =====" << endl;cout << "所有玩家闭眼..." << endl;system("pause");system("cls");nightDeaths.clear();hunterShot = -1;guardProtect = -1;if (!cupidDone && hasAliveRole(CUPID)) {cupidAction();cupidDone = true;}if (hasAliveRole(GUARD)) guardAction();if (hasAliveRole(WEREWOLF)) wolfAction();if (hasAliveRole(SEER)) seerAction();if (hasAliveRole(WITCH)) witchAction();processNightDeaths();processLoverDeath();}void dayPhase() {cout << "\n===== 白天阶段 =====" << endl;announceNightDeaths();if (isGameOver()) return;int vote = getValidPlayerInput("请所有玩家讨论后投票(输入玩家ID):");executePlayer(vote);if (hunterShot != -1) {cout << "猎人发动技能,开枪射杀玩家" << hunterShot << endl;executePlayer(hunterShot);processLoverDeath();}}void start() {cout << "欢迎来到狼人杀游戏!" << endl;cout << "玩家数量:" << playerCount << endl;cout << "狼人数量:" << wolfCount << endl;cout << "特殊角色:预言家、女巫、猎人、守卫、丘比特、白痴" << endl;for (const auto& p : players) {system("cls");cout << "\n玩家" << p.getId() << "请查看自己的身份(其他人请回避)..." << endl;system("pause");cout << "\n玩家" << p.getId() << ",你的身份是:" << p.getRoleName() << endl;system("pause");system("cls");}int day = 1;while (!isGameOver()) {cout << "\n========= 第" << day << "天 =========" << endl;nightPhase();if (isGameOver()) break;dayPhase();day++;}cout << "\n===== 游戏结束,所有身份 =====" << endl;for (const auto& p : players) {cout << "玩家" << p.getId() << ": " << p.getRoleName()<< (p.alive() ? (p.getIsExiled() ? "(被放逐)" : "(存活)") : "(已死亡)") << endl;}}private:bool hasAliveRole(Role role) const {for (const auto& p : players) {if (p.alive() && p.getRole() == role) return true;}return false;}int getValidPlayerInput(const string& prompt) {int input;while (true) {cout << prompt;5 行未改动}cout << "输入无效,请重新输入!" << endl;}}void cupidAction() {system("cls");cout << "===== 丘比特行动 =====" << endl;cout << "请丘比特睁眼(其他玩家请保持闭眼)" << endl;system("pause");int p1, p2;while (true) {cout << "请选择两名玩家成为情侣(输入两个玩家ID,用空格分隔):";cin >> p1 >> p2;bool valid1 = false, valid2 = false;for (const auto& p : players) {if (p.getId() == p1 && p.alive()) valid1 = true;if (p.getId() == p2 && p.alive()) valid2 = true;}if (valid1 && valid2 && p1 != p2) break;cout << "输入无效,请重新选择!" << endl;}for (auto& p : players) {if (p.getId() == p1) p.setLover(p2);if (p.getId() == p2) p.setLover(p1);}cout << "情侣已确定,丘比特请闭眼。" << endl;system("pause");system("cls");}void guardAction() {system("cls");cout << "===== 守卫行动 =====" << endl;cout << "请守卫睁眼(其他玩家请保持闭眼)" << endl;system("pause");int target = getValidPlayerInput("请选择要保护的玩家(输入玩家ID):");guardProtect = target;for (auto& p : players) {if (p.getId() == target) {p.protect();break;}}cout << "守卫请闭眼。" << endl;system("pause");system("cls");}void wolfAction() {system("cls");cout << "===== 狼人行动 =====" << endl;cout << "请所有狼人睁眼并讨论(其他玩家请保持闭眼)" << endl;system("pause");int target = getValidPlayerInput("请选择要袭击的玩家(输入玩家ID):");nightDeaths.push_back(target);cout << "狼人请闭眼。" << endl;system("pause");system("cls");}void seerAction() {system("cls");cout << "===== 预言家行动 =====" << endl;cout << "请预言家睁眼(其他玩家请保持闭眼)" << endl;system("pause");int target = getValidPlayerInput("请选择要查验的玩家(输入玩家ID):");for (const auto& p : players) {if (p.getId() == target) {cout << "该玩家是" << (p.getRole() == WEREWOLF ? "狼人" : "好人") << endl;break;}}cout << "预言家请闭眼。" << endl;system("pause");system("cls");}void witchAction() {system("cls");cout << "===== 女巫行动 =====" << endl;cout << "请女巫睁眼(其他玩家请保持闭眼)" << endl;system("pause");cout << "女巫当前状态:" << (witchHasPotion ? "有解药 " : "") << (witchHasPoison ? "有毒药" : "") << endl;bool hasAttack = !nightDeaths.empty();if (hasAttack && witchHasPotion) {cout << "今晚有玩家被袭击,是否使用解药?(1-使用,0-不使用):";int usePotion;cin >> usePotion;4 行未改动}} else if (hasAttack) {cout << "今晚有玩家被袭击,但你已没有解药。" << endl;}if (witchHasPoison) {cout << "是否使用毒药?(1-使用,0-不使用):";int usePoison;cin >> usePoison;2 行未改动nightDeaths.push_back(target);witchHasPoison = false;}}cout << "女巫请闭眼。" << endl;system("pause");system("cls");}void processNightDeaths() {for (int target : nightDeaths) {for (auto& p : players) {if (p.getId() == target && p.alive()) {5 行未改动}}}}void processLoverDeath() {vector<int> loversToKill;for (const auto& p : players) {if (!p.alive() && p.getIsLover()) {5 行未改动}}}}for (int id : loversToKill) {for (auto& p : players) {if (p.getId() == id) {p.kill();5 行未改动}}}}void announceNightDeaths() {if (nightDeaths.empty()) {cout << "昨晚是平安夜,没有人死亡。" << endl;return;}cout << "昨晚死亡的玩家是:";for (int id : nightDeaths) cout << "玩家" << id << " ";cout << endl;}void executePlayer(int id) {for (auto& p : players) {if (p.getId() == id && p.alive()) {if (p.getRole() == IDIOT) {28 行未改动