423 lines
23 KiB
JavaScript
423 lines
23 KiB
JavaScript
#!/usr/bin/env node
|
||
// verifyChineseCheckers.js — headless checks for Chinese Checkers ("Moonlit
|
||
// Tea Pavilion"):
|
||
// • topology: 121 holes, six 10-hole points, neighbour / jump tables, the
|
||
// rows-to-travel metric
|
||
// • seatings for 2–6 players (opposite / alternating / pairs)
|
||
// • rules on hand-built positions: steps, multi-hop chains (path
|
||
// continuity), passing through forbidden points, no re-entering home,
|
||
// target pegs stay in the target, strict vs anti-blocking finishes, turn
|
||
// order skipping finishers, game end, partner teams, boxed-in players,
|
||
// stalemate passes and the no-progress detector
|
||
// • AI: every skill returns a legal move, takes a finishing move, null
|
||
// when boxed in
|
||
// • self-play soak at every player count (and teams / no anti-block):
|
||
// games finish, no two marbles share a hole, nobody rests where they
|
||
// mayn't, home flags never un-set
|
||
// • skill ladder (mirrored seats, two players)
|
||
// • layout: plaques, cards and buttons on screen, clear of each other and
|
||
// of the board
|
||
// Usage: node tools/verifyChineseCheckers.js [--quick]
|
||
|
||
import * as L from '../src/games/chinesecheckers/ChineseCheckersLogic.js';
|
||
import * as AI from '../src/games/chinesecheckers/ChineseCheckersAI.js';
|
||
import * as Lay from '../src/games/chinesecheckers/ChineseCheckersLayout.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}`);
|
||
const id = (q, r) => { const v = L.cellId(q, r); if (v < 0) throw new Error(`no cell ${q},${r}`); return v; };
|
||
AI.SKILL[5].budgetMs = 0;
|
||
|
||
// A position: pegs given per colour (cell ids, padded from an unused pool far
|
||
// from the action when fewer than 10 are listed).
|
||
function pos(seats, pegs, { current = 0, left = null, opts = {} } = {}) {
|
||
const s = L.createInitialState(seats, opts);
|
||
const used = new Set(Object.values(pegs).flat());
|
||
for (const c of seats) {
|
||
if (pegs[c]) {
|
||
const list = pegs[c].slice();
|
||
// pad with home cells not otherwise used
|
||
for (const h of L.TRI_CELLS[L.HOME[c]]) if (list.length < L.PEGS && !used.has(h)) { list.push(h); used.add(h); }
|
||
s.pegs[c] = list;
|
||
}
|
||
for (const h of s.pegs[c]) used.add(h);
|
||
}
|
||
if (left) for (const [c, arr] of Object.entries(left)) s.left[c] = arr.slice();
|
||
s.current = current;
|
||
return s;
|
||
}
|
||
const dests = (s, c, i) => L.getMovesForPeg(s, c, i).map((m) => m.to).sort((a, b) => a - b);
|
||
|
||
// ── topology ────────────────────────────────────────────────────────────────
|
||
section('Topology');
|
||
{
|
||
ok('121 holes', L.NUM_CELLS === 121);
|
||
const byTri = {};
|
||
for (const c of L.CELLS) byTri[c.tri] = (byTri[c.tri] ?? 0) + 1;
|
||
ok('61 centre holes', byTri.center === 61);
|
||
ok('six points of ten', Object.keys(L.TRIANGLES).every((t) => byTri[t] === 10));
|
||
let sym = true, jumps = true;
|
||
for (let a = 0; a < L.NUM_CELLS; a++) {
|
||
for (let d = 0; d < 6; d++) {
|
||
const b = L.NEIGH[a * 6 + d];
|
||
if (b >= 0 && L.NEIGH[b * 6 + ((d + 3) % 6)] !== a) sym = false;
|
||
const j = L.JUMP[a * 6 + d];
|
||
if (j >= 0 && (b < 0 || L.NEIGH[b * 6 + d] !== j)) jumps = false;
|
||
}
|
||
}
|
||
ok('neighbour table symmetric', sym);
|
||
ok('jump = neighbour of neighbour, same direction', jumps);
|
||
for (const t of Object.keys(L.TRIANGLES)) {
|
||
const tip = L.TRI_CELLS[t][9];
|
||
ok(`${t}: tip is row 0`, L.ROWS[t][tip] === 0);
|
||
}
|
||
ok('ideal finish totals 20 rows', L.MIN_ROWS === 20);
|
||
const s = L.createInitialState(L.seatColors(6));
|
||
ok('every colour starts 120 rows above the ideal', s.seats.every((c) => L.rowsToGo(s, c) === 120));
|
||
ok('far tip is 16 rows', L.rowsLeft('blue', L.TRI_CELLS['r+'][9]) === 16);
|
||
ok('lateral 0 on the centre line', L.LATERAL['r-'][id(0, 0)] === 0 && L.LATERAL['r-'][id(2, -4)] === 0);
|
||
}
|
||
|
||
// ── seatings ────────────────────────────────────────────────────────────────
|
||
section('Seatings');
|
||
{
|
||
ok('2 → opposite points', L.seatColors(2).join() === 'blue,red' && L.OPPOSITE.blue === 'red');
|
||
const three = L.seatColors(3);
|
||
ok('3 → alternating, every target empty', three.every((c) => !three.includes(L.OWNER[L.TARGET[c]])));
|
||
const four = L.seatColors(4);
|
||
ok('4 → two opposite pairs', four.every((c) => four.includes(L.OPPOSITE[c])));
|
||
ok('5 → six-star minus one', L.seatColors(5).length === 5 && !L.seatColors(5).includes('green'));
|
||
ok('6 → all', L.seatColors(6).length === 6);
|
||
ok('human always seat 0 (south)', [2, 3, 4, 5, 6].every((n) => L.seatColors(n)[0] === L.HUMAN && L.HOME[L.HUMAN] === 'r+'));
|
||
ok('clamps', L.seatColors(1).length === 2 && L.seatColors(9).length === 6);
|
||
ok('teams only with six', L.createInitialState(L.seatColors(4), { teams: true }).teams === null);
|
||
const t6 = L.createInitialState(L.seatColors(6), { teams: true });
|
||
ok('six → three opposite pairs', t6.teams.length === 3 && t6.teams.every(([a, b]) => L.OPPOSITE[a] === b));
|
||
ok('partnerOf', L.partnerOf(t6, 'blue') === 'red' && L.partnerOf(L.createInitialState(L.seatColors(6)), 'blue') === null);
|
||
let threw = false;
|
||
try { L.createInitialState(['blue']); } catch (_) { threw = true; }
|
||
ok('rejects one seat', threw);
|
||
}
|
||
|
||
// ── rules ───────────────────────────────────────────────────────────────────
|
||
section('Rules');
|
||
{
|
||
const seats = L.seatColors(2);
|
||
// opening: only the front row and jumps over it move
|
||
const s0 = L.createInitialState(seats);
|
||
const opening = L.getAllValidMoves(s0, 'blue');
|
||
ok('opening has moves', opening.length > 0);
|
||
ok('opening: every move lands in the centre', opening.every((m) => L.CELLS[m.to].tri === 'center'));
|
||
ok('opening: back-row marbles can jump out', opening.some((m) => m.isJump));
|
||
|
||
// a three-hop ladder through the centre
|
||
const a = id(0, 2);
|
||
const s1 = pos(seats, { blue: [a, id(0, 1), id(0, -1), id(0, -3)], red: [] });
|
||
const m1 = L.getMovesForPeg(s1, 'blue', 0);
|
||
const far = m1.find((m) => m.to === id(0, -4));
|
||
ok('chain: 3-hop ladder reaches (0,-4)', !!far && far.path.length === 4, JSON.stringify(m1.map((m) => m.to)));
|
||
let cont = true;
|
||
for (const m of m1.filter((x) => x.isJump)) {
|
||
for (let k = 1; k < m.path.length; k++) {
|
||
const p = L.CELLS[m.path[k - 1]], q = L.CELLS[m.path[k]];
|
||
const mid = L.cellId((p.q + q.q) / 2, (p.r + q.r) / 2);
|
||
if (L.hexDistance(m.path[k - 1], m.path[k]) !== 2 || !s1.pegs.blue.includes(mid)) cont = false;
|
||
}
|
||
}
|
||
ok('chain paths hop over a marble every time', cont);
|
||
ok('no duplicate landings', new Set(m1.map((m) => m.to)).size === m1.length);
|
||
ok('steps listed', m1.filter((m) => !m.isJump).every((m) => L.hexDistance(m.from, m.to) === 1));
|
||
|
||
// passing through a forbidden point (q+ = yellow home, empty in 2p)
|
||
// peg at (4,-1) jumps over (5,-2)… → (6,-3) is in q+ (forbidden rest), then back out over (5,-3)? build:
|
||
const p0 = id(3, 0), over1 = id(4, -1), land1 = id(5, -2); // land1 in q+
|
||
const over2 = id(4, -2), land2 = id(3, -2); // back into centre
|
||
const s2 = pos(seats, { blue: [p0, over1, over2], red: [] });
|
||
const m2 = L.getMovesForPeg(s2, 'blue', 0).map((m) => m.to);
|
||
ok('may not rest in a foreign point', L.CELLS[land1].tri === 'q+' && !m2.includes(land1));
|
||
ok('may pass through it mid-chain', m2.includes(land2), JSON.stringify(m2));
|
||
|
||
// no re-entering home once left: a marble just outside the front row
|
||
const homeCell = L.TRI_CELLS['r+'][0]; // (-1,5)
|
||
const s3 = L.createInitialState(seats);
|
||
s3.pegs.blue[0] = id(-1, 4); // stepped out
|
||
ok('home cell free for the test', !s3.pegs.blue.includes(homeCell));
|
||
s3.left.blue[0] = true;
|
||
ok('left home: can\'t step back in', !dests(s3, 'blue', 0).includes(homeCell));
|
||
s3.left.blue[0] = false;
|
||
ok('never left: may step back in', dests(s3, 'blue', 0).includes(homeCell));
|
||
const s3b = L.applyMove(s3, L.getMovesForPeg(s3, 'blue', 0).find((m) => L.CELLS[m.to].tri === 'center'));
|
||
ok('applyMove marks home as left', s3b.left.blue[0] === true);
|
||
|
||
// inside the target only target moves (red has cleared out of the way)
|
||
const tgtCell = L.TRI_CELLS['r-'][0]; // (1,-5) base row
|
||
const s4 = L.createInitialState(seats);
|
||
s4.pegs.blue = [tgtCell, id(0, 0), id(1, 0), id(2, 0), id(-1, 0), id(-2, 0), id(0, 1), id(0, 2), id(1, 1), id(2, 1)];
|
||
s4.pegs.red = [id(-3, 3), id(-2, 3), id(-1, 3), id(-3, 2), id(-2, 2), id(-1, 2), id(-4, 3), id(-4, 2), id(-4, 4), id(-3, 4)];
|
||
s4.left.blue = Array(10).fill(true);
|
||
s4.left.red = Array(10).fill(true);
|
||
const d4 = L.getMovesForPeg(s4, 'blue', 0);
|
||
ok('target marble only moves within the target', d4.length > 0 && d4.every((m) => L.CELLS[m.to].tri === 'r-'), JSON.stringify(d4.map((m) => L.CELLS[m.to].tri)));
|
||
}
|
||
|
||
// ── finishing ───────────────────────────────────────────────────────────────
|
||
section('Finishing');
|
||
{
|
||
const seats = L.seatColors(2);
|
||
const tgt = L.TRI_CELLS['r-'];
|
||
// nine in the target, the tenth one step from the last hole
|
||
const last = tgt[1]; // (2,-5)
|
||
const start = id(2, -4);
|
||
const pegs = { blue: [...tgt.filter((t) => t !== last), start], red: [id(0, 0), id(1, 0), id(2, 0), id(3, 0), id(-1, 0), id(-2, 0), id(-3, 0), id(0, 1), id(1, 1), id(-1, 1)] };
|
||
const s = pos(seats, pegs, { left: { blue: Array(10).fill(true) } });
|
||
const win = L.getMovesForPeg(s, 'blue', 9).find((m) => m.to === last);
|
||
ok('finishing step exists', !!win);
|
||
const s2 = L.applyMove(s, win);
|
||
ok('strict fill finishes', L.isFinished(s2, 'blue') && s2.winner === 'blue' && s2.lastEvent?.type === 'finished');
|
||
ok('2 players: game over at the first finisher', s2.phase === 'game_over' && s2.finished.join() === 'blue,red' && s2.endReason === 'finished');
|
||
ok('AI takes the finishing move', L.sameMove(AI.chooseMove(s, 5), win) && L.sameMove(AI.chooseMove(s, 3), win));
|
||
|
||
// anti-blocking: one red marble never left its home (= blue's target)
|
||
const blocker = tgt[9];
|
||
const sb = (antiBlock) => {
|
||
const st = pos(seats, {
|
||
blue: [...tgt.filter((t) => t !== blocker && t !== last), start, id(0, 0)],
|
||
red: [blocker, id(-3, 3), id(-2, 3), id(-1, 3), id(-3, 2), id(-2, 2), id(-1, 2), id(-4, 3), id(-4, 2), id(-4, 4)],
|
||
}, { opts: { antiBlock }, left: { blue: Array(10).fill(true) } });
|
||
return L.applyMove(st, L.getMovesForPeg(st, 'blue', 8).find((m) => m.to === last));
|
||
};
|
||
ok('anti-block: 9 + a blocker counts as filled', L.isFinished(sb(true), 'blue'));
|
||
ok('strict: a blocker spoils the fill', !L.isFinished(sb(false), 'blue'));
|
||
|
||
// four players: finishers skipped, game ends when three are home
|
||
const s4 = L.createInitialState(L.seatColors(4));
|
||
s4.finished = ['orange', 'red'];
|
||
s4.current = 0;
|
||
const m = L.getAllValidMoves(s4, 'blue')[0];
|
||
const after = L.applyMove(s4, m);
|
||
ok('turn order skips finished seats', L.currentColor(after) === 'yellow');
|
||
const s5 = L.createInitialState(L.seatColors(4));
|
||
s5.finished = ['orange', 'red'];
|
||
s5.pegs.blue = [...L.TRI_CELLS['r-'].filter((x) => x !== id(2, -5)), id(2, -4)];
|
||
s5.left.blue = Array(10).fill(true);
|
||
s5.pegs.red = L.TRI_CELLS['r+'].slice(); // red parked at home is fine for the test
|
||
const fin = L.getMovesForPeg(s5, 'blue', 9).find((x) => L.CELLS[x.to].tri === 'r-');
|
||
const e5 = fin && L.applyMove(s5, fin);
|
||
ok('4 players: ends when three are home, last seat placed', e5 && e5.phase === 'game_over' && e5.finished.length === 4 && e5.finished[3] === 'yellow');
|
||
ok('rankings = finishing order', e5 && L.rankings(e5).join() === e5.finished.join());
|
||
|
||
// teams: first pair home wins
|
||
const t = L.createInitialState(L.seatColors(6), { teams: true });
|
||
t.finished = ['red'];
|
||
t.pegs.blue = [...L.TRI_CELLS['r-'].filter((x) => x !== id(2, -5)), id(2, -4)];
|
||
t.left.blue = Array(10).fill(true);
|
||
t.pegs.red = L.TRI_CELLS['r+'].slice();
|
||
const tf = L.getMovesForPeg(t, 'blue', 9).find((x) => L.CELLS[x.to].tri === 'r-');
|
||
const te = tf && L.applyMove(t, tf);
|
||
ok('teams: pair home ends the game', te && te.phase === 'game_over' && te.endReason === 'team' && te.winningTeam.slice().sort().join() === 'blue,red');
|
||
ok('teams: winners rank first', te && L.rankings(te).slice(0, 2).sort().join() === 'blue,red');
|
||
ok('teamRankings puts the winners first', te && L.teamRankings(te)[0].sort().join() === 'blue,red');
|
||
|
||
// stalemate: everyone passes in a row
|
||
let st = L.createInitialState(L.seatColors(3));
|
||
for (let k = 0; k < 3; k++) st = L.passTurn(st);
|
||
ok('a full round of passes ends the game', st.phase === 'game_over' && st.endReason === 'stalemate' && st.finished.length === 3);
|
||
ok('passes counted', st.stats.blue.passes === 1);
|
||
|
||
// boxed in for good: blue's never-moved tip marble walled in by finished red
|
||
const boxed = (n) => {
|
||
const st = L.createInitialState(L.seatColors(n));
|
||
const tip = id(-4, 8);
|
||
st.pegs.blue = [tip, id(0, 0), id(1, 0), id(2, 0), id(-1, 0), id(-2, 0), id(0, 1), id(1, 1), id(-1, 1), id(2, -1)];
|
||
st.left.blue = [false, ...Array(9).fill(true)];
|
||
st.pegs.red = [...L.TRI_CELLS['r+'].filter((x) => x !== tip), id(3, -3)];
|
||
st.left.red = Array(10).fill(true);
|
||
st.finished = ['red'];
|
||
st.winner = 'red';
|
||
st.current = 1; // orange (4p) / purple (3p has no red — skip)
|
||
const mv = L.getAllValidMoves(st)[0];
|
||
return L.applyMove(st, mv);
|
||
};
|
||
const b4 = boxed(4);
|
||
ok('boxed in: detected after the next move', b4.stuck.includes('blue') && b4.newlyStuck.includes('blue'));
|
||
ok('boxed in: no moves offered', L.getAllValidMoves({ ...b4, current: 0 }, 'blue').length === 0);
|
||
ok('boxed in: game plays on with two racing', b4.phase === 'play' && L.isActive(b4, 'orange') && L.isActive(b4, 'yellow'));
|
||
// walk the turn order once: blue is never on move
|
||
let w = b4, sawBlue = false;
|
||
for (let k = 0; k < 6 && w.phase === 'play'; k++) { if (L.currentColor(w) === 'blue') sawBlue = true; const m = L.getAllValidMoves(w)[0]; w = m ? L.applyMove(w, m) : L.passTurn(w); }
|
||
ok('boxed in: turns skipped', !sawBlue);
|
||
const b5 = (() => { const st = L.cloneState(b4); st.finished.push('orange'); st.current = 3; return L.applyMove(st, L.getAllValidMoves(st, 'yellow')[0]); })();
|
||
ok('boxed in: one racer left → game over', b5.phase === 'game_over' && b5.endReason === 'boxed', `${b5.phase} ${b5.endReason}`);
|
||
ok('boxed in: ranks last', L.rankings(b5).at(-1) === 'blue' && L.rankings(b5).join() === 'red,orange,yellow,blue', L.rankings(b5).join());
|
||
|
||
// no progress: two seats shuffling forever end as a stalemate
|
||
let np = L.createInitialState(L.seatColors(2));
|
||
np.bestTotal = -1; // pretend we've already done better
|
||
let steps = 0;
|
||
while (np.phase === 'play' && steps++ < 1000) np = L.applyMove(np, L.getAllValidMoves(np)[0]);
|
||
ok('no new progress for STALL_ROUNDS rounds → stalemate', np.phase === 'game_over' && np.endReason === 'stalemate' && steps === L.STALL_ROUNDS * 2, `${np.endReason} after ${steps}`);
|
||
|
||
// clone independence
|
||
const c0 = L.createInitialState(L.seatColors(2));
|
||
const c1 = L.cloneState(c0);
|
||
c1.pegs.blue[0] = 0; c1.left.blue[0] = true; c1.stats.blue.moves = 9;
|
||
ok('cloneState is deep', c0.pegs.blue[0] !== 0 && !c0.left.blue[0] && c0.stats.blue.moves === 0);
|
||
ok('stats: hops and longest', (() => {
|
||
const s = L.createInitialState(L.seatColors(2));
|
||
const j = L.getAllValidMoves(s).find((x) => x.isJump);
|
||
const a2 = L.applyMove(s, j);
|
||
return a2.stats.blue.jumps === 1 && a2.stats.blue.hops === j.path.length - 1 && a2.stats.blue.longest === j.path.length - 1 && a2.lastMoves.length === 1;
|
||
})());
|
||
}
|
||
|
||
// ── AI legality ─────────────────────────────────────────────────────────────
|
||
section('AI');
|
||
{
|
||
const rng = AI.mulberry32(9);
|
||
let legal = true;
|
||
for (let g = 0; g < (QUICK ? 6 : 20); g++) {
|
||
let s = L.createInitialState(L.seatColors(2 + (g % 5)));
|
||
for (let k = 0; k < 40 + g * 3 && s.phase === 'play'; k++) {
|
||
const all = L.getAllValidMoves(s);
|
||
const mv = all[Math.floor(rng() * all.length)];
|
||
s = mv ? L.applyMove(s, mv) : L.passTurn(s);
|
||
}
|
||
if (s.phase !== 'play') continue;
|
||
const all = L.getAllValidMoves(s);
|
||
for (let k = 1; k <= 5; k++) {
|
||
const m = AI.chooseMove(s, k, { rng });
|
||
if (all.length ? !all.some((x) => L.sameMove(x, m)) : m !== null) legal = false;
|
||
}
|
||
}
|
||
ok('every skill picks a legal move', legal);
|
||
// boxed in: a marble with no move → null
|
||
const s = L.createInitialState(L.seatColors(2));
|
||
s.phase = 'game_over';
|
||
ok('no move after the game', AI.chooseMove(s, 3) === null);
|
||
const h = AI.hint(L.createInitialState(L.seatColors(2)));
|
||
ok('hint returns a move and a reason', h && h.move && typeof h.reason === 'string');
|
||
}
|
||
|
||
// ── soak ────────────────────────────────────────────────────────────────────
|
||
section('Self-play soak');
|
||
{
|
||
const plans = [];
|
||
for (const n of [2, 3, 4, 5, 6]) plans.push({ n, opts: {} });
|
||
plans.push({ n: 6, opts: { teams: true } }, { n: 6, opts: { antiBlock: false } }, { n: 2, opts: { antiBlock: false } });
|
||
const reps = QUICK ? 1 : 3;
|
||
let games = 0, worstMs = 0, stalemates = 0;
|
||
for (const { n, opts } of plans) {
|
||
for (let r = 0; r < reps; r++) {
|
||
const rng = AI.mulberry32(100 + r * 7 + n);
|
||
let s = L.createInitialState(L.seatColors(n), opts);
|
||
const skill = Object.fromEntries(s.seats.map((c, i) => [c, 1 + ((i + r) % 5)]));
|
||
let okRest = true, okOcc = true, okLeft = true;
|
||
const t0 = Date.now();
|
||
let guard = 0;
|
||
while (s.phase === 'play' && guard++ < 20000) {
|
||
const c = L.currentColor(s);
|
||
const prevLeft = s.left[c].slice();
|
||
const m = AI.chooseMove(s, skill[c], { rng });
|
||
s = m ? L.applyMove(s, m) : L.passTurn(s);
|
||
const occ = new Set();
|
||
for (const col of s.seats) {
|
||
s.pegs[col].forEach((cell, i) => {
|
||
if (occ.has(cell)) okOcc = false;
|
||
occ.add(cell);
|
||
const tri = L.CELLS[cell].tri;
|
||
const allowed = tri === 'center' || tri === L.TARGET[col] || (tri === L.HOME[col] && !s.left[col][i]);
|
||
if (!allowed) okRest = false;
|
||
});
|
||
}
|
||
if (prevLeft.some((v, i) => v && !s.left[c][i])) okLeft = false;
|
||
}
|
||
const ms = Date.now() - t0;
|
||
worstMs = Math.max(worstMs, ms);
|
||
games++;
|
||
const label = `${n}p${opts.teams ? ' teams' : ''}${opts.antiBlock === false ? ' strict' : ''} #${r}`;
|
||
// strict rules can spoiler-deadlock (the no-progress detector ends those);
|
||
// with anti-blocking every game must be played out
|
||
const fine = opts.antiBlock === false ? ['finished', 'team', 'boxed', 'stalemate'] : ['finished', 'team', 'boxed'];
|
||
ok(`${label}: ends normally`, s.phase === 'game_over' && fine.includes(s.endReason), `${s.endReason} after ${s.turn}`);
|
||
if (s.endReason === 'stalemate') stalemates++;
|
||
ok(`${label}: one marble per hole`, okOcc);
|
||
ok(`${label}: rests only where allowed`, okRest);
|
||
ok(`${label}: home flags never reset`, okLeft);
|
||
ok(`${label}: everyone ranked`, L.rankings(s).length === n);
|
||
}
|
||
}
|
||
console.log(` ${games} games, slowest ${worstMs} ms, ${stalemates} strict-rule stalemates`);
|
||
}
|
||
|
||
// ── ladder ──────────────────────────────────────────────────────────────────
|
||
section('Skill ladder (2 players, mirrored seats)');
|
||
{
|
||
const pairs = QUICK ? 8 : 25;
|
||
for (const [hi, lo] of [[2, 1], [3, 2], [4, 3], [5, 4]]) {
|
||
let wins = 0, n = 0;
|
||
for (let g = 0; g < pairs; g++) {
|
||
for (const hiSeat of [0, 1]) {
|
||
const rng = AI.mulberry32(4000 + g);
|
||
let s = L.createInitialState(L.seatColors(2));
|
||
const hc = s.seats[hiSeat];
|
||
let guard = 0;
|
||
while (s.phase === 'play' && guard++ < 5000) {
|
||
const c = L.currentColor(s);
|
||
let m;
|
||
if (s.turn < 4) { const ms = L.getAllValidMoves(s).filter((x) => L.moveGain(x) > 0); m = ms[Math.floor(rng() * ms.length)]; }
|
||
else m = AI.chooseMove(s, c === hc ? hi : lo, { rng });
|
||
s = m ? L.applyMove(s, m) : L.passTurn(s);
|
||
}
|
||
if (s.finished[0] === hc) wins++;
|
||
n++;
|
||
}
|
||
}
|
||
const pct = (100 * wins) / n;
|
||
console.log(` ${hi} beats ${lo}: ${pct.toFixed(1)}% of ${n}`);
|
||
ok(`skill ${hi} beats ${lo} most of the time`, pct > 55);
|
||
}
|
||
}
|
||
|
||
// ── layout ──────────────────────────────────────────────────────────────────
|
||
section('Layout');
|
||
{
|
||
const inScreen = (r) => r.x >= 0 && r.y >= 0 && r.x + r.w <= Lay.W && r.y + r.h <= Lay.H;
|
||
const overlap = (a, b) => a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h;
|
||
const hex = Lay.boardHex();
|
||
const inHex = (p) => {
|
||
let inside = true;
|
||
for (let i = 0; i < 6; i++) {
|
||
const a = hex[i], b = hex[(i + 1) % 6];
|
||
if ((b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x) < 0) inside = false;
|
||
}
|
||
return inside;
|
||
};
|
||
ok('every hole inside the board', Lay.CELL_XY.every((p) => inHex({ x: p.x, y: p.y }) && Lay.cellAt(p.x, p.y) >= 0));
|
||
ok('cellAt round-trips', Lay.CELL_XY.every((p, i) => Lay.cellAt(p.x, p.y) === i));
|
||
ok('hole spacing', Math.abs(Math.hypot(Lay.CELL_XY[L.cellId(1, 0)].x - Lay.CELL_XY[L.cellId(0, 0)].x, 0) - Lay.CELL) < 0.01);
|
||
const rects = {
|
||
...Object.fromEntries(Object.keys(Lay.PLATES).map((c) => [`plate ${c}`, Lay.plateRect(c)])),
|
||
status: Lay.STATUS, log: Lay.LOG,
|
||
...Object.fromEntries(Object.entries(Lay.BUTTONS).map(([k, b]) => [`btn ${k}`, { x: b.x - Lay.BTN.w / 2, y: b.y - Lay.BTN.h / 2, w: Lay.BTN.w, h: Lay.BTN.h }])),
|
||
};
|
||
for (const [k, r] of Object.entries(rects)) {
|
||
ok(`${k} on screen`, inScreen(r));
|
||
const corners = [{ x: r.x, y: r.y }, { x: r.x + r.w, y: r.y }, { x: r.x, y: r.y + r.h }, { x: r.x + r.w, y: r.y + r.h }];
|
||
ok(`${k} clear of the board`, corners.every((p) => !inHex(p)), JSON.stringify(r));
|
||
}
|
||
const keys = Object.keys(rects);
|
||
for (let i = 0; i < keys.length; i++) for (let j = i + 1; j < keys.length; j++) {
|
||
ok(`${keys[i]} / ${keys[j]} don't overlap`, !overlap(rects[keys[i]], rects[keys[j]]));
|
||
}
|
||
for (const c of Object.keys(Lay.PLATES)) ok(`bubble ${c} on screen`, inScreen(Lay.plateParts(c).bubble));
|
||
for (const t of Object.keys(L.TRIANGLES)) ok(`${t} outline inside the board`, Lay.triPolygon(t).every(inHex));
|
||
}
|
||
|
||
console.log(`\n${pass} passed, ${fail} failed`);
|
||
process.exit(fail ? 1 : 0);
|