/** * Hand shape analysis on 34-kind count arrays (§5.6): standard 4 sets + pair, 칠대자, 국사무쌍; all decompositions * for 고점법 (§6.2) and tenpai waits (§5.5). */ import { YAOCHU, type Kind } from './tiles'; export interface SetPart { t: 'shuntsu' | 'koutsu'; /** Lowest kind of a sequence, or the triplet kind. */ k: Kind; } export interface Decomposition { pair: Kind; sets: SetPart[]; } /** Can the counts be split into exactly `need` sets (no pair)? Mutates and restores `c`. */ function canSets(c: number[], need: number, from = 0): boolean { let i = from; while (i < 34 && c[i] === 0) i++; if (i === 34) return need === 0; if (need === 0) return false; if (c[i]! >= 3) { c[i]! -= 3; const ok = canSets(c, need - 1, i); c[i]! += 3; if (ok) return true; } if (i < 27 && i % 9 <= 6 && c[i + 1]! > 0 && c[i + 2]! > 0) { c[i]!--; c[i + 1]!--; c[i + 2]!--; const ok = canSets(c, need - 1, i); c[i]!++; c[i + 1]!++; c[i + 2]!++; if (ok) return true; } return false; } export function isStandardAgari(c: number[], sets: number): boolean { for (let p = 0; p < 34; p++) { if (c[p]! < 2) continue; c[p]! -= 2; const ok = canSets(c, sets); c[p]! += 2; if (ok) return true; } return false; } export function isChiitoi(c: number[]): boolean { let pairs = 0; for (let k = 0; k < 34; k++) { if (c[k] === 2) pairs++; else if (c[k] !== 0) return false; } return pairs === 7; } export function isKokushi(c: number[]): boolean { let total = 0; let pair = false; for (const k of YAOCHU) { const n = c[k]!; if (n === 0 || n > 2) return false; if (n === 2) { if (pair) return false; pair = true; } total += n; } return pair && total === 14; } /** Winning shape for the concealed counts given `meldCount` called/kan sets. */ export function isAgariShape(c: number[], meldCount: number): boolean { if (meldCount === 0 && (isChiitoi(c) || isKokushi(c))) return true; return isStandardAgari(c, 4 - meldCount); } function collect(c: number[], need: number, from: number, acc: SetPart[], out: SetPart[][]): void { let i = from; while (i < 34 && c[i] === 0) i++; if (i === 34) { if (need === 0) out.push([...acc]); return; } if (need === 0) return; if (c[i]! >= 3) { c[i]! -= 3; acc.push({ t: 'koutsu', k: i }); collect(c, need - 1, i, acc, out); acc.pop(); c[i]! += 3; } if (i < 27 && i % 9 <= 6 && c[i + 1]! > 0 && c[i + 2]! > 0) { c[i]!--; c[i + 1]!--; c[i + 2]!--; acc.push({ t: 'shuntsu', k: i }); collect(c, need - 1, i, acc, out); acc.pop(); c[i]!++; c[i + 1]!++; c[i + 2]!++; } } /** Every standard decomposition of the concealed counts into `sets` sets + 1 pair. */ export function decompositions(c: number[], sets: number): Decomposition[] { const out: Decomposition[] = []; const work = [...c]; for (let p = 0; p < 34; p++) { if (work[p]! < 2) continue; work[p]! -= 2; const found: SetPart[][] = []; collect(work, sets, 0, [], found); for (const s of found) out.push({ pair: p, sets: s }); work[p]! += 2; } return out; } /** * Tenpai waits of a hand that needs one tile (13, 10, 7, 4 or 1 concealed tiles). A kind whose four copies the * player already holds (hand + melds, `owned`) is not a wait (§5.5). */ export function waitsOf(c: number[], meldCount: number, owned?: number[]): Kind[] { const out: Kind[] = []; for (let k = 0; k < 34; k++) { if ((owned ? owned[k]! : c[k]!) >= 4) continue; // Prune: a wait must touch a held tile (or be any yaochu kind for 국사무쌍). if (!near(c, k) && !(meldCount === 0 && YAOCHU.includes(k))) continue; c[k]!++; const ok = isAgariShape(c, meldCount); c[k]!--; if (ok) out.push(k); } return out; } function near(c: number[], k: Kind): boolean { if (c[k]! > 0) return true; if (k >= 27) return false; const n = k % 9; for (let d = -2; d <= 2; d++) { if (d === 0) continue; const m = n + d; if (m >= 0 && m <= 8 && c[k + d]! > 0) return true; } return false; }