/** Computers' luck: choosing the shuffle (docs/04 §5.1, docs/09 §9.1). */ import { describe, expect, test } from 'bun:test'; import { SeededRng, seedN, type ChaChaSeed } from '@bg/engine'; import { GAMES } from '../index'; import { BOT_STRATEGIES } from './index'; import { chooseLuckySeed, luckApplies, playOutScores, shufflesFor, type LuckJob } from './luck'; const words = (r: SeededRng) => Array.from({ length: 8 }, () => Math.floor(r.next() * 2 ** 32) >>> 0) as ChaChaSeed; function setup(n: number) { const r = SeededRng.fromSeed(seedN(n)); return { makeSeed: () => words(r), random: () => r.next() }; } const job = (gameId: string, luck: Record, players = ['human', 'bot:1:a']): LuckJob => ({ gameId, players, options: GAMES[gameId]!.defaultOptions, now: 1_700_000_000_000, gameNo: 1, luck, levels: { 'bot:1:a': 30 }, budgetMs: 60_000, }); describe('luck', () => { test('how many shuffles: none near 0, up to 5 at ±1, the strongest luck at the table counts', () => { expect(shufflesFor([])).toBe(1); expect(shufflesFor([0.1, -0.12])).toBe(1); expect(shufflesFor([0.5])).toBe(3); expect(shufflesFor([-1])).toBe(5); expect(shufflesFor([0.2, -0.9])).toBe(5); }); test('luck is for games with cards, tiles, dice or hidden roles — not for board games', () => { for (const id of ['omok', 'chess', 'janggi', 'othello', 'baduk', 'quoridor', 'connect-four', 'mafia', 'dobble', 'gemblo']) expect(luckApplies(id)).toBe(false); for (const id of Object.keys(BOT_STRATEGIES).filter((g) => !['omok', 'chess', 'janggi', 'othello', 'baduk', 'quoridor', 'connect-four', 'gemblo'].includes(g))) { expect(luckApplies(id)).toBe(true); } }); test('tries one shuffle without luck or in a board game, more with luck, and keeps one of them', () => { const { makeSeed, random } = setup(1); expect(chooseLuckySeed(GAMES['one-card']!, job('one-card', { 'bot:1:a': 0 }), makeSeed, random).tried).toBe(1); expect(chooseLuckySeed(GAMES.omok!, job('omok', { 'bot:1:a': 1 }), makeSeed, random).tried).toBe(1); const made: string[] = []; const tracked = () => { const s = makeSeed(); made.push(s.join(',')); return s; }; const r = chooseLuckySeed(GAMES['one-card']!, job('one-card', { 'bot:1:a': -1 }), tracked, random); expect(r.tried).toBe(5); expect(made).toHaveLength(5); expect(made).toContain(r.seed.join(',')); }); test('a played-out game gives every player a place score in [0, 1]', () => { const { makeSeed, random } = setup(2); const s = playOutScores(GAMES['one-card']!, { ...job('one-card', {}, ['a', 'b', 'c']), seed: makeSeed(), random }); expect(s).not.toBeNull(); expect(Object.keys(s!).sort()).toEqual(['a', 'b', 'c']); for (const v of Object.values(s!)) expect(v >= 0 && v <= 1).toBe(true); }); test('lucky computers do better than unlucky ones (원카드, 1:1)', () => { const { makeSeed, random } = setup(3); const def = GAMES['one-card']!; const mean = (luck: number) => { let sum = 0; const n = 50; for (let i = 0; i < n; i++) { const { seed } = chooseLuckySeed(def, job('one-card', { 'bot:1:a': luck }), makeSeed, random); // The real game: the same deal, but every choice made afresh. sum += playOutScores(def, { ...job('one-card', {}), seed, random })?.['bot:1:a'] ?? 0.5; } return sum / n; }; expect(mean(1) - mean(-1)).toBeGreaterThan(0.15); }); });