/** * 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 { secureRandom, type AnyGameDefinition, type PlayerId } from '@bg/engine'; import type { BotLevel } from './persona'; import type { BotReaction, 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, oneCardReaction } 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 { oldMaidBot } from '../old-maid/bot'; import { mafiaBot } from '../mafia/bot'; import { appleBot } from '../apple/bot'; import { clueBot } from '../clue/bot'; import { connectFourBot } from '../connect-four/bot'; import { unoBot } from '../uno/bot'; import { liarBot } from '../liar/bot'; import { magicalAthleteBot } from '../magical-athlete/bot'; import { blackjackBot } from '../blackjack/bot'; import { cobBot } from '../castles-of-burgundy/bot'; export type { BotInput, BotReaction, BotStrategy, LiveBotDecision, LiveBotInput, LiveBotStrategy } from './types'; export { botDraw, botPace, botPersona, hash01, idLevel, type BotLevel, type BotPersona } from './persona'; export { botPlay } from './policy'; /** Strategy per bot-enabled game (must match `BOT_GAMES` in @bg/shared). */ export const BOT_STRATEGIES: Record = { 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, clue: clueBot, 'connect-four': connectFourBot, 'old-maid': oldMaidBot, uno: unoBot, 'magical-athlete': magicalAthleteBot, blackjack: blackjackBot, 'castles-of-burgundy': cobBot, }; /** Games whose computers also react out of turn on their own clock (원카드 외치기·잡기). */ export const BOT_REACTIONS: Record = { 'one-card': oneCardReaction, }; /** When `me` reacts out of turn (server time), 'ignore', or null (nothing to react to / the game has no reactions). */ export function botReactionAt(def: AnyGameDefinition, state: unknown, me: PlayerId, level?: BotLevel): number | 'ignore' | null { const react = BOT_REACTIONS[def.id]; if (!react) return null; try { return react({ me, view: def.view(state, me), level }); } catch { return null; } } /** 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; level?: BotLevel }, ): unknown | null { let legal = botCandidates(def, state, me); if (!legal.length) return null; const random = ctx.random ?? secureRandom; 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, level: ctx.level }); } 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: 마피아·라이어 게임 talk, 사과 게임 races on its own board); also in `BOT_GAMES`. */ export const LIVE_BOT_STRATEGIES: Record = { mafia: mafiaBot, apple: appleBot, liar: liarBot, };