狼人杀c++

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <limits>
#include <conio.h>
#include <windows.h>

using namespace std;

// 角色类型
enum class Role {
	WEREWOLF,   // 狼人
	VILLAGER,   // 村民
	SEER,       // 预言家
	WITCH,      // 女巫
	HUNTER,     // 猎人
	GUARDIAN    // 守卫
};

// 玩家类
class Player {
public:
	string name;
	Role role;
	bool isAlive;
	bool isProtected;
	bool isPoisoned;
	
	Player(const string& n) : name(n), role(Role::VILLAGER), isAlive(true), 
	isProtected(false), isPoisoned(false) {}
	
	string getRoleName() const {
		switch (role) {
			case Role::WEREWOLF: return " 狼人";
			case Role::VILLAGER: return " 村民";
			case Role::SEER:    return " 预言家";
			case Role::WITCH:   return " 女巫";
			case Role::HUNTER:  return " 猎人";
			case Role::GUARDIAN:return " 守卫";
			default: return " 未知";
		}
	}
};

// 游戏类
class WerewolfGame {
private:
	vector<Player> players;
	int dayCount;
	bool gameRunning;
	int witchAntidote;
	int witchPoison;
	
	// 清空输入缓冲区
	void clearInputBuffer() {
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
	}
	
	// 安全获取整数输入
	int getSafeIntInput(int minVal, int maxVal) {
		int input;
		while (true) {
			cin >> input;
			if (cin.fail() || input < minVal || input > maxVal) {
				cout << "输入无效,请重新输入 (" << minVal << "-" << maxVal << "): ";
				clearInputBuffer();
			} else {
				clearInputBuffer();
				return input;
			}
		}
	}
	
	// 分配角色
	void assignRoles() {
		int playerCount = players.size();
		vector<Role> roles;
		
		// 确定角色配置
		int werewolfCount = max(1, playerCount / 3);
		for (int i = 0; i < werewolfCount; i++) {
			roles.push_back(Role::WEREWOLF);
		}
		
		// 添加特殊角色(确保至少4人才有特殊角色)
		if (playerCount >= 4) roles.push_back(Role::SEER);
		if (playerCount >= 5) roles.push_back(Role::WITCH);
		if (playerCount >= 6) roles.push_back(Role::HUNTER);
		if (playerCount >= 7) roles.push_back(Role::GUARDIAN);
		
		// 剩余为村民
		while (roles.size() < playerCount) {
			roles.push_back(Role::VILLAGER);
		}
		
		// 随机分配角色
		srand(static_cast<unsigned int>(time(nullptr)));
		for (size_t i = 0; i < roles.size(); i++) {
			int randomIndex = rand() % roles.size();
			swap(roles[i], roles[randomIndex]);
		}
		
		for (size_t i = 0; i < players.size(); i++) {
			players[i].role = roles[i];
		}
		
		witchAntidote = 1;
		witchPoison = 1;
	}
	
	// 显示存活玩家列表
	void displayAlivePlayers(bool showNumbers = true) {
		cout << "\n存活玩家列表:" << endl;
		for (size_t i = 0; i < players.size(); i++) {
			if (players[i].isAlive) {
				if (showNumbers) {
					cout << i + 1 << ". ";
				}
				cout << players[i].name;
				if (players[i].isPoisoned) {
					cout << " [中毒]";
				}
				cout << endl;
			}
		}
	}
	
	// 获取存活玩家数量
	int getAlivePlayerCount() const {
		int count = 0;
		for (const auto& player : players) {
			if (player.isAlive) count++;
		}
		return count;
	}
	
	// 获取存活玩家索引
	vector<int> getAlivePlayerIndices() const {
		vector<int> indices;
		for (size_t i = 0; i < players.size(); i++) {
			if (players[i].isAlive) {
				indices.push_back(static_cast<int>(i));
			}
		}
		return indices;
	}
	
	// 选择玩家
	int selectPlayer(const string& prompt) {
		auto aliveIndices = getAlivePlayerIndices();
		if (aliveIndices.empty()) return -1;
		
		cout << prompt << endl;
		displayAlivePlayers();
		
		cout << "请选择 (1-" << aliveIndices.size() << "): ";
		int choice = getSafeIntInput(1, aliveIndices.size());
		return aliveIndices[choice - 1];
	}
	
