This commit is contained in:
Forrest 2026-09-28 01:56:25 -04:00
parent 746d218eda
commit e590baf28d
44 changed files with 8308 additions and 0 deletions

125
sim/test/avatars.test.ts Normal file
View file

@ -0,0 +1,125 @@
import assert from 'node:assert/strict';
import { avatarCores, openWindow } from '../src/engine.ts';
import { card } from './card.ts';
import { Table, type Side } from './table.ts';
/** South's Avatar 1 (`south`) on B1 attacks North's Avatar 1 (`north`) on C1. */
function scene(south: string, north: string, o: { south?: Side; north?: Side } = {}) {
const others = ['Broken Bits', 'Pixelated Picture', 'Corrupt Driver', 'Code Zero', 'Beta Version 1.0', 'Data Leaker'];
const four = (first: string) => [first, ...others.filter((n) => n !== first)].slice(0, 4);
const t = new Table({ south: { ...o.south, avatars: four(south) }, north: { ...o.north, avatars: four(north) } });
t.own(0, 'B1');
t.own(1, 'C1');
t.place(0, 1, 'B1');
t.place(1, 1, 'C1');
return t;
}
const fight = (t: Table) => t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' }).act({ type: 'end' });
const dice = (t: Table, side: 'atk' | 'def') => {
const m = t.logText().match(/Roll: \S+(?: \S+)* \[([\d,]*)\]=\d+ vs \S+(?: \S+)* \[([\d,]*)\]/)!;
return m[side === 'atk' ? 1 : 2].split(',').length;
};
const avatarCard = (t: Table, p: 0 | 1, slot: number) => t.avatar(p, slot).card;
card('Carbon NanoBots', () => {
const t = scene('Carbon NanoBots', 'Broken Bits', { south: { main: ['Additional Core'] } });
t.own(0, 'A2');
t.hardware(0, 1, 'Additional Core');
t.ram(0, 5).use(avatarCard(t, 0, 1), { to: 'A2' }).settle();
assert.equal(t.avatar(0, 5).at, 'A2', 'the copy is in play');
assert.equal(avatarCores(t.s, 0, 5, 'defend'), 3, "2 Cores + the original's Additional Core");
// The copy attacks and loses: the original keeps its Hardware.
t.place(0, 1, 'S1');
t.s.players[0].avatars[4].at = 'B1';
t.to('attack').go({ type: 'attack', slot: 5, to: 'C1' });
t.rig(1, 1, 1, 6, 6, 6, 6).end();
assert.equal(t.avatar(0, 5).at, null);
assert.equal(t.avatar(0, 1).hardware.length, 1);
});
card('Corrupt Driver', () => {
const t = scene('Corrupt Driver', 'Broken Bits', { north: { main: ['Additional Core'] } });
const hw = t.hardware(1, 1, 'Additional Core');
t.ram(0, 4);
fight(t);
t.rig(1, 1, 1, 6, 6, 6, 6);
t.use(avatarCard(t, 0, 1), { card: hw }).settle();
assert.equal(dice(t, 'def'), 4, 'the Additional Core is off');
assert.ok(t.s.cards[hw].disabledUntil > t.s.clock);
});
card('Pixelated Picture', () => {
const t = scene('Broken Bits', 'Pixelated Picture');
t.ram(1, 3);
fight(t);
assert.equal(t.decider, 1);
t.rig(6, 6, 6, 1, 1, 1);
t.use(avatarCard(t, 1, 1)).settle();
assert.equal(dice(t, 'atk'), 3, 'the attacker has -1 Core');
});
card('Beta Version 1.0', () => {
const t = scene('Beta Version 1.0', 'Broken Bits', { south: { main: ['Additional Core', 'RAM Expansion', 'Packet Accelerator'] } });
t.hardware(0, 1, 'RAM Expansion');
t.hardware(0, 1, 'Packet Accelerator');
const [ac] = t.hand(0, 'Additional Core');
t.ram(0, 4 + 5);
fight(t);
t.rig(6, 6, 6, 6, 6, 6, 6, 6, 1, 1, 1, 1);
t.use(avatarCard(t, 0, 1), { card: ac }).settle();
assert.equal(dice(t, 'atk'), 4 + 2 + 1 + 1, 'a third Hardware on a 2-port Avatar');
t.to('playAgain').end();
assert.ok(t.s.players[0].discard.includes(ac), 'gone at the end of the turn');
});
card('Code Zero', () => {
const t = scene('Broken Bits', 'Code Zero');
t.avatar(0, 1).infection = 1;
t.ram(1, 4);
fight(t);
assert.equal(t.decider, 1);
t.rig(6, 6, 6, 1, 1);
t.use(avatarCard(t, 1, 1)).settle();
assert.equal(t.avatar(0, 1).infection >= 2 || t.avatar(0, 1).at === null, true);
assert.equal(dice(t, 'atk'), 3, '2 Infection counters: -1 Core while Code Zero is in play');
});
card('Data Leaker', () => {
const t = scene('Data Leaker', 'Broken Bits', { south: { main: ['Additional Core'] }, north: { main: ['Cooling System', 'Hotfix'] } });
const [cool] = t.hand(1, 'Cooling System', 'Hotfix');
t.top(0, 'Additional Core');
t.ram(0, 3);
// Breach first: take an undefended enemy location this turn.
t.own(1, 'C2');
t.go({ type: 'advance', slot: 1, to: 'C2' }).to('attack').end();
assert.equal(t.s.locations.C2.owner, 0);
t.use(avatarCard(t, 0, 1));
assert.equal(t.s.choice?.kind, 'Data Leaker:discard');
t.choose('Cooling System').settle();
assert.ok(t.s.players[1].discard.includes(cool));
assert.equal(t.s.players[1].ram, 0, 'no RAM for the discard');
assert.equal(t.s.players[0].hand.length, 1, 'it breached this turn, so it draws');
});
card('Backup Machine', () => {
const t = scene('Backup Machine', 'Broken Bits', { south: { main: ['Additional Core'] } });
const ac = t.take(0, 'Additional Core');
t.s.players[0].discard.push(ac);
t.ram(0, 5 + 3).use(avatarCard(t, 0, 1), { card: ac });
assert.equal(t.s.choice?.kind, 'Backup Machine:install');
t.choose('Install').settle();
assert.deepEqual(t.avatar(0, 1).hardware, [ac]);
assert.equal(t.s.players[0].ram, 0, '5 for the Ability, 5 - 2 for the Hardware');
});
card('Broken Bits', () => {
const t = scene('Broken Bits', 'Broken Bits');
t.ram(0, 4);
t.rig(1, 1, 2, 2, 3, 3, 3, 3);
fight(t);
assert.equal(openWindow(t.s)?.kind, 'postRoll', 'nothing to do before the roll');
t.rig(6, 6, 6, 6);
t.use(avatarCard(t, 0, 1), { k: 2 }).settle();
assert.match(t.logText(), /Bit Flip rerolls 4 dice/);
assert.equal(t.s.locations.C1.owner, 0);
});

6
sim/test/card.ts Normal file
View file

@ -0,0 +1,6 @@
import { test } from 'node:test';
/** A test for one card. test/coverage.test.ts checks that every card in cards.csv has one. */
export const card = (name: string, fn: () => void): void => {
test(`card: ${name}`, fn);
};

53
sim/test/cards.test.ts Normal file
View file

