/** 솔리테어 computers (docs/games/solitaire.md §13): never refused, always finish (no endless loops), stronger levels do better. */ import { describe, expect, test } from 'bun:test'; import { seedN } from '@bg/engine'; import { liveBotPlayout } from '../bots/live-playout'; import { chooseMove, scoreMove, solitaireBot } from './bot'; import { solitaire, type SolitaireBoardView, type SolitaireOptions, type SolitaireState } from './index'; const isBot = (p: string) => p.startsWith('bot:'); const MODES: Partial[] = [{ mode: 'klondike', draw: 1 }, { mode: 'klondike', draw: 3 }, { mode: 'freecell' }, { mode: 'spider', suits: 1 }, { mode: 'spider', suits: 4 }]; describe('solitaire · 한 판 전체', () => { test('사람(가만히) + 컴퓨터, 컴퓨터끼리만(무제한): 거절 0, 모두 끝까지, 빠른 판단', () => { for (const [k, o] of MODES.entries()) { for (const humans of [1, 0]) { const n = 2 + k % 2; const players = Array.from({ length: n }, (_, i) => (i < humans ? `human${i}` : `bot:${i + 1}:s${k}${i}`)); const r = liveBotPlayout(solitaire, solitaireBot, { players, options: solitaire.optionsSchema.parse({ ...o, timeLimitSec: humans ? 900 : null }), seed: seedN(k * 10 + humans), isBot, levelOf: (p) => 1 + ((p.length * 7 + k * 13) % 60), }); expect(r.finished).toBe(true); expect(r.rejected).toEqual([]); expect(r.maxBotMs).toBeLessThan(50); const s = r.state as SolitaireState; if (!humans) for (const p of players) expect(['solved', 'done']).toContain(s.status[p]!); } } }); test('강한 컴퓨터가 약한 컴퓨터보다 평균적으로 더 많이 올려요(같은 패)', () => { const total = { easy: 0, hard: 0 }; for (let k = 0; k < 12; k++) { const r = liveBotPlayout(solitaire, solitaireBot, { players: ['bot:1:easy', 'bot:2:hard'], options: solitaire.optionsSchema.parse(MODES[k % MODES.length]), seed: seedN(700 + k), isBot, levelOf: (p) => (p.endsWith('easy') ? 3 : 58), }); const s = r.state as SolitaireState; const prog = (p: string) => solitaire.view(s, p).seats.find((x) => x.id === p)!.progress; total.easy += prog('bot:1:easy'); total.hard += prog('bot:2:hard'); } expect(total.hard).toBeGreaterThan(total.easy); }); test('먼저 파운데이션, 다음 뒤집힌 카드를 여는 수; 같은 자리에서 같은 수는 두 번 하지 않아요', () => { const v: SolitaireBoardView = { mode: 'klondike', draw: 1, t: [[null, '9H'], ['TS'], ['AD'], [], [], [], []], stock: 3, waste: [], cells: [], f: ['', '', '', ''], done: '', canUndo: false, undos: 0, }; expect(scoreMove(v, { type: 'move', from: 't2', n: 1, to: 'f' })).toBeGreaterThan(scoreMove(v, { type: 'move', from: 't0', n: 1, to: 't1' })); expect(scoreMove(v, { type: 'move', from: 't0', n: 1, to: 't1' })).toBeGreaterThan(scoreMove(v, { type: 'draw' })); const mem = {}; expect(chooseMove(v, 0, () => 0, mem)).toEqual({ type: 'move', from: 't2', n: 1, to: 'f' }); expect(chooseMove(v, 0, () => 0, mem)).toEqual({ type: 'move', from: 't0', n: 1, to: 't1' }); expect(chooseMove(v, 0, () => 0, mem)).toEqual({ type: 'draw' }); // Nothing left to try on this screen → give up. expect(chooseMove({ ...v, stock: 0 }, 0, () => 0, { tried: {} })).not.toEqual({ type: 'stop' }); expect(chooseMove({ ...v, t: [['KS'], [], [], [], [], [], []], stock: 0 }, 0, () => 0, {})).toEqual({ type: 'stop' }); }); });