/** * 윷판 그래프와 윷가락 (docs/games/yut.md §5.1, §5.2, §6). * Pure helpers shared by the rules module, its tests and the screen (node names, throw labels). */ import type { Rng } from '@bg/engine'; /** 0..28, see the table in §5.2. 0 = 참먹이(출발·도착), 22 = 방. */ export type NodeId = number; export type Throw = 'do' | 'gae' | 'geol' | 'yut' | 'mo' | 'backdo'; export type Sticks = [boolean, boolean, boolean, boolean]; export const NODE_COUNT = 29; export const GOAL = 'goal' as const; /** * Screen names (2026-11-08: plain numbers instead of 참먹이·모·뒷모·찌모): the corners are 1번(출발) → 2번 → 3번 → 4번 * in the moving direction, `N번 다음 k칸` is the k-th spot after corner N on the outer ring, `N번 지름길 k칸` the k-th * spot from corner N toward 방 on a diagonal. */ export const NODE_NAMES: readonly string[] = [ '1번(출발)', '1번 다음 1칸', '1번 다음 2칸', '1번 다음 3칸', '1번 다음 4칸', '2번', '2번 다음 1칸', '2번 다음 2칸', '2번 다음 3칸', '2번 다음 4칸', '3번', '3번 다음 1칸', '3번 다음 2칸', '3번 다음 3칸', '3번 다음 4칸', '4번', '4번 다음 1칸', '4번 다음 2칸', '4번 다음 3칸', '4번 다음 4칸', '2번 지름길 1칸', '2번 지름길 2칸', '방', '4번 지름길 2칸', '4번 지름길 1칸', '3번 지름길 1칸', '3번 지름길 2칸', '1번 지름길 2칸', '1번 지름길 1칸', ]; export const THROW_STEPS: Record = { do: 1, gae: 2, geol: 3, yut: 4, mo: 5, backdo: -1 }; export const THROW_KO: Record = { do: '도', gae: '개', geol: '걸', yut: '윷', mo: '모', backdo: '빽도' }; /** Display order of the result chips. */ export const THROW_ORDER: Throw[] = ['backdo', 'do', 'gae', 'geol', 'yut', 'mo']; /** Nodes where a piece that stopped there has two roads ahead: 모, 뒷모, 방. */ export const FORKS: readonly NodeId[] = [5, 10, 22]; export const isFork = (n: NodeId | 'home'): boolean => n === 5 || n === 10 || n === 22; /** * One step forward (§5.2 table). `starting` = this step leaves the node the piece stood on, * `prev` = the node passed just before `node` within the same move (only matters at 방). * `alt` (지역 룰 `forkRule = choose`): when starting on a fork, take the other road — 모/뒷모 keep to the outer ring, * 방 goes on toward 찌모 (23). * Returns GOAL when the piece leaves 참먹이 after a lap. */ export function step(node: NodeId | 'home', starting: boolean, prev: NodeId | null, alt = false): NodeId | typeof GOAL { if (node === 'home') return 1; if (node === 0) return GOAL; if (starting && alt && isFork(node)) return node === 22 ? 23 : node + 1; if (starting) { if (node === 5) return 20; if (node === 10) return 25; if (node === 22) return 27; } switch (node) { case 19: return 0; case 22: return prev === 26 ? 27 : 23; case 24: return 15; case 26: return 22; case 28: return 0; default: return node + 1; // 1..18 outer ring, 20→21, 21→22, 23→24, 25→26, 27→28 } } export interface Advance { /** Nodes entered, in order (the start node is not included; GOAL is not a node). */ path: NodeId[]; /** Final node, or GOAL when the piece finished. */ to: NodeId | typeof GOAL; } /** * Moves `n` (≥ 1) steps forward from `from`. Reaching GOAL drops the remaining steps. * `alt`: take the other road at the starting fork (see `step`). * With `reach`, arriving at 참먹이 after a lap (always the case when entering 0 by moving forward) finishes at once. */ export function advance(from: NodeId | 'home', n: number, finishRule: 'pass' | 'reach', alt = false): Advance { let cur: NodeId | 'home' = from; let prev: NodeId | null = null; const path: NodeId[] = []; for (let i = 0; i < n; i++) { const nxt = step(cur, i === 0, prev, alt && i === 0); if (nxt === GOAL) return { path, to: GOAL }; path.push(nxt); prev = cur === 'home' ? null : cur; cur = nxt; if (cur === 0 && finishRule === 'reach') return { path, to: GOAL }; } return { path, to: cur as NodeId }; } /** Fallback predecessor when a piece has no usable trail (only after 참먹이/빽도 corner cases). */ export function defaultPrev(node: NodeId): NodeId { switch (node) { case 0: return 19; case 20: return 5; case 25: return 10; case 22: return 21; case 23: return 22; case 27: return 22; default: return node - 1; // 2..19, 21, 24, 26, 28 } } /** Minimum steps to finish under `pass` (§6), index = node; HOME = 12. `reach` subtracts 1. */ const DIST_PASS: readonly number[] = [1, 11, 10, 9, 8, 7, 11, 10, 9, 8, 7, 10, 9, 8, 7, 6, 5, 4, 3, 2, 6, 5, 4, 8, 7, 6, 5, 3, 2]; export const HOME_DIST_PASS = 12; export function remainingDistance(node: NodeId | 'home', finishRule: 'pass' | 'reach'): number { const d = node === 'home' ? HOME_DIST_PASS : DIST_PASS[node]!; return finishRule === 'reach' ? d - 1 : d; } /** Maps the four sticks (true = 배(flat side) up; index 0 is the 빽도-marked stick) to a result (§5.1). */ export function throwOf(sticks: Sticks, backdo: boolean): Throw { const k = sticks.filter(Boolean).length; switch (k) { case 0: return 'mo'; case 1: return backdo && sticks[0] ? 'backdo' : 'do'; case 2: return 'gae'; case 3: return 'geol'; default: return 'yut'; } } export const FLAT_P = { realistic: 0.6, fair: 0.5 } as const; /** One throw (§8.4): 낙 check first (if enabled), then each stick independently. */ export function rollSticks( rng: Rng, o: { stickModel: keyof typeof FLAT_P; backdo: boolean; nakPercent: number }, ): { sticks: Sticks; result: Throw } | { sticks: null; result: 'nak' } { if (o.nakPercent > 0 && rng.next() < o.nakPercent / 100) return { sticks: null, result: 'nak' }; const p = FLAT_P[o.stickModel]; const sticks = [rng.next() < p, rng.next() < p, rng.next() < p, rng.next() < p] as Sticks; return { sticks, result: throwOf(sticks, o.backdo) }; } /** Exact result probabilities (§5.1 table). */ export function throwProbabilities(p: number, backdo = true): Record { const q = 1 - p; const one = 4 * p * q ** 3; return { backdo: backdo ? p * q ** 3 : 0, do: backdo ? 3 * p * q ** 3 : one, gae: 6 * p ** 2 * q ** 2, geol: 4 * p ** 3 * q, yut: p ** 4, mo: q ** 4, }; } // ---------------------------------------------------------------- geometry (증강 윷놀이) /** * Node centres on a 0..100 board (same layout as the screen): 참먹이 bottom-right, 모 top-right, 뒷모 top-left, * 찌모 bottom-left. Used by augments that fold, turn or flick the board. */ export const NODE_POS: readonly (readonly [number, number])[] = (() => { const LO = 8; const HI = 92; const GAP = (HI - LO) / 5; const p: [number, number][] = []; p[0] = [HI, HI]; for (let i = 1; i <= 4; i++) p[i] = [HI, HI - GAP * i]; p[5] = [HI, LO]; for (let i = 6; i <= 9; i++) p[i] = [HI - GAP * (i - 5), LO]; p[10] = [LO, LO]; for (let i = 11; i <= 14; i++) p[i] = [LO, LO + GAP * (i - 10)]; p[15] = [LO, HI]; for (let i = 16; i <= 19; i++) p[i] = [LO + GAP * (i - 15), HI]; const lerp = (a: [number, number], b: [number, number], t: number): [number, number] => [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t]; [20, 21, 22, 23, 24].forEach((n, k) => (p[n] = lerp(p[5]!, p[15]!, (k + 1) / 6))); [25, 26].forEach((n, k) => (p[n] = lerp(p[10]!, p[0]!, (k + 1) / 6))); [27, 28].forEach((n, k) => (p[n] = lerp(p[10]!, p[0]!, (k + 4) / 6))); return p; })(); /** The six straight lines of the board, end to end (4 sides, 2 diagonals through 방). */ export const STRAIGHT_LINES: readonly (readonly NodeId[])[] = [ [0, 1, 2, 3, 4, 5], [5, 6, 7, 8, 9, 10], [10, 11, 12, 13, 14, 15], [15, 16, 17, 18, 19, 0], [5, 20, 21, 22, 23, 24, 15], [10, 25, 26, 22, 27, 28, 0], ]; /** Nodes joined by a board line (graph neighbours, both directions). */ export const NEIGHBOURS: readonly (readonly NodeId[])[] = (() => { const nb: Set[] = Array.from({ length: NODE_COUNT }, () => new Set()); for (const line of STRAIGHT_LINES) for (let i = 0; i + 1 < line.length; i++) { nb[line[i]!]!.add(line[i + 1]!); nb[line[i + 1]!]!.add(line[i]!); } return nb.map((x) => [...x].sort((a, b) => a - b)); })(); /** Node whose centre is `pt` (exact layout match); throws if none. */ function nodeAt(pt: readonly [number, number]): NodeId { const i = NODE_POS.findIndex((q) => Math.abs(q[0] - pt[0]) < 1e-6 && Math.abs(q[1] - pt[1]) < 1e-6); if (i < 0) throw new Error(`no node at ${pt}`); return i; } const mapNodes = (f: (p: readonly [number, number]) => [number, number]): NodeId[] => NODE_POS.map((p) => nodeAt(f(p))); /** Mirror over the 참먹이–뒷모 diagonal: walking the mirrored board forward = walking this one backwards (달걸음). */ export const MIRROR_DIAG: readonly NodeId[] = mapNodes(([x, y]) => [y, x]); /** Left ↔ right, top ↔ bottom mirrors and a quarter turn clockwise (as seen on screen). */ export const MIRROR_LR: readonly NodeId[] = mapNodes(([x, y]) => [100 - x, y]); export const MIRROR_TB: readonly NodeId[] = mapNodes(([x, y]) => [x, 100 - y]); export const ROTATE_CW: readonly NodeId[] = mapNodes(([x, y]) => [100 - y, x]); /** Nodes on the two diagonals, corners excluded (크고 빨간 버튼). */ export const DIAGONAL_NODES: readonly NodeId[] = [20, 21, 22, 23, 24, 25, 26, 27, 28]; /** Result of `k` sticks of which `flats` show 배 (0 = 모, ≥ 4 = 윷); the marked stick alone gives 빽도. */ export function throwOfCount(flats: number, markedOnly: boolean, backdo: boolean): Throw { if (flats === 0) return 'mo'; if (flats === 1) return backdo && markedOnly ? 'backdo' : 'do'; if (flats === 2) return 'gae'; if (flats === 3) return 'geol'; return 'yut'; }