	// 守卫行动
	void guardianAction() {
		for (const auto& player : players) {
			if (player.role == Role::GUARDIAN && player.isAlive) {
				cout << "\n=== " << player.name << " (守卫) 行动 ===" << endl;
				int target = selectPlayer("请选择要保护的玩家:");
				if (target != -1) {
					players[target].isProtected = true;
					cout << "你保护了 " << players[target].name << endl;
				}
				break;
			}
		}
	}
	
	// 狼人行动
	void werewolfAction() {
		cout << "\n=== 狼人行动 ===" << endl;
		vector<int> werewolfIndices;
		for (size_t i = 0; i < players.size(); i++) {
			if (players[i].role == Role::WEREWOLF && players[i].isAlive) {
				werewolfIndices.push_back(static_cast<int>(i));
			}
		}
		
		if (werewolfIndices.empty()) return;
		
		// 让第一个狼人代表选择
		cout << players[werewolfIndices[0]].name << " (狼人),请选择要击杀的玩家:" << endl;
		int target = selectPlayer("");
		
		if (target != -1) {
			if (players[target].isProtected) {
				cout << players[target].name << " 被守卫保护,击杀失败!" << endl;
				players[target].isProtected = false;
			} else {
				players[target].isAlive = false;
				cout << "狼人击杀了 " << players[target].name << endl;
			}
		}
	}
	
	// 预言家行动
	void seerAction() {
		for (const auto& player : players) {
			if (player.role == Role::SEER && player.isAlive) {
				cout << "\n=== " << player.name << " (预言家) 行动 ===" << endl;
				int target = selectPlayer("请选择要查验的玩家:");
				if (target != -1) {
					if (players[target].role == Role::WEREWOLF) {
						cout << players[target].name << " 是  狼人!" << endl;
					} else {
						cout << players[target].name << " 是  好人" << endl;
					}
					Sleep(2000);
				}
				break;
			}
		}
	}
	
	// 女巫行动
	void witchAction() {
		for (auto& player : players) {
			if (player.role == Role::WITCH && player.isAlive) {
				cout << "\n=== " << player.name << " (女巫) 行动 ===" << endl;
				cout << "你有: ";
				if (witchAntidote > 0) cout << "解药x" << witchAntidote << " ";
				if (witchPoison > 0) cout << "毒药x" << witchPoison;
				cout << endl;
				
				if (witchAntidote > 0 || witchPoison > 0) {
					cout << "1. 使用解药" << endl;
					cout << "2. 使用毒药" << endl;
					cout << "3. 不使用药物" << endl;
					cout << "选择: ";
					
					int choice = getSafeIntInput(1, 3);
					
					if (choice == 1 && witchAntidote > 0) {
						int target = selectPlayer("请选择要救活的玩家:");
						if (target != -1) {
							witchAntidote--;
							players[target].isAlive = true;
							cout << "使用解药救活了 " << players[target].name << endl;
						}
					}
					else if (choice == 2 && witchPoison > 0) {
						int target = selectPlayer("请选择要毒杀的玩家:");
						if (target != -1) {
							witchPoison--;
							players[target].isPoisoned = true;
							cout << "使用毒药毒杀了 " << players[target].name << endl;
						}
					}
				}
				break;
			}
		}
	}
	
	// 处理夜间死亡
	void processNightDeaths() {
		cout << "\n 夜幕降临..." << endl;
		Sleep(1500);
		
		// 重置保护状态
		for (auto& player : players) {
			player.isProtected = false;
		}
		
		// 特殊角色行动
		guardianAction();
		werewolfAction();
		seerAction();
		witchAction();
		
		// 处理毒药效果
		for (auto& player : players) {
			if (player.isPoisoned) {
				player.isAlive = false;
				player.isPoisoned = false;
			}
		}
		
		Sleep(1500);
	}
	
