fertig-classic-games/tools/genCribbageCribTable.js

48 lines
1.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
// genCribbageCribTable.js — exact expected crib value for every two-card
// lay-away, used by CribbageAI.js (CRIB_EV / CRIB_EV_SUITED).
//
// For each rank pair (and suited / off-suit), averages scoreHand(crib, starter,
// true) over EVERY other two cards the opponent could lay away (from the 50
// unseen) and every starter (from the 48 left). Prints the two 13×13 tables.
// Usage: node tools/genCribbageCribTable.js (≈ 30 s)
import { makeCard, fullDeck, scoreHand, RANKS } from '../src/games/cribbage/CribbageData.js';
function expectedCrib(a, b) {
const rest = fullDeck().filter(c => c.id !== a.id && c.id !== b.id);
let sum = 0, n = 0;
for (let i = 0; i < rest.length; i++) {
for (let j = i + 1; j < rest.length; j++) {
const crib = [a, b, rest[i], rest[j]];
for (let k = 0; k < rest.length; k++) {
if (k === i || k === j) continue;
sum += scoreHand(crib, rest[k], true).total;
n++;
}
}
}
return sum / n;
}
const off = [], suited = [];
for (let r1 = 0; r1 < 13; r1++) {
off.push(new Array(13).fill(0));
suited.push(new Array(13).fill(0));
}
for (let r1 = 0; r1 < 13; r1++) {
for (let r2 = r1; r2 < 13; r2++) {
const v = expectedCrib(makeCard(RANKS[r1], 's'), makeCard(RANKS[r2], 'h'));
off[r1][r2] = off[r2][r1] = +v.toFixed(2);
if (r2 !== r1) {
const s = expectedCrib(makeCard(RANKS[r1], 's'), makeCard(RANKS[r2], 's'));
suited[r1][r2] = suited[r2][r1] = +s.toFixed(2);
}
}
process.stderr.write(`${RANKS[r1]} `);
}
process.stderr.write('\n');
const fmt = (t) => '[\n' + t.map(row => ' [' + row.map(v => v.toFixed(2).padStart(5)).join(',') + '],').join('\n') + '\n]';
console.log(`export const CRIB_EV = ${fmt(off)};`);
console.log(`export const CRIB_EV_SUITED = ${fmt(suited)};`);