컴퓨터 난이도 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>
This commit is contained in:
EJClaw
2026-10-11 17:46:13 +09:00
parent 3cbc3698d3
commit 84d64b27f2
40 changed files with 1349 additions and 133 deletions

View File

@@ -1,8 +1,8 @@
/** 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';
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('');
@@ -21,30 +21,47 @@ describe('secureRandom', () => {
});
describe('bot level', () => {
test('the roll comes from the id’s random characters and keeps the level for the same id', () => {
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(botPersona(id)).toEqual(botPersona(id));
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('levels come out at the documented odds; 똑똑한 편 is half, every 쉬움 허술, every 어려움 똑똑', () => {
test('id levels are uniform over 1~60; 똑똑한 편 (start above 30) is half', () => {
const rng = SeededRng.fromSeed(seedN(3));
const n = 40_000;
const count: Record<BotLevel, number> = { easy: 0, normal: 0, hard: 0 };
const n = 60_000;
const bands = [0, 0, 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);
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 level of ['easy', 'normal', 'hard'] as const) expect(Math.abs(count[level] / n - BOT_LEVEL_ODDS[level])).toBeLessThan(0.015);
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(levelOf(0.3499)).toBe('easy');
expect(levelOf(0.35)).toBe('normal');
expect(levelOf(0.75)).toBe('hard');
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));
});
});
@@ -70,24 +87,29 @@ describe('humanBest', () => {
});
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');
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 [easy, normal, hard]) expect(s.blunder ?? 0).toBe(0);
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) - (BOT_PLAY.normal.slip + BOT_PLAY.normal.lapse / 2))).toBeLessThan(0.03);
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('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);
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);
});
});