Files
joke-app/packages/games/src/rummikub/bot.ts
EJClaw 6390e3e64c 게임 4개 원래 이름으로, .5 + NPMplus 배포 구성
- 보석 상인 → 스플렌더, 벽 쌓기 길찾기 → 쿼리도, 숫자 타일 → 루미큐브, 주사위 땅따먹기 → 부루마불
- deploy/docker-compose.proxy.yml: Caddy 없이 앱만(.5:2970), HTTPS·헤더는 NPMplus
- deploy/npmplus-advanced.conf: Caddyfile과 같은 보안 헤더
- 문서: 2026-10-06 결정, 배포 7절 (e2e 구십구 0 카드 방향 버튼 반영 포함)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 22:16:32 +09:00

63 lines
3.0 KiB
TypeScript

/**
* 루미큐브 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 };
};