primordial-soup/test/five-six.test.js

185 lines
6.5 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import * as E from "../engine/index.js";
import { scoringLegend } from "../viewer/scoring-legend.js";
import { food } from "../viewer/rich-text.js";
const act = (s, type, details = {}) => {
const action = E.legalMoves(s).find(
(m) =>
m.type === type && Object.entries(details).every(([k, v]) => m[k] === v),
);
assert.ok(action, `Missing ${type}`);
return E.move(s, action, E.currentPlayer(s));
};
function fixture(players, genes = [], kind = "feeding") {
const s = E.init(players, [], {}, "six-player-rules");
s.round = 2;
s.phase = kind === "genes" ? 2 : kind === "division" ? 3 : 0;
s.players.forEach((p, i) => {
p.score = players - i;
p.bp = 20;
});
s.players[0].genes = genes.map((id) => ({ id, round: 1 }));
s.amoebas = s.players.map((_, p) => ({
id: `${p}-1`,
p,
n: 1,
cell: E.CELLS[p],
hp: 0,
}));
s.tasks = [{ kind, ...(kind === "feeding" ? { a: "0-1" } : { p: 0 }) }];
return s;
}
function feedAt(s, values) {
const cell = E.amoeba(s, "0-1").cell;
values.forEach((n, p) => {
s.supply[p] += s.food[cell][p] - n;
});
s.food[cell] = values;
}
function assertConserved(s) {
for (let p = 0; p < s.players.length; p++) {
const carried = s.tasks
.filter((t) => t.kind === "carry")
.reduce((n, t) => n + t.cargo.filter((c) => c === p).length, 0);
assert.equal(
s.food.reduce((n, row) => n + row[p], s.supply[p]) + carried,
55,
);
assert.ok(s.players[p].bp >= 0);
assert.ok(E.colony(s, p).every((a) => a.n <= 6));
assert.ok(E.colony(s, p).length <= 6);
}
}
test("five/six-player setup offers six scores, six amoebas, six distinct colours and normal initial wounds", () => {
for (const n of [5, 6]) {
let s = E.init(n, [], {}, "setup-six");
assert.equal(new Set(s.players.map((p) => p.color)).size, n);
assert.deepEqual(
E.legalMoves(s).map((m) => m.score),
[1, 2, 3, 4, 5, 6],
);
while (s.phase === "setup-score") s = act(s, "score");
assert.deepEqual(
[...new Set(E.legalMoves(s).map((m) => m.n))],
[1, 2, 3, 4, 5, 6],
);
while (s.round === 0) s = act(s, "place");
for (let p = 0; p < n; p++)
assert.deepEqual(
E.colony(s, p).map((a) => a.hp),
[1, 0],
);
assertConserved(s);
}
});
test("the 39-card pool applies replacement sensitivity and Frugality price in purchases and defects", () => {
assert.deepEqual(
[3, 4, 5, 6].map((n) =>
E.GENES.reduce((sum, g) => sum + g.copies[n - 3], 0),
),
[20, 30, 39, 39],
);
for (const n of [5, 6]) {
let s = fixture(n, [], "genes");
s.players[0].bp = 5;
s = act(s, "buy", { gene: "frugality" });
assert.equal(s.players[0].bp, 0);
s = fixture(n, ["streamline", "struggle"], "genes");
assert.equal(E.mutation(s, 0), 10);
s.phase = 1;
s.tasks = [{ kind: "defect", p: 0, debt: 6 }];
s = act(s, "discard", { gene: "streamline" });
assert.equal(s.tasks[0].debt, 1);
s = act(s, "pay-defect");
assert.equal(s.players[0].bp, 19);
s = fixture(n, [], "genes");
s.players[1].genes = [{ id: "streamline", round: 1 }];
s.players[2].genes = [{ id: "streamline", round: 1 }];
assert.ok(!E.legalMoves(s).some((m) => m.gene === "streamline"));
}
assert.equal(E.geneFor("frugality", 4).cost, 6);
assert.equal(E.geneFor("struggle", 4).mutation, 4);
});
test("unmodified amoebas eat four distinct foreign colours, choosing the omitted colour with six players", () => {
for (const n of [5, 6]) {
let s = fixture(n);
const meals = E.menus(s, E.amoeba(s, "0-1"));
assert.equal(meals.length, n === 5 ? 1 : 5);
for (const { food } of meals) {
assert.equal(food[0], 0);
assert.equal(food.filter((x) => x === 1).length, 4);
}
const cell = E.amoeba(s, "0-1").cell;
s = act(s, "eat");
assert.equal(s.food[cell][0], 4);
assertConserved(s);
s = fixture(n);
feedAt(s, [0, 4, 4, ...Array(n - 3).fill(0)]);
assert.equal(E.menus(s, E.amoeba(s, "0-1")).length, 0);
s = act(s, "starve");
assert.equal(E.amoeba(s, "0-1").hp, 1);
}
});
test("five/six-player feeding adaptations combine without accepting the old three-cube diet", () => {
for (const n of [5, 6]) {
let s = fixture(n, ["substitution"]);
feedAt(s, [0, 3, 1, 1, ...Array(n - 4).fill(0)]);
assert.ok(E.menus(s, E.amoeba(s, "0-1")).some((m) => m.food[1] === 3));
assertConserved(act(s, "eat"));
s = fixture(n, ["frugality"]);
feedAt(s, [0, 1, 1, 1, ...Array(n - 4).fill(0)]);
assert.ok(E.menus(s, E.amoeba(s, "0-1")).length);
s = fixture(n, ["frugality", "substitution", "parasitism"]);
E.amoeba(s, "1-1").cell = E.amoeba(s, "0-1").cell;
feedAt(s, [0, 0, 3, ...Array(n - 3).fill(0)]);
assert.ok(
E.menus(s, E.amoeba(s, "0-1")).some(
(m) => m.host === 1 && m.food[2] === 3,
),
);
const after = act(s, "eat");
assert.equal(after.players[1].bp, 19);
assertConserved(after);
s.players[1].bp = 0;
assert.equal(E.menus(s, E.amoeba(s, "0-1")).length, 0);
}
});
test("five/six-player income, population limit and scoring use the expansion rules", () => {
for (const n of [5, 6]) {
let s = fixture(n, [], "genes");
s = act(s, "done");
assert.equal(s.players[0].bp, 31);
s = fixture(n, [], "division");
s.amoebas = s.amoebas.filter((a) => a.p !== 0);
for (let i = 1; i <= 6; i++)
s.amoebas.push({ id: `0-${i}`, p: 0, n: i, cell: E.CELLS[i + 6], hp: 0 });
assert.ok(!E.legalMoves(s).some((m) => m.type === "divide"));
assert.equal(E.evolution(s, 0), 5);
assert.deepEqual(
Array.from({ length: 7 }, (_, i) => E.populationPoints(i, n)),
[0, 0, 1, 2, 3, 4, 5],
);
assert.ok(!scoringLegend(6, 2, "burgundy", false, n).includes("undefined"));
}
assert.ok(food(4).includes("black"));
assert.ok(food(5).includes("burgundy"));
});
test("complete five/six-player games conserve resources and replay after save/reload", () => {
for (const n of [5, 6])
for (let seed = 0; seed < 3; seed++) {
let s = E.init(n, [], {}, `complete-six-${seed}`),
steps = 0;
while (!E.ended(s) && steps++ < 2000) {
s = E.moveAI(s, E.currentPlayer(s));
if (steps % 17 === 0) s = JSON.parse(JSON.stringify(s));
assertConserved(s);
}
assert.ok(E.ended(s));
assert.ok(s.round <= 10);
const replay = E.replay(s);
assert.deepEqual(E.scores(replay), E.scores(s));
assert.deepEqual(replay.amoebas, s.amoebas);
assert.equal(replay.winner, s.winner);
}
});