277 lines
16 KiB
JavaScript
277 lines
16 KiB
JavaScript
#!/usr/bin/env node
|
|
// verifySkipBo.js — headless checks for Skip-Bo: rules (deal, plays, wilds,
|
|
// completing a pile, recycling, refills, discards, stuck passes, wins, match
|
|
// scoring), the AI (legal actions, the stock-chain search, skill timing), the
|
|
// dining-table layout for 2/3/4 players (on screen, on the table, clear of the
|
|
// HUD and each other, hit-tests), and an all-AI soak (every game ends, 162
|
|
// cards conserved, skill ordering).
|
|
// Usage: node tools/verifySkipBo.js [--quick]
|
|
|
|
import * as S from '../src/games/skipbo/SkipBoLogic.js';
|
|
import { chooseAction, planChain, suggestAction, suggestDiscard } from '../src/games/skipbo/SkipBoAI.js';
|
|
import { aiTiming, mulberry32 } from '../src/ui/cardtable/AITiming.js';
|
|
import {
|
|
CARD_W, CARD_H, cardBounds, inPose, fanHit, fanBounds, inRect, rectsOverlap, onScreen, hudFurniture, plateRect, unionRect,
|
|
} from '../src/ui/cardtable/TableGeometry.js';
|
|
import {
|
|
T, layoutFor, stockPose, discardPose, discardRect, buildPose, handPose, stockBadge, handHome, spotFor, placematCorners,
|
|
} from '../src/games/skipbo/SkipBoTable.js';
|
|
|
|
const QUICK = process.argv.includes('--quick');
|
|
let pass = 0, fail = 0;
|
|
function ok(label, cond, detail = '') {
|
|
if (cond) pass++;
|
|
else { console.error(` ✗ ${label}${detail ? ` — ${detail}` : ''}`); fail++; }
|
|
}
|
|
const section = (n) => console.log(`\n${n}`);
|
|
let nid = 9000;
|
|
const C = (value) => ({ value, id: nid++, playedAs: null });
|
|
|
|
function rig({ n = 2, hand = [], stock = [C(7)], discards = [[], [], [], []], builds = [[], [], [], []], current = 0, target = 0 }) {
|
|
const gs = S.createInitialState({ playerCount: n, seed: 5, target });
|
|
gs.currentPlayer = current;
|
|
const p = gs.players[current];
|
|
p.hand = hand;
|
|
p.stock = stock;
|
|
p.discards = discards;
|
|
gs.buildPiles = builds;
|
|
return gs;
|
|
}
|
|
const run = (vals) => vals.map(v => ({ value: v, id: nid++, playedAs: null }));
|
|
|
|
// ── Rules ────────────────────────────────────────────────────────────────────
|
|
section('Rules');
|
|
{
|
|
const deck = S.buildDeck();
|
|
ok('162 cards', deck.length === 162);
|
|
ok('12 of each number + 18 wilds', [1, 7, 12].every(v => deck.filter(c => c.value === v).length === 12) && deck.filter(c => c.value === 'wild').length === 18);
|
|
for (const n of [2, 3, 4]) {
|
|
for (const stockSize of [10, 20, 30]) {
|
|
const gs = S.createInitialState({ playerCount: n, seed: 3 + n, stockSize });
|
|
ok(`${n}p stock ${stockSize}: stocks dealt`, gs.players.every(p => p.stock.length === stockSize));
|
|
ok(`${n}p stock ${stockSize}: opener holds 5`, gs.players[0].hand.length === 5 && gs.players.slice(1).every(p => p.hand.length === 0));
|
|
ok(`${n}p stock ${stockSize}: 162 conserved`, S.countCards(gs) === 162);
|
|
}
|
|
}
|
|
}
|
|
{
|
|
const gs = rig({ hand: [C(1), C(2), C('wild'), C(9), C(9)], stock: [C(5), C(4)] });
|
|
ok('empty pile needs a 1', S.nextRequired(gs, 0) === 1 && S.canPlayOnBuild(gs, C(1), 0) && !S.canPlayOnBuild(gs, C(2), 0));
|
|
let g = S.applyPlay(gs, { source: 'hand', sourceIdx: 0, buildIdx: 0 });
|
|
ok('play from hand builds up', S.nextRequired(g, 0) === 2 && g.players[0].hand.length === 4);
|
|
g = S.applyPlay(g, { source: 'hand', sourceIdx: 0, buildIdx: 0 });
|
|
g = S.applyPlay(g, { source: 'hand', sourceIdx: 0, buildIdx: 0 });
|
|
ok('a wild plays as the needed number', S.buildPileTopValue(g, 0) === 3 && g.buildPiles[0][2].playedAs === 3);
|
|
g = S.applyPlay(g, { source: 'stock', sourceIdx: 0, buildIdx: 0 });
|
|
ok('stock top plays', S.nextRequired(g, 0) === 5 && g.players[0].stock.length === 1);
|
|
ok('play log records it', g.log.some(e => e.kind === 'play' && e.source === 'stock'));
|
|
}
|
|
{
|
|
const builds = [run([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]), [], [], []];
|
|
const gs = rig({ hand: [C(12), C(3), C(4), C(5), C(6)], builds });
|
|
const g = S.applyPlay(gs, { source: 'hand', sourceIdx: 0, buildIdx: 0 });
|
|
ok('reaching 12 clears the pile', g.buildPiles[0].length === 0 && g.completedPile.length === 12);
|
|
ok('completion is logged', g.log.some(e => e.kind === 'complete'));
|
|
ok('completed wilds forget their number', g.completedPile.every(c => c.playedAs === null));
|
|
}
|
|
{
|
|
const gs = rig({ hand: [C(1)], stock: [C(9), C(9)] });
|
|
const g = S.applyPlay(gs, { source: 'hand', sourceIdx: 0, buildIdx: 0 });
|
|
ok('emptying the hand draws five', g.players[0].hand.length === 5);
|
|
const d = rig({ hand: [C(8), C(3)], stock: [C(9), C(9)] });
|
|
const dn = S.applyDiscard(d, 0, 2);
|
|
ok('discard ends the turn', dn.currentPlayer === 1 && dn.players[0].discards[2].length === 1);
|
|
ok('next player is dealt up to five', dn.players[1].hand.length === 5);
|
|
ok('bad discard pile index rejected', S.applyDiscard(d, 0, 4) === d);
|
|
}
|
|
{
|
|
const gs = rig({ hand: [C(2)], stock: [C(1)] });
|
|
gs.drawPile = [];
|
|
gs.completedPile = run([1, 2, 3, 4, 5, 6]);
|
|
const g = S.applyPlay(gs, { source: 'stock', sourceIdx: 0, buildIdx: 0 });
|
|
ok('stock emptied → win', g.phase === 'gameover' && g.winner === 0);
|
|
const r = rig({ hand: [C(5)], stock: [C(9), C(9)] });
|
|
r.drawPile = [];
|
|
r.completedPile = run([1, 2, 3, 4, 5, 6, 7]);
|
|
const r2 = S.applyPlay(rig({ hand: [C(1)], stock: [C(9), C(9)] }), { source: 'hand', sourceIdx: 0, buildIdx: 0 });
|
|
void r2;
|
|
r.players[0].hand = [C(1)];
|
|
const rr = S.applyPlay(r, { source: 'hand', sourceIdx: 0, buildIdx: 0 });
|
|
ok('empty draw pile recycles the completed piles', rr.players[0].hand.length === 5 && rr.log.some(e => e.kind === 'recycle'));
|
|
}
|
|
{
|
|
const gs = rig({ hand: [], stock: [C(9)] });
|
|
gs.drawPile = []; gs.completedPile = [];
|
|
ok('no hand, no draw, no play → stuck', S.isPlayerStuck(gs));
|
|
const p = S.applyAutoPass(gs);
|
|
ok('stuck player passes', p.currentPlayer === 1 && p.consecutiveStuckTurns === 1);
|
|
}
|
|
{
|
|
const gs = rig({ n: 3, hand: [C(2)], stock: [C(1)], target: 500 });
|
|
gs.players[1].stock = run([5, 5, 5]);
|
|
gs.players[2].stock = run([5]);
|
|
const g = S.applyPlay(gs, { source: 'stock', sourceIdx: 0, buildIdx: 0 });
|
|
ok('match: winner scores 25 + 5 per stock card left', g.scores[0] === 25 + 20 && g.gameResult.points === 45);
|
|
ok('match: below target → next game', g.phase === 'handOver');
|
|
const nx = S.startNextGame(g);
|
|
ok('next game keeps scores, rotates the opener', nx.scores[0] === 45 && nx.gameNum === 2 && nx.currentPlayer === 1);
|
|
const big = rig({ hand: [C(2)], stock: [C(1)], target: 30 });
|
|
big.players[1].stock = run([5]);
|
|
ok('match: reaching the target ends it', S.applyPlay(big, { source: 'stock', sourceIdx: 0, buildIdx: 0 }).phase === 'gameover');
|
|
}
|
|
|
|
// ── AI ───────────────────────────────────────────────────────────────────────
|
|
section('AI');
|
|
{
|
|
ok('slower hands at low skill', aiTiming(1).carry > aiTiming(3).carry && aiTiming(3).carry > aiTiming(5).carry);
|
|
// chain: stock needs 4; builds at 1 (needs 2); hand has 2 and 3
|
|
const gs = rig({ hand: [C(9), C(3), C(11), C(2), C(10)], stock: [C(4)], builds: [run([1]), [], [], []] });
|
|
const ch = planChain(gs, 0, 6);
|
|
ok('chain search finds 2 → 3 → stock 4', ch && ch.source === 'hand' && gs.players[0].hand[ch.sourceIdx].value === 2 && ch.buildIdx === 0);
|
|
const a5 = chooseAction(gs, { skill: 5, rng: () => 0.5 });
|
|
ok('skill 5 starts the chain', a5.type === 'play' && a5.source === 'hand' && gs.players[0].hand[a5.sourceIdx].value === 2);
|
|
const viaDiscard = rig({ hand: [C(9), C(10), C(11), C(12), C(12)], stock: [C(3)], discards: [run([2]), [], [], []], builds: [run([1]), [], [], []] });
|
|
const cd = planChain(viaDiscard, 0, 6);
|
|
ok('chain uses discard tops', cd && cd.source === 'discard' && cd.sourceIdx === 0);
|
|
const stockFirst = rig({ hand: [C(1), C(2)], stock: [C(1)] });
|
|
ok('stock card always first', chooseAction(stockFirst, { skill: 3, rng: () => 0.5 }).source === 'stock');
|
|
const block = rig({ n: 2, hand: [C(5), C(9), C(9), C(10), C(10)], stock: [C(12)], builds: [run([1, 2, 3, 4]), [], [], []] });
|
|
block.players[1].stock = run([6]);
|
|
const ab = chooseAction(block, { skill: 4, rng: () => 0.5 });
|
|
ok("skill 4 won't build up to the next player's stock card", !(ab.type === 'play' && ab.source === 'hand' && block.players[0].hand[ab.sourceIdx].value === 5));
|
|
const sd = suggestDiscard(block);
|
|
ok('suggested discard is legal', sd.type === 'discard' && sd.handIdx >= 0 && sd.handIdx < 5 && sd.discardIdx >= 0 && sd.discardIdx < 4);
|
|
const sug = suggestAction(rig({ hand: [C(1)], stock: [C(9), C(9)] }));
|
|
ok('hint suggests a play', sug.type === 'play');
|
|
}
|
|
|
|
// ── Layout ───────────────────────────────────────────────────────────────────
|
|
section('Layout');
|
|
for (const n of [2, 3, 4]) {
|
|
const L = layoutFor(n);
|
|
const furn = hudFurniture();
|
|
for (let s = 1; s < n; s++) furn.push({ name: `plate ${s}`, r: plateRect(L.seats[s].plate) });
|
|
for (let i = 0; i < furn.length; i++) for (let j = i + 1; j < furn.length; j++) ok(`${n}p: ${furn[i].name} clear of ${furn[j].name}`, !rectsOverlap(furn[i].r, furn[j].r));
|
|
const centre = [L.draw, ...L.builds, L.completed].map(p => cardBounds({ ...p, y: p.y - 12 }));
|
|
for (let i = 0; i < centre.length; i++) for (let j = i + 1; j < centre.length; j++) ok(`${n}p: centre piles ${i}/${j} apart`, !rectsOverlap(centre[i], centre[j]));
|
|
const centreBox = centre.reduce((a, b) => unionRect(a, b), null);
|
|
const centreArea = { ...centreBox, y0: centreBox.y0 - 6, y1: centreBox.y1 + 30 }; // 'needs N' labels below
|
|
ok(`${n}p: centre row on the table`, T.onFelt({ x: centreBox.x0, y: centreBox.y0 }) && T.onFelt({ x: centreBox.x1, y: centreBox.y1 }));
|
|
const tableau = [];
|
|
for (let s = 0; s < n; s++) {
|
|
let tb = cardBounds({ ...stockPose(L, s, 30), y: stockPose(L, s, 30).y });
|
|
for (let d = 0; d < 4; d++) for (const len of [1, 12]) tb = unionRect(tb, discardRect(L, s, d, len, 0));
|
|
tableau[s] = tb;
|
|
ok(`${n}p seat ${s}: tableau on screen`, onScreen(tb), JSON.stringify(tb));
|
|
ok(`${n}p seat ${s}: placemat on the table`, placematCorners(L, s).every(p => T.onFelt(p)));
|
|
ok(`${n}p seat ${s}: tableau clear of the centre`, !rectsOverlap(tb, centreArea), `${JSON.stringify(tb)} vs ${JSON.stringify(centreArea)}`);
|
|
for (const f of furn) ok(`${n}p seat ${s}: tableau clear of ${f.name}`, !rectsOverlap(tb, f.r), `${JSON.stringify(tb)} vs ${JSON.stringify(f.r)}`);
|
|
const hb = fanBounds(L.seats[s].fan, 5, s === 0 ? 40 : 0);
|
|
ok(`${n}p seat ${s}: hand on screen`, onScreen(hb, s === 0 ? 50 : 10));
|
|
for (const f of furn) ok(`${n}p seat ${s}: hand clear of ${f.name}`, !rectsOverlap(hb, f.r), `${JSON.stringify(hb)} vs ${JSON.stringify(f.r)}`);
|
|
ok(`${n}p seat ${s}: hand clear of the centre`, !rectsOverlap(hb, centreArea));
|
|
const b = stockBadge(L, s);
|
|
const br = { x0: b.x - 19, x1: b.x + 19, y0: b.y - 30, y1: b.y + 19 };
|
|
ok(`${n}p seat ${s}: stock badge on screen`, onScreen(br));
|
|
for (const f of furn) ok(`${n}p seat ${s}: badge clear of ${f.name}`, !rectsOverlap(br, f.r));
|
|
if (s > 0) {
|
|
const hh = handHome(L, s);
|
|
ok(`${n}p seat ${s}: ghost hand rests on screen`, hh.x > 0 && hh.x < 1920 && hh.y > 0 && hh.y < 1080);
|
|
}
|
|
ok(`${n}p seat ${s}: spotlight sized`, spotFor(L, s).w > 100);
|
|
}
|
|
for (let s = 0; s < n; s++) {
|
|
for (let t = s + 1; t < n; t++) ok(`${n}p tableaux ${s}/${t} apart`, !rectsOverlap(tableau[s], tableau[t]));
|
|
for (let t = 0; t < n; t++) {
|
|
if (t === s) continue;
|
|
ok(`${n}p hand ${s} clear of tableau ${t}`, !rectsOverlap(fanBounds(L.seats[s].fan, 5), tableau[t]));
|
|
}
|
|
}
|
|
ok(`${n}p: human hand clear of own tableau`, !rectsOverlap(fanBounds(L.seats[0].fan, 5, 40), tableau[0]));
|
|
// hit-tests
|
|
let rt = true;
|
|
for (let i = 0; i < 5; i++) {
|
|
const p = handPose(L, 0, i, 5);
|
|
if (fanHit(L.seats[0].fan, 5, { x: p.x - CARD_W * 0.3, y: p.y - CARD_H * 0.3 }) !== i) rt = false;
|
|
}
|
|
ok(`${n}p: hand hit-test round trip`, rt);
|
|
ok(`${n}p: stock top hit`, inPose(stockPose(L, 0, 12), stockPose(L, 0, 12)));
|
|
let dt = true;
|
|
for (let d = 0; d < 4; d++) for (const len of [1, 6, 14]) if (!inRect(discardPose(L, 0, d, len - 1, len), discardRect(L, 0, d, len, 4))) dt = false;
|
|
ok(`${n}p: discard tops inside their drop rects`, dt);
|
|
let ov = true;
|
|
for (let d = 0; d < 3; d++) if (rectsOverlap(discardRect(L, 0, d, 12, 4), discardRect(L, 0, d + 1, 12, 4))) ov = false;
|
|
ok(`${n}p: discard drop rects don't overlap`, ov);
|
|
ok(`${n}p: build piles hit`, L.builds.every((b, i) => inPose(buildPose(L, i, 5), buildPose(L, i, 5))));
|
|
}
|
|
|
|
// ── Soak ─────────────────────────────────────────────────────────────────────
|
|
section('Soak');
|
|
function game(skills, seed, { stockSize = 30, target = 0 } = {}) {
|
|
const rand = mulberry32(seed);
|
|
let gs = S.createInitialState({ playerCount: skills.length, seed, stockSize, target });
|
|
let steps = 0, noop = 0, conserve = true;
|
|
while (!S.isGameDone(gs) && steps++ < 30000) {
|
|
if (S.isPlayerStuck(gs)) { gs = S.applyAutoPass(gs); continue; }
|
|
const a = chooseAction(gs, { skill: skills[gs.currentPlayer], rng: rand });
|
|
let nx = !a ? gs : a.type === 'play' ? S.applyPlay(gs, a) : S.applyDiscard(gs, a.handIdx, a.discardIdx);
|
|
if (!a) { gs = S.applyAutoPass(gs); continue; } // empty hand, nothing worth playing
|
|
if (nx === gs) { noop++; nx = S.applyDiscard(gs, 0, 0); if (nx === gs) break; }
|
|
gs = nx;
|
|
if (S.countCards(gs) !== 162) conserve = false;
|
|
}
|
|
return { gs, done: S.isGameDone(gs), noop, conserve };
|
|
}
|
|
{
|
|
const M = QUICK ? 15 : 60;
|
|
for (const n of [2, 3, 4]) {
|
|
for (const stockSize of [10, 30]) {
|
|
let done = 0, noops = 0, conserved = true, draws = 0, turns = 0;
|
|
for (let i = 0; i < M; i++) {
|
|
const r = game(Array.from({ length: n }, (_, s) => 1 + ((i + s * 2) % 5)), 200 + i * 13 + n, { stockSize });
|
|
if (r.done) done++;
|
|
noops += r.noop;
|
|
if (!r.conserve) conserved = false;
|
|
if (r.gs.winner === -1) draws++;
|
|
turns += r.gs.turnCount;
|
|
}
|
|
const tag = `${n}p stock ${stockSize}`;
|
|
ok(`${tag}: every game ends`, done === M, `${done}/${M}`);
|
|
ok(`${tag}: AI never makes an illegal move`, noops === 0, `${noops}`);
|
|
ok(`${tag}: 162 cards conserved`, conserved);
|
|
ok(`${tag}: stalemates are rare`, draws <= Math.ceil(M * 0.1), `${draws}`);
|
|
console.log(` ${tag}: avg ${(turns / M).toFixed(0)} turns, ${draws} stalemates`);
|
|
}
|
|
}
|
|
// a short match to 500 finishes
|
|
let gs = S.createInitialState({ playerCount: 3, seed: 77, stockSize: 10, target: 200 });
|
|
const rand = mulberry32(77);
|
|
let games = 0;
|
|
while (gs.phase !== 'gameover' && games < 40) {
|
|
let st = 0;
|
|
while (!S.isGameDone(gs) && st++ < 30000) {
|
|
if (S.isPlayerStuck(gs)) { gs = S.applyAutoPass(gs); continue; }
|
|
const a = chooseAction(gs, { skill: 3, rng: rand });
|
|
gs = !a ? S.applyAutoPass(gs) : a.type === 'play' ? S.applyPlay(gs, a) : S.applyDiscard(gs, a.handIdx, a.discardIdx);
|
|
}
|
|
games++;
|
|
if (gs.phase === 'handOver') gs = S.startNextGame(gs);
|
|
}
|
|
ok('a match to 200 finishes with a winner at the target', gs.phase === 'gameover' && gs.scores[gs.matchWinner] >= 200, `${games} games`);
|
|
|
|
const duel = (a, b, M2) => {
|
|
let w = 0;
|
|
for (let i = 0; i < M2; i++) { const sa = i % 2; if (game(sa ? [b, a] : [a, b], 5000 + i).gs.winner === sa) w++; }
|
|
return w / M2;
|
|
};
|
|
const G = QUICK ? 80 : 300;
|
|
const r51 = duel(5, 1, G), r31 = duel(3, 1, G), r53 = duel(5, 3, G);
|
|
console.log(` win rates: 5v1 ${(r51 * 100).toFixed(0)}% · 3v1 ${(r31 * 100).toFixed(0)}% · 5v3 ${(r53 * 100).toFixed(0)}%`);
|
|
ok('skill 5 beats skill 1', r51 >= 0.8);
|
|
ok('skill 3 beats skill 1', r31 >= 0.75);
|
|
ok('skill 5 beats skill 3', r53 >= (QUICK ? 0.55 : 0.58));
|
|
}
|
|
|
|
console.log(`\n${pass} passed, ${fail} failed`);
|
|
process.exit(fail ? 1 : 0);
|