컴퓨터 난이도(쉬움 35%·보통 40%·어려움 25%)와 사람 같은 수, 서버 난수를 OS 보안 난수로

- secureRandom(): crypto.getRandomValues 53비트 실수. 서버의 Math.random(컴퓨터 수·생각 시간·AI 말·방 코드·마감 유예)을 모두 교체
- 컴퓨터 id의 ULID를 secureRandom으로 만들고, 그 무작위 부분 30비트로 난이도·말투(smart)를 정함(botDraw)
- humanBest: 오목·체스·장기·오델로·바둑에서 난이도별로 비슷한 수(slip)·사고(lapse)·꼭 둬야 할 수 놓침(miss)
- 문서 09 §9.1(확률표·게임별 기준·대국 결과), 06 §12; 테스트 bots/level.test.ts, 레벨별 완주 테스트

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-11 16:19:06 +09:00
parent 07fe46562f
commit 3cbc3698d3
20 changed files with 312 additions and 41 deletions

View File

@@ -1,5 +1,6 @@
/** Default "random but sensible" bot policy and small helpers shared by per-game strategies. */
import type { BotInput } from './types';
import type { BotLevel } from './persona';
type Act = { type?: string; [k: string]: unknown };
@@ -30,6 +31,60 @@ export function best<A>(xs: readonly A[], score: (a: A) => number, random: () =>
return pick(top.length ? top : xs, random);
}
/** How each level slips (docs/09-game-engine.md §9.1). */
export interface BotPlay {
/** Chance per move of a small inaccuracy: a move almost as good (within `window` × the game's `small`). */
slip: number;
window: number;
/** Chance per move of a real blunder (사고): one of the next best few, up to `LAPSE_WINDOW` × `small` worse. */
lapse: number;
/** Chance of overlooking a must-play move (a win, mate, blocking a five, a free corner). */
miss: number;
}
export const BOT_PLAY: Readonly<Record<BotLevel, BotPlay>> = {
easy: { slip: 0.35, window: 2, lapse: 0.06, miss: 0.12 },
normal: { slip: 0.2, window: 1, lapse: 0.02, miss: 0.03 },
hard: { slip: 0.08, window: 0.5, lapse: 0.005, miss: 0 },
};
export const LAPSE_WINDOW = 8;
/**
* Like `best`, but played by a person of `level`: usually the best move, now and then one that looked almost as good,
* rarely a real mistake, and once in a while it doesn't see the winning / saving move. `small` is what a small
* inaccuracy is worth in the game's score units; scores ≥ `critical` mark must-play moves. Without a level (tests,
* `best` callers) it is exactly `best`.
*/
export function humanBest<A>(
xs: readonly A[],
score: (a: A) => number,
random: () => number,
level: BotLevel | undefined,
scale: { small: number; critical: number },
): A {
if (!level || xs.length < 2) return best(xs, score, random);
const scored = xs.map((a) => ({ a, s: score(a) }));
const topScore = Math.max(...scored.map((x) => x.s));
const top = pick(scored.filter((x) => x.s === topScore), random).a;
const rest = scored.filter((x) => x.a !== top).sort((x, y) => y.s - x.s);
const play = BOT_PLAY[level];
const r = random();
if (topScore >= scale.critical) {
if (r >= play.miss) return top;
// Didn't see it: plays one of the next moves that looked fine.
return pick(rest.slice(0, 3), random).a;
}
if (r < play.lapse) {
const near = rest.filter((x) => x.s >= topScore - LAPSE_WINDOW * scale.small).slice(0, 8);
if (near.length) return pick(near, random).a;
} else if (r < play.lapse + play.slip) {
const near = rest.filter((x) => x.s >= topScore - play.window * scale.small).slice(0, 5);
if (near.length) return pick(near, random).a;
}
return top;
}
/**
* Answers to the human's requests: undo is accepted (it's practice), draw offers are declined.
* Returns null when no request is pending.