#!/usr/bin/env node // verifyOldMaid.js — headless checks for Old Maid: rules (deal, pairs, blind // draws, safe players, the Old Maid never pairs, game end + loser, shuffles), // the AI (memory, shuffles hide positions, tells honest vs bluffing by skill), // the parlour layout for 2/3/4 players, and an all-AI soak (every game ends, // 53 cards conserved, better players get stuck less). // Usage: node tools/verifyOldMaid.js [--quick] import * as O from '../src/games/oldmaid/OldMaidLogic.js'; import { Card } from '../src/games/cards/Deck.js'; import { createMemory, observeLog, chooseDraw, wantsShuffle, tellFor } from '../src/games/oldmaid/OldMaidAI.js'; import { aiTiming, mulberry32 } from '../src/ui/cardtable/AITiming.js'; import { CARD_W, CARD_H, cardBounds, fanHit, fanBounds, rectsOverlap, onScreen, hudFurniture, plateRect, unionRect, } from '../src/ui/cardtable/TableGeometry.js'; import { T, layoutFor, handPose, offerPose, pilePose, pileRect, candlePose, handBadge, handHome, tellAt, OFFER_FAN, REVEAL, showPose, } from '../src/games/oldmaid/OldMaidTable.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 = 700; const C = (key) => Object.assign(new Card(key[0], key[1]), { id: nid++ }); const OM = () => ({ rank: O.OLD_MAID_RANK, suit: null, value: 0, key: 'OM', isOldMaid: true, id: nid++ }); function rig(hands, current = 0) { const gs = O.createInitialState({ playerCount: hands.length, seed: 4 }); gs.players.forEach((p, s) => { p.hand = hands[s]; p.safe = false; p.finishOrder = null; p.discardPairs = 0; }); gs.discarded = []; gs.currentPlayer = current; gs.phase = 'play'; gs.log = []; return gs; } // ── Rules ──────────────────────────────────────────────────────────────────── section('Rules'); for (const n of [2, 3, 4]) { const gs = O.createInitialState({ playerCount: n, seed: 20 + n }); ok(`${n}p: 53 cards (deck + Old Maid)`, O.countCards(gs) === 53); ok(`${n}p: no pairs left in any hand`, gs.players.every(p => { const r = p.hand.map(c => c.rank).filter(x => x !== 'OM'); return new Set(r).size === r.length; })); ok(`${n}p: dealt pairs kept for the pile`, gs.discarded.length > 0 && gs.discarded.every(pr => pr.length === 2 && pr[0].rank === pr[1].rank)); ok(`${n}p: exactly one Old Maid`, gs.players.flatMap(p => p.hand).filter(c => c.isOldMaid).length === 1); } { const gs = rig([[C('7s'), C('2d')], [C('7h'), OM(), C('Kc')]]); ok('draw target is the next player', O.drawTargetSeat(gs, 0) === 1); const d = O.applyDraw(gs, 0, gs.players[1].hand[0].id); ok('drawing a match throws the pair in', d.players[0].hand.length === 1 && d.discarded.length === 1); ok('turn passes', d.currentPlayer === 1); const om = O.applyDraw(gs, 0, gs.players[1].hand[1].id); ok('the Old Maid never pairs', om.players[0].hand.some(c => c.isOldMaid) && om.discarded.length === 0); ok('cannot draw a card they do not hold', O.applyDraw(gs, 0, 99999) === gs); } { const gs = rig([[C('7s')], [C('7h'), OM()], [C('Kc'), C('Kd')]]); const d = O.applyDraw(gs, 0, gs.players[1].hand[0].id); ok('empty hand → safe', d.players[0].safe && d.log.some(e => e.kind === 'safe')); const end = rig([[OM()], [C('7h')]]); end.players[1].hand = [C('7h')]; end.players[0].hand = [C('7c'), OM()]; const e = O.applyDraw(end, 0, end.players[1].hand[0].id); ok('last card holder of the Old Maid loses', O.isGameOver(e) && e.loserSeat === 0); const sh = O.applyShuffle(rig([[C('2s'), C('3s'), C('4s'), C('5s'), C('6s'), C('7s')], [C('8h')]]), 0, mulberry32(3)); ok('shuffle keeps the cards, logs it', sh.players[0].hand.length === 6 && sh.log.some(e2 => e2.kind === 'shuffle')); } // ── AI ─────────────────────────────────────────────────────────────────────── section('AI'); { ok('slower hands at low skill', aiTiming(1).carry > aiTiming(3).carry && aiTiming(3).carry > aiTiming(5).carry); // seat 2 (who draws from seat 0) takes the Old Maid → seat 0 knows exactly where it is const gs = rig([[OM(), C('2s')], [C('3h'), C('4h')], [C('5c'), C('6c'), C('9d')]], 2); const g1 = O.applyDraw(gs, 2, gs.players[0].hand[0].id); const mem = createMemory(3); observeLog(mem, g1, 0); ok('memory: knows who took the Old Maid', mem.omLocation === 2 && mem.posKnown); const sh = O.applyShuffle(g1, 2, mulberry32(1)); observeLog(mem, sh, 0); ok('their shuffle hides the position', mem.omLocation === 2 && !mem.posKnown); ok('careful players shuffle while holding the Old Maid', wantsShuffle(g1, 2, 4, () => 0.9) && !wantsShuffle(g1, 2, 1)); let honest = 0, bluff = 0; const N = 4000; const r = mulberry32(9); for (let i = 0; i < N; i++) { if (tellFor(true, 1, r) === 'hopeful') honest++; if (tellFor(true, 5, r) === 'hopeful') bluff++; } ok('beginners give honest tells over the Old Maid', honest / N > 0.45, `${(honest / N).toFixed(2)}`); ok('experts mostly bluff', bluff / N < 0.2, `${(bluff / N).toFixed(2)}`); const d = chooseDraw(g1, 2, createMemory(3), 3); ok('draws a real card from the target', g1.players[O.drawTargetSeat(g1, 2)].hand.some(c => c.id === d)); } // ── 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 pile = pileRect(); ok(`${n}p: pair pile on the table`, T.onFelt({ x: pile.x0, y: pile.y0 }) && T.onFelt({ x: pile.x1, y: pile.y1 })); let pileOk = true; for (let id = 0; id < 53; id++) { const b = cardBounds(pilePose(id, 20)); if (b.x0 < pile.x0 || b.x1 > pile.x1 || b.y0 < pile.y0 - 12 || b.y1 > pile.y1) pileOk = false; } ok(`${n}p: pile cards stay in the pile area`, pileOk); let offer = null; for (let i = 0; i < 14; i++) offer = unionRect(offer, cardBounds(offerPose(i, 14, 30))); ok(`${n}p: offered hand on screen + on the table`, onScreen(offer) && T.onFelt({ x: offer.x0, y: offer.y1 }) && T.onFelt({ x: offer.x1, y: offer.y1 })); ok(`${n}p: offered hand clear of the pile`, !rectsOverlap(offer, pile)); for (const f of furn) ok(`${n}p: offered hand clear of ${f.name}`, !rectsOverlap(offer, f.r)); const reveal = cardBounds({ ...REVEAL, rot: -0.05, q: 1 }); ok(`${n}p: the reveal card on screen`, onScreen(reveal)); for (let c = 0; c < 2; c++) { const cp = candlePose(c); const cr = { x0: cp.x - 40, x1: cp.x + 40, y0: cp.y - 140 * cp.s, y1: cp.y + 14 }; ok(`${n}p candle ${c} clear of the offered hand`, !rectsOverlap(cr, offer)); for (let s = 0; s < n; s++) ok(`${n}p candle ${c} clear of hand ${s}`, !rectsOverlap(cr, fanBounds(L.seats[s].fan, 14)), JSON.stringify(cr)); for (const f of furn) ok(`${n}p candle ${c} clear of ${f.name}`, !rectsOverlap(cr, f.r)); } for (let s = 0; s < n; s++) { for (const cards of [1, 8, 14]) { const hb = fanBounds(L.seats[s].fan, cards, s === 0 ? 36 : 0); ok(`${n}p seat ${s} (${cards}): hand on screen`, onScreen(hb, s === 0 ? 50 : 10)); for (const f of furn) ok(`${n}p seat ${s} (${cards}): hand clear of ${f.name}`, !rectsOverlap(hb, f.r), `${JSON.stringify(hb)} vs ${JSON.stringify(f.r)}`); ok(`${n}p seat ${s} (${cards}): hand clear of the offered hand`, !rectsOverlap(hb, offer)); ok(`${n}p seat ${s} (${cards}): hand clear of the pile`, !rectsOverlap(hb, pile)); } if (s > 0) { const b = handBadge(L, s), br = { x0: b.x - 17, x1: b.x + 17, y0: b.y - 17, y1: b.y + 17 }; ok(`${n}p seat ${s}: badge on screen`, onScreen(br)); for (const f of furn) ok(`${n}p seat ${s}: badge clear of ${f.name}`, !rectsOverlap(br, f.r)); const t = tellAt(L, s), tr = { x0: t.x - 26, x1: t.x + 26, y0: t.y - 26, y1: t.y + 26 }; ok(`${n}p seat ${s}: tell face on screen`, onScreen(tr)); for (let u = 0; u < n; u++) ok(`${n}p seat ${s}: tell clear of hand ${u}`, !rectsOverlap(tr, fanBounds(L.seats[u].fan, 14))); 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); } } // showcase spots: a pair (or up to 6 dealt pairs) held up, clear of everything const shows = []; for (let s = 0; s < n; s++) { let r = null; for (const cnt of [1, 2, 12]) for (let i = 0; i < cnt; i++) r = unionRect(r, cardBounds(showPose(L, s, i, cnt))); shows[s] = r; ok(`${n}p seat ${s}: showcase on screen`, onScreen(r)); ok(`${n}p seat ${s}: showcase clear of the pile`, !rectsOverlap(r, pile)); for (const f of furn) ok(`${n}p seat ${s}: showcase clear of ${f.name}`, !rectsOverlap(r, f.r), `${JSON.stringify(r)} vs ${JSON.stringify(f.r)}`); for (let u = 0; u < n; u++) ok(`${n}p showcase ${s} clear of hand ${u}`, !rectsOverlap(r, fanBounds(L.seats[u].fan, 14, u === 0 ? 30 : 0)), `${JSON.stringify(r)}`); for (let c = 0; c < 2; c++) { const cp = candlePose(c); ok(`${n}p showcase ${s} clear of candle ${c}`, !rectsOverlap(r, { x0: cp.x - 40, x1: cp.x + 40, y0: cp.y - 140 * cp.s, y1: cp.y + 14 })); } if (s > 0) ok(`${n}p showcase ${s} clear of the offered hand`, !rectsOverlap(r, offer)); } for (let s = 0; s < n; s++) for (let t = s + 1; t < n; t++) ok(`${n}p showcases ${s}/${t} apart`, !rectsOverlap(shows[s], shows[t])); let rt = true; for (let i = 0; i < 9; i++) { const p = offerPose(i, 9); if (fanHit(OFFER_FAN, 9, { x: p.x - CARD_W * 0.25, y: p.y - CARD_H * 0.25 }) !== i) rt = false; } ok(`${n}p: offered-hand hit-test round trip`, rt); rt = true; for (let i = 0; i < 12; i++) { const p = handPose(L, 0, i, 12); if (fanHit(L.seats[0].fan, 12, { 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); } // ── Soak ───────────────────────────────────────────────────────────────────── section('Soak'); function game(skills, seed) { const rand = mulberry32(seed); let gs = O.createInitialState({ playerCount: skills.length, seed }); const mem = skills.map(() => createMemory(skills.length)); let steps = 0, noop = 0, conserve = true; while (!O.isGameOver(gs) && steps++ < 2000) { mem.forEach((m, s) => observeLog(m, gs, s)); const s = gs.currentPlayer; const id = chooseDraw(gs, s, mem[s], skills[s]); const from = O.drawTargetSeat(gs, s); const nx = O.applyDraw(gs, s, id); if (nx === gs) { noop++; break; } gs = nx; for (const t of [s, from]) if (!gs.players[t].safe && !O.isGameOver(gs) && wantsShuffle(gs, t, skills[t], rand)) gs = O.applyShuffle(gs, t, rand); if (O.countCards(gs) !== 53) conserve = false; } return { gs, over: O.isGameOver(gs), noop, conserve }; } { const M = QUICK ? 60 : 250; for (const n of [2, 3, 4]) { let over = 0, noop = 0, conserved = true, loserHolds = true; for (let i = 0; i < M; i++) { const r = game(Array.from({ length: n }, (_, s) => 1 + ((i + s) % 5)), 300 + i * 11 + n); if (r.over) over++; noop += r.noop; if (!r.conserve) conserved = false; if (r.over && !r.gs.players[r.gs.loserSeat].hand.some(c => c.isOldMaid)) loserHolds = false; } ok(`${n}p: every game ends`, over === M, `${over}/${M}`); ok(`${n}p: AI never draws illegally`, noop === 0); ok(`${n}p: 53 cards conserved`, conserved); ok(`${n}p: the loser holds the Old Maid`, loserHolds); } const stuck = (a, b, games) => { let lost = 0; for (let i = 0; i < games; i++) { const seat = i % 4; const sk = [b, b, b, b]; sk[seat] = a; if (game(sk, 9000 + i).gs.loserSeat === seat) lost++; } return lost / games; }; const G = QUICK ? 600 : 2000; const s5 = stuck(5, 1, G), s1 = stuck(1, 5, G); console.log(` 4p: skill 5 among skill 1s is stuck ${(s5 * 100).toFixed(0)}% of games · skill 1 among skill 5s ${(s1 * 100).toFixed(0)}% (fair 25%)`); ok('skill 5 gets stuck less than its fair share', s5 < 0.22); ok('skill 1 gets stuck more than its fair share', s1 > 0.28); } console.log(`\n${pass} passed, ${fail} failed`); process.exit(fail ? 1 : 0);