#!/usr/bin/env node // verifyGoFish.js — headless checks for Go Fish: rules (deal, asks, catches, // go fish + lucky draws, books AND pairs, refills, sitting out, game end), the // memory AI (always legal, skill-scaled recall), the dock layout for 2/3/4 // players (pond, book rows, hands, HUD all clear), and an all-AI soak (every // game ends, 52 cards conserved, better memory wins more). // Usage: node tools/verifyGoFish.js [--quick] import * as G from '../src/games/gofish/GoFishLogic.js'; import { Card } from '../src/games/cards/Deck.js'; import { createMemory, observeLog, chooseAction, RECALL, caughtFromThisTurn } from '../src/games/gofish/GoFishAI.js'; import { aiTiming, mulberry32 } from '../src/ui/cardtable/AITiming.js'; import { CARD_W, CARD_H, cardBounds, cardCorners, fanHit, fanBounds, rectsOverlap, onScreen, hudFurniture, plateRect, inRect, unionRect, } from '../src/ui/cardtable/TableGeometry.js'; import { T, layoutFor, handPose, poolPose, pondOutline, inPond, bookPose, bookRowRect, handBadge, handHome, bubbleAt, seatTargetRect, POND, showPose, } from '../src/games/gofish/GoFishTable.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 = 900; const C = (key) => Object.assign(new Card(key[0], key[1]), { id: nid++ }); function rig(hands, { matchSize = 4, pool = ['9s', '9h', 'Kd', '2c', '3c'], current = 0 } = {}) { const gs = G.createInitialState({ playerCount: hands.length, matchSize, seed: 3 }); gs.players.forEach((p, s) => { p.hand = hands[s].map(C); p.books = []; p.pairs = 0; p.pairedRanks = []; p.sittingOut = false; }); gs.pool = pool.map(C); gs.currentPlayer = current; gs.phase = 'play'; gs.log = []; return gs; } // ── Rules ──────────────────────────────────────────────────────────────────── section('Rules'); for (const n of [2, 3, 4]) for (const m of [2, 4]) { const gs = G.createInitialState({ playerCount: n, matchSize: m, seed: 10 + n + m }); ok(`${n}p ${m === 4 ? 'books' : 'pairs'}: 52 conserved`, G.countCards(gs) === 52); ok(`${n}p ${m === 4 ? 'books' : 'pairs'}: dealt 7 (minus deal books)`, gs.players.every(p => p.hand.length + p.books.flat().length === 7 || p.hand.length > 0)); } { const gs = rig([['7s', '7h', '2d'], ['7d', '7c', 'Kh']]); ok('cannot ask for a rank you lack', G.applyAsk(gs, 1, 'K') === gs); ok('cannot ask yourself', G.applyAsk(gs, 0, '7') === gs); const c = G.applyAsk(gs, 1, '7'); ok('catch takes every card of the rank', c.lastAsk.result === 'catch' && c.lastAsk.cardsTransferred.length === 2); ok('four of a kind makes a book', c.players[0].pairs === 1 && c.players[0].books[0].length === 4 && c.players[0].hand.length === 1); ok('book logged', c.log.some(e => e.kind === 'pair' && e.rank === '7')); ok('catch: asker goes again', c.currentPlayer === 0); ok('52 conserved after a catch', G.countCards({ ...c, pool: c.pool }) === G.countCards(gs)); } { const gs = rig([['7s', '2d'], ['Kd', 'Kh']], { pool: ['3c', '7h'] }); const f = G.applyAsk(gs, 1, '7'); ok('miss → go fish (pick phase)', f.phase === 'pick' && f.lastAsk.result === 'fish'); const lucky = G.applyFishPick(f, f.pool.find(c => c.rank === '7').id); ok('fishing up the asked rank is lucky: go again', lucky.lastAsk.result === 'lucky' && lucky.currentPlayer === 0); const plain = G.applyFishPick(f, f.pool.find(c => c.rank === '3').id); ok('otherwise the turn passes', plain.lastAsk.result === 'fish' && plain.currentPlayer === 1); } { const gs = rig([['7s', '2d'], ['7d', 'Kh']], { matchSize: 2 }); const c = G.applyAsk(gs, 1, '7'); ok('pairs mode: two make a pair', c.players[0].pairs === 1 && c.players[0].books[0].length === 2); const empty = rig([['7s'], ['7d']], { matchSize: 2, pool: ['2c', '3c', '4c', '5c', '6c', '8c', '9c', 'Tc', 'Jc', 'Qc'] }); const e = G.applyAsk(empty, 1, '7'); ok('emptied hands refill to five', e.players[0].hand.length === 5 && e.players[1].hand.length === 5); const out = rig([['7s'], ['7d']], { matchSize: 2, pool: [] }); const o = G.applyAsk(out, 1, '7'); ok('no cards and an empty pond → game over', G.isGameOver(o)); } { // a full game always conserves cards const gs = G.createInitialState({ playerCount: 3, matchSize: 4, seed: 99 }); ok('countCards', G.countCards(gs) === 52); } // ── AI ─────────────────────────────────────────────────────────────────────── section('AI'); { ok('slower hands at low skill', aiTiming(1).carry > aiTiming(3).carry && aiTiming(3).carry > aiTiming(5).carry); ok('recall rises with skill', RECALL[1] < RECALL[3] && RECALL[3] < RECALL[5] && RECALL[5] === 1); const gs = rig([['7s', '2d'], ['Kd', 'Kh'], ['Qs', '9d']], { current: 1 }); let g = G.applyAsk(gs, 0, 'K'); // seat 1 asks the human for kings: go fish g = G.applyFishPick(g, g.pool[0].id); const mem = createMemory(3, { skill: 5, matchSize: 4, rng: () => 0.5 }); observeLog(mem, g, 2); ok('perfect memory: seat 1 holds kings', mem.beliefs[1].K >= 1); g.currentPlayer = 2; g.players[2].hand.push(C('Kc')); const a = chooseAction(g, 2, mem); ok('asks the player who showed the rank', a && a.targetSeat === 1 && a.rank === 'K'); // after a catch, the asker won't go back to the same player this turn const t3 = rig([['7s', '9d', 'Qh'], ['7d', 'Kh'], ['9s', 'Qc'], ['2c', '3d']], { current: 0 }); const caught = G.applyAsk(t3, 1, '7'); ok('catch noted for this turn', caughtFromThisTurn(caught, 0).has(1) && caught.currentPlayer === 0); let againOk = true; for (const sk of [1, 2, 3, 4, 5]) for (let k = 0; k < 20; k++) { const m = createMemory(4, { skill: sk, matchSize: 4, rng: mulberry32(k * 31 + sk) }); observeLog(m, caught, 0); const a = chooseAction(caught, 0, m); if (!a || a.targetSeat === 1) againOk = false; } ok('no skill asks the same player twice in one turn', againOk); const nextTurn = G.applyAsk(caught, 2, '7'); // a different player's ask ends the run nextTurn.currentPlayer = 1; ok('the rule resets on the next turn', caughtFromThisTurn({ ...nextTurn, log: [...nextTurn.log, { kind: 'ask', askerSeat: 3, targetSeat: 0, rank: '2', result: 'fish' }] }, 0).size === 0); const heads = rig([['7s', '9d'], ['7d', '9h', 'Kh']], { current: 0 }); const h1 = G.applyAsk(heads, 1, '7'); const hm = createMemory(2, { skill: 5, matchSize: 4, rng: () => 0.5 }); observeLog(hm, h1, 0); const ha = chooseAction(h1, 0, hm); ok('heads-up: the only opponent can still be asked again', ha && ha.targetSeat === 1); const forgetful = createMemory(3, { skill: 1, matchSize: 4, rng: () => 0.99 }); observeLog(forgetful, g, 2); ok('skill 1 can miss what it saw', (forgetful.beliefs[1].K || 0) === 0); } // ── 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 pond = pondOutline(1.07); const pondBox = { x0: Math.min(...pond.map(p => p.x)), x1: Math.max(...pond.map(p => p.x)), y0: Math.min(...pond.map(p => p.y)) - 30, y1: Math.max(...pond.map(p => p.y)) + 34 }; ok(`${n}p: pond on the dock`, pond.every(p => T.onFelt(p))); let inside = true; for (let id = 0; id < 52; id++) { const p = poolPose(id); if (!inPond(p)) inside = false; } ok(`${n}p: every pool card floats inside the pond`, inside); const rows = []; for (let s = 0; s < n; s++) { const row = bookRowRect(L, s, 13); rows[s] = row; ok(`${n}p seat ${s}: book row on the dock`, T.onFelt({ x: row.x0, y: row.y0 }) && T.onFelt({ x: row.x1, y: row.y1 }), JSON.stringify(row)); ok(`${n}p seat ${s}: book row clear of the pond`, !rectsOverlap(row, pondBox), `${JSON.stringify(row)} vs ${JSON.stringify(pondBox)}`); for (const f of furn) ok(`${n}p seat ${s}: book row clear of ${f.name}`, !rectsOverlap(row, f.r), `${JSON.stringify(row)} vs ${JSON.stringify(f.r)}`); for (const cards of [1, 7, 15, 22]) { const hb = fanBounds(L.seats[s].fan, cards, s === 0 ? 40 : 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 pond`, !rectsOverlap(hb, pondBox)); for (let t = 0; t < n; t++) ok(`${n}p hand ${s} (${cards}) clear of book row ${t}`, !rectsOverlap(hb, bookRowRect(L, t, 13))); } 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 bb = bubbleAt(L, s); ok(`${n}p seat ${s}: speech bubble on screen`, bb.x > 120 && bb.x < 1800 && bb.y > 40 && bb.y < 1000); const tr = seatTargetRect(L, s, 7); ok(`${n}p seat ${s}: ask target covers their hand`, inRect(handPose(L, s, 3, 7), tr)); 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); } } for (let s = 0; s < n; s++) for (let t = s + 1; t < n; t++) ok(`${n}p book rows ${s}/${t} apart`, !rectsOverlap(rows[s], rows[t])); // show spots: up to 3 cards held up / laid out, clear of everything else const shows = []; for (let s = 0; s < n; s++) { let r = null; const corners = []; for (let i = 0; i < 3; i++) { const p = showPose(L, s, i, 3); r = unionRect(r, cardBounds(p)); corners.push(...cardCorners(p)); } shows[s] = r; ok(`${n}p seat ${s}: show spot on screen`, onScreen(r)); ok(`${n}p seat ${s}: show spot clear of the pond`, corners.every(p => !inPond(p))); for (const f of furn) ok(`${n}p seat ${s}: show spot clear of ${f.name}`, !rectsOverlap(r, f.r), `${JSON.stringify(r)} vs ${JSON.stringify(f.r)}`); for (let t = 0; t < n; t++) { ok(`${n}p show ${s} clear of book row ${t}`, !rectsOverlap(r, rows[t]), `${JSON.stringify(r)} vs ${JSON.stringify(rows[t])}`); ok(`${n}p show ${s} clear of hand ${t}`, !rectsOverlap(r, fanBounds(L.seats[t].fan, 15, t === 0 ? 40 : 0)), `${JSON.stringify(r)}`); } } for (let s = 0; s < n; s++) for (let t = s + 1; t < n; t++) ok(`${n}p show spots ${s}/${t} apart`, !rectsOverlap(shows[s], shows[t])); for (let s = 1; s < n; s++) for (let t = s + 1; t < n; t++) ok(`${n}p ask targets ${s}/${t} apart`, !rectsOverlap(seatTargetRect(L, s, 7), seatTargetRect(L, t, 7))); let rt = true; for (let i = 0; i < 7; i++) { const p = handPose(L, 0, i, 7); if (fanHit(L.seats[0].fan, 7, { 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: books compress to fit 13`, bookPose(L, 0, 12, 13).x !== bookPose(L, 0, 0, 13).x); } void POND; void cardBounds; // ── Soak ───────────────────────────────────────────────────────────────────── section('Soak'); function game(skills, seed, matchSize = 4) { const rand = mulberry32(seed); let gs = G.createInitialState({ playerCount: skills.length, matchSize, seed }); const mem = skills.map((sk) => createMemory(skills.length, { skill: sk, matchSize, rng: rand })); let steps = 0, noop = 0, conserve = true, repeats = 0; while (!G.isGameOver(gs) && steps++ < 3000) { mem.forEach((m, s) => observeLog(m, gs, s)); if (gs.phase === 'pick') { gs = G.applyFishPick(gs, gs.pool[Math.floor(rand() * gs.pool.length)].id); continue; } const s = gs.currentPlayer; const a = chooseAction(gs, s, mem[s]); if (!a) { noop++; break; } const open = gs.players.filter(p => p.seat !== s && !p.sittingOut && p.hand.length > 0).map(p => p.seat); const before = caughtFromThisTurn(gs, s); if (before.has(a.targetSeat) && open.some(t => !before.has(t))) repeats++; const nx = G.applyAsk(gs, a.targetSeat, a.rank); if (nx === gs) { noop++; break; } gs = nx; if (G.countCards(gs) !== 52) conserve = false; } return { gs, over: G.isGameOver(gs), noop, conserve, repeats }; } { const M = QUICK ? 40 : 150; for (const m of [4, 2]) for (const n of [2, 3, 4]) { let over = 0, noop = 0, conserved = true, books = true, repeats = 0; for (let i = 0; i < M; i++) { const r = game(Array.from({ length: n }, (_, s) => 1 + ((i + s) % 5)), 100 + i * 7 + n, m); if (r.over) over++; noop += r.noop; if (!r.conserve) conserved = false; repeats += r.repeats; const total = r.gs.players.reduce((a, p) => a + p.pairs, 0); if (m === 4 && total !== 13) books = false; } const tag = `${n}p ${m === 4 ? 'books' : 'pairs'}`; ok(`${tag}: every game ends`, over === M, `${over}/${M}`); ok(`${tag}: AI never makes an illegal ask`, noop === 0); ok(`${tag}: 52 cards conserved`, conserved); ok(`${tag}: never re-asks a player already caught from this turn`, repeats === 0, `${repeats}`); if (m === 4) ok(`${tag}: all 13 books get made`, books); } // skill: in 4-player games memory matters a lot; heads-up books is mostly luck const one = (a, b, games) => { let w = 0; for (let i = 0; i < games; i++) { const seat = i % 4; const sk = [b, b, b, b]; sk[seat] = a; const r = game(sk, 7000 + i, 4); if (r.gs.winnerSeats.includes(seat)) w += 1 / r.gs.winnerSeats.length; } return w / games; }; const G4 = QUICK ? 300 : 800; const w51 = one(5, 1, G4), w53 = one(5, 3, G4), w15 = one(1, 5, G4); console.log(` 4p win rates (fair 25%): skill 5 vs 1s ${(w51 * 100).toFixed(0)}% · 5 vs 3s ${(w53 * 100).toFixed(0)}% · 1 vs 5s ${(w15 * 100).toFixed(0)}%`); ok('skill 5 dominates a table of skill 1s', w51 >= 0.45); // measured over 800 games after the no-repeat-target rule: ≈ 30% (fair 25%) ok('skill 5 beats a table of skill 3s', w53 >= (QUICK ? 0.25 : 0.28)); ok('skill 1 loses to a table of skill 5s', w15 <= 0.16); } console.log(`\n${pass} passed, ${fail} failed`); process.exit(fail ? 1 : 0);