版本比较
【AI生成】C++游戏——狼人杀
正文
增加 64 行删除 64 行
22 行未改动23242526272728293029303132323334353637383940414041424344454647474849505152525354555618 行未改动757677787979808182838481828384858686878888899091919293949596979899991001011021021031041051051061071081091101101111121131141151161171181181191201211221231241251261271271281291301311321331341351361361371381391407 行未改动1481491501511521521531541551551561571581591601611611621631641651651661671681691701701711721731741751751761771781791791801811821831841841851861871871881891901911921931941941951961971981992002002012022032042052062072082092092102112122132142152162172182182192202212212222232242252252262272282292302312322332332342352362375 行未改动2432442452462472472482492502512522532532542552562572582582592602612622632642642652662672682682692702712722732732742752762772782782792802812822832842842852862872882892902912922932932942952962972982982993003013023033043043053063073073083093103113123123133143153163173183183193203213223233243253263263273283293303313313323333343353363373373383393403403413423433444 行未改动3493503513523533533543553563572 行未改动3603613623633643643653663673683693693703713723735 行未改动3793803813823833833843853863875 行未改动3933943953963973973983994004015 行未改动40740840941041141141241341441541641741841942042142142242342442528 行未改动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), 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() {
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), WerewolfGame(int count) : playerCount(count), witchHasPotion(true), witchHasPoison(true), hunterShot(-1), guardProtect(-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() 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 行未改动