import assert from 'node:assert/strict'; import { test } from 'node:test'; import { DEFAULT_GREEDY, greedyBot } from '../src/bots/greedy.ts'; import { randomBot } from '../src/bots/random.ts'; import { loadCards } from '../src/cards.ts'; import { playGame } from '../src/play.ts'; import { DEFAULT_RULES, withRule } from '../src/rules.ts'; const pool = loadCards(); // playGame with `check` throws on the first broken invariant (card conservation, RAM, placement). test('random bots play 200 games with every card without breaking a rule', () => { for (let seed = 1; seed <= 200; seed++) { const s = playGame({ pool, bots: [randomBot, randomBot], seed, check: true }); assert.ok(s.over); } }); test('greedy bots play 200 games without breaking a rule', () => { let wins = 0; for (let seed = 1; seed <= 200; seed++) { const s = playGame({ pool, bots: [() => greedyBot(), () => greedyBot()], seed, check: true }); assert.ok(s.over); if (s.endReason === 'win') wins++; } // A rare game stalls: every location defended and no attack at good odds. assert.ok(wins >= 196, `only ${wins}/200 games ended in a win`); }); test('bots play the recruit rule without breaking a rule', () => { for (const k of [3, 7]) { const rules = withRule(withRule(DEFAULT_RULES, 'recruit.on=true'), `recruit.avatarsInDeck=${k}`); for (let seed = 1; seed <= 25; seed++) { assert.ok(playGame({ pool, bots: [randomBot, randomBot], seed, rules, check: true }).over); assert.ok(playGame({ pool, bots: [() => greedyBot(), () => greedyBot()], seed, rules, check: true }).over); } } }); test('bots play the buy-Avatars rule without breaking a rule', () => { const rules = withRule(DEFAULT_RULES, 'buyAvatars.on=true'); for (let seed = 1; seed <= 25; seed++) { assert.ok(playGame({ pool, bots: [randomBot, randomBot], seed, rules, check: true }).over); assert.ok(playGame({ pool, bots: [() => greedyBot(), () => greedyBot()], seed, rules, check: true }).over); } }); test('bots play the copy-rule variants without breaking a rule', () => { for (const spec of ['copies.waitATurn=true', 'copies.needAvatar2=true', 'copies.allowed=false']) { const rules = withRule(DEFAULT_RULES, spec); for (let seed = 1; seed <= 15; seed++) { assert.ok(playGame({ pool, bots: [randomBot, randomBot], seed, rules, check: true }).over); assert.ok(playGame({ pool, bots: [() => greedyBot(), () => greedyBot()], seed, rules, check: true }).over); } } }); test('bots play with the health rule off (one-roll combats) without breaking a rule', () => { const rules = withRule(DEFAULT_RULES, 'health.on=false'); for (let seed = 1; seed <= 25; seed++) { assert.ok(playGame({ pool, bots: [randomBot, randomBot], seed, rules, check: true }).over); assert.ok(playGame({ pool, bots: [() => greedyBot(), () => greedyBot()], seed, rules, check: true }).over); } }); test('bots play the health rule without recall, and with Health 8, without breaking a rule', () => { for (const spec of ['health.recall=false', 'health.override=8']) { const rules = withRule(DEFAULT_RULES, spec); for (let seed = 1; seed <= 15; seed++) { assert.ok(playGame({ pool, bots: [randomBot, randomBot], seed, rules, check: true }).over); assert.ok(playGame({ pool, bots: [() => greedyBot(), () => greedyBot()], seed, rules, check: true }).over); } } }); test('bots play the command rule without breaking a rule', () => { const rules = withRule(DEFAULT_RULES, 'commands.on=true'); for (let seed = 1; seed <= 25; seed++) { assert.ok(playGame({ pool, bots: [randomBot, randomBot], seed, rules, check: true }).over); assert.ok(playGame({ pool, bots: [() => greedyBot(), () => greedyBot()], seed, rules, check: true }).over); } }); test('greedy beats random', () => { let wins = 0; for (let seed = 1; seed <= 100; seed++) { const s = playGame({ pool, bots: [() => greedyBot(), randomBot], seed }); if (s.winner === 0) wins++; } assert.ok(wins >= 90, `greedy won ${wins}/100`); }); test('playing cards beats ignoring them', () => { let wins = 0; const nocards = () => greedyBot({ ...DEFAULT_GREEDY, useCards: false }); for (let seed = 1; seed <= 200; seed++) { const s = playGame({ pool, bots: seed % 2 ? [() => greedyBot(), nocards] : [nocards, () => greedyBot()], seed }); if (s.winner === (seed % 2 ? 0 : 1)) wins++; } assert.ok(wins >= 116, `the card-playing bot won ${wins}/200`); });