337 lines
21 KiB
JavaScript
337 lines
21 KiB
JavaScript
#!/usr/bin/env node
|
|
// verifyUno.js — headless checks for Uno: rules (legality, actions, Wild +4
|
|
// challenge, UNO! call / catch, scoring to a target, both house rules), the AI
|
|
// (always legal, challenge odds, skill timing), the kitchen-table layout for
|
|
// 2/3/4 players (on screen, clear of the HUD, hit-tests), and an all-AI soak
|
|
// (every match ends, 108 cards conserved, no stalls, skill ordering).
|
|
// Usage: node tools/verifyUno.js [--quick]
|
|
|
|
import * as U from '../src/games/uno/UnoLogic.js';
|
|
import { buildDeck, UnoCard } from '../src/games/uno/UnoDeck.js';
|
|
import { chooseAction, challengeOdds, holdOdds, suggestPlay, likelyVoids, UNO_CALL_CHANCE, CATCH_CHANCE } from '../src/games/uno/UnoAI.js';
|
|
import { aiTiming, HAND_SPEED, mulberry32 } from '../src/ui/cardtable/AITiming.js';
|
|
import {
|
|
W, H, CARD_W, CARD_H, cardBounds, inPose, fanHit, fanInsertIndex, fanZone, fanBounds, inRect,
|
|
rectsOverlap, onScreen, hudFurniture, plateRect,
|
|
} from '../src/ui/cardtable/TableGeometry.js';
|
|
import { T, layoutFor, handPose, handHome, handBadge, spotFor, ringPoints } from '../src/games/uno/UnoTable.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 = 5000;
|
|
const C = (color, kind, value = null) => Object.assign(new UnoCard({ color, kind, value }), { id: nid++ });
|
|
const N = (color, v) => C(color, 'number', v);
|
|
|
|
// A controlled table: seat hands, top card, colour, whose turn.
|
|
function rig({ n = 2, hands, top, color, current = 0, rules = {}, target = 500, draw = null }) {
|
|
const gs = U.createInitialState({ playerCount: n, seed: 3, rules, target });
|
|
gs.players.forEach((p, s) => { p.hand = hands[s] ?? [N('r', 1)]; });
|
|
gs.discardPile = [top];
|
|
gs.currentColor = color ?? top.color;
|
|
gs.currentPlayer = current;
|
|
gs.phase = 'play';
|
|
gs.direction = 1;
|
|
gs.pendingDraw = null;
|
|
gs.pendingWild4 = null;
|
|
gs.unoCalled = new Array(n).fill(false);
|
|
gs.unoVulnerable = null;
|
|
if (draw) gs.drawPile = draw; else gs.drawPile = Array.from({ length: 30 }, (_, i) => N('g', i % 10));
|
|
return gs;
|
|
}
|
|
|
|
// ── Rules ────────────────────────────────────────────────────────────────────
|
|
section('Rules');
|
|
{
|
|
const deck = buildDeck();
|
|
ok('108 cards', deck.length === 108);
|
|
ok('4 wild + 4 wild4', deck.filter(c => c.kind === 'wild').length === 4 && deck.filter(c => c.kind === 'wild4').length === 4);
|
|
ok('one 0 and two 1-9 per colour', ['r', 'y', 'g', 'b'].every(col => deck.filter(c => c.color === col && c.value === 0).length === 1 && deck.filter(c => c.color === col && c.value === 7).length === 2));
|
|
ok('points: face / 20 / 50', N('r', 7).points === 7 && C('r', 'skip').points === 20 && C('w', 'wild4').points === 50);
|
|
for (const n of [2, 3, 4]) {
|
|
const gs = U.createInitialState({ playerCount: n, seed: 11 + n });
|
|
ok(`${n}p: 7 each (start card may add 2)`, gs.players.every(p => p.hand.length === 7 || p.hand.length === 9));
|
|
ok(`${n}p: 108 conserved`, U.countCards(gs) === 108);
|
|
ok(`${n}p: top is never Wild +4`, U.topDiscard(gs).kind !== 'wild4');
|
|
}
|
|
// seeded deals re-flip a +4 with a fresh seed each try (a fixed seed could cycle forever)
|
|
let flipsOk = true;
|
|
for (let seed = 1; seed <= 3000; seed++) if (U.topDiscard(U.createInitialState({ playerCount: 2, seed })).kind === 'wild4') flipsOk = false;
|
|
ok('3000 seeded deals all finish with a non-+4 top card', flipsOk);
|
|
}
|
|
{
|
|
const gs = rig({ hands: [[N('r', 3), N('b', 5), C('w', 'wild4'), C('w', 'wild')], [N('g', 1), N('g', 2)]], top: N('r', 5) });
|
|
const legal = U.legalPlays(gs, 0);
|
|
ok('colour match legal', legal.includes(gs.players[0].hand[0].id));
|
|
ok('number match legal', legal.includes(gs.players[0].hand[1].id));
|
|
ok('Wild +4 may be played as a bluff', legal.includes(gs.players[0].hand[2].id) && U.isBluff(gs, gs.players[0].hand[2], gs.players[0].hand));
|
|
ok('Wild +4 with no colour match is honest', !U.isBluff(gs, gs.players[0].hand[2], gs.players[0].hand.filter(c => c.color !== 'r')));
|
|
ok('Wild always legal', legal.includes(gs.players[0].hand[3].id));
|
|
const nx = U.applyPlay(gs, 0, gs.players[0].hand[0].id);
|
|
ok('number play passes the turn', nx.currentPlayer === 1 && nx.currentColor === 'r');
|
|
ok('not your turn → no-op', U.applyPlay(nx, 0, nx.players[0].hand[0].id) === nx);
|
|
}
|
|
{
|
|
// skip / reverse / draw2 in 2p and 3p
|
|
const s2 = rig({ hands: [[C('r', 'skip'), N('r', 1)], [N('g', 1), N('g', 2)]], top: N('r', 5) });
|
|
ok('2p skip: you go again', U.applyPlay(s2, 0, s2.players[0].hand[0].id).currentPlayer === 0);
|
|
const r2 = rig({ hands: [[C('r', 'reverse'), N('r', 1)], [N('g', 1), N('g', 2)]], top: N('r', 5) });
|
|
ok('2p reverse acts as skip', U.applyPlay(r2, 0, r2.players[0].hand[0].id).currentPlayer === 0);
|
|
const r3 = rig({ n: 3, hands: [[C('r', 'reverse'), N('r', 1)], [N('g', 1), N('g', 2)], [N('y', 1), N('y', 2)]], top: N('r', 5) });
|
|
const r3n = U.applyPlay(r3, 0, r3.players[0].hand[0].id);
|
|
ok('3p reverse flips direction', r3n.direction === -1 && r3n.currentPlayer === 2);
|
|
const d2 = rig({ n: 3, hands: [[C('r', 'draw2'), N('r', 1)], [N('g', 1)], [N('y', 1)]], top: N('r', 5) });
|
|
const d2n = U.applyPlay(d2, 0, d2.players[0].hand[0].id);
|
|
ok('draw2: next draws 2 and is skipped', d2n.players[1].hand.length === 3 && d2n.currentPlayer === 2);
|
|
}
|
|
{
|
|
// wild + colour, wild4 challenge outcomes
|
|
const w = rig({ hands: [[C('w', 'wild'), N('b', 1)], [N('g', 1), N('g', 2)]], top: N('r', 5) });
|
|
let x = U.applyPlay(w, 0, w.players[0].hand[0].id);
|
|
ok('wild asks for a colour', x.phase === 'choosingColor' && x.currentPlayer === 0);
|
|
x = U.applyChooseColor(x, 'b');
|
|
ok('colour set + turn passes', x.currentColor === 'b' && x.currentPlayer === 1 && U.topDiscard(x).chosenColor === 'b');
|
|
|
|
const honest = rig({ hands: [[C('w', 'wild4'), N('b', 1), N('g', 3)], [N('g', 1), N('g', 2)]], top: N('r', 5) });
|
|
let h = U.applyChooseColor(U.applyPlay(honest, 0, honest.players[0].hand[0].id), 'g');
|
|
ok('wild4 opens a challenge window', h.phase === 'challengeWindow' && h.pendingWild4.challengerSeat === 1 && h.pendingWild4.priorColor === 'r');
|
|
const lose = U.applyChallengeWild4(h, true);
|
|
ok('failed challenge: challenger draws 6, skipped', lose.players[1].hand.length === 8 && lose.currentPlayer === 0);
|
|
const acc = U.applyChallengeWild4(h, false);
|
|
ok('accept: draws 4, skipped', acc.players[1].hand.length === 6 && acc.currentPlayer === 0);
|
|
|
|
const bluff = rig({ hands: [[C('w', 'wild4'), N('r', 1), N('g', 3)], [N('g', 1), N('g', 2)]], top: N('b', 5), color: 'b' });
|
|
bluff.players[0].hand[0] = Object.assign(new UnoCard({ color: 'w', kind: 'wild4' }), { id: nid++ });
|
|
bluff.currentColor = 'y'; // nothing yellow in hand → the +4 is legal
|
|
let b = U.applyPlay(bluff, 0, bluff.players[0].hand[0].id);
|
|
b.pendingWild4.hadColorMatch = true; // they held the colour
|
|
b = U.applyChooseColor(b, 'g');
|
|
const win = U.applyChallengeWild4(b, true);
|
|
ok('successful challenge: player draws 4, challenger plays', win.players[0].hand.length === 6 && win.currentPlayer === 1);
|
|
}
|
|
{
|
|
// UNO! call and catch
|
|
const g = rig({ n: 3, hands: [[N('r', 3), N('r', 4)], [N('g', 1), N('g', 2)], [N('y', 1), N('y', 2)]], top: N('r', 5) });
|
|
const forgot = U.applyPlay(g, 0, g.players[0].hand[0].id);
|
|
ok('forgetting UNO leaves you exposed', forgot.unoVulnerable === 0 && U.canCatchUno(forgot, 1) && !U.canCatchUno(forgot, 0));
|
|
const caught = U.applyCatchUno(forgot, 2);
|
|
ok('caught → draw 2, window closes', caught.players[0].hand.length === 3 && caught.unoVulnerable === null && caught.currentPlayer === forgot.currentPlayer);
|
|
const late = U.applyCallUno(forgot, 0);
|
|
ok('calling late (before anyone acts) is safe', late.unoVulnerable === null && !U.canCatchUno(late, 1));
|
|
const moved = U.applyDraw(forgot, 1);
|
|
ok('next player acting closes the window', moved.unoVulnerable === null);
|
|
const called = U.applyPlay(U.applyCallUno(g, 0), 0, g.players[0].hand[0].id);
|
|
ok('calling first protects you', called.unoVulnerable === null && called.unoCalled[0]);
|
|
ok('cannot call with 3+ cards', U.applyCallUno(rig({ hands: [[N('r', 1), N('r', 2), N('r', 3)], [N('g', 1)]], top: N('r', 5) }), 0).unoCalled[0] === false);
|
|
}
|
|
{
|
|
// scoring + match target
|
|
const g = rig({ n: 3, hands: [[N('r', 3)], [N('g', 7), C('w', 'wild')], [C('y', 'skip'), N('y', 2)]], top: N('r', 5), target: 500 });
|
|
const out = U.applyPlay(g, 0, g.players[0].hand[0].id);
|
|
ok('going out scores the other hands', out.phase === 'handOver' && out.scores[0] === 7 + 50 + 20 + 2 && out.handResult.points === 79);
|
|
const next = U.startNextHand(out);
|
|
ok('next hand keeps scores, rotates the opener', next.scores[0] === 79 && next.handNum === 2 && next.startingPlayer === 1 && next.phase !== 'handOver');
|
|
const fin = rig({ hands: [[C('r', 'draw2')], [N('g', 9)]], top: N('r', 5), target: 20 });
|
|
const f = U.applyPlay(fin, 0, fin.players[0].hand[0].id);
|
|
ok('last-card Draw 2 still makes the next player draw', f.players[1].hand.length === 3);
|
|
ok('reaching the target ends the match', f.phase === 'gameOver' && f.matchWinner === 0);
|
|
const single = rig({ hands: [[N('r', 3)], [N('g', 9)]], top: N('r', 5), target: 0 });
|
|
ok('target 0 = one hand', U.applyPlay(single, 0, single.players[0].hand[0].id).phase === 'gameOver');
|
|
}
|
|
{
|
|
// house rule: stacking
|
|
const g = rig({ n: 3, rules: { stacking: true }, hands: [[C('r', 'draw2'), N('r', 1)], [C('b', 'draw2'), N('g', 2)], [N('y', 1), N('y', 2)]], top: N('r', 5) });
|
|
const a = U.applyPlay(g, 0, g.players[0].hand[0].id);
|
|
ok('stacking: +2 waits on the next player', a.pendingDraw?.count === 2 && a.currentPlayer === 1 && a.players[1].hand.length === 2);
|
|
ok('stacking: only a Draw 2 is legal', U.legalPlays(a, 1).length === 1);
|
|
const b = U.applyPlay(a, 1, a.players[1].hand[0].id);
|
|
ok('stacking: +2 on +2 = +4 on the next', b.pendingDraw?.count === 4 && b.currentPlayer === 2);
|
|
const c = U.applyDraw(b, 2);
|
|
ok('stacking: taking it draws all and skips', c.players[2].hand.length === 6 && c.pendingDraw === null && c.currentPlayer === 0);
|
|
|
|
const w = rig({ n: 3, rules: { stacking: true }, hands: [[C('w', 'wild4'), N('b', 1)], [C('w', 'wild4'), N('g', 2)], [N('y', 1), N('y', 2)]], top: N('r', 5) });
|
|
let x = U.applyChooseColor(U.applyPlay(w, 0, w.players[0].hand[0].id), 'g');
|
|
ok('stacking: challenger may stack a +4', U.stackableWild4(x, 1).length === 1);
|
|
x = U.applyStackWild4(x, 1, x.players[1].hand[0].id);
|
|
ok('stacked +4 asks for a colour', x.phase === 'choosingColor' && x.currentPlayer === 1 && x.pendingWild4.count === 8);
|
|
x = U.applyChooseColor(x, 'y');
|
|
const take = U.applyChallengeWild4(x, false);
|
|
ok('stacked +4: next takes 8', take.players[2].hand.length === 10);
|
|
const plain = rig({ n: 3, hands: [[C('w', 'wild4'), N('b', 1)], [C('w', 'wild4'), N('g', 2)], [N('y', 1)]], top: N('r', 5) });
|
|
const px = U.applyChooseColor(U.applyPlay(plain, 0, plain.players[0].hand[0].id), 'g');
|
|
ok('no stacking without the rule', px.phase === 'challengeWindow' && U.stackableWild4(px, 1).length === 0);
|
|
}
|
|
{
|
|
// house rule: draw until playable
|
|
const draw = [N('r', 9), N('g', 1), N('g', 2), N('g', 3)]; // popped from the end: 3g, 2g, 1g, 9r
|
|
const g = rig({ rules: { drawUntilPlayable: true }, hands: [[N('b', 1)], [N('y', 1)]], top: N('r', 5), draw });
|
|
const a = U.applyDraw(g, 0);
|
|
ok('draw-until-playable keeps drawing', a.players[0].hand.length === 5 && a.phase === 'mustPlayDrawn' && a.pendingDrawn.value === 9);
|
|
const plain = rig({ hands: [[N('b', 1)], [N('y', 1)]], top: N('r', 5), draw: [N('r', 9), N('g', 1)] });
|
|
const b = U.applyDraw(plain, 0);
|
|
ok('normal draw takes one', b.players[0].hand.length === 2 && b.currentPlayer === 1);
|
|
}
|
|
|
|
// ── AI ───────────────────────────────────────────────────────────────────────
|
|
section('AI');
|
|
{
|
|
ok('hand speeds as in Nerts', HAND_SPEED[1] === 0.25 && HAND_SPEED[3] === 0.5 && HAND_SPEED[5] === 1);
|
|
ok('slower hands at low skill', aiTiming(1).carry > aiTiming(3).carry && aiTiming(3).carry > aiTiming(5).carry);
|
|
ok('UNO call / catch chances rise with skill', UNO_CALL_CHANCE[1] < UNO_CALL_CHANCE[5] && CATCH_CHANCE[1] < CATCH_CHANCE[5]);
|
|
const g = rig({ n: 3, hands: [[N('r', 3)], [C('w', 'wild4'), N('g', 2), N('b', 3), N('b', 4), N('b', 5), N('b', 6)], [N('y', 1)]], top: N('y', 5), color: 'y', current: 1 });
|
|
let x = U.applyChooseColor(U.applyPlay(g, 1, g.players[1].hand[0].id), 'b');
|
|
const p = challengeOdds(x, 2);
|
|
ok('challenge odds are a probability', p > 0 && p < 1, p.toFixed(3));
|
|
const big = rig({ n: 3, hands: [[N('r', 3)], [C('w', 'wild4'), ...Array.from({ length: 12 }, (_, i) => N('b', i % 10))], [N('y', 1)]], top: N('g', 5), color: 'r', current: 1 });
|
|
const bx = U.applyChooseColor(U.applyPlay(big, 1, big.players[1].hand[0].id), 'b');
|
|
ok('more cards in hand → likelier they held the colour', holdOdds(bx, 2) > holdOdds(x, 2) && challengeOdds(bx, 2) > p);
|
|
ok('bluffs are rare: challenge odds below the raw hold odds', challengeOdds(bx, 2) < holdOdds(bx, 2));
|
|
const v = rig({ n: 3, hands: [[N('r', 3)], [N('g', 1)], [N('y', 1)]], top: N('b', 5) });
|
|
v.log.push({ kind: 'draw', seat: 1, count: 1, color: 'b' });
|
|
ok('likely voids come from the log', likelyVoids(v, 1).has('b'));
|
|
const sp = rig({ hands: [[N('r', 3), N('g', 4)], [N('y', 1)]], top: N('r', 5) });
|
|
ok('hint suggests a legal play', U.legalPlays(sp, 0).includes(suggestPlay(sp, 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 deckB = cardBounds({ ...L.deck, y: L.deck.y - 14 }), discB = cardBounds(L.discard);
|
|
ok(`${n}p: piles apart`, !rectsOverlap(deckB, discB));
|
|
ok(`${n}p: piles on the cloth`, T.onFelt({ x: deckB.x0, y: deckB.y0 }) && T.onFelt({ x: discB.x1, y: discB.y1 }));
|
|
// the colour wheel (radius 162) around the discard stays on screen and off the human hand
|
|
const wheel = { x0: L.discard.x - 162, x1: L.discard.x + 162, y0: L.discard.y - 192, y1: L.discard.y + 162 };
|
|
ok(`${n}p: colour wheel on screen`, onScreen(wheel));
|
|
for (let s = 0; s < n; s++) {
|
|
for (const cards of [1, 7, 15, 25]) {
|
|
const fb = fanBounds(L.seats[s].fan, cards, s === 0 ? 40 : 0);
|
|
ok(`${n}p seat ${s} (${cards} cards): hand on screen`, onScreen(fb, s === 0 ? 50 : 10), JSON.stringify(fb));
|
|
for (const f of furn) ok(`${n}p seat ${s} (${cards}): hand clear of ${f.name}`, !rectsOverlap(fb, f.r), `${JSON.stringify(fb)} vs ${JSON.stringify(f.r)}`);
|
|
ok(`${n}p seat ${s} (${cards}): hand clear of piles`, !rectsOverlap(fb, deckB) && !rectsOverlap(fb, discB));
|
|
if (s > 0) ok(`${n}p seat ${s} (${cards}): clear of the wheel`, !rectsOverlap(fb, wheel));
|
|
}
|
|
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));
|
|
for (let t = 1; t < n; t++) ok(`${n}p seat ${s}: badge clear of hand ${t}`, !rectsOverlap(br, fanBounds(L.seats[t].fan, 25)));
|
|
const hh = handHome(L, s);
|
|
ok(`${n}p seat ${s}: ghost hand rests on screen`, hh.x > 0 && hh.x < W && hh.y > 0 && hh.y < H);
|
|
}
|
|
const sp = spotFor(L, s);
|
|
ok(`${n}p seat ${s}: spotlight sized`, sp.w > 100 && sp.h > 100);
|
|
}
|
|
for (const cards of [1, 7, 12, 25]) {
|
|
let rt = true;
|
|
for (let i = 0; i < cards; i++) {
|
|
const p = handPose(L, 0, i, cards);
|
|
const step = cards > 1 ? handPose(L, 0, 1, cards).x - handPose(L, 0, 0, cards).x : CARD_W;
|
|
const pt = { x: p.x - CARD_W / 2 + Math.min(step, CARD_W) * 0.45, y: p.y - CARD_H * 0.3 };
|
|
if (fanHit(L.seats[0].fan, cards, pt) !== i) rt = false;
|
|
}
|
|
ok(`${n}p: hand hit-test round trip (${cards})`, rt);
|
|
const z = fanZone(L.seats[0].fan, cards);
|
|
ok(`${n}p: hand zone covers the fan (${cards})`, inRect(handPose(L, 0, 0, cards), z) && inRect(handPose(L, 0, cards - 1, cards), z));
|
|
ok(`${n}p: insert index ends (${cards})`, fanInsertIndex(L.seats[0].fan, cards, 0) === 0 && fanInsertIndex(L.seats[0].fan, cards, W) === cards);
|
|
}
|
|
ok(`${n}p: deck + discard hit`, inPose(L.deck, L.deck) && inPose(L.discard, L.discard));
|
|
ok(`${n}p: direction ring on the cloth`, ringPoints(16, 0.3).every(p => T.onFelt(p)));
|
|
}
|
|
|
|
// ── Soak ─────────────────────────────────────────────────────────────────────
|
|
section('Soak');
|
|
function step(gs, skills, rand) {
|
|
const seat = gs.phase === 'challengeWindow' ? gs.pendingWild4.challengerSeat : gs.currentPlayer;
|
|
const a = chooseAction(gs, seat, { skill: skills[seat], rng: rand });
|
|
let g = gs, nx;
|
|
const callFirst = () => { if (g.players[seat].hand.length === 2 && rand() < UNO_CALL_CHANCE[skills[seat]]) g = U.applyCallUno(g, seat); };
|
|
if (a.kind === 'play') { callFirst(); nx = U.applyPlay(g, seat, a.cardId); if (nx !== g && nx.phase === 'choosingColor') nx = U.applyChooseColor(nx, a.chosenColor ?? 'r'); }
|
|
else if (a.kind === 'draw') nx = U.applyDraw(g, seat);
|
|
else if (a.kind === 'playDrawn') { callFirst(); nx = U.applyPlayDrawn(g, seat); if (nx !== g && nx.phase === 'choosingColor') nx = U.applyChooseColor(nx, a.chosenColor ?? 'r'); }
|
|
else if (a.kind === 'passDrawn') nx = U.applyPassAfterDraw(g, seat);
|
|
else if (a.kind === 'chooseColor') nx = U.applyChooseColor(g, a.color);
|
|
else if (a.kind === 'challenge') nx = U.applyChallengeWild4(g, a.doChallenge);
|
|
else if (a.kind === 'stack') nx = U.applyStackWild4(g, seat, a.cardId);
|
|
if (nx === g) return { gs, noop: a };
|
|
if (nx.unoVulnerable != null) {
|
|
for (let s = 0; s < nx.players.length; s++) {
|
|
if (s !== nx.unoVulnerable && rand() < CATCH_CHANCE[skills[s]] * 0.5) { nx = U.applyCatchUno(nx, s); break; }
|
|
}
|
|
}
|
|
return { gs: nx };
|
|
}
|
|
function playMatch(n, skills, seed, rules = {}, target = 500) {
|
|
const rand = mulberry32(seed);
|
|
let gs = U.createInitialState({ playerCount: n, seed, rules, target });
|
|
let hands = 0, noops = 0, conserve = true;
|
|
for (;;) {
|
|
let st = 0;
|
|
while (!U.isHandOver(gs) && st++ < 4000) {
|
|
const o = step(gs, skills, rand);
|
|
if (o.noop) { noops++; break; }
|
|
gs = o.gs;
|
|
if (U.countCards(gs) !== 108) conserve = false;
|
|
}
|
|
if (!U.isHandOver(gs)) return { stuck: true, noops, conserve, hands };
|
|
hands++;
|
|
if (gs.phase === 'gameOver') return { gs, hands, noops, conserve };
|
|
if (hands > 80) return { stuck: true, noops, conserve, hands };
|
|
gs = U.startNextHand(gs);
|
|
}
|
|
}
|
|
{
|
|
const M = QUICK ? 10 : 40;
|
|
for (const rules of [{}, { stacking: true }, { drawUntilPlayable: true }, { stacking: true, drawUntilPlayable: true }]) {
|
|
for (const n of [2, 3, 4]) {
|
|
let ended = 0, noops = 0, conserved = true, hands = 0, scoreOk = true;
|
|
for (let i = 0; i < M; i++) {
|
|
const skills = Array.from({ length: n }, (_, s) => 1 + ((i + s * 2) % 5));
|
|
const r = playMatch(n, skills, 300 + i * 17 + n, rules);
|
|
if (!r.stuck) ended++;
|
|
noops += r.noops;
|
|
if (!r.conserve) conserved = false;
|
|
hands += r.hands;
|
|
if (r.gs) {
|
|
const w = r.gs.matchWinner;
|
|
if (r.gs.scores[w] < 500 || r.gs.scores.some((x, s) => s !== w && x >= 500)) scoreOk = false;
|
|
}
|
|
}
|
|
const tag = `${n}p ${JSON.stringify(rules)}`;
|
|
ok(`${tag}: every match ends`, ended === M, `${ended}/${M}`);
|
|
ok(`${tag}: AI never makes an illegal move`, noops === 0, `${noops}`);
|
|
ok(`${tag}: 108 cards conserved`, conserved);
|
|
ok(`${tag}: winner reached 500, nobody else did`, scoreOk);
|
|
if (!rules.stacking && !rules.drawUntilPlayable) console.log(` ${n}p: ${M} matches to 500, avg ${(hands / M).toFixed(1)} hands`);
|
|
}
|
|
}
|
|
const duel = (a, b, games, target) => {
|
|
let w = 0;
|
|
for (let i = 0; i < games; i++) {
|
|
const sa = i % 2;
|
|
const r = playMatch(2, sa === 0 ? [a, b] : [b, a], 9000 + i * 7, {}, target);
|
|
if (r.gs?.matchWinner === sa) w++;
|
|
}
|
|
return w / games;
|
|
};
|
|
const G = QUICK ? 300 : 2000;
|
|
const r51 = duel(5, 1, G, 0), r31 = duel(3, 1, G, 0), r53 = duel(5, 3, G, 0);
|
|
console.log(` single-hand win rates: 5v1 ${(r51 * 100).toFixed(0)}% · 3v1 ${(r31 * 100).toFixed(0)}% · 5v3 ${(r53 * 100).toFixed(0)}%`);
|
|
ok('skill 5 beats skill 1', r51 >= (QUICK ? 0.55 : 0.6), `${(r51 * 100).toFixed(0)}%`);
|
|
ok('skill 3 beats skill 1', r31 >= (QUICK ? 0.55 : 0.58), `${(r31 * 100).toFixed(0)}%`);
|
|
// Uno is luck-heavy: measured over 10k hands, 5v3 ≈ 54%, 5v1 ≈ 72%
|
|
ok('skill 5 edges skill 3', r53 >= (QUICK ? 0.45 : 0.5), `${(r53 * 100).toFixed(0)}%`);
|
|
}
|
|
|
|
console.log(`\n${pass} passed, ${fail} failed`);
|
|
process.exit(fail ? 1 : 0);
|