M1 기반: 모노레포, 공통 엔진, 공용 프로토콜, 오목 엔진, 서버 골격
- packages/engine: 시드 RNG(sfc32), GameDefinition 타입, 무작위 대국·정보 유출 테스트 도구 - packages/shared: WS 프로토콜 스키마, 닉네임·방 코드 규칙, 게임 카탈로그 - packages/games/omok: 한국식·렌주·자유룰, 재귀 금수 판정, 무르기·무승부·시계 (테스트 27개) - apps/server: SQLite 스키마, 게스트·디스코드 로그인, 세션, 방 관리(저장·복구·타이머·재접속), WS 게이트웨이, HTTP API Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
91
packages/engine/src/rng.ts
Normal file
91
packages/engine/src/rng.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* Deterministic seeded PRNG (sfc32). The state is four 32-bit unsigned ints and is
|
||||
* JSON-serialisable so it can be persisted next to the game state (docs/09 §3).
|
||||
*/
|
||||
export type RngState = [number, number, number, number];
|
||||
|
||||
export interface Rng {
|
||||
/** Float in [0, 1). */
|
||||
next(): number;
|
||||
/** Unbiased integer in [0, maxExclusive). */
|
||||
int(maxExclusive: number): number;
|
||||
/** Fisher–Yates shuffle, returns a new array. */
|
||||
shuffle<T>(arr: readonly T[]): T[];
|
||||
}
|
||||
|
||||
export function createSeed(): RngState {
|
||||
const buf = new Uint32Array(4);
|
||||
crypto.getRandomValues(buf);
|
||||
// An all-zero state would be degenerate for sfc32's counter; guard anyway.
|
||||
if (buf[0] === 0 && buf[1] === 0 && buf[2] === 0 && buf[3] === 0) buf[3] = 1;
|
||||
return [buf[0]!, buf[1]!, buf[2]!, buf[3]!];
|
||||
}
|
||||
|
||||
export function seedToHex(seed: RngState): string {
|
||||
return seed.map((n) => (n >>> 0).toString(16).padStart(8, '0')).join('');
|
||||
}
|
||||
|
||||
export function seedFromHex(hex: string): RngState {
|
||||
if (!/^[0-9a-f]{32}$/.test(hex)) throw new Error('invalid seed hex');
|
||||
return [0, 1, 2, 3].map((i) => parseInt(hex.slice(i * 8, i * 8 + 8), 16) >>> 0) as RngState;
|
||||
}
|
||||
|
||||
/** Mutable PRNG over a copy of `state`; read the advanced state back with `rng.state()`. */
|
||||
export class SeededRng implements Rng {
|
||||
private a: number;
|
||||
private b: number;
|
||||
private c: number;
|
||||
private d: number;
|
||||
|
||||
constructor(state: RngState) {
|
||||
[this.a, this.b, this.c, this.d] = state.map((n) => n >>> 0) as RngState;
|
||||
}
|
||||
|
||||
/** Seeds from a hex string, running a few warm-up rounds as recommended for sfc32. */
|
||||
static fromSeed(seed: RngState): SeededRng {
|
||||
const rng = new SeededRng(seed);
|
||||
for (let i = 0; i < 12; i++) rng.nextUint32();
|
||||
return rng;
|
||||
}
|
||||
|
||||
state(): RngState {
|
||||
return [this.a, this.b, this.c, this.d];
|
||||
}
|
||||
|
||||
nextUint32(): number {
|
||||
const t = (((this.a + this.b) >>> 0) + this.d) >>> 0;
|
||||
this.d = (this.d + 1) >>> 0;
|
||||
this.a = this.b ^ (this.b >>> 9);
|
||||
this.b = (this.c + (this.c << 3)) >>> 0;
|
||||
this.c = ((this.c << 21) | (this.c >>> 11)) >>> 0;
|
||||
this.c = (this.c + t) >>> 0;
|
||||
return t;
|
||||
}
|
||||
|
||||
next(): number {
|
||||
return this.nextUint32() / 4294967296;
|
||||
}
|
||||
|
||||
int(maxExclusive: number): number {
|
||||
if (!Number.isInteger(maxExclusive) || maxExclusive <= 0 || maxExclusive > 4294967296) {
|
||||
throw new Error(`rng.int: invalid bound ${maxExclusive}`);
|
||||
}
|
||||
// Rejection sampling to avoid modulo bias.
|
||||
const limit = 4294967296 - (4294967296 % maxExclusive);
|
||||
for (;;) {
|
||||
const x = this.nextUint32();
|
||||
if (x < limit) return x % maxExclusive;
|
||||
}
|
||||
}
|
||||
|
||||
shuffle<T>(arr: readonly T[]): T[] {
|
||||
const out = arr.slice();
|
||||
for (let i = out.length - 1; i > 0; i--) {
|
||||
const j = this.int(i + 1);
|
||||
const tmp = out[i]!;
|
||||
out[i] = out[j]!;
|
||||
out[j] = tmp;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user