Rules v0.8: rounds of alternating commands; sim updates

Rules (rules.md, new file):
- Rounds of 2 commands per player in snake order (A B B A, then B A A B).
  A command is one advance or one attack; each Avatar acts once per round.
- Flat 4 RAM captures, first row Lv 1, A2-C2 and C2-E2 lines cut,
  45-card decks, Code Zero at 3 Cores with Patient Zero at 1+ counters.

Simulator:
- Command rule (default on): commands.perPlayer, commands.leads,
  commands.snake; commands.on=false restores whole turns.
- Greedy bot values Avatar unlocks (unlockPoints) and picks one action
  per command.
- Prototype switches, off by default: Avatars in the deck (recruit.*),
  buying Avatars (buyAvatars.*), copy limits (copies.*), turn-1 draw and
  second-player draw options.
- cards.csv card-text revisions, with scripts and tests to match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Forrest 2026-09-28 14:07:45 -04:00
parent 6382de19c9
commit a3eb6850ee
21 changed files with 1089 additions and 96 deletions

View file

@ -2,6 +2,7 @@ import assert from 'node:assert/strict';
import { test } from 'node:test';
import { avatarCores, openWindow } from '../src/engine.ts';
import { hasRoom } from '../src/query.ts';
import { withRule } from '../src/rules.ts';
import { card } from './card.ts';
import { Table, type Side } from './table.ts';
@ -39,6 +40,35 @@ card('Carbon NanoBots', () => {
assert.equal(t.avatar(0, 1).hardware.length, 1);
});
test('copy rule variants: a new copy defends but only advances or attacks from its next turn', () => {
const t = scene('Carbon NanoBots', 'Broken Bits');
t.s.rules = withRule(t.s.rules, 'copies.waitATurn=true');
t.own(0, 'A2');
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, defending A2');
const acts = () => t.legal().filter((a) => (a.type === 'advance' || a.type === 'attack') && a.slot === 5);
assert.deepEqual(acts(), []);
assert.ok(t.legal().some((a) => a.type === 'advance' && a.slot === 1), 'the original still can');
t.to('attack');
assert.deepEqual(acts(), []);
t.to('playAgain').end().to('playAgain').end();
assert.ok(acts().length > 0, 'next turn it can advance');
});
test('copy rule variants: copies can need Avatar 2 unlocked, or be off', () => {
const t = scene('Carbon NanoBots', 'Broken Bits');
t.own(0, 'A2');
t.ram(0, 5);
const replicate = () => t.legal().some((a) => a.type === 'activate' && a.name === 'Carbon NanoBots');
assert.ok(replicate());
t.s.rules = withRule(t.s.rules, 'copies.needAvatar2=true');
assert.ok(!replicate(), 'Avatar 2 is still face down');
t.avatar(0, 2).faceUp = true;
assert.ok(replicate());
t.s.rules = withRule(t.s.rules, 'copies.allowed=false');
assert.ok(!replicate());
});
test('no copy can enter a location while a combat is going on there', () => {
const t = scene('Broken Bits', 'Carbon NanoBots', { north: { servers: ['Cluster Server'] } });
t.own(1, 'C1', 'Cluster Server');
@ -86,14 +116,14 @@ card('Beta Version 1.0', () => {
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.rig(6, 6, 6, 1, 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');
assert.equal(t.avatar(0, 1).infection >= 1 || t.avatar(0, 1).at === null, true);
assert.equal(dice(t, 'atk'), 3, '1 Infection counter: -1 Core while Code Zero is in play');
assert.equal(dice(t, 'def'), 3, 'Code Zero has 3 Cores');
});
card('Data Leaker', () => {

View file

@ -5,7 +5,8 @@ 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, withRule } from '../src/rules.ts';
import { DEFAULT_RULES, withRule, type RulesConfig } from '../src/rules.ts';
import { commanderOf, roundOf } from '../src/state.ts';
import { Table } from './table.ts';
const pool = loadCards();
@ -36,6 +37,31 @@ test('the first player skips the draw; the second player draws', () => {
assert.equal(t.s.players[1].hand.length, 1);
});
test('rule variants: the first player draws on turn 1', () => {
const pool = loadCards();
const hand = (rules: RulesConfig) => {
const s = createGame({ pool, decks: [randomDeck(pool, makeRng(1), rules), randomDeck(pool, makeRng(2), rules)], seed: 5, rules });
return s.players[s.firstPlayer].hand.length;
};
assert.equal(hand(withRule(DEFAULT_RULES, 'firstTurn.draw=true')), hand(DEFAULT_RULES) + 1);
});
test('rule variants: the second player draws extra in their first draw step only', () => {
const t = new Table({ north: { main: ['Additional Core', 'Additional Core', 'Additional Core', 'Additional Core'] } });
t.s.rules = withRule(t.s.rules, 'secondPlayer.firstDraw=1');
t.to('playAgain').end();
assert.equal(t.s.players[1].hand.length, 2);
t.to('playAgain').end().to('playAgain').end();
assert.equal(t.s.players[1].hand.length, 3);
});
test('rule variants: more cards per draw step', () => {
const t = new Table({ north: { main: ['Additional Core', 'Additional Core'] } });
t.s.rules = withRule(t.s.rules, 'drawPerTurn=2');
t.to('playAgain').end();
assert.equal(t.s.players[1].hand.length, 2);
});
test('advancing captures an unowned location in step 5 and pays 4 RAM; Lv 1 unlocks nothing', () => {
const t = new Table();
t.ram(0, 4);
@ -240,3 +266,200 @@ test('an opponent with a response gets a window after a declaration; without one
t.act({ type: 'pass' });
assert.equal(t.decider, 0);
});
// ---------------------------------------------------------------------------------------------
// Recruit rule (prototype): Avatars in the deck, bought with RAM
/** A table under the recruit rule: Avatar 1 stays, Avatars 2–4 move to their owner's hand, priced at 6 RAM. */
function recruitTable(o: ConstructorParameters<typeof Table>[0] = {}): Table {
const t = new Table(o);
t.s.rules = withRule(t.s.rules, 'recruit.on=true');
for (const p of [0, 1] as const) {
const avs = t.s.players[p].avatars;
for (let i = 1; i < 4; i++) {
const name = t.s.cards[avs[i].card].name;
t.s.defs[name] = { ...t.s.defs[name], ram: 6 };
t.s.players[p].hand.push(avs[i].card);
avs[i].card = avs[i + 4].card = -1;
avs[i].faceUp = false;
}
}
return t;
}
const recruits = (t: Table) => t.legal().filter((a) => a.type === 'recruit');
test('recruit rule: buy an Avatar from hand into an empty slot; it enters play on a later turn', () => {
const t = recruitTable();
const card = t.s.players[0].hand[0];
t.ram(0, 5).to('play');
assert.equal(recruits(t).length, 0, "can't afford 6 RAM");
t.ram(0, 6);
assert.deepEqual(recruits(t).map((a) => a.type === 'recruit' && a.slot), [2, 1, 2, 1, 2, 1], 'first empty slot, or over Avatar 1');
t.go({ type: 'recruit', card, slot: 2 });
assert.equal(t.s.players[0].ram, 0);
assert.equal(t.avatar(0, 2).card, card);
assert.equal(t.avatar(0, 2).faceUp, true);
assert.ok(!t.s.players[0].hand.includes(card));
t.to('playAgain').end().to('playAgain').end();
assert.ok(t.can({ type: 'place', slot: 2, to: 'S1' }));
});
test('recruit rule: captures unlock nothing, and a salvaged Avatar pays its price', () => {
const t = recruitTable();
t.own(0, 'B1');
t.place(0, 1, 'B1');
t.go({ type: 'advance', slot: 1, to: 'C2' }).to('attack').end();
assert.equal(t.s.locations.C2.owner, 0);
assert.equal(t.avatar(0, 2).card, -1);
const ram = t.s.players[0].ram;
t.go({ type: 'salvage', card: t.s.players[0].hand[0] });
assert.equal(t.s.players[0].ram, ram + 6);
});
test('recruit rule: copies count toward the 4 Avatars', () => {
const t = recruitTable({ south: { avatars: ['Carbon NanoBots', 'Broken Bits', 'Pixelated Picture', 'Corrupt Driver'] } });
t.own(0, 'A2');
t.place(0, 1, 'A2');
const [a, b] = t.s.players[0].hand;
t.ram(0, 30).to('play');
t.go({ type: 'recruit', card: a, slot: 2 }).go({ type: 'recruit', card: b, slot: 3 });
const replicate = t.legal().find((x) => x.type === 'activate' && x.name === 'Carbon NanoBots' && x.args.to === 'S1');
assert.ok(replicate, '3 Avatars, so a copy fits');
t.go(replicate).settle();
assert.equal(t.avatar(0, 5).at, 'S1');
assert.ok(!recruits(t).some((r) => r.type === 'recruit' && r.slot === 4), 'no room for a fourth with the copy out');
});
test('recruit rule: recruiting over an Avatar retires it and its Hardware', () => {
const t = recruitTable({ south: { main: ['Additional Core'] } });
t.own(0, 'A2');
const ref = t.place(0, 1, 'A2');
const old = t.avatar(0, 1).card;
const hw = t.hardware(ref.player, ref.slot, 'Additional Core');
const card = t.s.players[0].hand.find((id) => t.s.defs[t.s.cards[id].name].kind === 'avatar')!;
t.ram(0, 6).to('play').go({ type: 'recruit', card, slot: 1 });
assert.equal(t.avatar(0, 1).card, card);
assert.equal(t.avatar(0, 1).at, null);
assert.deepEqual(t.avatar(0, 1).hardware, []);
assert.ok(t.s.players[0].discard.includes(old) && t.s.players[0].discard.includes(hw));
assert.equal(t.s.locations.A2.owner, 0, 'the location stays yours');
});
test('recruit rule: decks keep one Avatar outside and shuffle the rest into the main deck', () => {
const pool = loadCards();
const rules = withRule(withRule(DEFAULT_RULES, 'recruit.on=true'), 'recruit.avatarsInDeck=5');
const deck = randomDeck(pool, makeRng(3), rules);
assert.equal(deck.avatars.length, 1);
assert.equal(deck.main.length, 45);
assert.equal(deck.main.filter((n) => pool.byName.get(n)!.kind === 'avatar').length, 5);
const s = createGame({ pool, decks: [deck, randomDeck(pool, makeRng(4), rules)], seed: 1, rules });
assert.deepEqual(s.players[0].avatars.slice(0, 4).map((a) => a.card >= 0), [true, false, false, false]);
assert.equal(checkInvariants(s).length, 0);
});
// ---------------------------------------------------------------------------------------------
// Buy-Avatars rule (prototype): pay RAM to flip the next Avatar instead of unlocking by level
test('buy rule: pay to flip the next face-down Avatar; it enters play on a later turn', () => {
const t = new Table();
t.s.rules = withRule(withRule(t.s.rules, 'buyAvatars.on=true'), 'buyAvatars.prices=8,12,16');
t.ram(0, 7).to('play');
assert.ok(!t.can({ type: 'buyAvatar', slot: 2 }), "can't afford 8 RAM");
t.ram(0, 20).go({ type: 'buyAvatar', slot: 2 });
assert.equal(t.avatar(0, 2).faceUp, true);
assert.equal(t.s.players[0].ram, 12);
assert.ok(t.can({ type: 'buyAvatar', slot: 3 }), 'Avatar 3 is next, at 12');
t.go({ type: 'buyAvatar', slot: 3 });
assert.equal(t.s.players[0].ram, 0);
t.to('playAgain').end().to('playAgain').end();
assert.ok(t.can({ type: 'place', slot: 2, to: 'S1' }));
});
test('buy rule: captures unlock nothing', () => {
const t = new Table();
t.s.rules = withRule(t.s.rules, 'buyAvatars.on=true');
t.own(0, 'B1');
t.place(0, 1, 'B1');
t.go({ type: 'advance', slot: 1, to: 'C2' }).to('attack').end();
assert.equal(t.s.locations.C2.owner, 0);
assert.deepEqual([t.avatar(0, 2).faceUp, t.avatar(0, 3).faceUp], [false, false]);
});
// ---------------------------------------------------------------------------------------------
// Command rule (prototype): alternating commands, 2 per player per round
/** Command rule with plain A B A B rounds, so tests can predict whose command it is. */
function commandTable(): Table {
const t = new Table();
t.s.rules = withRule(withRule(t.s.rules, 'commands.on=true'), 'commands.snake=false');
return t;
}
test('command rule: players alternate commands, and the lead alternates by round', () => {
const t = commandTable();
assert.deepEqual([1, 2, 3, 4, 5, 6, 7, 8, 9].map((turn) => commanderOf(t.s, turn)), [0, 1, 0, 1, 1, 0, 1, 0, 0]);
assert.deepEqual([1, 4, 5, 8, 9].map((turn) => roundOf(t.s, turn)), [1, 1, 2, 2, 3]);
t.to('playAgain').end();
assert.equal(t.s.active, 1);
t.to('playAgain').end().to('playAgain').end().to('playAgain').end();
assert.equal(t.s.turn, 5);
assert.equal(t.s.active, 1, 'North leads round 2');
});
test('command rule: the order of commands can follow a pattern of round leads, or snake', () => {
const order = (...specs: string[]) => {
const t = commandTable();
for (const sp of specs) t.s.rules = withRule(t.s.rules, sp);
return Array.from({ length: 16 }, (_, i) => 'FS'[commanderOf(t.s, i + 1)]).join('');
};
assert.equal(order(), 'FSFS' + 'SFSF' + 'FSFS' + 'SFSF');
assert.equal(order('commands.leads=F'), 'FSFS'.repeat(4));
assert.equal(order('commands.leads=FFSS'), 'FSFS' + 'FSFS' + 'SFSF' + 'SFSF');
assert.equal(order('commands.leads=thue'), 'FSFS' + 'SFSF' + 'SFSF' + 'FSFS');
assert.equal(order('commands.snake=true', 'commands.leads=F'), 'FSSF'.repeat(4));
assert.equal(order('commands.snake=true'), 'FSSF' + 'SFFS' + 'FSSF' + 'SFFS', 'the default order');
assert.equal(DEFAULT_RULES.commands.snake, true);
});
test('command rule: one advance or attack per command, each Avatar once a round', () => {
const t = commandTable();
t.own(0, 'A2');
t.own(0, 'A1');
t.place(0, 1, 'A2');
t.place(0, 2, 'A1');
t.go({ type: 'advance', slot: 1, to: 'A3' });
assert.ok(!t.legal().some((a) => a.type === 'advance'), 'the command is spent');
t.to('attack');
assert.ok(!t.legal().some((a) => a.type === 'attack'));
t.to('playAgain').end().to('playAgain').end();
assert.equal(t.s.turn, 3);
const movers = new Set(t.legal().filter((a) => a.type === 'advance').map((a) => a.type === 'advance' && a.slot));
assert.deepEqual([...movers], [2], 'Avatar 1 already acted this round');
});
test('command rule: an Avatar unlocked this round enters play next round', () => {
const t = commandTable();
t.own(0, 'A2');
t.place(0, 1, 'A2');
t.go({ type: 'advance', slot: 1, to: 'B1' }).to('attack').end();
assert.equal(t.avatar(0, 2).faceUp, true);
t.to('playAgain').end().to('playAgain').end();
assert.equal(t.s.turn, 3);
assert.ok(!t.legal().some((a) => a.type === 'place' && a.slot === 2));
t.to('playAgain').end().to('playAgain').end().to('playAgain').end();
assert.equal(t.s.turn, 6);
assert.ok(t.legal().some((a) => a.type === 'place' && a.slot === 2));
});
test('command rule: each player draws at their first command of a round only', () => {
const t = commandTable();
const hand = () => t.s.players[1].hand.length;
t.to('playAgain').end();
assert.equal(hand(), 1, "North's first command of round 1");
t.to('playAgain').end().to('playAgain').end();
assert.equal(hand(), 1, 'no draw on the second command');
t.to('playAgain').end();
assert.equal(t.s.turn, 5);
assert.equal(hand(), 2, 'round 2');
});

View file

@ -4,6 +4,7 @@ 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();
@ -26,6 +27,42 @@ test('greedy bots play 200 games without breaking a rule', () => {
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 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++) {

View file

@ -36,8 +36,11 @@ export class Table {
main: [...(x.main ?? []), ...Array(12).fill(FILLER)],
});
const decks = [side(o.south), side(o.north)] as const;
// Classic turns (one turn each, every Avatar acts), which the card tests are written for. The
// command rule has its own tests in engine.test.ts.
const rules: RulesConfig = {
...DEFAULT_RULES,
commands: { ...DEFAULT_RULES.commands, on: false },
openingHand: 0,
deck: { avatars: 4, servers: decks[0].servers.length, main: decks[0].main.length, maxCopies: 99 },
};