forked from WikiDeck/wikideck
night-lead's rule on #5. The engine-level chooser now receives view.engine = { view: PlayerView }; a test checks that nothing else reaches it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
77 lines
3.5 KiB
TypeScript
77 lines
3.5 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 seen: unknown[] = [];
|
|
const bot = makeUiBot('normal', (ev, _r) => { seen.push(ev); return { 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: { view: { you: 1 } } } as unknown as MatchView;
|
|
assert.equal(bot.choose(view, legal, rand(1)), legal[1]);
|
|
assert.deepEqual(seen, [{ view: { you: 1 } }], 'the engine chooser gets only the viewFor output');
|
|
});
|