import java.util.List;
import java.util.ArrayList;
/**
* 会议系统核心架构
* 支持:线上狼人杀、头脑风暴、辩论赛等场景
*/
public class MeetingSystem {
// ================= 核心接口定义 =================
public interface Participant {
String getId();
String getName();
boolean isHost();
}
/**
* 发言动作策略接口
*/
public interface SpeakingStrategy {
void performAction(List<Participant> speakers, StageContext context);
}
/**
* 主持人决策接口
*/
public interface HostDecisionMaker {
boolean shouldHostSpeak(StageContext context);
SpeakingStrategy selectSpeakingStrategy(StageContext context);
List<Participant> selectParticipants(StageContext context);
}
/**
* 环节状态上下文
*/
public static class StageContext {
private final int currentStage;
private final int currentRound;
private final List<Participant> allParticipants;
public StageContext(int stage, int round, List<Participant> participants) {
this.currentStage = stage;
this.currentRound = round;
this.allParticipants = participants;
}
// Getters
public int getCurrentStage() { return currentStage; }
public int getCurrentRound() { return currentRound; }
public List<Participant> getAllParticipants() { return allParticipants; }
}
// ================= 核心实现类 =================
public static class Meeting {
private final List<Stage> stages = new ArrayList<>();
private final HostDecisionMaker hostDecisionMaker;
private final List<Participant> participants;
private final Participant host;
public Meeting(List<Participant> participants, Participant host, HostDecisionMaker decisionMaker) {
this.participants = participants;
this.host = host;
this.hostDecisionMaker = decisionMaker;
}
public void addStage(Stage stage) {
stages.add(stage);
}
public void start() {
for (int stageIndex = 0; stageIndex < stages.size(); stageIndex++) {
Stage stage = stages.get(stageIndex);
stage.execute(new StageContext(stageIndex, 0, participants));
// 主持人总结环节
hostSummary(stageIndex);
}
}
private void hostSummary(int stageIndex) {
System.out.println("【环节" + (stageIndex+1) + "结束】主持人开始总结...");
// 主持人总结逻辑
}
}
public static class Stage {
private final int rounds;
private final HostDecisionMaker decisionMaker;
public Stage(int rounds, HostDecisionMaker decisionMaker) {
this.rounds = rounds;
this.decisionMaker = decisionMaker;
}
public void execute(StageContext context) {
for (int round = 0; round < rounds; round++) {
StageContext roundContext = new StageContext(
context.getCurrentStage(),
round,
context.getAllParticipants()
);
// 主持人决策发言策略
SpeakingStrategy strategy = decisionMaker.selectSpeakingStrategy(roundContext);
List<Participant> speakers = decisionMaker.selectParticipants(roundContext);
// 执行发言策略
strategy.performAction(speakers, roundContext);
// 主持人发言判断
if (decisionMaker.shouldHostSpeak(roundContext)) {
System.out.println("主持人进行补充发言...");
}
}
}
}
// ================= 策略实现 =================
/**
* 文本发言策略
*/
public static class TextSpeakingStrategy implements SpeakingStrategy {
@Override
public void performAction(List<Participant> speakers, StageContext context) {
System.out.printf("【第%d环节-第%d轮】文本发言开始(%d人参与)%n",
context.getCurrentStage()+1, context.getCurrentRound()+1, speakers.size());
for (Participant speaker : speakers) {
System.out.printf("%s 发言中...%n", speaker.getName());
}
}
}
/**
* 投票策略
*/
public static class VoteStrategy implements SpeakingStrategy {
@Override
public void performAction(List<Participant> voters, StageContext context) {
System.out.printf("【第%d环节-第%d轮】投票开始(%d人参与)%n",
context.getCurrentStage()+1, context.getCurrentRound()+1, voters.size());
// 模拟投票逻辑
voters.forEach(voter ->
System.out.println(voter.getName() + " 投出了选票"));
}
}
/**
* 自由辩论策略
*/
public static class DebateStrategy implements SpeakingStrategy {
@Override
public void performAction(List<Participant> debaters, StageContext context) {
System.out.printf("【第%d环节-第%d轮】自由辩论开始(%d人参与)%n",
context.getCurrentStage()+1, context.getCurrentRound()+1, debaters.size());
// 随机发言顺序
Collections.shuffle(debaters);
debaters.forEach(debater ->
System.out.println(debater.getName() + " 发表观点"));
}
}
// ================= 主持人决策实现 =================
/**
* 基础主持人决策器
*/
public static class BasicHostDecisionMaker implements HostDecisionMaker {
@Override
public boolean shouldHostSpeak(StageContext context) {
// 每两轮主持人发言一次
return context.getCurrentRound() % 2 == 0;
}
@Override
public SpeakingStrategy selectSpeakingStrategy(StageContext context) {
// 根据环节选择策略
if (context.getCurrentStage() == 0) {
return new TextSpeakingStrategy();
} else if (context.getCurrentStage() % 2 == 1) {
return new VoteStrategy();
} else {
return new DebateStrategy();
}
}
@Override
public List<Participant> selectParticipants(StageContext context) {
// 默认选择所有非主持人参与者
return context.getAllParticipants().stream()
.filter(p -> !p.isHost())
.collect(Collectors.toList());
}
}
/**
* 狼人杀专用决策器
*/
public static class WerewolfHostDecisionMaker implements HostDecisionMaker {
@Override
public boolean shouldHostSpeak(StageContext context) {
// 每晚结束后主持人必须发言
return context.getCurrentRound() > 0;
}
@Override
public SpeakingStrategy selectSpeakingStrategy(StageContext context) {
// 根据回合数切换策略
return switch (context.getCurrentRound()) {
case 0 -> new TextSpeakingStrategy(); // 首轮发言
case 1 -> new VoteStrategy(); // 投票环节
default -> new DebateStrategy(); // 自由讨论
};
}
@Override
public List<Participant> selectParticipants(StageContext context) {
// 模拟选择存活玩家
return context.getAllParticipants().stream()
.filter(p -> !p.isHost())
.limit(8) // 假设存活8人
.collect(Collectors.toList());
}
}
// ================= 使用示例 =================
public static void main(String[] args) {
// 创建参与者
List<Participant> participants = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
final int id = i;
participants.add(new Participant() {
@Override public String getId() { return "P" + id; }
@Override public String getName() { return "玩家" + id; }
@Override public boolean isHost() { return false; }
});
}
// 创建主持人
Participant host = new Participant() {
@Override public String getId() { return "host-01"; }
@Override public String getName() { return "主持人"; }
@Override public boolean isHost() { return true; }
};
participants.add(host);
// 创建会议(3个环节,每环节2轮)
Meeting meeting = new Meeting(participants, host, new WerewolfHostDecisionMaker());
meeting.addStage(new Stage(2, new WerewolfHostDecisionMaker())); // 狼人杀第一夜
meeting.addStage(new Stage(2, new WerewolfHostDecisionMaker())); // 白天讨论
meeting.addStage(new Stage(3, new WerewolfHostDecisionMaker())); // 投票环节
// 开始会议
meeting.start();
}
}
架构设计说明
核心组件:
Participant:参与者接口,区分主持人和普通参与者
Meeting:会议主控类,管理整个流程
Stage:环节类,包含多轮发言
StageContext:环节上下文,传递当前状态
策略模式:
SpeakingStrategy:发言策略接口
TextSpeakingStrategy:文本发言
VoteStrategy:投票策略
DebateStrategy:自由辩论
可扩展其他策略(如:抢答策略、分组讨论策略等)
主持人决策系统:
HostDecisionMaker:主持人决策接口
BasicHostDecisionMaker:基础决策实现
WerewolfHostDecisionMaker:狼人杀专用决策
三个关键决策点:
shouldHostSpeak():是否发言
selectSpeakingStrategy():选择发言策略
selectParticipants():选择参与者
扩展点设计:
新增发言策略:实现SpeakingStrategy接口
自定义主持人逻辑:实现HostDecisionMaker接口
特殊环节设计:继承Stage类重写执行逻辑
动态参与者选择:通过selectParticipants()实现复杂筛选
场景适配示例
线上狼人杀:
使用WerewolfHostDecisionMaker
添加夜晚行动阶段、投票阶段等特殊环节
实现WerewolfVoteStrategy(带角色能力的投票)
头脑风暴:
java
public class BrainstormingDecisionMaker implements HostDecisionMaker {
@Override
public SpeakingStrategy selectSpeakingStrategy(StageContext context) {
// 随机选择发言方式
return new Random().nextBoolean() ?
new TextSpeakingStrategy() : new FreeDiscussionStrategy();
}
}
辩论赛:
java
public class DebateHostDecisionMaker implements HostDecisionMaker {
@Override
public List<Participant> selectParticipants(StageContext context) {
// 根据当前回合选择正反方辩手
int round = context.getCurrentRound();
return context.getAllParticipants().stream()
.filter(p -> round % 2 == 0 ?
p.getTeam().equals("正方") :
p.getTeam().equals("反方"))
.collect(Collectors.toList());
}
}