import java.util.ArrayList;
import java.util.List;
import java.util.Random;// 游戏角色接口
interface Role {
void act(List<Role> players);
}// 狼人角色
class Werewolf implements Role {
@Override
public void act(List<Role> players) {
System.out.println("Werewolf is choosing a player to kill...");
Random random = new Random();
int targetIndex = random.nextInt(players.size());
Role target = players.get(targetIndex);
players.remove(target);
System.out.println("Werewolf killed " + target.getClass().getSimpleName());
}
}// 村民角色
class Villager implements Role {
@Override
public void act(List<Role> players) {
System.out.println("Villager is discussing with other players...");
}
}// 白狼王角色
class WhiteWerewolf implements Role {
@Override
public void act(List<Role> players) {
System.out.println("White Werewolf is choosing a player to kill...");
Random random = new Random();
int targetIndex = random.nextInt(players.size());
Role target = players.get(targetIndex);
players.remove(target);
System.out.println("White Werewolf killed " + target.getClass().getSimpleName());
}
}// 预言家角色
class Prophet implements Role {
@Override
public void act(List<Role> players) {
System.out.println("Prophet is checking a player...");
Random random = new Random();
int targetIndex = random.nextInt(players.size());
Role target = players.get(targetIndex);
System.out.println("Prophet checked " + target.getClass().getSimpleName());
}
}// 女巫角色
class Witch implements Role {
@Override
public void act(List<Role> players) {
System.out.println("Witch is using potion...");
}
}// 猎人角色
class Hunter implements Role {
@Override
public void act(List<Role> players) {
System.out.println("Hunter is shooting...");
Random random = new Random();
int targetIndex = random.nextInt(players.size());
Role target = players.get(targetIndex);
players.remove(target);
System.out.println("Hunter shot " + target.getClass().getSimpleName());
}
}// 白痴角色
class Fool implements Role {
@Override
public void act(List<Role> players) {
System.out.println("Fool is acting foolishly...");
}
}// 守卫角色
class Guard implements Role {
@Override
public void act(List<Role> players) {
System.out.println("Guard is protecting a player...");
}
}// 裁判角色
class Referee implements Role {
@Override
public void act(List<Role> players) {
System.out.println("Referee is moderating the game...");
}
}public class WerewolfGame {
public static void main(String[] args) {
List<Role> players = new ArrayList<>();
players.add(new Werewolf());
players.add(new Werewolf());
players.add(new Villager());
players.add(new WhiteWerewolf());
players.add(new Prophet());
players.add(new Witch());
players.add(new Hunter());
players.add(new Fool());
players.add(new Guard());
players.add(new Referee());while (true) {
for (Role player : players) {
player.act(players);
if (players.stream().filter(p -> p instanceof Werewolf).count() >= players.stream().filter(p -> p instanceof Villager).count()) {
System.out.println("Werewolves win!");
return;
} else if (players.stream().filter(p -> p instanceof Werewolf).count() == 0) {
System.out.println("Villagers win!");
return;
}
}// 投票阶段
System.out.println("Voting phase...");
Role votedOut = players.get(new Random().nextInt(players.size()));
players.remove(votedOut);
System.out.println(votedOut.getClass().getSimpleName() + " was voted out.");if (players.stream().filter(p -> p instanceof Werewolf).count() >= players.stream().filter(p -> p instanceof Villager).count()) {
System.out.println("Werewolves win!");
return;
} else if (players.stream().filter(p -> p instanceof Werewolf).count() == 0) {
System.out.println("Villagers win!");
return;
}
}
}
}