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,62 @@
/**
* 숫자 타일 computer player. A turn is two decisions: first one `setDraft` with the whole plan (rack-only sets, then
* single tiles laid off on table / new sets), then `commit` once the view says the draft is valid. If the draft is
* not valid (should not happen) it draws, which also throws the draft away — it never leaves a half-built draft.
* Table sets are never rearranged beyond adding tiles to their ends.
*/
import type { BotStrategy } from '../bots/types';
import { answerRequests } from '../bots/policy';
import { extendSet, isJoker, pickDisjoint, type JokerAs } from './rules';
import type { RummikubAction, RummikubView } from './index';
type Plan = { tileIds: string[]; kind: 'run' | 'group'; jokerAs: Record<string, JokerAs> };
/** Lays rack tiles off on the given sets in place (repeating, so 5-6-7 + 8 + 9 works). */
function layOff(sets: Plan[], rack: string[], keepJokers: boolean): void {
const used = new Set<string>();
for (let changed = true; changed; ) {
changed = false;
for (const id of rack) {
if (used.has(id) || (keepJokers && isJoker(id))) continue;
for (let i = 0; i < sets.length; i++) {
const ext = extendSet(sets[i]!, id);
if (!ext) continue;
sets[i] = ext;
used.add(id);
changed = true;
break;
}
}
}
}
export const rummikubBot: BotStrategy<RummikubView, RummikubAction> = ({ view, legal }) => {
const req = answerRequests(legal);
if (req) return req;
if (legal.some((a) => a.type === 'ready')) return { type: 'ready' };
const my = view.my!;
const endTurn: RummikubAction = legal.find((a) => a.type === 'pass') ?? { type: 'draw' };
// Second step of a turn: the plan is on the table — commit it, or give up cleanly (draw resets the draft).
if (my.changed) return my.check?.ok ? { type: 'commit', turn: view.turnNo } : endTurn;
const rack = my.rack;
const table: Plan[] = view.table.map((t) => ({ tileIds: [...t.tileIds], kind: t.kind, jokerAs: t.jokerAs }));
const disjoint = pickDisjoint(rack);
const fresh = disjoint.sets.map((n): Plan => ({ tileIds: n.tileIds, kind: n.kind, jokerAs: n.jokerAs }));
const rest = rack.filter((id) => !fresh.some((s) => s.tileIds.includes(id)));
// A lone joker is only laid off near the end (it's the most flexible tile, but costs 30 if still on the rack).
const keepJokers = rack.length > 4;
let sets: Plan[];
if (!my.melded) {
// First meld: every disjoint rack set at once, if together they reach the minimum. Table sets stay untouched.
if (!fresh.length || disjoint.points < view.options.initialMeldPoints) return endTurn;
layOff(fresh, rest, keepJokers);
sets = [...table, ...fresh];
} else {
sets = [...table, ...fresh];
layOff(sets, rest, keepJokers);
}
const before = new Set(view.table.flatMap((t) => t.tileIds));
if (!sets.some((s) => s.tileIds.some((id) => !before.has(id)))) return endTurn;
return { type: 'setDraft', sets: sets.map((s) => ({ tileIds: s.tileIds, as: s.kind })), turn: view.turnNo };
};