컴퓨터 난이도(쉬움 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:
93
packages/games/src/bots/level.test.ts
Normal file
93
packages/games/src/bots/level.test.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
/** Computer levels and human-like choices (docs/09-game-engine.md §9.1). */
|
||||
import { describe, expect, test } from 'bun:test';
|
||||
import { SeededRng, secureRandom, seedN } from '@bg/engine';
|
||||
import { BOT_PLAY, humanBest } from './policy';
|
||||
import { BOT_LEVEL_ODDS, botDraw, botPersona, levelOf, type BotLevel } from './persona';
|
||||
|
||||
const CROCKFORD = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
|
||||
const fakeUlid = (r: () => number) => '01J' + Array.from({ length: 23 }, () => CROCKFORD[Math.floor(r() * 32)]).join('');
|
||||
|
||||
describe('secureRandom', () => {
|
||||
test('uniform floats in [0, 1)', () => {
|
||||
const n = 100_000;
|
||||
const bins = new Array(10).fill(0);
|
||||
for (let i = 0; i < n; i++) {
|
||||
const x = secureRandom();
|
||||
expect(x >= 0 && x < 1).toBe(true);
|
||||
bins[Math.floor(x * 10)]++;
|
||||
}
|
||||
for (const b of bins) expect(Math.abs(b - n / 10)).toBeLessThan(n / 10 * 0.05);
|
||||
});
|
||||
});
|
||||
|
||||
describe('bot level', () => {
|
||||
test('the roll comes from the id’s random characters and keeps the level for the same id', () => {
|
||||
const id = `bot:2:${fakeUlid(secureRandom)}`;
|
||||
expect(botPersona(id)).toEqual(botPersona(id));
|
||||
expect(botDraw('bot:1:01J0000000000000000000000Z')).toBe(0);
|
||||
expect(botDraw('bot:1:01J0000000ZZZZZZ0000000000')).toBeCloseTo(1, 6);
|
||||
});
|
||||
|
||||
test('levels come out at the documented odds; 똑똑한 편 is half, every 쉬움 허술, every 어려움 똑똑', () => {
|
||||
const rng = SeededRng.fromSeed(seedN(3));
|
||||
const n = 40_000;
|
||||
const count: Record<BotLevel, number> = { easy: 0, normal: 0, hard: 0 };
|
||||
let smart = 0;
|
||||
for (let i = 0; i < n; i++) {
|
||||
const p = botPersona(`bot:${(i % 7) + 1}:${fakeUlid(() => rng.next())}`);
|
||||
count[p.level]++;
|
||||
if (p.smart) smart++;
|
||||
if (p.level === 'easy') expect(p.smart).toBe(false);
|
||||
if (p.level === 'hard') expect(p.smart).toBe(true);
|
||||
}
|
||||
for (const level of ['easy', 'normal', 'hard'] as const) expect(Math.abs(count[level] / n - BOT_LEVEL_ODDS[level])).toBeLessThan(0.015);
|
||||
expect(Math.abs(smart / n - 0.5)).toBeLessThan(0.015);
|
||||
expect(levelOf(0.3499)).toBe('easy');
|
||||
expect(levelOf(0.35)).toBe('normal');
|
||||
expect(levelOf(0.75)).toBe('hard');
|
||||
});
|
||||
});
|
||||
|
||||
describe('humanBest', () => {
|
||||
// Scores: 100 best, 95 close, 60 a slip away for easy, 0 a blunder.
|
||||
const moves = ['best', 'close', 'meh', 'blunder'];
|
||||
const value: Record<string, number> = { best: 100, close: 95, meh: 70, blunder: 0 };
|
||||
const scale = { small: 10, critical: 1000 };
|
||||
const share = (level: BotLevel | undefined, xs = moves, v = value) => {
|
||||
const rng = SeededRng.fromSeed(seedN(11));
|
||||
const n = 20_000;
|
||||
const out: Record<string, number> = {};
|
||||
for (let i = 0; i < n; i++) {
|
||||
const m = humanBest(xs, (a) => v[a]!, () => rng.next(), level, scale);
|
||||
out[m] = (out[m] ?? 0) + 1;
|
||||
}
|
||||
for (const k in out) out[k]! /= n;
|
||||
return out;
|
||||
};
|
||||
|
||||
test('without a level it is the plain best move', () => {
|
||||
expect(share(undefined)).toEqual({ best: 1 });
|
||||
});
|
||||
|
||||
test('stronger levels play the best move more often and never pick a move far worse than a lapse allows', () => {
|
||||
const easy = share('easy');
|
||||
const normal = share('normal');
|
||||
const hard = share('hard');
|
||||
expect(easy.best!).toBeLessThan(normal.best!);
|
||||
expect(normal.best!).toBeLessThan(hard.best!);
|
||||
expect(hard.best!).toBeGreaterThan(0.9);
|
||||
// "meh" (30 worse = 3 × small) is past every slip window (≤ 2 × small): only a lapse (8 × small) plays it.
|
||||
expect(hard.meh ?? 0).toBeLessThan(0.01);
|
||||
// 100 worse is beyond every window: never played.
|
||||
for (const s of [easy, normal, hard]) expect(s.blunder ?? 0).toBe(0);
|
||||
// Close alternative: roughly slip share of the time.
|
||||
expect(Math.abs((normal.close ?? 0) - (BOT_PLAY.normal.slip + BOT_PLAY.normal.lapse / 2))).toBeLessThan(0.03);
|
||||
});
|
||||
|
||||
test('a must-play move (≥ critical) is missed only at the documented rate', () => {
|
||||
const v = { ...value, best: 5000 };
|
||||
expect(share('hard', moves, v).best).toBe(1);
|
||||
expect(Math.abs(1 - share('normal', moves, v).best! - BOT_PLAY.normal.miss)).toBeLessThan(0.01);
|
||||
expect(Math.abs(1 - share('easy', moves, v).best! - BOT_PLAY.easy.miss)).toBeLessThan(0.015);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user