/** * 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 = { 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 = { mafia: mafiaBot, };