Files
joke-app/packages/games/src/bots/level.test.ts
EJClaw 84d64b27f2 컴퓨터 난이도 1~60단계·방장 선택·판마다 변화·운, 공정성 확인에 표기, 업데이트 알림 팝업
- 난이도 1~60(쉬움 1~20·보통 21~40·어려움 41~60): 넣을 때 랜덤, 방장이 대기실에서 [랜덤][쉬움][보통][어려움] 선택(botLevel) → 범위 안 무작위
- 다음 판마다 -2~+2, 처음 단계 ±5·같은 범위 안(drift); 말투는 처음 단계(>30 존댓말)
- humanBest 확률을 단계별로 보간(BOT_PLAY_ANCHORS: 1·10·30·50·60)
- 운(-1~+1): 카드·주사위 게임에서 시작 전에 시드 후보 1~5개를 작업 스레드에서 끝까지 둬 보고 운에 맞는 것을 고름(luck.ts, luck-worker.ts). 해시 공개는 그대로
- 결과 화면 공정성 확인에 컴퓨터별 단계·운, 섞어 본 후보 수
- 업데이트 알림: 빌드 ID meta + /api/version + welcome.build → 상단 팝업 [지금 새로고침하기] [✕]
- 문서 03·04 §5.1·06 §12·07 §11.1·09 §9.1, 테스트(bot-level.room, luck, version, e2e bots·pwa)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 17:46:13 +09:00

116 lines
4.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/** 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_ANCHORS, botPlay, humanBest } from './policy';
import { botDraw, botPersona, idLevel, 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('a computer without a saved level gets one from its id’s random characters, the same every time', () => {
const id = `bot:2:${fakeUlid(secureRandom)}`;
expect(idLevel(id)).toBe(idLevel(id));
expect(botDraw('bot:1:01J0000000000000000000000Z')).toBe(0);
expect(botDraw('bot:1:01J0000000ZZZZZZ0000000000')).toBeCloseTo(1, 6);
expect(idLevel('bot:1:01J0000000000000000000000Z')).toBe(1);
expect(idLevel('bot:1:01J0000000ZZZZZZ0000000000')).toBe(60);
});
test('id levels are uniform over 1~60; 똑똑한 편 (start above 30) is half', () => {
const rng = SeededRng.fromSeed(seedN(3));
const n = 60_000;
const bands = [0, 0, 0];
let smart = 0;
for (let i = 0; i < n; i++) {
const id = `bot:${(i % 7) + 1}:${fakeUlid(() => rng.next())}`;
const level = idLevel(id);
expect(level >= 1 && level <= 60).toBe(true);
bands[Math.floor((level - 1) / 20)]!++;
if (botPersona(id).smart) smart++;
}
for (const b of bands) expect(Math.abs(b / n - 1 / 3)).toBeLessThan(0.015);
expect(Math.abs(smart / n - 0.5)).toBeLessThan(0.015);
expect(botPersona('bot:1:x', 30).smart).toBe(false);
expect(botPersona('bot:1:x', 31).smart).toBe(true);
});
test('play settings move steadily from level 1 to 60 and hit the anchors', () => {
for (const [level, play] of BOT_PLAY_ANCHORS) {
const p = botPlay(level);
for (const k of ['slip', 'window', 'lapse', 'miss'] as const) expect(p[k]).toBeCloseTo(play[k], 9);
}
for (let l = 2; l <= 60; l++) {
const a = botPlay(l - 1);
const b = botPlay(l);
expect(b.slip).toBeLessThanOrEqual(a.slip);
expect(b.lapse).toBeLessThanOrEqual(a.lapse);
expect(b.miss).toBeLessThanOrEqual(a.miss);
}
expect(botPlay(0)).toEqual(botPlay(1));
expect(botPlay(99)).toEqual(botPlay(60));
});
});
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 weakest = share(1);
const easy = share(10);
const normal = share(30);
const hard = share(50);
const strongest = share(60);
expect(weakest.best!).toBeLessThan(easy.best!);
expect(hard.best!).toBeLessThan(strongest.best!);
expect(strongest.best!).toBeGreaterThan(0.96);
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 [weakest, easy, normal, hard, strongest]) expect(s.blunder ?? 0).toBe(0);
// Close alternative: roughly slip share of the time.
expect(Math.abs((normal.close ?? 0) - (botPlay(30).slip + botPlay(30).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(50, moves, v).best).toBe(1);
expect(share(60, moves, v).best).toBe(1);
for (const level of [1, 10, 30, 41]) expect(Math.abs(1 - share(level, moves, v).best! - botPlay(level).miss)).toBeLessThan(0.015);
});
});