#!/usr/bin/env node // verifyCheckers.js — headless checks for Checkers ("Gus's Back Room"): // • geometry: 32 squares, standard numbering, diagonal rays // • rules on hand-built positions, per variant: steps, forced capture, // multi-jumps, crowning stops a chain (American) / continues as a man // (Pool) / continues as a flying king (Russian), flying kings, Turkish // strike (no double jumps), giveaway wins, casual optional jumps // • draws: threefold repetition, the 40-move no-progress rule, agreement, // resignation // • notation: every legal move round-trips through notate/parseMove // • every ballot opening is legal from the start // • AI: every skill returns a legal move in budget; finds short shots; // draw decisions are sane // • self-play soak per variant: games end, piece counts never grow, no // piece on a light square // • skill ladder (mirrored colours) // • layout: HUD rectangles on screen and clear of the board // Usage: node tools/verifyCheckers.js [--quick] import * as L from '../src/games/checkers/CheckersLogic.js'; import * as AI from '../src/games/checkers/CheckersAI.js'; import { BALLOTS } from '../src/games/checkers/CheckersData.js'; import { mulberry32 } from '../src/ui/cardtable/AITiming.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 sq = (n) => n - 1; // Build a state from { b: [squares], B: [kings], r: [...], R: [...] }. function pos(spec, { variant = 'american', turn = 'black' } = {}) { const gs = L.createGame({ variant }); const board = new Array(32).fill(0); for (const n of spec.b ?? []) board[sq(n)] = 1; for (const n of spec.B ?? []) board[sq(n)] = 2; for (const n of spec.r ?? []) board[sq(n)] = -1; for (const n of spec.R ?? []) board[sq(n)] = -2; return { ...gs, board, turn, reps: { [L.posKey(board, turn)]: 1 } }; } const names = (gs) => L.legalMoves(gs).map(L.notate).sort(); // ── geometry ───────────────────────────────────────────────────────────────── section('geometry'); ok('32 squares', L.RC.length === 32); ok('square 1 is row 0 col 1', L.RC[0][0] === 0 && L.RC[0][1] === 1); ok('square 5 is row 1 col 0', L.RC[4][0] === 1 && L.RC[4][1] === 0); ok('square 32 is row 7 col 6', L.RC[31][0] === 7 && L.RC[31][1] === 6); ok('all dark', L.RC.every(([r, c]) => (r + c) % 2 === 1)); ok('ray from 1 down-right', L.RAYS[0][3].map((i) => i + 1).join(',') === '6,10,15,19,24,28', L.RAYS[0][3].map((i) => i + 1).join(',')); { const b = L.initialBoard(); const c = L.counts(b); ok('12 v 12 at start', c.black === 12 && c.red === 12); } // ── basic American rules ───────────────────────────────────────────────────── section('american rules'); { const gs = L.createGame(); ok('black moves first', gs.turn === 'black'); ok('7 opening moves', L.legalMoves(gs).length === 7, names(gs).join(' ')); ok('11-15 legal', names(gs).includes('11-15')); const s2 = L.applyMove(gs, L.parseMove(gs, '11-15')); ok('turn passes', s2.turn === 'red' && s2.ply === 1); ok('history notated', s2.history[0].n === '11-15'); } { // forced capture: black 15, red 18 → 15x22 only (22 empty) const gs = pos({ b: [15, 1], r: [18, 30] }); ok('capture forced', names(gs).join(' ') === '15x22', names(gs).join(' ')); } { // men don't capture backwards in American const gs = pos({ b: [15], r: [11, 30] }); ok('no backward man jump', !names(gs).some((n) => n.includes('x')), names(gs).join(' ')); } { // double jump: black 6 over red 10 and 19 → 6x15x24 const gs = pos({ b: [6], r: [10, 19, 32] }); ok('double jump', names(gs).join(' ') === '6x15x24', names(gs).join(' ')); const s = L.applyMove(gs, L.legalMoves(gs)[0]); ok('both captured', L.counts(s.board).red === 1); } { // crowning stops the chain: black 22 over 26 to 31 (crown row), a red on 27 that a king could jump next const gs = pos({ b: [22], r: [26, 27, 3] }); const ms = L.legalMoves(gs); ok('crown ends turn (American)', ms.length === 1 && L.notate(ms[0]) === '22x31' && ms[0].crown, ms.map(L.notate).join(' ')); } { // king steps one square only const gs = pos({ B: [18], r: [1] }); ok('king steps 4 ways', names(gs).length === 4, names(gs).join(' ')); } { // Turkish strike: a king can't jump the same piece twice. // black king 19 (r4c5)… use a ring: B on 10, reds on 14,15,22,23 — loop 10x17x26x19x10 legal (4 caps) but not reusing const gs = pos({ B: [10], r: [14, 15, 22, 23] }); const ms = L.legalMoves(gs); ok('ring capture exists', ms.some((m) => m.caps.length === 4), ms.map(L.notate).join(' ')); ok('no piece jumped twice', ms.every((m) => new Set(m.caps).size === m.caps.length)); } // ── pool / russian ─────────────────────────────────────────────────────────── section('pool & russian'); { const gs = pos({ b: [15], r: [11, 30] }, { variant: 'pool' }); ok('pool man jumps backward', names(gs).join(' ') === '15x8', names(gs).join(' ')); } { // flying king on 1 sees red on 15 (ray 6,10,15,19,24,28): lands 19, 24 or 28 const gs = pos({ B: [1], r: [15, 4] }, { variant: 'pool' }); ok('flying king captures at range', names(gs).join(' ') === '1x19 1x24 1x28', names(gs).join(' ')); const q = pos({ B: [1], r: [4] }, { variant: 'pool' }); ok('flying king slides', names(q).length === 7, names(q).join(' ')); } { // must land where the capture continues: B on 1, red 10 and red 20 → from 15? (ray 1:6,10,15,19,24,28) // red at 10: lands 15,19,24,28. From 15 (r3c4) down-left ray 18? put red 18: 15x22 continues → only 1x15x22 const gs = pos({ B: [1], r: [10, 18, 4] }, { variant: 'pool' }); ok('flying king must choose continuing landing', names(gs).every((n) => n.split('x').length === 3), names(gs).join(' ')); } { // a black man reaching row 7 mid-jump: 22 over 26 → 31 then 31 could jump 27? men backwards allowed in pool: 31 over 27 → 24? (31 r7c4, 27 r6c5 → 24? r5c6 is sq 24) const pool = pos({ b: [22], r: [26, 27, 1] }, { variant: 'pool' }); const pm = L.legalMoves(pool); ok('pool: continues as a man through the crown row', pm.length === 1 && L.notate(pm[0]) === '22x31x24' && !pm[0].crown, pm.map(L.notate).join(' ')); const rus = pos({ b: [22], r: [26, 27, 1] }, { variant: 'russian' }); const rm = L.legalMoves(rus); ok('russian: crowns and continues as a king', rm.length >= 1 && rm.every((m) => m.crown && m.caps.length === 2), rm.map(L.notate).join(' ')); const am = L.legalMoves(pos({ b: [22], r: [26, 27, 1] })); ok('american: stops on the crown', am.length === 1 && am[0].caps.length === 1 && am[0].crown); } { const gs = L.createGame({ variant: 'russian' }); ok('russian: red (white) starts', gs.turn === 'red'); } // ── giveaway / casual ──────────────────────────────────────────────────────── section('giveaway & casual'); { const gs = pos({ b: [15], r: [18, 30] }, { variant: 'giveaway' }); const s = L.applyMove(gs, L.legalMoves(gs)[0]); ok('giveaway capture still forced', L.legalMoves(gs).length === 1); void s; // black's last piece jumped → black has none → black WINS const g2 = pos({ b: [18], r: [22, 1] }, { variant: 'giveaway', turn: 'red' }); const s2 = L.applyMove(g2, L.legalMoves(g2)[0]); ok('giveaway: losing every piece wins', s2.phase === 'game_over' && s2.winner === 'black', `${s2.winner} ${L.legalMoves(g2).map(L.notate)}`); const a2 = L.applyMove(pos({ b: [18], r: [22, 1] }, { turn: 'red' }), L.legalMoves(pos({ b: [18], r: [22, 1] }, { turn: 'red' }))[0]); ok('american: losing every piece loses', a2.winner === 'red' && a2.endReason === 'wiped'); } { const gs = pos({ b: [15, 1], r: [18, 30] }, { variant: 'casual' }); const n = names(gs); ok('casual: jump optional', n.includes('15x22') && n.some((x) => x.includes('-')), n.join(' ')); } { // blocked: black man on 29? black men move down; black on 32 (row 7) can't move as a man → no moves const gs = pos({ b: [28], r: [32, 1] }, { turn: 'red' }); // red 32 → 27? leave black on 28 r6c7 needs 32 (r7c6) empty → after red moves 32-27, 32 free: let's instead block fully void gs; const g = pos({ b: [29], r: [5] }, { turn: 'red' }); // black man on 29 (row 7) can never move const s = L.applyMove(g, L.legalMoves(g)[0]); ok('no legal move loses', s.phase === 'game_over' && s.winner === 'red' && s.endReason === 'blocked', `${s.phase} ${s.winner} ${s.endReason}`); } // ── draws ──────────────────────────────────────────────────────────────────── section('draws'); { let gs = pos({ B: [1], R: [32] }); const seq = ['1-6', '32-27', '6-1', '27-32', '1-6', '32-27', '6-1', '27-32']; for (const t of seq) { if (gs.phase !== 'playing') break; gs = L.applyMove(gs, L.parseMove(gs, t)); } ok('threefold repetition draws', gs.phase === 'game_over' && gs.winner === 'draw' && gs.endReason === 'repetition', `${gs.phase} ${gs.endReason} ply ${gs.ply}`); } { let gs = { ...pos({ B: [1, 3], R: [30, 32] }), quietLimit: 5 }; const rng = mulberry32(7); let n = 0; while (gs.phase === 'playing' && n < 40) { const ms = L.legalMoves(gs); gs = L.applyMove(gs, ms[Math.floor(rng() * ms.length)]); n++; } ok('no-progress rule ends king shuffles', gs.phase === 'game_over' && gs.winner === 'draw', `${gs.endReason} after ${n}`); ok('…by quiet or repetition', ['quiet', 'repetition'].includes(gs.endReason)); } { const gs = L.createGame(); ok('agreed draw', L.agreeDraw(gs).winner === 'draw'); const r = L.resign(gs, 'red'); ok('resign', r.winner === 'black' && r.endReason === 'resign'); ok('quietLeft at start', L.quietLeft(gs) === 40); } // ── notation round-trip ────────────────────────────────────────────────────── section('notation'); { const rng = mulberry32(11); let bad = 0, tested = 0; for (const variant of L.VARIANT_ORDER) { let gs = L.createGame({ variant }); for (let i = 0; i < 120 && gs.phase === 'playing'; i++) { const ms = L.legalMoves(gs); const uniq = ms.filter((m) => ms.filter((x) => L.notate(x) === L.notate(m)).length === 1); for (const m of uniq) { tested++; if (L.notate(L.parseMove(gs, L.notate(m)) ?? { caps: [], from: -1, to: -1 }) !== L.notate(m)) bad++; } gs = L.applyMove(gs, ms[Math.floor(rng() * ms.length)]); } } ok(`${tested} moves round-trip`, bad === 0, `${bad} bad`); } // ── ballots ────────────────────────────────────────────────────────────────── section('ballots'); for (const b of BALLOTS) { let g = null, err = ''; try { g = L.createGame({ ballot: b }); } catch (e) { err = e.message; } ok(`${b.name} ${b.moves.join(' ')}`, g && g.ply === 3 && g.turn === 'red' && g.ballot?.name === b.name, err); } ok('ballot ignored outside American', L.createGame({ variant: 'pool', ballot: BALLOTS[0] }).ply === 0); // ── AI ─────────────────────────────────────────────────────────────────────── section('AI'); { const gs = L.createGame(); for (let k = 1; k <= 5; k++) { const t0 = Date.now(); const m = AI.chooseMove(gs, k, { rng: mulberry32(k) }); const dt = Date.now() - t0; ok(`skill ${k} legal`, m && L.legalMoves(gs).some((x) => L.moveKey(x) === L.moveKey(m))); ok(`skill ${k} in budget (${dt}ms)`, dt < AI.SKILL[k].ms + 400); } } { // a shot: give one, take two. Black 18, red 23, 27? Simpler: forced win of material // black to move: man on 15 with red on 19 & 26 — 15-18? Just require the AI takes a free double jump over a single. const gs = pos({ b: [6, 12], r: [10, 19, 16, 32] }, { variant: 'casual' }); const m = AI.chooseMove(gs, 5); ok('takes the double jump', m && m.caps.length === 2, m && L.notate(m)); } { // KvK vs KKvK: the side with two kings should not offer/accept losing; trivial: AI finds mate-in-few: black K on 28? (skip deep proof) — check eval sign const gs = pos({ B: [14, 15], R: [32] }); const ev = AI.evaluate(gs.board, 'black', L.rulesOf('american')); ok('two kings vs one is good for black', ev > 100, ev); const gv = AI.evaluate(gs.board, 'black', L.rulesOf('giveaway')); ok('giveaway inverts material', gv < 0, gv); } { const h = AI.hint(L.createGame(), { ms: 200 }); ok('hint returns a legal move with a reason', h && h.reason && L.legalMoves(L.createGame()).some((m) => L.moveKey(m) === L.moveKey(h.move))); const early = AI.drawDecision(L.createGame(), 'red', 3, { ms: 100 }); ok('no draws in the opening', !early.accept); const ahead = pos({ B: [14, 15, 18], R: [32] }); const d = AI.drawDecision({ ...ahead, ply: 60 }, 'black', 5, { ms: 150 }); ok('won side declines a draw', !d.accept, JSON.stringify(d)); const behind = AI.drawDecision({ ...ahead, ply: 60, turn: 'red' }, 'red', 5, { ms: 150 }); ok('lost side accepts a draw', behind.accept, JSON.stringify(behind)); } // ── soak ───────────────────────────────────────────────────────────────────── section('self-play soak'); function play(variant, skillB, skillR, seed, { ballot = null, ms = null } = {}) { const rng = mulberry32(seed); let gs = L.createGame({ variant, ballot }); let errs = 0; let prev = L.counts(gs.board); while (gs.phase === 'playing') { const k = gs.turn === 'black' ? skillB : skillR; const m = AI.chooseMove(gs, k, { rng, ms }); if (!m) { errs++; break; } gs = L.applyMove(gs, m); const c = L.counts(gs.board); if (c.black > prev.black || c.red > prev.red) errs++; if (gs.board.some((p, i) => p && (L.RC[i][0] + L.RC[i][1]) % 2 === 0)) errs++; prev = c; } return { gs, errs }; } { const per = QUICK ? 1 : 3; for (const v of L.VARIANT_ORDER) { let errs = 0, ended = 0, plies = 0; const tally = { black: 0, red: 0, draw: 0 }; for (let i = 0; i < per; i++) { const r = play(v, 1 + (i % 3), 2 + (i % 3), 100 + i, { ms: 30 }); errs += r.errs; if (r.gs.phase === 'game_over') ended++; plies += r.gs.ply; tally[r.gs.winner]++; } ok(`${v}: ${per} games end cleanly (avg ${Math.round(plies / per)} plies; ${JSON.stringify(tally)})`, errs === 0 && ended === per, `${errs} errs`); } } // ── ladder ─────────────────────────────────────────────────────────────────── section('skill ladder'); { const pairs = QUICK ? [[1, 5]] : [[1, 3], [2, 5], [3, 5]]; for (const [lo, hi] of pairs) { let hiPts = 0, games = 0; const n = QUICK ? 2 : 4; for (let i = 0; i < n; i++) { const hiBlack = i % 2 === 0; const r = play('american', hiBlack ? hi : lo, hiBlack ? lo : hi, 900 + i * 7 + lo, { ms: hi === 5 ? 120 : 60 }); const w = r.gs.winner; if (w === 'draw') hiPts += 0.5; else if ((w === 'black') === hiBlack) hiPts += 1; games++; } ok(`skill ${hi} ≥ skill ${lo} (${hiPts}/${games})`, hiPts >= games / 2, `${hiPts}/${games}`); } } // ── layout ─────────────────────────────────────────────────────────────────── section('layout'); try { const Lay = await import('../src/games/checkers/CheckersLayout.js'); const rects = Lay.HUD_RECTS(); const B = Lay.BOARD_RECT; const inter = (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; for (const [name, r] of Object.entries(rects)) { ok(`${name} on screen`, r.x >= 0 && r.y >= 0 && r.x + r.w <= 1920 && r.y + r.h <= 1080, JSON.stringify(r)); ok(`${name} clear of board`, !inter(r, B), 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`, !inter(rects[keys[i]], rects[keys[j]])); } } } catch (e) { ok('layout module loads', false, e.message); } console.log(`\n${pass} passed, ${fail} failed`); process.exit(fail ? 1 : 0);