Files
joke-app/packages/games/src/bots/index.ts
EJClaw 92e8b2100f 마피아 컴퓨터 플레이어: 각자 타이머로 말하고 투표하는 라이브 봇, 똑똑한 편/허술한 편, 반응 속도 차이
- BotDriver live 모드: 컴퓨터마다 자기 타이머로 화면·채팅을 다시 보고 행동 하나
- mafia/bot.ts: 근거 추리(경찰 결과·경찰 주장·투표 기록) vs 느낌·분위기, 존댓말 vs 반말
- 채팅: 인사, 아침 소식, 지목·동의·반박, 이름 불리면 대답, 반론, 마피아 상의
- 연습 방 6명(PRACTICE_PLAYERS), BOT_GAMES에 mafia
- 테스트: 옵션 조합별 전체 판, 똑똑한 편 승률, 자기 팀 지목 금지, 반응 속도, 대답 E2E

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 02:54:14 +09:00

102 lines
3.9 KiB
TypeScript

/**
* Computer players (docs/09-game-engine.md §9). `decideBotAction` is the only place that touches the game state:
* it hands the strategy the bot's own view and its legal actions — nothing else — so a bot can't peek at hidden
* information (other hands, deck order).
*/
import type { AnyGameDefinition, PlayerId } from '@bg/engine';
import type { BotStrategy, LiveBotStrategy } from './types';
import { pick, playable, randomPolicy } from './policy';
import { omokBot } from '../omok/bot';
import { othelloBot } from '../othello/bot';
import { chessBot } from '../chess/bot';
import { janggiBot } from '../janggi/bot';
import { badukBot } from '../baduk/bot';
import { seotdaBot } from '../seotda/bot';
import { pokerBot } from '../poker/bot';
import { yachtBot } from '../yacht/bot';
import { yutBot } from '../yut/bot';
import { oneCardBot } from '../one-card/bot';
import { rummikubBot } from '../rummikub/bot';
import { gostopBot } from '../gostop/bot';
import { bangBot } from '../bang/bot';
import { davinciBot } from '../davinci-code/bot';
import { mahjongBot } from '../mahjong/bot';
import { marbleBot } from '../marble/bot';
import { dalmutiBot } from '../dalmuti/bot';
import { splendorBot } from '../splendor/bot';
import { quoridorBot } from '../quoridor/bot';
import { lasVegasBot } from '../las-vegas/bot';
import { cockroachPokerBot } from '../cockroach-poker/bot';
import { ninetyNineBot } from '../ninety-nine/bot';
import { mafiaBot } from '../mafia/bot';
export type { BotInput, BotStrategy, LiveBotDecision, LiveBotInput, LiveBotStrategy } from './types';
export { botPace, botPersona, hash01, type BotPersona } from './persona';
/** Strategy per bot-enabled game (must match `BOT_GAMES` in @bg/shared). */
export const BOT_STRATEGIES: Record<string, BotStrategy> = {
omok: omokBot,
othello: othelloBot,
chess: chessBot,
janggi: janggiBot,
baduk: badukBot,
seotda: seotdaBot,
poker: pokerBot,
yacht: yachtBot,
yut: yutBot,
'one-card': oneCardBot,
rummikub: rummikubBot,
gostop: gostopBot,
bang: bangBot,
'davinci-code': davinciBot,
mahjong: mahjongBot,
marble: marbleBot,
dalmuti: dalmutiBot,
splendor: splendorBot,
quoridor: quoridorBot,
'las-vegas': lasVegasBot,
'cockroach-poker': cockroachPokerBot,
'ninety-nine': ninetyNineBot,
};
/** The bot's legal actions as the game lists them (validated lazily: only the chosen one, see below). */
export function botCandidates(def: AnyGameDefinition, state: unknown, me: PlayerId): unknown[] {
return def.legalActions ? def.legalActions(state, me) : [];
}
/**
* Picks the bot's next action, or null when it has nothing to do. The strategy's choice is checked with `validate`
* (with the server clock); a rejected choice is dropped and the strategy asked again, so every returned action is
* one the server accepts. Validating only the chosen action keeps a move fast (chess validates ~0.3ms per move).
*/
export function decideBotAction(
def: AnyGameDefinition,
state: unknown,
me: PlayerId,
ctx: { now: number; random?: () => number },
): unknown | null {
let legal = botCandidates(def, state, me);
if (!legal.length) return null;
const random = ctx.random ?? Math.random;
const valid = (a: unknown) => def.validate(state, me, a, { now: ctx.now }).ok;
const view = def.view(state, me);
const strategy = BOT_STRATEGIES[def.id] ?? randomPolicy;
for (let attempt = 0; attempt < 3 && legal.length; attempt++) {
let action: unknown;
try {
action = strategy({ me, view: structuredClone(view), legal, random });
} catch {
break;
}
if (action !== undefined && valid(action)) return action;
legal = legal.filter((a) => a !== action);
}
const rest = legal.filter(valid);
return rest.length ? pick(playable(rest), random) : null;
}
/** Live strategies (games where everybody acts at once and talks); also in `BOT_GAMES`. */
export const LIVE_BOT_STRATEGIES: Record<string, LiveBotStrategy> = {
mafia: mafiaBot,
};