版本比较
狼人杀
正文
增加 42 行删除 13 行
1234567891011121314151617181,588 行未改动全部展开1607160816091610159815991611161216131614160216151616161716181619162016211622446 行未改动全部展开2069207020712072205720732074207520762061207720782063206420792080208120822083206620842068206920702071208520862087208820892090209120922093209420952096209720982099210021012075210221032104210521062107625 行未改动全部展开273327342735273627372738273927402741702 行未改动全部展开```cpp/* 局域网联机狼人杀(最小可运行骨架) - 每台电脑运行同一个程序:既能“创建房间(充当该房间主机/服务器)”,也能“加入房间(作为客户端)” - 房间发现:UDP广播(局域网内自动发现房间) - 房间通信:TCP(主机负责转发聊天、维护玩家列表;提供一个 /start 的简单发牌入口)
编译(Windows + MinGW64 / Dev-C++): g++ main.cpp -std=c++17 -O2 -lws2_32 -o lan_werewolf.exe
编译(Linux/macOS): g++ main.cpp -std=c++17 -O2 -pthread -o lan_werewolf*/
#include <algorithm>#include <atomic>#include <chrono>#include <condition_variable>1,588 行未改动全部展开 sendLine(cs, "PRIV|你已选择过身份。"); return; } std::string v = normPayload; if (v != "1" && v != "2") { sendLine(cs, "PRIV|请输入 1 或 2"); if (v != "1" && v != "2" && v != "3") { sendLine(cs, "PRIV|请输入 1、2 或 3"); return; } Role chosen = (v == "1") ? thiefOpt1_ : thiefOpt2_; Role chosen; if (v == "1") chosen = thiefOpt1_; else if (v == "2") chosen = thiefOpt2_; else chosen = thiefOpt3_; me.role = chosen; thiefChosen_ = true; sendLine(cs, "PRIV|你已变身为:" + roleToString(chosen)); sendLine(cs, "ROLE|" + roleToString(chosen));446 行未改动全部展开 } } }
// 盗贼:选牌变身 // 盗贼:选牌变身(标准规则:从底牌池 3 选 1,有狼牌则强制为狼) socket_t thief = findAliveByRoleLocked(Role::Thief); if (thief != kInvalidSocket) { thiefSock_ = thief; // 可选池:好人身份和狼人身份分开,确保至少一张好人、一张狼人 // 底牌池:包含游戏中未分配的所有角色(好人 + 狼人) std::vector<Role> goodPool = {Role::Villager, Role::Seer, Role::Witch, Role::Hunter, Role::Guard, Role::Gravedigger, Role::Elder, Role::Idiot, Role::Knight, Role::Raven, Role::Bear}; std::vector<Role> wolfPool = {Role::Werewolf, Role::HiddenWolf}; Role::Gravedigger, Role::Elder, Role::Idiot, Role::Knight, Role::Raven, Role::Bear, Role::DreamCatcher, Role::Magician}; std::vector<Role> wolfPool = {Role::Werewolf, Role::HiddenWolf, Role::WolfKing, Role::WhiteWolfKing, Role::WolfBeauty}; // 从好人池抽 2 张,从狼池抽 1 张(保证有狼) std::uniform_int_distribution<size_t> gDist(0, goodPool.size() - 1); std::uniform_int_distribution<size_t> wDist(0, wolfPool.size() - 1); thiefOpt1_ = goodPool[gDist(rng)]; thiefOpt2_ = wolfPool[wDist(rng)]; // 随机决定哪个是选项1哪个是选项2 if (gDist(rng) % 2 == 0) std::swap(thiefOpt1_, thiefOpt2_); sendLine(thief, "PRIV|【盗贼】你的技能:开局从两张身份牌中选一张变身。"); do { thiefOpt2_ = goodPool[gDist(rng)]; } while (thiefOpt2_ == thiefOpt1_); std::uniform_int_distribution<size_t> wDist(0, wolfPool.size() - 1); thiefOpt3_ = wolfPool[wDist(rng)]; // 随机打乱 3 张牌的顺序 int order[3] = {0, 1, 2}; std::shuffle(order, order + 3, rng); Role opts[3] = {thiefOpt1_, thiefOpt2_, thiefOpt3_}; thiefOpt1_ = opts[order[0]]; thiefOpt2_ = opts[order[1]]; thiefOpt3_ = opts[order[2]];
bool hasWolf = (isWolfRole(thiefOpt1_) || isWolfRole(thiefOpt2_) || isWolfRole(thiefOpt3_)); std::string wolfHint = hasWolf ? "底牌中有狼人牌,你将变身为狼人阵营!" : "底牌中没有狼人牌。"; sendLine(thief, "PRIV|【盗贼】你的技能:从 3 张底牌中选 1 张变身。" + wolfHint); sendLine(thief, "PRIV|请选择一张身份牌变身:"); sendLine(thief, "PRIV| 1) " + roleToString(thiefOpt1_)); sendLine(thief, "PRIV| 2) " + roleToString(thiefOpt2_)); sendLine(thief, "PRIV|输入 1/2,或 /choose 1/2;/pass 视为默认选1(60秒超时)。"); sendLine(thief, "PRIV| 3) " + roleToString(thiefOpt3_)); sendLine(thief, "PRIV|输入 1/2/3,或 /choose 1/2/3;/pass 视为默认选1(60秒超时)。"); sendLine(thief, "ACTION|THIEF_CHOOSE"); waitFor(lk, [&] { return thiefChosen_ || thiefPass_; }, 60000); if (!thiefChosen_) { Role chosen = thiefOpt1_;625 行未改动全部展开 // 盗贼 socket_t thiefSock_ = kInvalidSocket; Role thiefOpt1_ = Role::Villager; Role thiefOpt2_ = Role::Villager; Role thiefOpt3_ = Role::Villager; // 盗贼 3 选 1 bool thiefChosen_ = false; bool thiefPass_ = false;
// 狼美人702 行未改动全部展开