狼人杀代码java
时间: 2023-07-03 08:23:22 浏览: 263
狼人杀是一款比较复杂的游戏,需要实现很多不同的角色和规则。以下是一个简单的狼人杀游戏的 Java 代码示例,仅供参考。
```
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;
public class Werewolf {
// 玩家类
static class Player {
String name;
String role;
boolean alive;
boolean voted; // 是否投票
boolean poisoned; // 是否被毒死
boolean guarded; // 是否被守卫
Player guardedBy; // 被守卫的人
Player poisonedBy; // 毒死的人
public Player(String name) {
this.name = name;
this.alive = true;
this.voted = false;
this.poisoned = false;
this.guarded = false;
this.poisonedBy = null;
this.guardedBy = null;
}
public void setRole(String role) {
this.role = role;
}
}
static ArrayList<Player> players = new ArrayList<>();
static ArrayList<String> roles = new ArrayList<>();
static int werewolfCount = 0;
static int villagerCount = 0;
static int prophetCount = 0;
static int witchCount = 0;
static int hunterCount = 0;
static int guardCount = 0;
static Random random = new Random();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入玩家人数:");
int playerCount = scanner.nextInt();
initRoles(playerCount);
initPlayers(playerCount);
while (werewolfCount > 0 && villagerCount > 0) {
night();
vote();
}
if (werewolfCount == 0) {
System.out.println("村民胜利!");
} else {
System.out.println("狼人胜利!");
}
}
// 初始化角色列表
public static void initRoles(int playerCount) {
int werewolfMaxCount = playerCount / 3;
roles.add("狼人");
werewolfCount++;
if (playerCount >= 4) {
roles.add("预言家");
prophetCount++;
}
if (playerCount >= 6) {
roles.add("女巫");
witchCount++;
}
if (playerCount >= 8) {
roles.add("猎人");
hunterCount++;
}
if (playerCount >= 10) {
roles.add("守卫");
guardCount++;
}
for (int i = 0; i < playerCount - werewolfMaxCount - prophetCount - witchCount - hunterCount - guardCount; i++) {
roles.add("村民");
villagerCount++;
}
Collections.shuffle(roles);
}
// 初始化玩家列表
public static void initPlayers(int playerCount) {
Scanner scanner = new Scanner(System.in);
for (int i = 1; i <= playerCount; i++) {
System.out.println("请输入玩家" + i + "的名字:");
String name = scanner.next();
Player player = new Player(name);
players.add(player);
String role = roles.get(i - 1);
player.setRole(role);
switch (role) {
case "狼人":
break;
case "预言家":
System.out.println(name + "是预言家");
break;
case "女巫":
System.out.println(name + "是女巫");
break;
case "猎人":
System.out.println(name + "是猎人");
break;
case "守卫":
System.out.println(name + "是守卫");
break;
case "村民":
break;
}
}
}
// 夜晚
public static void night() {
System.out.println("天黑请闭眼");
// 狼人杀人
for (Player player : players) {
if (player.role.equals("狼人") && player.alive) {
Player target = chooseTarget();
if (target != null) {
System.out.println(player.name + "选择了杀死" + target.name);
target.alive = false;
werewolfCount--;
}
}
}
// 预言家验人
for (Player player : players) {
if (player.role.equals("预言家") && player.alive) {
Player target = chooseTarget();
if (target != null) {
System.out.println(player.name + "验了" + target.name + ",发现他是" + target.role);
}
}
}
// 女巫使用药
for (Player player : players) {
if (player.role.equals("女巫") && player.alive) {
if (player.poisoned) {
System.out.println(player.name + "昨晚被毒死了");
player.alive = false;
werewolfCount--;
player.poisonedBy = null;
} else {
System.out.println(player.name + "请选择是否使用解药(1:使用,0:不使用):");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
if (choice == 1) {
System.out.println(player.name + "请选择要救的人:");
Player target = chooseTarget();
if (target != null) {
System.out.println(player.name + "救了" + target.name);
target.poisoned = false;
target.poisonedBy = null;
}
}
}
}
}
// 守卫守人
for (Player player : players) {
if (player.role.equals("守卫") && player.alive) {
System.out.println(player.name + "请选择要守护的人:");
Player target = chooseTarget();
if (target != null) {
System.out.println(player.name + "守护了" + target.name);
player.guarded = true;
player.guardedBy = target;
}
}
}
System.out.println("天亮了");
}
// 投票
public static void vote() {
System.out.println("请进行投票");
for (Player player : players) {
if (player.alive) {
player.voted = false;
}
}
while (true) {
Player target = chooseTarget();
if (target != null && target.alive) {
target.voted = true;
break;
}
}
for (Player player : players) {
if (player.alive && !player.voted) {
Player target = chooseTarget();
if (target != null && target.alive) {
target.voted = true;
}
}
}
ArrayList<Player> votedPlayers = new ArrayList<>();
for (Player player : players) {
if (player.voted && player.alive) {
votedPlayers.add(player);
}
}
if (votedPlayers.size() == 0) {
System.out.println("本轮无人出局");
} else if (votedPlayers.size() == 1) {
Player target = votedPlayers.get(0);
target.alive = false;
System.out.println(target.name + "被投票出局了");
if (target.role.equals("狼人")) {
werewolfCount--;
} else {
villagerCount--;
}
} else {
System.out.println("本轮有平票,重新投票");
vote();
}
}
// 随机选择一个玩家作为目标
public static Player chooseTarget() {
ArrayList<Player> availablePlayers = new ArrayList<>();
for (Player player : players) {
if (player.alive && !player.guarded) {
availablePlayers.add(player);
}
}
if (availablePlayers.size() == 0) {
return null;
}
int index = random.nextInt(availablePlayers.size());
return availablePlayers.get(index);
}
}
```
阅读全文