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:
58
packages/games/src/bots/policy.ts
Normal file
58
packages/games/src/bots/policy.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
/** Default "random but sensible" bot policy and small helpers shared by per-game strategies. */
|
||||
import type { BotInput } from './types';
|
||||
|
||||
type Act = { type?: string; [k: string]: unknown };
|
||||
|
||||
/** Actions a bot never starts on its own: requests to the human, resigning, chatting, editing tricks. */
|
||||
const META = new Set(['requestUndo', 'offerDraw', 'claimDraw', 'resign', 'concede', 'leave', 'undoDraft', 'report', 'chat', 'reaction', 'emote', 'adjustTime']);
|
||||
|
||||
export const pick = <T>(xs: readonly T[], random: () => number): T => xs[Math.floor(random() * xs.length)]!;
|
||||
|
||||
export const typeOf = (a: unknown): string => String((a as Act | null)?.type ?? '');
|
||||
|
||||
/** Legal actions minus meta ones (kept if nothing else is left). */
|
||||
export function playable<A>(legal: A[]): A[] {
|
||||
const out = legal.filter((a) => !META.has(typeOf(a)));
|
||||
return out.length ? out : legal;
|
||||
}
|
||||
|
||||
/** Highest-scoring action; ties broken at random. */
|
||||
export function best<A>(xs: readonly A[], score: (a: A) => number, random: () => number): A {
|
||||
let top: A[] = [];
|
||||
let topScore = -Infinity;
|
||||
for (const a of xs) {
|
||||
const s = score(a);
|
||||
if (s > topScore) {
|
||||
topScore = s;
|
||||
top = [a];
|
||||
} else if (s === topScore) top.push(a);
|
||||
}
|
||||
return pick(top.length ? top : xs, random);
|
||||
}
|
||||
|
||||
/**
|
||||
* Answers to the human's requests: undo is accepted (it's practice), draw offers are declined.
|
||||
* Returns null when no request is pending.
|
||||
*/
|
||||
export function answerRequests<A>(legal: A[]): A | null {
|
||||
for (const a of legal) {
|
||||
const t = typeOf(a);
|
||||
if (t === 'respondUndo') return { ...(a as object), accept: true } as A;
|
||||
if (t === 'respondDraw') return { ...(a as object), accept: false } as A;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Random legal action, preferring to answer requests and skipping meta actions. */
|
||||
export function randomPolicy<A>({ legal, random }: BotInput<unknown, A>): A {
|
||||
return answerRequests(legal) ?? pick(playable(legal), random);
|
||||
}
|
||||
|
||||
/** Random action, but "pass"-like actions only when nothing else is possible. */
|
||||
export function preferPlay<A>(input: BotInput<unknown, A>, passTypes: string[] = ['pass', 'draw']): A {
|
||||
const req = answerRequests(input.legal);
|
||||
if (req) return req;
|
||||
const all = playable(input.legal);
|
||||
const plays = all.filter((a) => !passTypes.includes(typeOf(a)));
|
||||
return pick(plays.length ? plays : all, input.random);
|
||||
}
|
||||
Reference in New Issue
Block a user