狼人杀(Java版相对完善)

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;
            }
        }
    }
}

 

  • 26
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值