Files
joke-app/packages/games/src/bots/persona.ts
EJClaw 3cbc3698d3 컴퓨터 난이도(쉬움 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>
2026-10-11 16:19:06 +09:00

65 lines
2.7 KiB
TypeScript

/** Each computer's character, fixed by its id (docs/09-game-engine.md §9). */
/** FNV-1a of the parts → [0, 1). Stable: the same bot decides the same way about the same thing. */
export function hash01(...parts: (string | number)[]): number {
let h = 2166136261;
const s = parts.join('\u0001');
for (let i = 0; i < s.length; i++) h = Math.imul(h ^ s.charCodeAt(i), 16777619);
return (h >>> 0) / 2 ** 32;
}
/**
* Each computer's own pace (0.7 quick … 1.5 careful), fixed for the bot id so "컴퓨터 2" keeps its character for the
* whole game, while a new room's 컴퓨터 2 (new id) may be different.
*/
export function botPace(bot: string): number {
let h = 2166136261;
for (let i = 0; i < bot.length; i++) h = Math.imul(h ^ bot.charCodeAt(i), 16777619);
return 0.7 + ((h >>> 0) / 2 ** 32) * 0.8;
}
/** How well a computer plays (docs/09-game-engine.md §9.1). */
export type BotLevel = 'easy' | 'normal' | 'hard';
/** Share of computers at each level: 쉬움 35%, 보통 40%, 어려움 25%. */
export const BOT_LEVEL_ODDS: Readonly<Record<BotLevel, number>> = { easy: 0.35, normal: 0.4, hard: 0.25 };
const CROCKFORD = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
/**
* The computer's own die roll in [0, 1), read straight from the random part of its id: the server makes bot ids as
* `bot:<n>:<ULID>` with OS secure randomness (`secureRandom`), so the first 6 random characters are 30 uniform bits.
* Other ids (tests, hand-made) fall back to a hash. Fixed for the id, so the computer keeps its level after a restart.
*/
export function botDraw(bot: string): number {
const u = bot.split(':')[2] ?? '';
if (!/^[0-9A-HJKMNP-TV-Z]{26}$/.test(u)) return hash01(bot, 'level');
let v = 0;
for (const ch of u.slice(10, 16)) v = v * 32 + CROCKFORD.indexOf(ch);
return v / 32 ** 6;
}
export function levelOf(draw: number): BotLevel {
if (draw < BOT_LEVEL_ODDS.easy) return 'easy';
return draw < BOT_LEVEL_ODDS.easy + BOT_LEVEL_ODDS.normal ? 'normal' : 'hard';
}
export interface BotPersona {
/** 쉬움 / 보통 / 어려움: how often it slips in board games (`humanBest`). */
level: BotLevel;
/**
* 똑똑한 편(근거를 보고 추리, 존댓말) / 허술한 편(느낌과 분위기를 따름, 반말): half of the computers each, by the
* same roll as the level — every 쉬움 is 허술, every 어려움 똑똑, 보통 split in two.
*/
smart: boolean;
/** Reaction speed multiplier (`botPace`): 0.7 quick … 1.5 slow. */
pace: number;
/** 0.5 … 1: how likely it speaks up. */
chatty: number;
}
export function botPersona(bot: string): BotPersona {
const draw = botDraw(bot);
return { level: levelOf(draw), smart: draw >= 0.5, pace: botPace(bot), chatty: 0.5 + hash01(bot, 'chatty') * 0.5 };
}