M9: 컴퓨터 상대 — 게임 22종 컴퓨터 전략, 서버 구동(BotDriver), 연습 방

- packages/shared/src/bots.ts: 컴퓨터 id(bot:<n>:<ulid>)·프로필(컴퓨터 n)·BOT_GAMES
  (도블·할리갈리·그림 맞히기·마피아 제외)
- packages/games/src/bots: 전략 계약(자기 view + 합법 수만), decideBotAction(validate 재검사),
  게임별 <게임>/bot.ts 22개 + 행동 테스트, 전 게임 판 끝까지 돌리는 공통 테스트
- 서버: Room.addBot/removeBot(방장·대기실만, 방은 친구만), RoomManager.createPractice
  (POST /api/rooms {practice:true}), BotDriver(사람 같은 지연, 사람과 같은 창이면 더 천천히,
  재시작 후 다시 예약), 연습 게임은 전적 제외(result_json.practice), 사람이 모두 나가면 무효
- 프로토콜 addBot/removeBot, 문서 03·06 §12·08·09 §8

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-06 13:32:28 +09:00
parent c27cb0a11a
commit a3f81ded37
67 changed files with 5113 additions and 18 deletions

View File

@@ -0,0 +1,94 @@
/**
* Computer players (docs/09-game-engine.md §8). `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 } 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';
export type { BotInput, BotStrategy } from './types';
/** 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;
}