1.0版本
C++#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <set>
using namespace std;
enum Role {
WEREWOLF, VILLAGER, SEER, WITCH, HUNTER, GUARD, CUPID, IDIOT
};
class Player {
private:
int id;
Role role;
bool isAlive;
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 "村民";
case SEER: return "预言家";
case WITCH: return "女巫";
case HUNTER: return "猎人";
case GUARD: return "守卫";
case CUPID: return "丘比特";
case IDIOT: return "白痴";
default: return "未知";
}
}
};
class WerewolfGame {
private:
vector<Player> players;
int playerCount;
int wolfCount;
bool witchHasPotion;
bool witchHasPoison;
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());
lovers.insert(p.getLoverId());
}
}
if (lovers.size() == 2) {
int aliveCount = 0;
for (const auto& p : players) if (p.alive()) aliveCount++;
if (aliveCount == 2) {
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;
cin >> input;
for (const auto& p : players) {
if (p.getId() == input && p.alive() && !p.getIsExiled()) {
return input;
}
}
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;
if (usePotion == 1) {
nightDeaths.erase(nightDeaths.begin());
witchHasPotion = false;
cout << "已使用解药。" << endl;
}
} else if (hasAttack) {
cout << "今晚有玩家被袭击,但你已没有解药。" << endl;
}
if (witchHasPoison) {
cout << "是否使用毒药?(1-使用,0-不使用):";
int usePoison;
cin >> usePoison;
if (usePoison == 1) {
int target = getValidPlayerInput("请选择要毒杀的玩家(输入玩家ID):");
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()) {
p.kill();
if (p.getRole() == HUNTER) {
hunterShot = target;
}
break;
}
}
}
}
void processLoverDeath() {
vector<int> loversToKill;
for (const auto& p : players) {
if (!p.alive() && p.getIsLover()) {
int loverId = p.getLoverId();
for (const auto& lover : players) {
if (lover.getId() == loverId && lover.alive()) {
loversToKill.push_back(loverId);
break;
}
}
}
}
for (int id : loversToKill) {
for (auto& p : players) {
if (p.getId() == id) {
p.kill();
cout << "玩家" << id << "因情侣死亡而殉情!" << endl;
if (p.getRole() == HUNTER) {
hunterShot = id;
}
break;
}
}
}
}
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) {
p.exile();
cout << "玩家" << id << "是白痴,被放逐但不会死亡!" << endl;
return;
}
p.kill();
cout << "玩家" << id << "被处决了!" << endl;
if (p.getRole() == HUNTER) {
hunterShot = getValidPlayerInput("猎人发动技能,请选择要射杀的玩家(输入玩家ID):");
}
break;
}
}
}
};
int main() {
int playerCount;
cout << "请输入玩家数量(8-12人):";
cin >> playerCount;
if (playerCount < 8 || playerCount > 12) {
cout << "玩家数量必须在8-12人之间!" << endl;
return 1;
}
WerewolfGame game(playerCount);
game.start();
return 0;
}