Files
wikideck/tests/ui-bots.test.ts
T
claudeandClaude Opus 5.5 5d1aeb1632 Browser bots: easy and normal opponents in ui-a's Bot shape
Issue #5. src/game/ai exports BOTS (easy, normal) for the Play section.
They play from the MatchView view model, so they work on the mock engine
now; makeUiBot(level, engineChoice) is the hook for wiring sim-balance's
shared/bots once the shared/game adapter attaches engine state.
On the mock engine (100 matches each): normal beats random 86.5%, easy
beats random 59.5%, normal beats easy 73%. Bots never concede or un-queue.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 23:18:34 -05:00

75 lines
3.4 KiB
TypeScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { engine, STANDARD_RULESET } from '../src/game/adapter.ts';
import { randomBot } from '../src/game/bots.ts';
import { starterDeck } from '../src/game/cards.ts';
import { BOTS, makeUiBot } from '../src/game/ai/index.ts';
import type { MatchView, PlayerIndex, UIAction } from '../src/game/types.ts';
type B = { choose(view: MatchView, legal: UIAction[], rand: () => number): UIAction };
function rand(seed: number) { return () => ((seed = (seed * 1103515245 + 12345) >>> 0) / 4294967296); }
/** Plays one match through ui-a's adapter, the way the match screen drives a bot seat. */
function play(bots: [B, B], seed: string) {
let s = engine.createMatch(STANDARD_RULESET, [starterDeck(), starterDeck()], seed);
const r = rand(seed.length * 7919 + seed.charCodeAt(seed.length - 1));
for (let steps = 0; steps < 2000 && !engine.isOver(s); steps++) {
for (const p of [0, 1] as PlayerIndex[]) {
const legal = engine.legal(s, p);
if (!legal.length) continue;
const pick = bots[p].choose(engine.view(s, p), legal.filter(a => a.type !== 'concede'), r);
assert.notEqual(pick.type, 'concede');
s = engine.apply(s, p, pick);
}
}
assert.ok(engine.isOver(s), 'match finishes');
return engine.result(s, 0).winner;
}
function winRate(a: B, b: B, n: number) {
let score = 0;
for (let i = 0; i < n; i++) {
const flip = i % 2 === 1;
const w = play(flip ? [b, a] : [a, b], `bots-${i}`);
score += w === null ? 0.5 : w === (flip ? 1 : 0) ? 1 : 0;
}
return score / n;
}
const [easy, normal] = BOTS;
test('BOTS exports easy and normal in the Bot shape', () => {
assert.deepEqual(BOTS.map(b => b.id), ['easy', 'normal']);
for (const b of BOTS) assert.ok(b.name && b.description);
});
test('normal bot beats random play more than 80% of the time', () => {
const r = winRate(normal, randomBot, 100);
console.log(`normal vs random ${r}`); assert.ok(r > 0.8, `normal vs random ${r}`);
});
test('easy bot beats random, and normal beats easy', () => {
const e = winRate(easy, randomBot, 100), g = winRate(normal, easy, 100);
console.log(`easy vs random ${e}, normal vs easy ${g}`); assert.ok(e > 0.5, `easy vs random ${e}`);
assert.ok(g > 0.6, `normal vs easy ${g}`);
});
test('bots never concede or un-queue, and end the turn when nothing fits', () => {
const view = { hand: [], lanes: [] } as unknown as MatchView;
for (const b of BOTS) {
const only: UIAction[] = [{ type: 'unstage', cardId: 'x' }, { type: 'concede' }, { type: 'endTurn' }];
for (let i = 0; i < 20; i++) assert.equal(b.choose(view, only, rand(i)).type, 'endTurn');
}
});
test('an engine-level chooser is used when the adapter attaches engine actions', () => {
const bot = makeUiBot('normal', (_ev, _r) => ({ type: 'play', player: 1, cardId: 'b', lane: 2 }));
const legal = [
{ type: 'play', cardId: 'a', lane: 0, engine: { type: 'play', player: 1, cardId: 'a', lane: 0 } },
{ type: 'play', cardId: 'b', lane: 2, engine: { lane: 2, cardId: 'b', player: 1, type: 'play' } },
{ type: 'endTurn', engine: { type: 'commit', player: 1 } },
] as unknown as UIAction[];
const view = { hand: [], lanes: [], engine: { state: {}, seat: 1 } } as unknown as MatchView;
assert.equal(bot.choose(view, legal, rand(1)), legal[1]);
});