/** * The 40-card Italian deck (DESIGN §3). A card is a number 0..39 so states stay plain JSON and the fairness sim stays fast: * suit = floor(id / 10) (0 denari, 1 coppe, 2 spade, 3 bastoni) * rank = id % 10 + 1 (1..7, 8 Fante, 9 Cavallo, 10 Re) */ export type Card = number; export type Suit = 0 | 1 | 2 | 3; /** Seat at the table: the UI puts the player on 0 and the CPU (or the second player) on 1. */ export type Seat = 0 | 1; export const SUITS = ['denari', 'coppe', 'spade', 'bastoni'] as const; export type SuitName = (typeof SUITS)[number]; export const DENARI: Suit = 0; export const FANTE = 8; export const CAVALLO = 9; export const RE = 10; /** The deck in its fixed reference order (before any shuffle): denari 1..10, coppe 1..10, spade 1..10, bastoni 1..10. */ export const FULL_DECK: readonly Card[] = Object.freeze(Array.from({ length: 40 }, (_, i) => i)); export const SETTEBELLO: Card = cardOf(DENARI, 7); export const RE_DI_DENARI: Card = cardOf(DENARI, RE); export function cardOf(suit: Suit, rank: number): Card { return suit * 10 + rank - 1; } export function suitOf(c: Card): Suit { return Math.floor(c / 10) as Suit; } export function rankOf(c: Card): number { return (c % 10) + 1; } /** Short code for logs, fixtures and the verifier page: rank + suit letter, e.g. "7D" (settebello), "10B" (Re di bastoni). */ export function cardCode(c: Card): string { return `${rankOf(c)}${'DCSB'[suitOf(c)]}`; } /** Inverse of `cardCode` ("7D" -> 6). Throws on anything else, so fixtures fail loudly. */ export function parseCard(code: string): Card { const m = /^(10|[1-9])([DCSB])$/.exec(code.trim().toUpperCase()); if (!m) throw new Error(`bad card code: ${code}`); return cardOf('DCSB'.indexOf(m[2]) as Suit, Number(m[1])); } export function parseCards(codes: string): Card[] { return codes.trim() ? codes.trim().split(/\s+/).map(parseCard) : []; } /** Scopa capture value: the rank (Fante 8, Cavallo 9, Re 10). */ export function scopaValue(c: Card): number { return rankOf(c); } /** Primiera value by rank 1..10 (DESIGN §3-1): 7=21, 6=18, A=16, 5=15, 4=14, 3=13, 2=12, figures=10. */ const PRIMIERA = [0, 16, 12, 13, 14, 15, 18, 21, 10, 10, 10]; export function primieraValue(c: Card): number { return PRIMIERA[rankOf(c)]; } /** Briscola strength by rank 1..10 (higher wins): A > 3 > Re > Cavallo > Fante > 7 > 6 > 5 > 4 > 2. */ const STRENGTH = [0, 10, 1, 9, 2, 3, 4, 5, 6, 7, 8]; export function briscolaStrength(c: Card): number { return STRENGTH[rankOf(c)]; } /** Briscola card points by rank 1..10: A 11, 3 10, Re 4, Cavallo 3, Fante 2, others 0 (120 in the deck). */ const POINTS = [0, 11, 0, 10, 0, 0, 0, 0, 2, 3, 4]; export function briscolaPoints(c: Card): number { return POINTS[rankOf(c)]; }