files-tcg/app/test/moves.test.ts
2026-10-01 17:20:33 -04:00

59 lines
2.8 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import assert from 'node:assert/strict';
import { test } from 'node:test';
import { greedyBot } from '../../sim/src/bots/greedy.ts';
import { describeAction } from '../../sim/src/describe.ts';
import { actionKey } from '../../sim/src/engine.ts';
import { loadCards } from '../../sim/src/load.ts';
import { LINE_BY_ID, LOCATIONS } from '../../sim/src/map.ts';
import { makeRng, nextFloat, pick } from '../../sim/src/rng.ts';
import type { GameState, PlayerId } from '../../sim/src/types.ts';
import { Match, randomDecks, rulesFrom } from '../src/match.ts';
import { movesOf, refOfKey, type Key } from '../src/moves.ts';
const pool = loadCards();
/** Whether the screen has something to click for `key`: a hand card, a token or slot, a location, a gateway. */
function onScreen(view: GameState, me: PlayerId, key: Key, asSource: boolean): boolean {
if (key.startsWith('hand:')) return view.players[me].hand.some((id) => view.cards[id].name === key.slice(5));
if (key.startsWith('loc:')) return LOCATIONS.includes(key.slice(4));
// A gateway is drawn when it has a Firewall or is marked as a source.
if (key.startsWith('gate:')) return LINE_BY_ID.get(key.slice(5))?.gateway === true && (asSource || !!view.firewalls[key.slice(5)]);
const r = refOfKey(key);
if (r) {
const av = view.players[r.player].anomalies[r.slot - 1];
// Tokens for Anomalies in play; panel rows for slots 1–4 (and copies in play).
return av.at !== null || r.slot <= 4;
}
return false;
}
function check(m: Match): void {
const view = m.view();
const legal = m.legal();
const moves = movesOf(view, legal, m.human);
const labels = new Set(moves.map((x) => x.label));
for (const a of legal) assert.ok(labels.has(describeAction(view, a)), `no move for ${actionKey(a)}`);
for (const x of moves) {
if (x.source === 'bar') continue;
assert.ok(onScreen(view, m.human, x.source, true), `nothing to click for the source of "${x.label}" (${x.source})`);
if (x.target !== 'none') assert.ok(onScreen(view, m.human, x.target, false), `nothing to click for the target of "${x.label}" (${x.target})`);
}
}
test('every legal move can be reached by clicking: a source on screen, then a target on screen or its menu', () => {
for (let seed = 1; seed <= 60; seed++) {
const rules = seed % 10 === 0 ? ['board=grid4'] : [];
const m = new Match(pool, { seed, human: (seed % 2) as PlayerId, decks: randomDecks(pool, seed, rulesFrom(rules)), rules }, { stops: 'all', autoSkip: false });
const me = greedyBot();
const rng = makeRng(seed);
for (let n = 0; !m.over && n < 5000; n++) {
if (m.botToAct()) {
m.runBot();
continue;
}
check(m);
const legal = m.legal();
m.act(seed % 3 === 0 && nextFloat(rng) < 0.5 ? pick(rng, legal) : me.choose(m.view(), legal, m.human));
}
}
});