本篇内容介绍了“怎么用C++实现简易的狼人杀游戏”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
以下是一个基本的狼人杀游戏的C++实现,其中包含游戏角色、游戏流程和基本的逻辑。你可以根据需要添加更多的功能和细节。
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <cstdlib> #include <ctime> using namespace std; // 玩家结构体 struct Player { string name; bool is_wolf; bool is_alive; bool is_voted; Player(string n, bool w) { name = n; is_wolf = w; is_alive = true; is_voted = false; } }; // 游戏类 class Game { private: vector<Player> players; int num_wolves; int num_villagers; public: Game(int num_players) { num_wolves = num_players / 3; num_villagers = num_players - num_wolves; // 添加狼人和村民 for (int i = 0; i < num_wolves; i++) { players.push_back(Player("狼人" + to_string(i + 1), true)); } for (int i = 0; i < num_villagers; i++) { players.push_back(Player("村民" + to_string(i + 1), false)); } // 打乱玩家顺序 srand(time(NULL)); random_shuffle(players.begin(), players.end()); } void start_game() { while (num_wolves > 0 && num_wolves < num_villagers) { // 显示存活的玩家 cout << "当前存活玩家:" << endl; for (int i = 0; i < players.size(); i++) { if (players[i].is_alive) { cout << players[i].name << endl; } } // 狼人讨论并投票 cout << "狼人请开始讨论并投票杀人:" << endl; for (int i = 0; i < players.size(); i++) { if (players[i].is_alive && players[i].is_wolf) { int vote = rand() % players.size(); while (players[vote].is_wolf || !players[vote].is_alive) { vote = rand() % players.size(); } players[vote].is_alive = false; cout << "狼人投票杀死了 " << players[vote].name << endl; num_villagers--; } } // 显示存活的玩家 cout << "当前存活玩家:" << endl; for (int i = 0; i < players.size(); i++) { if (players[i].is_alive) { cout << players[i].name << endl; } } // 村民讨论并投票 cout << "村民请开始讨论并投票杀人:" << endl; for (int i = 0; i < players.size(); i++) { if (players[i].is_alive && !players[i].is_wolf) { int vote; do { cout << players[i].name << " 请投票杀死一个玩家:"; cin >> vote; vote--; } while (vote < 0 || vote >= players.size() || !players[vote].is_alive || players[vote].is_voted); players[vote].is_voted = true; cout << players[i].name << " 投票杀死了 " << players[vote].name << endl; if (players[vote].is_wolf) { num_wolves--; } else { num_villagers--; } } } // 处理投票结果 int max_votes = 0; vector<int> vote_counts(players.size(), 0); for (int i = 0; i < players.size(); i++) { if (players[i].is_alive && !players[i].is_wolf) { vote_counts[i]++; } } for (int i = 0; i < players.size(); i++) { if (players[i].is_alive && !players[i].is_wolf) { int vote; do { vote = rand() % players.size(); } while (!players[vote].is_alive || players[vote].is_wolf); vote_counts[vote]++; if (vote_counts[vote] > max_votes) { max_votes = vote_counts[vote]; } } } vector<int> candidates; for (int i = 0; i < players.size(); i++) { if (vote_counts[i] == max_votes) { candidates.push_back(i); } } if (candidates.size() == 1) { cout << players[candidates[0]].name << " 被投票出局。" << endl; players[candidates[0]].is_alive = false; if (players[candidates[0]].is_wolf) { num_wolves--; } else { num_villagers--; } } else { cout << "平票,无人出局。" << endl; } // 重置投票状态 for (int i = 0; i < players.size(); i++) { if (players[i].is_alive) { players[i].is_voted = false; } } } // 游戏结束 if (num_wolves == 0) { cout << "村民获胜!" << endl; } else { cout << "狼人获胜!" << endl; } } }; int main() { int num_players; cout << "请输入玩家人数:"; cin >> num_players; Game game(num_players); game.start_game(); return 0; }
这个示例代码实现了一个基本的狼人杀游戏。它的规则如下:
每局游戏有一定数量的狼人和村民。
游戏开始时,所有玩家随机排列。
在每个白天里,狼人和村民轮流讨论并投票杀人。
在每个夜晚里,狼人讨论并投票杀人。
玩家被杀后出局,游戏直到所有狼人或村民都被淘汰才结束。