	// 白天讨论和投票
	void dayPhase() {
		cout << "\n 天亮了!第 " << dayCount << " 天开始" << endl;
		
		// 显示死亡信息
		bool deaths = false;
		cout << "昨夜死亡情况:" << endl;
		for (const auto& player : players) {
			if (!player.isAlive) {
				cout << "$ " << player.name << " 死亡" << endl;
				deaths = true;
			}
		}
		if (!deaths) {
			cout << " 平安夜!无人死亡" << endl;
		}
		
		// 讨论环节
		cout << "\n 开始讨论..." << endl;
		Sleep(2000);
		cout << "讨论结束,开始投票" << endl;
		
		// 投票
		vector<int> votes(players.size(), 0);
		auto aliveIndices = getAlivePlayerIndices();
		
		for (int index : aliveIndices) {
			cout << "\n" << players[index].name << " 请投票:" << endl;
			displayAlivePlayers();
			
			cout << "选择: ";
			int vote = getSafeIntInput(1, aliveIndices.size());
			int targetIndex = aliveIndices[vote - 1];
			votes[targetIndex]++;
			
			cout << players[index].name << " 投票给了 " << players[targetIndex].name << endl;
		}
		
		// 统计投票结果
		int maxVotes = 0;
		vector<int> candidates;
		for (int index : aliveIndices) {
			if (votes[index] > maxVotes) {
				maxVotes = votes[index];
				candidates.clear();
				candidates.push_back(index);
			} else if (votes[index] == maxVotes) {
				candidates.push_back(index);
			}
		}
		
		// 执行投票结果
		if (!candidates.empty() && maxVotes > 0) {
			int executedIndex = candidates[rand() % candidates.size()];
			players[executedIndex].isAlive = false;
			cout << "\n " << players[executedIndex].name << " 被处决!" << endl;
			
			// 猎人技能
			if (players[executedIndex].role == Role::HUNTER) {
				cout << " " << players[executedIndex].name << " 是猎人,可以开枪带走一人!" << endl;
				int target = selectPlayer("请选择要带走的玩家:");
				if (target != -1) {
					players[target].isAlive = false;
					cout << players[target].name << " 被猎人带走!" << endl;
				}
			}
		} else {
			cout << "\n 无人被处决" << endl;
		}
	}
	
	// 检查游戏结束条件
	bool checkGameEnd() {
		int werewolfCount = 0;
		int goodCount = 0;
		
		for (const auto& player : players) {
			if (player.isAlive) {
				if (player.role == Role::WEREWOLF) {
					werewolfCount++;
				} else {
					goodCount++;
				}
			}
		}
		
		if (werewolfCount == 0) {
			cout << "\n 好人阵营胜利!" << endl;
			return true;
		}
		if (werewolfCount >= goodCount) {
			cout << "\n 狼人阵营胜利!" << endl;
			return true;
		}
		
		return false;
	}
	
public:
	WerewolfGame() : dayCount(1), gameRunning(true), witchAntidote(0), witchPoison(0) {}
	
	// 设置游戏
	void setupGame() {
		cout << "===  简易多人狼人杀游戏 ===" << endl;
		cout << "请输入玩家数量 (4-8): ";
		int playerCount = getSafeIntInput(4, 8);
		
		for (int i = 0; i < playerCount; i++) {
			string name;
			cout << "请输入玩家 " << i + 1 << " 的名字: ";
			cin >> name;
			clearInputBuffer();
			players.emplace_back(name);
		}
		
		assignRoles();
		
		// 显示角色分配
		cout << "\n角色分配完成!按任意键查看自己的角色..." << endl;
		_getch();
		
		for (auto& player : players) {
			system("cls");
			cout << player.name << ",你的角色是: " << player.getRoleName() << endl;
			cout << "按任意键继续..." << endl;
			_getch();
		}
	}
	
	// 运行游戏
	void runGame() {
		while (gameRunning && getAlivePlayerCount() > 0) {
			system("cls");
			cout << "=== 第 " << dayCount << " 天 ===" << endl;
			displayAlivePlayers(false);
			
			processNightDeaths();
			
			if (checkGameEnd()) break;
			
			dayPhase();
			
			if (checkGameEnd()) break;
			
			dayCount++;
			cout << "\n按任意键进入下一夜..." << endl;
			_getch();
		}
		
		// 显示游戏结果
		cout << "\n 游戏结束!最终结果:" << endl;
		for (const auto& player : players) {
			cout << player.name << ": " << player.getRoleName() 
			<< " - " << (player.isAlive ? " 存活" : " 死亡") << endl;
		}
	}
};

int main() {
	srand(static_cast<unsigned int>(time(nullptr)));
	
	WerewolfGame game;
	game.setupGame();
	game.runGame();
	
	cout << "\n感谢游玩!按任意键退出..." << endl;
	_getch();
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值