@ -0,0 +1,53 @@
import assert from 'node:assert/strict';
import { readdirSync, readFileSync } from 'node:fs';
import { test } from 'node:test';
import { loadCards } from '../src/cards.ts';
import { randomDeck, validateDeck } from '../src/deck.ts';
import { makeRng } from '../src/rng.ts';
import { DEFAULT_RULES } from '../src/rules.ts';
import { staleScripts } from '../src/scripts.ts';
const pool = loadCards();
test('cards.csv loads every playable card', () => {
const count = (kind: string) => pool.all.filter((c) => c.kind === kind).length;
assert.equal(count('avatar'), 8);
assert.equal(count('server'), 13);
assert.equal(count('program'), 28);
assert.equal(count('hardware'), 16);
assert.equal(count('execution'), 18);
assert.equal(pool.byName.get('Broken Bits')?.cores, 4);
assert.equal(pool.byName.get('Carbon NanoBots')?.slots, 4);
assert.equal(pool.byName.get('Quantum Coprocessor')?.ram, 9);
});
test('every card has a script written for its current text', () => {
assert.deepEqual(staleScripts(pool), []);
});
test('every card has its own test', () => {
const dir = new URL('.', import.meta.url);
const tested = new Set<string>();
for (const file of readdirSync(dir).filter((f) => f.endsWith('.test.ts'))) {
for (const m of readFileSync(new URL(file, dir), 'utf8').matchAll(/\bcard\('([^']+)'/g)) tested.add(m[1]);
}
assert.deepEqual(pool.all.map((c) => c.name).filter((n) => !tested.has(n)), []);
});
test('random decks are legal', () => {
const rng = makeRng(3);
for (let i = 0; i < 50; i++) {
assert.deepEqual(validateDeck(pool, randomDeck(pool, rng, DEFAULT_RULES), DEFAULT_RULES), []);
}
});
test('deck validation catches size, kind and copy problems', () => {
const deck = randomDeck(pool, makeRng(1), DEFAULT_RULES);
deck.main[0] = deck.main[1] = deck.main[2] = 'Execute: Fork';
deck.servers.pop();
deck.avatars[0] = 'Cloud Compute';
const errors = validateDeck(pool, deck, DEFAULT_RULES).join('\n');
assert.match(errors, /Servers: 19 cards/);
assert.match(errors, /"Cloud Compute" is a server/);
assert.match(errors, /copies of "Execute: Fork"/);
});

194
sim/test/engine.test.ts Normal file
View file

@ -0,0 +1,194 @@
import assert from 'node:assert/strict';
import { test } from 'node:test';
import { loadCards } from '../src/cards.ts';
import { randomDeck } from '../src/deck.ts';
import { advanceTargets, avatarCores, createGame, legalActions, viewFor } from '../src/engine.ts';
import { checkInvariants } from '../src/invariants.ts';
import { makeRng } from '../src/rng.ts';
import { DEFAULT_RULES } from '../src/rules.ts';
import { Table } from './table.ts';
const pool = loadCards();
test('setup follows rules.md', () => {
const rng = makeRng(5);
const decks = [randomDeck(pool, rng, DEFAULT_RULES), randomDeck(pool, rng, DEFAULT_RULES)] as const;
const s = createGame({ pool, decks: [decks[0], decks[1]], seed: 3 });
for (const p of [0, 1] as const) {
const pl = s.players[p];
assert.ok(pl.hand.length >= 7, 'opening hand');
assert.equal(pl.serverPile.length, 19);
assert.ok(pl.ram >= 4);
assert.deepEqual(pl.avatars.slice(0, 4).map((a) => a.faceUp), [true, false, false, false]);
assert.ok(pl.avatars.slice(4).every((a) => !a.faceUp && a.at === null), 'no copies yet');
assert.equal(s.locations[p === 0 ? 'S1' : 'S2'].owner, p);
}
assert.equal(s.turn, 1);
assert.equal(s.active, s.firstPlayer);
assert.deepEqual(checkInvariants(s), []);
});
test('the first player skips the draw; the second player draws', () => {
const t = new Table({ south: { main: ['Additional Core'] }, north: { main: ['Additional Core'] } });
assert.equal(t.s.players[0].hand.length, 0);
t.to('playAgain').end();
assert.equal(t.s.active, 1);
assert.equal(t.s.players[1].hand.length, 1);
});
test('advancing captures an unowned location in step 5, pays its level and unlocks an Avatar', () => {
const t = new Table();
t.ram(0, 4);
assert.deepEqual(advanceTargets(t.s, 0), ['A2']);
t.go({ type: 'place', slot: 1, to: 'S1' }).go({ type: 'advance', slot: 1, to: 'A2' });
t.to('attack');
assert.equal(t.s.locations.A2.owner, null);
t.end();
assert.equal(t.s.locations.A2.owner, 0);
assert.equal(t.avatar(0, 1).at, 'A2');
assert.equal(t.s.players[0].ram, 8, '4 RAM + 4 for a Lv 2 capture');
assert.equal(t.avatar(0, 2).faceUp, true, 'Lv 2 unlocks Avatar 2');
assert.equal(t.s.step, 'playAgain');
});
test('placement closes once an advance is declared', () => {
const t = new Table();
t.go({ type: 'place', slot: 1, to: 'S1' }).go({ type: 'advance', slot: 1, to: 'A2' });
assert.ok(!t.legal().some((a) => a.type === 'place'));
});
test('a Firewall blocks movement until someone pays to clear it', () => {
const t = new Table();
t.own(0, 'A2');
t.s.firewalls['A1-A2'] = 2;
t.ram(0, 4).go({ type: 'place', slot: 1, to: 'S1' });
assert.ok(!advanceTargets(t.s, 0).includes('A1'));
assert.ok(!t.can({ type: 'advance', slot: 1, to: 'A1' }));
t.go({ type: 'clearFirewall', line: 'A1-A2' });
assert.equal(t.s.players[0].ram, 0, 'clearing a Lv 2 Firewall costs 4');
assert.ok(advanceTargets(t.s, 0).includes('A1'));
});
test('Firewalls go only on gateways, and upgrades cost the full price of the new level', () => {
const t = new Table({ south: { servers: ['Upgraded Hardware'] } });
t.own(0, 'A2', 'Upgraded Hardware');
t.ram(0, 20).to('play');
assert.ok(!t.can({ type: 'firewall', line: 'S1-A2', level: 1 }));
t.go({ type: 'firewall', line: 'A1-A2', level: 1 }).go({ type: 'firewall', line: 'A1-A2', level: 2 });
assert.equal(t.s.players[0].ram, 20 - 2 - 4);
assert.equal(t.s.firewalls['A1-A2'], 2);
});
test('combat: the loser goes back to its slot and loses its Hardware; the winner captures', () => {
for (const attackerWins of [true, false]) {
const t = new Table({ north: { main: ['Additional Core'] } });
t.own(0, 'B1');
t.own(1, 'C1');
t.place(0, 1, 'B1');
t.place(1, 1, 'C1');
const hw = t.hardware(1, 1, 'Additional Core');
t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' });
assert.equal(avatarCores(t.s, 0, 1, 'attack'), 4);
assert.equal(avatarCores(t.s, 1, 1, 'defend'), 5, '4 Cores + Additional Core');
t.rig(...(attackerWins ? [6, 6, 6, 6, 1, 1, 1, 1, 1] : [1, 1, 1, 1, 6, 6, 6, 6, 6]));
t.end();
if (attackerWins) {
assert.equal(t.s.locations.C1.owner, 0);
assert.equal(t.avatar(0, 1).at, 'C1');
assert.equal(t.avatar(1, 1).at, null);
assert.deepEqual(t.avatar(1, 1).hardware, []);
assert.ok(t.s.players[1].discard.includes(hw));
assert.equal(t.s.stats.attacksWon[0], 1);
} else {
assert.equal(t.avatar(0, 1).at, null);
assert.equal(t.avatar(1, 1).at, 'C1');
assert.deepEqual(t.avatar(1, 1).hardware, [hw]);
}
assert.equal(t.s.stats.attacks[0], 1);
}
});
test('ties reroll every die', () => {
const t = new Table();
t.own(0, 'B1');
t.own(1, 'C1');
t.place(0, 1, 'B1');
t.place(1, 1, 'C1');
t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' });
t.rig(3, 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 1, 1, 1, 1);
t.end();
assert.equal(t.s.locations.C1.owner, 0);
assert.match(t.logText(), /after 1 tie reroll/);
});
test('an attacker can only hit a defended location next to where it stands', () => {
const t = new Table();
t.own(0, 'B1');
t.own(1, 'C1');
t.own(1, 'C3');
t.place(1, 1, 'C1');
t.place(1, 2, 'C3');
t.place(0, 1, 'B1');
t.to('attack');
assert.deepEqual(
t.legal().filter((a) => a.type === 'attack'),
[{ type: 'attack', slot: 1, to: 'C1' }],
);
});
test('reaching the location target wins immediately', () => {
const t = new Table();
for (const loc of ['A1', 'A3', 'B1', 'B2', 'C1', 'C2', 'C3', 'D1', 'D2']) t.own(0, loc);
t.place(0, 1, 'S1');
t.go({ type: 'advance', slot: 1, to: 'A2' }).to('attack').end();
assert.equal(t.s.over, true);
assert.equal(t.s.winner, 0);
assert.equal(t.s.endReason, 'win');
assert.deepEqual(legalActions(t.s), []);
});
test('Salvage pays full printed RAM, from hand only', () => {
const t = new Table({ south: { main: ['Cooling System'] } });
const [card] = t.hand(0, 'Cooling System');
t.go({ type: 'salvage', card });
assert.equal(t.s.players[0].ram, 6);
assert.ok(t.s.players[0].discard.includes(card));
});
test('views hide the opponent hand, both decks, face-down Avatars and the RNG', () => {
const t = new Table({ south: { main: ['Additional Core'] }, north: { main: ['Additional Core'] } });
t.hand(0, 'Additional Core');
t.hand(1, 'Additional Core');
const v = viewFor(t.s, 0);
assert.deepEqual(v.players[1].hand, [-1]);
assert.deepEqual(v.players[0].hand, t.s.players[0].hand);
assert.ok(v.players[0].deck.every((id) => id === -1));
assert.ok(v.players[1].serverPile.every((id) => id === -1));
assert.equal(v.players[0].avatars[1].card, -1);
assert.equal(v.players[1].avatars[1].card, -1);
assert.equal(v.rng.s, 0);
assert.notEqual(t.s.rng.s, 0);
});
test('the same seed replays the same game', () => {
const rng = makeRng(9);
const decks = [randomDeck(pool, rng, DEFAULT_RULES), randomDeck(pool, rng, DEFAULT_RULES)] as const;
const a = createGame({ pool, decks: [decks[0], decks[1]], seed: 42 });
const b = createGame({ pool, decks: [decks[0], decks[1]], seed: 42 });
assert.deepEqual(a, b);
});
test('an opponent with a response gets a window after a declaration; without one it passes itself', () => {
const t = new Table({ north: { main: ['Execute: Ping Sweep'] } });
t.own(0, 'A2');
t.place(0, 1, 'S1');
t.place(0, 2, 'A2');
t.act({ type: 'advance', slot: 1, to: 'A1' });
assert.equal(t.decider, 0, 'North has no RAM, so no window');
t.hand(1, 'Execute: Ping Sweep');
t.ram(1, 4);
t.act({ type: 'advance', slot: 2, to: 'A3' });
assert.equal(t.decider, 1, 'North may respond');
t.act({ type: 'pass' });
assert.equal(t.decider, 0);
});

239
sim/test/executions.test.ts Normal file
View file

@ -0,0 +1,239 @@
import assert from 'node:assert/strict';
import { avatarCores, execBlocked, executionActions, openWindow } from '../src/engine.ts';
import { card } from './card.ts';
import { Table, type Side } from './table.ts';
function scene(o: { south?: Side; north?: Side; first?: 0 | 1 } = {}) {
const t = new Table(o);
t.own(0, 'B1');
t.own(1, 'C1');
t.place(0, 1, 'B1');
t.place(1, 1, 'C1');
return t;
}
/** South attacks C1 and the game stops at the first response window in the combat. */
const fight = (t: Table) => t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' }).act({ type: 'end' });
const attackerDice = (t: Table) => t.logText().match(/Roll: Broken Bits \[([\d,]*)\]/)![1].split(',').length;
const defenderDice = (t: Table) => t.logText().match(/Roll: .* vs Broken Bits \[([\d,]*)\]/)![1].split(',').length;
card('Execute: Disconnect', () => {
// A defender slips away before the roll: the attacker walks in, the defender keeps its Hardware.
const t = scene({ north: { main: ['Execute: Disconnect', 'Additional Core'] } });
t.own(1, 'D1');
const hw = t.hardware(1, 1, 'Additional Core');
t.hand(1, 'Execute: Disconnect');
t.ram(1, 4);
fight(t);
assert.equal(t.decider, 1);
t.play(1, 'Execute: Disconnect', 'C1', { ref: { player: 1, slot: 1 }, to: 'D1' }).settle();
assert.equal(t.avatar(1, 1).at, 'D1');
assert.deepEqual(t.avatar(1, 1).hardware, [hw]);
assert.equal(t.s.locations.C1.owner, 0, 'the attacker takes the empty location');
});
card('Execute: Disconnect', () => {
// An attacker retreats after a bad roll and isn't defeated.
const t = scene({ south: { main: ['Execute: Disconnect'] } });
t.hand(0, 'Execute: Disconnect');
t.ram(0, 4);
t.rig(1, 1, 1, 1, 6, 6, 6, 6);
fight(t);
assert.equal(openWindow(t.s)?.kind, 'preRoll');
t.act({ type: 'pass' });
assert.equal(openWindow(t.s)?.kind, 'postRoll');
t.play(0, 'Execute: Disconnect', 'C1', { ref: { player: 0, slot: 1 }, to: 'B1' }).settle();
assert.equal(t.avatar(0, 1).at, 'B1');
assert.equal(t.s.locations.C1.owner, 1);
assert.match(t.logText(), /combat at C1 ends/);
});
card('Execute: Sabotage', () => {
const t = scene({ south: { main: ['Execute: Sabotage'] }, north: { main: ['Additional Core'] } });
const hw = t.hardware(1, 1, 'Additional Core');
t.hand(0, 'Execute: Sabotage');
t.ram(0, 7);
fight(t);
t.rig(6, 6, 6, 6, 1, 1, 1, 1);
t.play(0, 'Execute: Sabotage', 'C1', { card: hw, mode: 'destroy' }).settle();
assert.equal(defenderDice(t), 4);
assert.ok(t.s.players[1].discard.includes(hw));
});
card('Execute: Kill Process', () => {
const t = scene({ south: { main: ['Execute: Kill Process'] }, north: { main: ['Stack Overflow'] } });
const so = t.program(1, 'C1', 'Stack Overflow');
assert.equal(avatarCores(t.s, 1, 1, 'defend'), 5);
t.hand(0, 'Execute: Kill Process');
t.ram(0, 5).play(0, 'Execute: Kill Process', 'C1', { card: so }).settle();
assert.equal(avatarCores(t.s, 1, 1, 'defend'), 4);
t.to('playAgain').end();
assert.equal(avatarCores(t.s, 1, 1, 'defend'), 5, "back on at its controller's next turn");
});
card('Execute: Force Reboot', () => {
const t = scene({ south: { main: ['Execute: Force Reboot', 'Additional Core', 'Dedicated GPU', 'Execute: Ping Sweep'] }, north: { main: ['Memory Leak'] } });
t.program(1, 'C1', 'Memory Leak');
t.hardware(0, 1, 'Additional Core');
t.hardware(0, 1, 'Dedicated GPU');
t.hand(0, 'Execute: Force Reboot', 'Execute: Ping Sweep');
const fromB1 = () => executionActions(t.s, 0, 'any').some((a) => a.type === 'play' && a.from === 'B1');
assert.ok(fromB1(), 'B1 can perform Executions');
t.ram(0, 5);
fight(t);
t.rig(6, 6, 6, 6, 6, 1, 1, 1, 1);
t.play(0, 'Execute: Force Reboot', 'C1', { ref: { player: 0, slot: 1 } }).settle();
assert.equal(attackerDice(t), 5, "Memory Leak's penalty is gone");
assert.ok(execBlocked(t.s, 'B1'), 'the Server it stands on is blocked for the turn');
assert.ok(!fromB1());
});
card('Execute: Fork', () => {
const t = scene({ south: { main: ['Execute: Fork', 'Stack Overflow'] } });
const so = t.program(0, 'B1', 'Stack Overflow');
t.hand(0, 'Execute: Fork');
t.ram(0, 7).play(0, 'Execute: Fork', 'B1', { card: so }).settle();
assert.equal(avatarCores(t.s, 0, 1, 'defend'), 6, 'two Stack Overflow effects, +1 each');
t.to('playAgain').end();
assert.equal(avatarCores(t.s, 0, 1, 'defend'), 5, 'until end of turn');
});
card('Execute: Ping Sweep', () => {
const t = new Table({ south: { main: ['Execute: Ping Sweep'] } });
t.hand(0, 'Execute: Ping Sweep');
t.ram(0, 4).play(0, 'Execute: Ping Sweep', null).settle();
assert.equal(t.s.players[0].hand.length, 2);
});
card('Execute: Trace Route', () => {
const t = scene({ south: { main: ['Execute: Trace Route'] } });
t.hand(0, 'Execute: Trace Route');
t.rig(6, 6, 6, 6, 6, 1, 1, 1, 1);
t.ram(0, 5).to('play').play(0, 'Execute: Trace Route', 'C1', { ref: { player: 0, slot: 1 } }).settle();
assert.equal(attackerDice(t), 5, 'attacks right away with +1 Core');
assert.equal(t.s.locations.C1.owner, 0);
assert.equal(t.avatar(0, 1).at, 'C1');
});
card('Execute: Override', () => {
const t = scene({ south: { main: ['Execute: Override'] }, north: { main: ['Stack Overflow'] } });
const so = t.program(1, 'C1', 'Stack Overflow');
t.hand(0, 'Execute: Override');
t.ram(0, 5).play(0, 'Execute: Override', 'C1', { card: so }).settle();
assert.equal(avatarCores(t.s, 1, 1, 'defend'), 4);
t.to('playAgain').end();
assert.equal(avatarCores(t.s, 1, 1, 'defend'), 5, 'until end of turn');
});
card('Execute: Memory Dump', () => {
const t = new Table({ south: { main: ['Execute: Memory Dump', 'Execute: Memory Dump'] } });
t.hand(0, 'Execute: Memory Dump', 'Execute: Memory Dump');
t.ram(0, 8).play(0, 'Execute: Memory Dump', 'C1');
assert.equal(t.s.choice?.kind, 'Execute: Memory Dump:discard', "not South's: draw 2, discard 1");
t.choose('Network Interface Upgrade').settle();
assert.equal(t.s.players[0].hand.length, 1 + 2 - 1);
t.play(0, 'Execute: Memory Dump', 'S1').settle();
assert.equal(t.s.choice, null);
assert.equal(t.s.players[0].hand.length, 1 + 3, "South's own: draw 3, no discard");
});
card('Execute: Rollback', () => {
const t = new Table({ first: 1, north: { main: ['Stack Overflow'] }, south: { main: ['Execute: Rollback'] } });
t.own(1, 'C1');
const [so] = t.hand(1, 'Stack Overflow');
t.hand(0, 'Execute: Rollback');
t.ram(0, 4);
t.ram(1, 7).to('play').act({ type: 'installProgram', card: so, at: 'C1' });
assert.equal(t.decider, 0, 'South may respond to the install');
t.play(0, 'Execute: Rollback', 'C1', { card: so }).settle();
assert.deepEqual(t.s.players[1].hand, [so]);
assert.deepEqual(t.s.locations.C1.programs, []);
});
card('Execute: Root Access', () => {
const t = new Table({ south: { main: ['Execute: Root Access'] } });
t.hand(0, 'Execute: Root Access');
t.ram(0, 4).play(0, 'Execute: Root Access', 'C1').settle();
assert.ok(t.s.locations.C1.rootAccess[0] > t.s.clock);
t.to('playAgain').end();
assert.ok(t.s.locations.C1.rootAccess[0] <= t.s.clock, 'until end of turn');
});
card('Execute: Packet Flood', () => {
const t = new Table({ north: { main: ['Execute: Packet Flood'] } });
t.own(0, 'B1');
t.place(0, 1, 'B1');
t.hand(1, 'Execute: Packet Flood');
t.ram(1, 5);
t.act({ type: 'advance', slot: 1, to: 'C2' });
assert.equal(t.decider, 1);
t.play(1, 'Execute: Packet Flood', 'B1', { to: 'B1' }).settle();
t.to('attack').end();
assert.equal(t.s.locations.C2.owner, null, "the advance fails: it can't move");
assert.equal(t.avatar(0, 1).at, 'B1');
});
card('Execute: Emergency Shutdown', () => {
const t = scene({ north: { main: ['Execute: Emergency Shutdown'] } });
t.hand(1, 'Execute: Emergency Shutdown');
t.ram(1, 6);
t.rig(6, 6, 6, 6, 1, 1, 1, 1);
fight(t);
assert.equal(openWindow(t.s)?.kind, 'preRoll');
t.act({ type: 'pass' });
assert.equal(openWindow(t.s)?.kind, 'postRoll', 'North waits to see the dice');
t.play(1, 'Execute: Emergency Shutdown', 'C1').settle();
assert.equal(t.s.locations.C1.owner, 1);
assert.equal(t.avatar(0, 1).at, 'B1');
assert.equal(t.avatar(1, 1).at, 'C1');
assert.ok(t.avatar(0, 1).disabledUntil > t.s.clock);
});
card('Execute: Process Clone', () => {
const t = scene({ south: { main: ['Execute: Process Clone', 'Stack Overflow'] } });
const so = t.program(0, 'B1', 'Stack Overflow');
t.hand(0, 'Execute: Process Clone');
t.ram(0, 7).play(0, 'Execute: Process Clone', 'B1', { card: so }).settle();
assert.equal(avatarCores(t.s, 0, 1, 'defend'), 4 + 2 + 2, 'two copies, each counting 2 Programs');
});
card('Execute: Defragment', () => {
const t = scene({ south: { main: ['Execute: Defragment', 'Stack Overflow'] } });
const so = t.program(0, 'B1', 'Stack Overflow');
t.s.cards[so].disabledUntil = 1e9;
t.hand(0, 'Execute: Defragment');
t.ram(0, 4).play(0, 'Execute: Defragment', 'B1', { card: so }).settle();
assert.equal(t.s.cards[so].disabledUntil, 0);
assert.equal(avatarCores(t.s, 0, 1, 'defend'), 5);
});
card('Execute: Remote Access', () => {
const t = scene({ first: 1, north: { main: ['Execute: Remote Access', 'Stack Overflow'] } });
t.own(1, 'D1');
const so = t.program(1, 'D1', 'Stack Overflow');
t.hand(1, 'Execute: Remote Access');
t.ram(1, 6).play(1, 'Execute: Remote Access', 'C1', { card: so }).settle();
assert.equal(avatarCores(t.s, 1, 1, 'defend'), 5, 'Stack Overflow runs on C1 too');
});
card('Execute: Purge', () => {
const t = scene({ south: { main: ['Execute: Purge', 'Additional Core'] } });
const hw = t.hardware(0, 1, 'Additional Core');
t.s.cards[hw].disabledUntil = 1e9;
Object.assign(t.avatar(0, 1), { infection: 2, permanentInfection: 1 });
t.hand(0, 'Execute: Purge');
t.ram(0, 4).play(0, 'Execute: Purge', 'B1', { to: 'B1' }).settle();
assert.equal(t.avatar(0, 1).infection + t.avatar(0, 1).permanentInfection, 0);
assert.equal(t.s.cards[hw].disabledUntil, 0);
});
card('Execute: Hard Reset', () => {
const t = new Table({ south: { main: ['Execute: Hard Reset', 'Stack Overflow', 'Hotfix'] } });
t.own(0, 'B1');
const so = t.program(0, 'B1', 'Stack Overflow');
const hf = t.program(0, 'B1', 'Hotfix');
t.hand(0, 'Execute: Hard Reset');
t.ram(0, 6).play(0, 'Execute: Hard Reset', 'B1', { cards: [so, hf] }).settle();
assert.deepEqual(t.s.locations.B1.programs, []);
assert.deepEqual(t.s.players[0].hand.sort(), [so, hf].sort());
assert.equal(t.s.players[0].ram, 2);
});

42
sim/test/fuzz.test.ts Normal file
View file

@ -0,0 +1,42 @@
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';
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', () => {
for (let seed = 1; seed <= 200; seed++) {
const s = playGame({ pool, bots: [() => greedyBot(), () => greedyBot()], seed, check: true });
assert.equal(s.endReason, 'win');
}
});
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`);
});

214
sim/test/hardware.test.ts Normal file
View file

@ -0,0 +1,214 @@
import assert from 'node:assert/strict';
import { addBugs } from '../src/cards/util.ts';
import { avatarCores, canWear, executionActions, executionCost, programCost } from '../src/engine.ts';
import { portsFree } from '../src/query.ts';
import { card } from './card.ts';
import { Table, type Side } from './table.ts';
function scene(o: { south?: Side; north?: Side; first?: 0 | 1 } = {}) {
const t = new Table(o);
t.own(0, 'B1');
t.own(1, 'C1');
t.place(0, 1, 'B1');
t.place(1, 1, 'C1');
return t;
}
const attackC1 = (t: Table) => t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' });
const attackerDice = (t: Table) => t.logText().match(/Roll: Broken Bits \[([\d,]*)\]/)![1].split(',').length;
card('RAM Expansion', () => {
const t = scene({ south: { main: ['RAM Expansion'] } });
t.hardware(0, 1, 'RAM Expansion');
assert.equal(avatarCores(t.s, 0, 1, 'defend'), 6);
});
card('Additional Core', () => {
const t = scene({ south: { main: ['Additional Core'] } });
t.hardware(0, 1, 'Additional Core');
assert.equal(avatarCores(t.s, 0, 1, 'defend'), 5);
});
card('Overclocked CPU', () => {
const t = scene({ south: { main: ['Overclocked CPU', 'Stack Overflow'] } });
t.hardware(0, 1, 'Overclocked CPU');
const so = t.program(0, 'B1', 'Stack Overflow');
assert.equal(avatarCores(t.s, 0, 1, 'attack'), 6, '+2 in combat');
attackC1(t);
t.rig(6, 6, 6, 6, 6, 6, 1, 1, 1, 1).end();
assert.equal(attackerDice(t), 6);
assert.ok(t.s.cards[so].disabledUntil > t.s.clock, 'a Program where it fought from is disabled');
t.to('playAgain').end();
assert.ok(t.s.cards[so].disabledUntil > t.s.clock, 'through the next turn');
t.to('playAgain').end();
assert.ok(t.s.cards[so].disabledUntil <= t.s.clock, 'back after 2 turns');
});
card('Solid-State Cache', () => {
const t = new Table({ south: { main: ['Solid-State Cache', 'Stack Overflow', 'Hotfix'] } });
t.own(0, 'B1');
t.place(0, 1, 'B1');
t.hardware(0, 1, 'Solid-State Cache');
const [so, hf] = t.hand(0, 'Stack Overflow', 'Hotfix');
assert.equal(programCost(t.s, 0, so, 'B1'), 4);
assert.equal(programCost(t.s, 0, so, 'S1'), 7, 'only on the occupied Server');
t.ram(0, 4).to('play').go({ type: 'installProgram', card: so, at: 'B1' });
assert.equal(programCost(t.s, 0, hf, 'B1'), 7, 'first each turn');
});
card('Redundant Power Supply', () => {
const t = scene({ south: { main: ['Redundant Power Supply', 'Additional Core'] } });
const rps = t.hardware(0, 1, 'Redundant Power Supply');
const ac = t.hardware(0, 1, 'Additional Core');
attackC1(t);
t.rig(1, 1, 1, 1, 1, 6, 6, 6, 6).end();
assert.equal(t.avatar(0, 1).at, null);
assert.deepEqual(t.avatar(0, 1).hardware, [rps, ac], 'kept, in its slot');
// The second defeat loses it.
t.to('playAgain').end().to('playAgain').end();
t.go({ type: 'place', slot: 1, to: 'B1' });
attackC1(t);
t.rig(1, 1, 1, 1, 1, 6, 6, 6, 6).end();
assert.deepEqual(t.avatar(0, 1).hardware, []);
});
card('Network Interface Upgrade', () => {
const t = new Table({ south: { avatars: ['Beta Version 1.0', 'Broken Bits', 'Code Zero', 'Pixelated Picture'], main: ['Additional Core', 'RAM Expansion', 'Network Interface Upgrade', 'Packet Accelerator'] } });
const beta = { player: 0 as const, slot: 1 };
t.place(0, 1, 'S1');
t.hardware(0, 1, 'Additional Core');
t.hardware(0, 1, 'RAM Expansion');
const [niu, pa] = t.hand(0, 'Network Interface Upgrade', 'Packet Accelerator');
assert.equal(portsFree(t.s, beta), 0);
assert.ok(!canWear(t.s, pa, beta));
assert.ok(canWear(t.s, niu, beta), 'fits with both ports full');
t.ram(0, 4).to('play').go({ type: 'installHardware', card: niu, slot: 1 });
assert.equal(portsFree(t.s, beta), 1, 'and adds a port');
assert.ok(canWear(t.s, pa, beta));
});
card('Dedicated GPU', () => {
const t = new Table({ south: { main: ['Dedicated GPU', 'Execute: Ping Sweep'] } });
t.place(0, 1, 'S1');
t.hardware(0, 1, 'Dedicated GPU');
t.hand(0, 'Execute: Ping Sweep');
t.ram(0, 4).play(0, 'Execute: Ping Sweep', null, {}, 'S1').settle();
assert.equal(t.s.players[0].hand.length, 3, 'performed by the Server it stands on: draws 2 + 1');
});
card('ECC Memory', () => {
const t = scene({ south: { main: ['ECC Memory', 'Additional Core'] }, north: { main: ['Memory Leak', 'Stock Rice'] } });
t.hardware(0, 1, 'ECC Memory');
t.hardware(0, 1, 'Additional Core');
t.program(1, 'C1', 'Memory Leak');
t.program(1, 'C1', 'Stock Rice');
attackC1(t);
t.rig(6, 6, 6, 6, 6, 1, 1, 1, 1).end();
assert.equal(attackerDice(t), 5, "neither Bug Program touches it");
assert.equal(addBugs(t.s, { player: 0, slot: 1 }, 1, 'a Bug card', 1), false, "and opponents can't give it Bug counters");
assert.equal(t.avatar(0, 1).bugs, 0);
assert.equal(addBugs(t.s, { player: 1, slot: 1 }, 1, 'a Bug card', 0), true, 'Avatars without it can get them');
});
card('Cooling System', () => {
const t = new Table({ south: { main: ['Cooling System', 'Solid-State Cache', 'Stack Overflow', 'Hotfix', 'Compatibility Layer'] } });
t.own(0, 'B1');
t.place(0, 1, 'B1');
t.hardware(0, 1, 'Cooling System');
t.hardware(0, 1, 'Solid-State Cache');
const [so, hf] = t.hand(0, 'Stack Overflow', 'Hotfix');
t.ram(0, 20).to('play');
t.go({ type: 'installProgram', card: so, at: 'B1' });
assert.equal(programCost(t.s, 0, hf, 'B1'), 4, 'Solid-State Cache works twice');
t.go({ type: 'installProgram', card: hf, at: 'B1' });
assert.equal(t.s.players[0].ram, 20 - 4 - 4);
});
card('RAID Array', () => {
const t = scene({ south: { main: ['RAID Array', 'Additional Core'] } });
t.hardware(0, 1, 'RAID Array');
t.hardware(0, 1, 'Additional Core');
t.ram(0, 5);
attackC1(t);
t.rig(1, 1, 1, 1, 1, 6, 6, 6, 6).end();
assert.equal(t.s.choice?.kind, 'RAID Array:keep');
t.choose('Keep').settle();
assert.equal(t.avatar(0, 1).hardware.length, 2);
assert.equal(t.s.players[0].ram, 0, 'half of 5 + 5');
});
card('Packet Accelerator', () => {
const t = scene({ south: { main: ['Packet Accelerator'] } });
t.hardware(0, 1, 'Packet Accelerator');
assert.equal(avatarCores(t.s, 0, 1, 'attack'), 5);
assert.equal(avatarCores(t.s, 0, 1, 'defend'), 4);
});
card('Firewall Appliance', () => {
const t = scene({ south: { main: ['Execute: Sabotage'] }, north: { main: ['Firewall Appliance'] } });
const fa = t.hardware(1, 1, 'Firewall Appliance');
const [sab] = t.hand(0, 'Execute: Sabotage');
t.ram(0, 10);
attackC1(t);
t.act({ type: 'end' });
assert.equal(executionCost(t.s, 0, sab, 'C1', { card: fa, mode: 'destroy' }), 10, '+3 for the first hostile Execution');
t.play(0, 'Execute: Sabotage', 'C1', { card: fa, mode: 'destroy' });
assert.equal(t.s.players[0].ram, 0);
});
card('Hardware Sandbox', () => {
const t = new Table({ first: 1, north: { main: ['Hardware Sandbox', 'Stack Overflow', 'Hotfix'] }, south: { main: ['Execute: Kill Process', 'Execute: Root Access'] } });
t.own(1, 'C1');
t.place(1, 1, 'C1');
const so = t.program(1, 'C1', 'Stack Overflow');
const hf = t.program(1, 'C1', 'Hotfix');
const [sandbox] = t.hand(1, 'Hardware Sandbox');
t.ram(1, 7).to('play').act({ type: 'installHardware', card: sandbox, slot: 1 });
t.choose('Stack Overflow').settle();
t.to('playAgain').end();
t.hand(0, 'Execute: Kill Process', 'Execute: Root Access');
t.ram(0, 9);
const kills = () => executionActions(t.s, 0, 'now').filter((a) => a.type === 'play' && t.s.cards[a.card].name === 'Execute: Kill Process').map((a) => a.type === 'play' && a.args.card);
assert.deepEqual(kills(), [hf], 'Stack Overflow is protected');
t.play(0, 'Execute: Root Access', 'C1').settle();
assert.deepEqual(kills().sort(), [so, hf].sort(), 'Root Access lifts it');
});
card('Diagnostic Module', () => {
const t = new Table({ first: 1, south: { main: ['Diagnostic Module', 'Additional Core', 'Cooling System'] } });
t.place(0, 1, 'S1');
t.hardware(0, 1, 'Diagnostic Module');
const [a, b] = t.top(0, 'Additional Core', 'Cooling System');
t.to('playAgain').end();
assert.equal(t.s.choice?.kind, 'Diagnostic Module:bottom');
t.choose('bottom').settle();
assert.deepEqual(t.s.players[0].hand, [b]);
assert.equal(t.s.players[0].deck.at(-1), a);
});
card('Hot-Swap Bay', () => {
const t = new Table({ south: { main: ['Hot-Swap Bay', 'Additional Core', 'Overclocked CPU'] } });
t.place(0, 1, 'S1');
const hsb = t.hardware(0, 1, 'Hot-Swap Bay');
const ac = t.hardware(0, 1, 'Additional Core');
const [oc] = t.hand(0, 'Overclocked CPU');
t.ram(0, 1).use(hsb, { card: ac, cards: [oc] }).settle();
assert.deepEqual(t.avatar(0, 1).hardware, [hsb, oc]);
assert.deepEqual(t.s.players[0].hand, [ac]);
assert.equal(t.s.players[0].ram, 0, 'pays the 1 RAM difference');
});
card('Quantum Coprocessor', () => {
const t = new Table({ south: { main: ['Quantum Coprocessor', 'Execute: Kill Process'] }, north: { main: ['Stack Overflow', 'Hotfix'] } });
t.own(1, 'C1');
const so = t.program(1, 'C1', 'Stack Overflow');
const hf = t.program(1, 'C1', 'Hotfix');
t.place(0, 1, 'S1');
const qc = t.hardware(0, 1, 'Quantum Coprocessor');
t.hand(0, 'Execute: Kill Process');
t.ram(0, 5).play(0, 'Execute: Kill Process', 'C1', { card: so }, 'S1').settle();
assert.equal(t.s.choice?.kind, 'Quantum Coprocessor:copy');
t.choose('Hotfix').settle();
assert.ok(t.s.cards[so].disabledUntil > t.s.clock);
assert.ok(t.s.cards[hf].disabledUntil > t.s.clock, 'the copy hit a second Program');
assert.ok(t.s.cards[qc].disabledUntil > t.s.clock, 'then it disables itself');
});

33
sim/test/map.test.ts Normal file
View file

@ -0,0 +1,33 @@
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { test } from 'node:test';
import { GRID, LINES, LINES_AT, LOCATIONS, levelFor, otherEnd } from '../src/map.ts';
const RULES_MD = readFileSync(new URL('../../rules.md', import.meta.url), 'utf8');
test('24 lines, 20 of them with gateways', () => {
assert.equal(LINES.length, 24);
assert.equal(LINES.filter((l) => l.gateway).length, 20);
});
test('adjacency matches the table in rules.md', () => {
const table = RULES_MD.split('### Adjacency')[1].split('###')[0];
const rows = [...table.matchAll(/^\| (S1|S2|[A-E]\d) \| ([^|]+) \|$/gm)];
assert.equal(rows.length, LOCATIONS.length);
for (const [, loc, list] of rows) {
const expected = list.split(',').map((x) => x.trim()).sort();
const actual = LINES_AT.get(loc)!.map((l) => otherEnd(l, loc)).sort();
assert.deepEqual(actual, expected, loc);
}
});
test('levels are mirrored between the players', () => {
const mirror = (loc: string) => ({ A: 'E', B: 'D', C: 'C', D: 'B', E: 'A' })[loc[0]]! + loc[1];
for (const loc of GRID) assert.equal(levelFor(0, loc), levelFor(1, mirror(loc)), loc);
assert.equal(levelFor(0, 'S1'), 1);
assert.equal(levelFor(0, 'A2'), 2);
assert.equal(levelFor(0, 'B1'), 2);
assert.equal(levelFor(0, 'C2'), 3);
assert.equal(levelFor(0, 'D1'), 4);
assert.equal(levelFor(0, 'E3'), 5);
});

18
sim/test/odds.test.ts Normal file
View file

@ -0,0 +1,18 @@
import assert from 'node:assert/strict';
import { test } from 'node:test';
import { winChance } from '../src/odds.ts';
test('equal Cores are a coin flip and odds are complementary', () => {
for (let a = 1; a <= 8; a++) {
assert.ok(Math.abs(winChance(a, a) - 0.5) < 1e-12);
for (let d = 1; d <= 8; d++) assert.ok(Math.abs(winChance(a, d) + winChance(d, a) - 1) < 1e-12);
}
});
test('matches the table in rules.md', () => {
assert.equal(Math.round(100 * winChance(2, 1)), 90);
assert.equal(Math.round(100 * winChance(3, 4)), 21);
assert.equal(Math.round(100 * winChance(2, 4)), 4);
assert.equal(Math.round(100 * winChance(6, 4)), 91);
assert.equal(Math.round(100 * winChance(8, 7)), 71);
});

372
sim/test/programs.test.ts Normal file
View file

@ -0,0 +1,372 @@
import assert from 'node:assert/strict';
import { avatarCores, execBlocked, executionActions, executionCost, openWindow } from '../src/engine.ts';
import { card } from './card.ts';
import { Table, type Side } from './table.ts';
/** South's Avatar 1 on B1, North's Avatar 1 on C1 (both Broken Bits, 4 Cores). */
function scene(o: { south?: Side; north?: Side; first?: 0 | 1 } = {}) {
const t = new Table(o);
t.own(0, 'B1');
t.own(1, 'C1');
t.place(0, 1, 'B1');
t.place(1, 1, 'C1');
return t;
}
const attackC1 = (t: Table) => t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' });
/** Dice South's attacker rolled, from the log. */
const attackerDice = (t: Table) => t.logText().match(/Roll: Broken Bits \[([\d,]*)\]/)![1].split(',').length;
const defenderDice = (t: Table) => t.logText().match(/Roll: .* vs Broken Bits \[([\d,]*)\]/)![1].split(',').length;
card('Recursive Infection', () => {
const t = scene({ north: { main: ['Recursive Infection'] } });
t.program(1, 'C1', 'Recursive Infection');
t.avatar(0, 1).infection = 2;
attackC1(t);
assert.equal(t.avatar(0, 1).infection, 3, 'a counter for attacking');
assert.equal(avatarCores(t.s, 0, 1, 'attack'), 2, '3+ counters: -2 Cores');
assert.equal(avatarCores(t.s, 0, 1, 'attack', 'S1'), 2, 'anywhere, while the Program is in play');
});
card('Recursive Infection', () => {
const t = new Table({ north: { main: ['Recursive Infection'] } });
t.own(0, 'B1');
t.own(1, 'C1');
t.program(1, 'C1', 'Recursive Infection');
t.place(0, 1, 'B1');
t.go({ type: 'advance', slot: 1, to: 'C1' }).to('attack').end();
assert.equal(t.avatar(0, 1).infection, 1, 'a counter for breaching');
});
card('Memory Parasite', () => {
const t = scene({ south: { main: ['Additional Core', 'RAM Expansion'] }, north: { main: ['Memory Parasite'] } });
t.program(1, 'C1', 'Memory Parasite');
t.hardware(0, 1, 'Additional Core');
t.hardware(0, 1, 'RAM Expansion');
attackC1(t);
t.rig(6, 6, 6, 6, 6, 6, 1, 1, 1, 1).end();
assert.equal(t.s.choice?.kind, 'Memory Parasite:disable');
assert.equal(t.s.choice?.player, 0, "the attacking Avatar's owner chooses");
t.choose('Additional Core').settle();
assert.equal(attackerDice(t), 6, '4 + RAM Expansion only');
});
card('Replication Routine', () => {
const t = new Table({ south: { main: ['Replication Routine', 'Resource Leech'] } });
t.own(0, 'B1');
const [rr, leech] = t.hand(0, 'Replication Routine', 'Resource Leech');
t.ram(0, 6).to('play').act({ type: 'installProgram', card: rr, at: 'B1' });
assert.equal(t.s.choice?.kind, 'Replication Routine:install');
t.choose('Resource Leech').settle();
assert.deepEqual(t.s.locations.B1.programs, [rr, leech]);
assert.equal(t.s.players[0].ram, 0, 'the second Virus is free');
});
card('Resource Leech', () => {
const t = scene({ north: { main: ['Resource Leech'] } });
t.program(1, 'C1', 'Resource Leech');
attackC1(t);
t.rig(1, 1, 1, 1, 6, 6, 6, 6).end();
assert.equal(t.avatar(0, 1).at, null);
assert.equal(t.s.players[1].ram, 5);
});
card('Worm Protocol', () => {
const t = new Table({ first: 1, north: { main: ['Worm Protocol', 'Memory Leak'] }, south: { main: ['Additional Core'] } });
t.own(1, 'C1');
t.own(1, 'D1');
t.own(0, 'C2');
t.program(1, 'C1', 'Memory Leak');
t.place(1, 1, 'D1');
const [worm] = t.hand(1, 'Worm Protocol');
t.ram(1, 4).to('play').act({ type: 'installProgram', card: worm, at: 'C1' });
assert.equal(t.s.choice?.kind, 'Worm Protocol:spread');
t.choose('D1').to('playAgain').end();
// South attacks D1, where Memory Leak now also applies.
t.place(0, 1, 'C2');
t.hardware(0, 1, 'Additional Core');
t.to('attack').go({ type: 'attack', slot: 1, to: 'D1' });
t.rig(6, 6, 6, 6, 1, 1, 1, 1).end();
assert.match(t.logText(), /Memory Leak: Avatar 1 \(Broken Bits\) loses 1 Core/);
assert.equal(attackerDice(t), 4, '4 + 1 - 1');
});
card('Payload Injector', () => {
const t = new Table({ south: { main: ['Payload Injector', 'Execute: Root Access', 'Execute: Root Access'] } });
t.own(0, 'B1');
t.program(0, 'B1', 'Payload Injector');
const [ra] = t.hand(0, 'Execute: Root Access', 'Execute: Root Access');
assert.equal(executionCost(t.s, 0, ra, 'B1', {}), 2);
assert.equal(executionCost(t.s, 0, ra, 'A2', {}), 4, 'only when targeting its Server');
t.ram(0, 2).play(0, 'Execute: Root Access', 'B1').settle();
assert.equal(t.s.players[0].ram, 0);
assert.equal(executionCost(t.s, 0, t.s.players[0].hand[0], 'B1', {}), 4, 'first each turn only');
});
card('Persistent Infection', () => {
const t = scene({ north: { main: ['Persistent Infection'] } });
t.program(1, 'C1', 'Persistent Infection');
attackC1(t);
t.rig(1, 1, 1, 1, 6, 6, 6, 6).end();
assert.equal(t.avatar(0, 1).at, null);
assert.equal(t.avatar(0, 1).permanentInfection, 1, 'stays after defeat');
});
card('Infinite Loop', () => {
const t = new Table({ south: { main: ['Infinite Loop'] } });
t.own(0, 'B1');
t.own(1, 'C1');
t.own(1, 'C2');
t.place(0, 1, 'B1');
t.place(1, 1, 'C1');
t.place(1, 2, 'C2');
t.program(0, 'B1', 'Infinite Loop');
t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' });
assert.ok(t.can({ type: 'attack', slot: 1, to: 'C2' }), 'a second attack');
t.go({ type: 'attack', slot: 1, to: 'C2' });
t.rig(6, 6, 6, 6, 1, 1, 1, 1, 6, 6, 6, 6, 1, 1, 1).end();
assert.equal(t.s.locations.C1.owner, 0);
assert.equal(t.s.locations.C2.owner, 0, 'both attacks resolve');
});
card('Stack Overflow', () => {
const t = scene({ north: { main: ['Stack Overflow', 'Resource Leech'] } });
t.program(1, 'C1', 'Stack Overflow');
t.program(1, 'C1', 'Resource Leech');
assert.equal(avatarCores(t.s, 1, 1, 'defend'), 6, '+1 per Program here');
assert.equal(avatarCores(t.s, 0, 1, 'defend'), 4);
});
card('Race Condition', () => {
for (const [atk, def, bonus] of [['Attack', 'Defend', true], ['Defend', 'Defend', false]] as const) {
const t = scene({ north: { main: ['Race Condition'] } });
t.program(1, 'C1', 'Race Condition');
attackC1(t);
t.rig(6, 6, 6, 6, 1, 1, 1, 1, 1, 1).end();
assert.equal(t.s.choice?.player, 0, 'attacker picks first');
t.choose(atk);
assert.equal(t.s.choice?.player, 1);
t.choose(def).settle();
assert.equal(defenderDice(t), bonus ? 6 : 4);
}
});
card('Memory Leak', () => {
const t = scene({ south: { main: ['Additional Core', 'RAM Expansion'] }, north: { main: ['Memory Leak'] } });
t.program(1, 'C1', 'Memory Leak');
t.hardware(0, 1, 'Additional Core');
t.hardware(0, 1, 'RAM Expansion');
attackC1(t);
t.rig(6, 6, 6, 6, 6, 1, 1, 1, 1).end();
assert.equal(attackerDice(t), 5, '4 + 3 - 2 Hardware');
});
card('Null Pointer', () => {
const t = scene({ south: { main: ['Execute: Kill Process', 'Execute: Kill Process'] }, north: { main: ['Null Pointer', 'Stack Overflow'] } });
t.program(1, 'C1', 'Null Pointer');
const so = t.program(1, 'C1', 'Stack Overflow');
t.hand(0, 'Execute: Kill Process', 'Execute: Kill Process');
t.ram(0, 10).play(0, 'Execute: Kill Process', 'C1', { card: so }).settle();
assert.equal(t.s.cards[so].disabledUntil, 0, 'the first enemy Execution has no effect');
t.play(0, 'Execute: Kill Process', 'C1', { card: so }).settle();
assert.ok(t.s.cards[so].disabledUntil > t.s.clock, 'once per turn');
});
card('Stock Rice', () => {
const t = scene({ south: { main: ['Additional Core'] }, north: { main: ['Stock Rice', 'Additional Core'] } });
t.program(1, 'C1', 'Stock Rice');
t.hardware(0, 1, 'Additional Core');
t.hardware(1, 1, 'Additional Core');
attackC1(t);
t.rig(6, 6, 6, 6, 1, 1, 1, 1).end();
assert.equal(attackerDice(t), 4);
assert.equal(defenderDice(t), 4, 'neither side gets Hardware bonuses');
});
card('Overflow Error', () => {
const t = scene({ south: { main: ['Stack Overflow'] }, north: { main: ['Overflow Error'] } });
t.program(1, 'C1', 'Overflow Error');
const so = t.program(0, 'B1', 'Stack Overflow');
attackC1(t);
t.rig(1, 1, 1, 1, 6, 6, 6, 6).end();
assert.equal(t.s.choice?.kind, 'Overflow Error:hit');
t.choose('Destroy Stack Overflow').settle();
assert.deepEqual(t.s.locations.B1.programs, []);
assert.ok(t.s.players[0].discard.includes(so));
});
card('Undefined Behavior', () => {
const t = new Table({ first: 1, south: { main: ['Stack Overflow'] }, north: { main: ['Undefined Behavior'] } });
t.own(0, 'B1');
t.own(1, 'C1');
t.place(1, 1, 'C1');
const ub = t.program(1, 'C1', 'Undefined Behavior');
const so = t.program(0, 'B1', 'Stack Overflow');
t.use(ub, { card: so }).settle();
assert.equal(avatarCores(t.s, 1, 1, 'defend'), 5, 'copied Stack Overflow: +1 for the one Program here');
assert.ok(t.s.cards[ub].disabledUntil > t.s.clock, 'Undefined Behavior shuts itself off');
t.to('playAgain').end();
assert.equal(t.s.active, 0);
assert.equal(avatarCores(t.s, 1, 1, 'defend'), 5, "the copy lasts through the opponent's turn");
});
card('Packet Loss', () => {
const t = new Table({ south: { main: ['Additional Core'] }, north: { main: ['Packet Loss'] } });
t.own(0, 'B1');
t.own(1, 'C1');
t.program(1, 'C1', 'Packet Loss');
t.place(0, 1, 'B1');
const [hw] = t.hand(0, 'Additional Core');
t.go({ type: 'advance', slot: 1, to: 'C1' }).to('attack').end();
assert.deepEqual(t.s.players[0].hand, []);
assert.ok(t.s.players[0].discard.includes(hw));
});
card('Desynchronization', () => {
for (const desync of [false, true]) {
const t = scene({ south: { main: ['Hot-Swap Bay', 'Additional Core', 'RAM Expansion'] }, north: { main: ['Desynchronization'] } });
if (desync) t.program(1, 'C1', 'Desynchronization');
t.hardware(0, 1, 'Hot-Swap Bay');
t.hardware(0, 1, 'Additional Core');
t.hand(0, 'RAM Expansion');
t.ram(0, 10);
attackC1(t);
t.act({ type: 'end' });
// Broken Bits' own Ability still works after the roll; only the Hardware ability is blocked.
const w = openWindow(t.s);
if (desync) assert.equal(w?.kind, 'postRoll', 'no Hot-Swap Bay before the roll');
else assert.equal(w?.kind, 'preRoll', 'South may use Hot-Swap Bay before the roll');
}
});
card('Visual Artifact', () => {
const t = scene({ north: { main: ['Visual Artifact'] } });
const va = t.program(1, 'C1', 'Visual Artifact');
attackC1(t);
t.rig(1, 1, 1, 6, 6, 6, 6).end();
assert.equal(attackerDice(t), 3);
assert.equal(t.s.cards[va].spent, true, 'only the first attack');
});
card('Ghost Process', () => {
const t = scene({ south: { main: ['Ghost Process', 'Execute: Kill Process'] }, north: { main: ['Stack Overflow'] } });
const so = t.program(1, 'C1', 'Stack Overflow');
t.hand(0, 'Execute: Kill Process');
t.ram(0, 5);
const reach = () => executionActions(t.s, 0, 'now').some((a) => a.type === 'play' && a.target === 'B1' && a.args.card === so);
assert.ok(!reach(), 'without Ghost Process, B1 only reaches B1');
t.program(0, 'B1', 'Ghost Process');
assert.ok(reach(), 'with it, a Kill Process on B1 can hit C1');
});
card('False Positive', () => {
const t = new Table({ first: 1, north: { main: ['False Positive'] }, south: { main: ['Execute: Force Reboot'] } });
t.own(1, 'C1');
t.own(0, 'B1');
t.own(0, 'A2');
t.program(1, 'C1', 'False Positive');
t.place(1, 1, 'C1');
t.place(0, 1, 'B1');
t.place(0, 2, 'A2');
t.hand(0, 'Execute: Force Reboot');
t.ram(0, 5);
t.to('attack').go({ type: 'attack', slot: 1, to: 'B1' }).act({ type: 'end' });
assert.equal(t.decider, 0, 'South may respond before the roll');
t.play(0, 'Execute: Force Reboot', 'B1', { ref: { player: 1, slot: 1 } });
assert.equal(t.s.choice?.kind, 'False Positive:redirect');
t.choose('Pixelated Picture').settle();
assert.ok(!execBlocked(t.s, 'C1'), "North's attacker is untouched");
assert.ok(execBlocked(t.s, 'A2'), "South's own Avatar took it");
});
card('Fragmentation', () => {
const t = scene({ south: { main: ['Execute: Root Access'] }, north: { main: ['Fragmentation'] } });
t.program(1, 'C1', 'Fragmentation');
const [ra] = t.hand(0, 'Execute: Root Access');
assert.equal(executionCost(t.s, 0, ra, 'C1', {}), 6);
assert.equal(executionCost(t.s, 0, ra, 'B1', {}), 4);
});
card('Latency Spike', () => {
for (const spike of [false, true]) {
const t = scene({ south: { main: ['Execute: Ping Sweep'] }, north: { main: ['Latency Spike'] } });
if (spike) t.program(1, 'C1', 'Latency Spike');
t.hand(0, 'Execute: Ping Sweep');
t.ram(0, 4);
attackC1(t);
t.act({ type: 'end' });
assert.equal(openWindow(t.s)?.kind, spike ? 'postRoll' : 'preRoll');
}
});
card('Hotfix', () => {
const t = scene({ south: { main: ['Hotfix', 'Stack Overflow'] }, north: { main: ['Overflow Error'] } });
t.program(1, 'C1', 'Overflow Error');
t.program(0, 'B1', 'Hotfix');
const so = t.program(0, 'B1', 'Stack Overflow');
attackC1(t);
t.rig(1, 1, 1, 1, 6, 6, 6, 6).end();
t.choose('Destroy Stack Overflow').settle();
assert.ok(t.s.locations.B1.programs.includes(so), 'still installed');
assert.ok(t.s.cards[so].disabledUntil > t.s.clock, 'but disabled');
});
card('Error Correction', () => {
const t = new Table({ first: 1, south: { main: ['Error Correction'] } });
t.own(0, 'B1');
t.program(0, 'B1', 'Error Correction');
t.place(0, 1, 'B1');
t.avatar(0, 1).infection = 2;
t.to('playAgain').end();
assert.equal(t.s.active, 0);
assert.equal(t.avatar(0, 1).infection, 1, 'one removed at the start of the turn');
});
card('Garbage Collection', () => {
const t = new Table({ south: { main: ['Garbage Collection', 'Stack Overflow'] } });
t.own(0, 'B1');
const gc = t.program(0, 'B1', 'Garbage Collection');
const so = t.program(0, 'B1', 'Stack Overflow');
t.use(gc, { card: so }).settle();
assert.equal(t.s.players[0].ram, 7);
assert.deepEqual(t.s.locations.B1.programs, [gc]);
});
card('Version Control', () => {
const t = new Table({ north: { main: ['Version Control', 'Stack Overflow'] } });
t.own(0, 'B1');
t.own(1, 'C1');
const vc = t.program(1, 'C1', 'Version Control');
const so = t.program(1, 'C1', 'Stack Overflow');
t.place(0, 1, 'B1');
t.go({ type: 'advance', slot: 1, to: 'C1' }).to('attack').end();
assert.equal(t.s.choice?.kind, 'Version Control:save');
t.choose('Stack Overflow').settle();
assert.deepEqual(t.s.players[1].hand, [so]);
assert.ok(t.s.players[1].discard.includes(vc));
});
card('Rollback Routine', () => {
const t = scene({ south: { main: ['Rollback Routine', 'Additional Core'] }, north: { main: ['Memory Leak'] } });
t.program(1, 'C1', 'Memory Leak');
const rr = t.program(0, 'B1', 'Rollback Routine');
t.hardware(0, 1, 'Additional Core');
attackC1(t);
t.act({ type: 'end' });
assert.equal(openWindow(t.s)?.kind, 'preRoll');
assert.equal(t.decider, 0);
t.rig(6, 6, 6, 6, 6, 1, 1, 1, 1);
t.use(rr, { ref: { player: 0, slot: 1 } }).settle();
assert.equal(attackerDice(t), 5, "Memory Leak's -1 is undone");
});
card('Compatibility Layer', () => {
const t = new Table({ south: { main: ['Compatibility Layer', 'Stack Overflow', 'Hotfix'] } });
t.own(0, 'B1');
const [cl, so, hf] = t.hand(0, 'Compatibility Layer', 'Stack Overflow', 'Hotfix');
t.ram(0, 6 + 7 + 5).to('play');
t.go({ type: 'installProgram', card: cl, at: 'B1' });
t.go({ type: 'installProgram', card: so, at: 'B1' });
t.go({ type: 'installProgram', card: hf, at: 'B1' });
assert.equal(t.s.locations.B1.programs.length, 3);
assert.equal(t.s.players[0].ram, 0, 'Hotfix, a Patch, cost 2 less');
});

139
sim/test/servers.test.ts Normal file
View file

@ -0,0 +1,139 @@
import assert from 'node:assert/strict';
import { avatarCores } from '../src/engine.ts';
import { card } from './card.ts';
import { Table } from './table.ts';
/** South advances its Avatar 1 from S1 into A2, drawing `server` for it. */
function captureA2(server: string) {
const t = new Table({ south: { servers: [server] } });
t.nextServer(0, server);
t.place(0, 1, 'S1');
t.go({ type: 'advance', slot: 1, to: 'A2' }).to('attack').end();
assert.equal(t.s.locations.A2.owner, 0);
return t;
}
card('High Efficiency RAM Server', () => {
const t = captureA2('High Efficiency RAM Server');
assert.equal(t.s.players[0].ram, 4 + 4, 'Lv 2 pays 4, the Server adds 4');
});
card('Power Server', () => {
const t = captureA2('Power Server');
assert.equal(t.s.players[0].ram, 8);
});
card('Server Farm', () => {
const t = captureA2('Server Farm');
assert.equal(t.s.players[0].ram, 8);
assert.equal(avatarCores(t.s, 0, 1, 'defend'), 4 + 2);
});
card('MultiCore Server', () => {
const t = new Table({ south: { servers: ['MultiCore Server'] } });
t.own(0, 'B1', 'MultiCore Server');
t.place(0, 1, 'B1');
assert.equal(avatarCores(t.s, 0, 1, 'defend'), 6);
assert.equal(avatarCores(t.s, 0, 1, 'attack'), 6, 'attacking from it counts too');
assert.equal(avatarCores(t.s, 0, 1, 'attack', 'S1'), 4, 'only on that location');
});
card('Cloud Compute', () => {
const t = new Table({ south: { servers: ['Cloud Compute'] } });
t.own(0, 'B1', 'Cloud Compute');
t.place(0, 1, 'B1');
assert.equal(avatarCores(t.s, 0, 1, 'defend'), 8);
});
const twoDefenders = (name: string) => () => {
const t = new Table({ north: { servers: [name] } });
t.own(0, 'B1');
t.own(1, 'C1', name);
t.place(0, 1, 'B1');
t.place(1, 1, 'C1');
t.place(1, 2, 'C1');
assert.equal(t.s.locations.C1.owner, 1);
t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' });
t.rig(6, 6, 6, 6, 1, 1, 1, 1, 1, 1, 1).end();
assert.match(t.logText(), /vs Broken Bits \[1,1,1,1\] \+ Pixelated Picture \[1,1,1\]=7/, 'both defenders roll together');
assert.equal(t.s.locations.C1.owner, 0);
assert.equal(t.avatar(1, 1).at, null);
assert.equal(t.avatar(1, 2).at, null, 'both defenders are defeated');
};
card('Cluster Server', twoDefenders('Cluster Server'));
card('Pingable Server', twoDefenders('Pingable Server'));
card('Backup Server', () => {
const t = new Table({ south: { servers: ['Backup Server'] } });
const backup = t.own(0, 'B1', 'Backup Server');
t.own(0, 'A2');
t.own(1, 'C1');
t.place(0, 1, 'B1');
t.place(1, 1, 'C1');
t.use(backup, { ref: { player: 0, slot: 1 }, to: 'A2' }).settle();
assert.equal(t.avatar(0, 5).at, 'A2', 'the copy of Avatar 1 is in play');
assert.equal(avatarCores(t.s, 0, 5, 'defend'), 4);
assert.ok(!t.legal().some((a) => a.type === 'activate'), 'once per turn');
t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' });
t.rig(1, 1, 1, 1, 6, 6, 6, 6).end();
assert.equal(t.avatar(0, 1).at, null);
assert.equal(t.avatar(0, 5).at, null, 'the copy goes when the original is defeated');
});
card('Upgraded Hardware', () => {
const t = new Table({ south: { servers: ['Upgraded Hardware'], main: ['Additional Core'] } });
t.own(0, 'B1', 'Upgraded Hardware');
t.place(0, 1, 'B1');
t.place(0, 2, 'S1');
const [hw] = t.hand(0, 'Additional Core');
t.ram(0, 3).to('play');
assert.ok(!t.can({ type: 'installHardware', card: hw, slot: 2 }), 'full price elsewhere');
t.go({ type: 'installHardware', card: hw, slot: 1 });
assert.equal(t.s.players[0].ram, 0);
});
card('Network Throttle', () => {
const t = new Table();
t.own(0, 'A2');
t.ram(0, 1).to('play');
t.go({ type: 'firewall', line: 'A1-A2', level: 1 });
assert.equal(t.s.players[0].ram, 0, 'Lv 1 costs 1 next to Network Throttle');
});
card('Duplicate Server', () => {
for (const [copy, check] of [['Cloud Compute', 'cores'], ['High Efficiency RAM Server', 'ram']] as const) {
const t = new Table({ south: { servers: ['Duplicate Server', 'Cloud Compute', 'High Efficiency RAM Server'] } });
t.own(0, 'B1', 'Cloud Compute');
t.own(0, 'B2', 'High Efficiency RAM Server');
t.nextServer(0, 'Duplicate Server');
t.place(0, 1, 'B1');
t.go({ type: 'advance', slot: 1, to: 'C2' }).to('attack').end();
assert.equal(t.s.choice?.kind, 'Duplicate Server:copy');
t.choose(copy);
if (check === 'cores') assert.equal(avatarCores(t.s, 0, 1, 'defend'), 8);
else assert.equal(t.s.players[0].ram, 6 + 4, 'Lv 3 pays 6, the copied bonus 4');
}
});
card('Protected Server', () => {
const t = new Table({ north: { servers: ['Protected Server'] } });
t.own(0, 'B1');
t.own(1, 'C1', 'Protected Server');
t.place(0, 1, 'B1');
t.place(1, 1, 'C1');
t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' });
t.rig(1, 1, 1, 1, 6, 6, 6, 6).end();
assert.equal(t.s.players[1].ram, 2);
});
card('Callable Server', () => {
const t = new Table({ first: 1, south: { servers: ['Callable Server'], main: ['Additional Core', 'Cooling System'] } });
t.own(0, 'B1', 'Callable Server');
const [a, b] = t.top(0, 'Additional Core', 'Cooling System');
t.to('playAgain').end();
assert.equal(t.s.choice?.kind, 'Callable Server:redraw');
t.choose('Bottom').settle();
const pl = t.s.players[0];
assert.deepEqual(pl.hand, [b]);
assert.equal(pl.deck[pl.deck.length - 1], a);
});

238
sim/test/table.ts Normal file
View file

@ -0,0 +1,238 @@
/**
* A test table: builds a game from small hand-picked decks, then lets a test arrange the board
* directly and drive decisions one at a time.
*/
import assert from 'node:assert/strict';
import { loadCards } from '../src/cards.ts';
import { actionKey, applyAction, createGame, decider, legalActions, openWindow } from '../src/engine.ts';
import { checkInvariants } from '../src/invariants.ts';
import type { LocId } from '../src/map.ts';
import { coresOf, fighterOf, sideCores } from '../src/query.ts';
import { DEFAULT_RULES, type RulesConfig } from '../src/rules.ts';
import { avatar, removeFrom } from '../src/state.ts';
import type { Action, Args, AvatarRef, GameState, PlayerId } from '../src/types.ts';
export const pool = loadCards();
/** Cards that do nothing in hand and can't be played in response, used to pad decks. */
const FILLER = 'Network Interface Upgrade';
const PLAIN_SERVER = 'Network Throttle';
/** Avatars whose Abilities only work in combat, so they don't open response windows. */
const DEFAULT_AVATARS = ['Broken Bits', 'Pixelated Picture', 'Corrupt Driver', 'Code Zero'];
export interface Side {
avatars?: string[];
servers?: string[];
main?: string[];
}
export class Table {
s: GameState;
constructor(o: { south?: Side; north?: Side; first?: PlayerId; seed?: number } = {}) {
const side = (x: Side = {}) => ({
avatars: x.avatars ?? DEFAULT_AVATARS,
servers: [...(x.servers ?? []), ...Array(12).fill(PLAIN_SERVER)],
main: [...(x.main ?? []), ...Array(12).fill(FILLER)],
});
const decks = [side(o.south), side(o.north)] as const;
const rules: RulesConfig = {
...DEFAULT_RULES,
openingHand: 0,
deck: { avatars: 4, servers: decks[0].servers.length, main: decks[0].main.length, maxCopies: 99 },
};
// Both decks must be the same size under one rules object; pad the shorter one.
const n = Math.max(decks[0].main.length, decks[1].main.length);
const m = Math.max(decks[0].servers.length, decks[1].servers.length);
for (const d of decks) {
while (d.main.length < n) d.main.push(FILLER);
while (d.servers.length < m) d.servers.push(PLAIN_SERVER);
}
rules.deck = { ...rules.deck, main: n, servers: m };
this.s = createGame({ pool, decks: [decks[0], decks[1]], seed: o.seed ?? 1, rules, log: true });
const first = o.first ?? 0;
this.s.firstPlayer = first;
this.s.active = first;
for (const p of [0, 1] as const) {
// Put the starting Server back and use a plain one, so tests start from a known board.
const start = p === 0 ? 'S1' : 'S2';
const L = this.s.locations[start];
this.s.players[p].serverPile.push(L.server!);
L.server = this.take(p, PLAIN_SERVER);
this.s.players[p].ram = 0;
// Avatars in the order given, slot 1 face up.
const want = (p === 0 ? o.south : o.north)?.avatars ?? DEFAULT_AVATARS;
const avs = this.s.players[p].avatars;
want.forEach((name, i) => {
const j = avs.findIndex((a, k) => k < 4 && k >= i && this.s.cards[a.card].name === name);
[avs[i].card, avs[j].card] = [avs[j].card, avs[i].card];
});
for (let i = 0; i < 4; i++) avs[i + 4].card = avs[i].card;
}
this.s.log = [];
}
// -------------------------------------------------------------------------------------------
// Arranging the board
/** Takes a card of `name` owned by `p` out of its deck, pile, hand or discard. */
take(p: PlayerId, name: string): number {
const pl = this.s.players[p];
for (const zone of [pl.deck, pl.serverPile, pl.hand, pl.discard]) {
const id = zone.find((c) => this.s.cards[c].name === name);
if (id !== undefined) {
removeFrom(zone, id);
return id;
}
}
throw new Error(`No ${name} left for player ${p}`);
}
hand(p: PlayerId, ...names: string[]): number[] {
return names.map((n) => {
const id = this.take(p, n);
this.s.players[p].hand.push(id);
return id;
});
}
/** Puts cards on top of `p`'s deck, first name on top. */
top(p: PlayerId, ...names: string[]): number[] {
const ids = names.map((n) => this.take(p, n));
this.s.players[p].deck.unshift(...ids);
return ids;
}
/** Makes `name` the next Server `p` draws on a capture. */
nextServer(p: PlayerId, name: string): number {
const id = this.take(p, name);
this.s.players[p].serverPile.unshift(id);
return id;
}
ram(p: PlayerId, n: number): this {
this.s.players[p].ram = n;
return this;
}
/** Gives `p` a location with a Server on it. */
own(p: PlayerId, loc: LocId, server = PLAIN_SERVER): number {
const L = this.s.locations[loc];
if (L.server !== null) this.s.players[this.s.cards[L.server].owner].serverPile.push(L.server);
L.owner = p;
L.server = this.take(p, server);
return L.server;
}
/** Puts Avatar `slot` of `p` in play on `at` (face up). */
place(p: PlayerId, slot: number, at: LocId): AvatarRef {
const av = this.s.players[p].avatars[slot - 1];
av.faceUp = true;
av.at = at;
return { player: p, slot };
}
program(p: PlayerId, loc: LocId, name: string): number {
const id = this.take(p, name);
this.s.locations[loc].programs.push(id);
return id;
}
hardware(p: PlayerId, slot: number, name: string): number {
const id = this.take(p, name);
this.s.players[p].avatars[slot - 1].hardware.push(id);
return id;
}
// -------------------------------------------------------------------------------------------
// Playing
get decider(): PlayerId {
return decider(this.s)!;
}
legal(): Action[] {
return legalActions(this.s);
}
can(a: Action): boolean {
const k = actionKey(a);
return this.legal().some((x) => actionKey(x) === k);
}
/** Applies an action for whoever decides now, and checks the state afterwards. */
act(a: Action): this {
applyAction(this.s, a);
assert.deepEqual(checkInvariants(this.s), [], 'invariants');
return this;
}
/** Passes every open window until a main step decision or a choice. */
settle(): this {
for (let i = 0; i < 50 && !this.s.over && !this.s.choice && openWindow(this.s); i++) this.act({ type: 'pass' });
return this;
}
/** Acts, then passes windows. */
go(a: Action): this {
return this.act(a).settle();
}
end(): this {
return this.settle().go({ type: 'end' });
}
/** Ends steps until the given step of the active player's turn. */
to(step: GameState['step']): this {
for (let i = 0; i < 10 && this.s.step !== step; i++) this.end();
assert.equal(this.s.step, step);
return this;
}
/** Plays an Execution from hand. `from` is the performing Server, when it matters. */
play(p: PlayerId, name: string, target: LocId | null, args: Args = {}, from: LocId | null = null): this {
const card = this.s.players[p].hand.find((id) => this.s.cards[id].name === name);
assert.ok(card !== undefined, `${name} is in hand`);
assert.equal(this.decider, p, `player ${p} decides`);
return this.act({ type: 'play', card, target, args, from });
}
/** Activates the ability of the card `id` (an Avatar's card id, a Program or Hardware). */
use(id: number, args: Args = {}): this {
return this.act({ type: 'activate', card: id, name: this.s.cards[id].name, args });
}
/** Picks the choice option whose label includes `text` (or the option at an index). */
choose(text: string | number): this {
const ch = this.s.choice;
assert.ok(ch, 'a choice is pending');
const index = typeof text === 'number' ? text : ch.options.findIndex((o) => o.label.includes(text));
assert.ok(index >= 0, `option "${text}" in ${ch.options.map((o) => o.label).join(' / ')}`);
return this.act({ type: 'choose', index });
}
// -------------------------------------------------------------------------------------------
// Reading
avatar(p: PlayerId, slot: number) {
return avatar(this.s, { player: p, slot });
}
cores(p: PlayerId, slot: number): number {
return coresOf(this.s, fighterOf(this.s, { player: p, slot }));
}
sides() {
return sideCores(this.s);
}
/** Sets the next dice results (attacker's dice first, then defenders', in roll order). */
rig(...rolls: number[]): this {
this.s.rigged = [...rolls];
return this;
}
logText(): string {
return (this.s.log ?? []).join('\n');
}
}