M4: 윷놀이 규칙·화면·테스트

- 규칙(packages/games/yut): 29칸 윷판 그래프(모서리·방은 멈췄다 출발할 때만 꺾음, 방에서는 항상 참먹이 쪽),
  가락 단위 확률(realistic 0.6 / fair 0.5, 빽도 표시 가락, 낙), 윷·모·잡기 추가 던지기, 모아 둔 결과 자유 순서,
  쓸 수 없는 빽도 자동 버림, 빽도는 왔던 길(trail)로, 도 자리 빽도(참먹이/바로 남), pass/reach 날 판정,
  업기·잡기(지나가면 안 잡음), 개인전 2~4명(1등 종료/꼴찌까지), 팀전 4·6·8명(청·홍 번갈아, 팀원 순환),
  기권(팀은 전원 기권 시 패배), 시간 초과 자동 행동(잡기 > 내기 > 업기 > 뒤처진 말 > 올리기), 3연속 초과 시 자리 비움(5초)
- 테스트: 문서 10절 시나리오 + 확률 10만 회(±0.5%p), 무작위 1,000판(불변식·onTimeout 합법·결정성·리플레이),
  자동 진행·무작위 기권 60판, apply p99 < 5ms. 서버 방 테스트 2개(팀전 인원 검사, 연결 끊김 자동 진행·재접속 View)
- 화면(apps/web/games/yut): SVG 윷판·말(편마다 색+모양), 윷가락 던지기 애니메이션, 결과 칩 → 말 → 한 번 더 눌러 확정,
  경로 미리보기와 잡기!/날!/업기 배지, 자동 이동, 칸마다 통통 이동 애니메이션, 진행 기록, 옵션·규칙 설명, 키보드 조작
- 엔진/서버(하위 호환): GameDefinition.playerCountError(옵션별 인원 규칙) 추가, 방 시작 시 검사.
  방 인원이 게임 최대였으면 옵션 변경으로 범위가 바뀔 때 새 최대를 따라감(팀전 8자리)
- 등록: GAMES, 카탈로그(쉬움·15~25분), 화면 레지스트리, 홈의 "준비 중" 목록에서 윷놀이 제거
- E2E: PC 방장 + 휴대폰 친구, 말 2개로 실제 화면 조작(키보드 2단계 확정)으로 끝까지, 다크 모드 화면 저장

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-05 04:29:46 +09:00
parent 5dd8e6ea07
commit 2610e37d49
12 changed files with 2706 additions and 1 deletions

View File

@@ -0,0 +1,152 @@
/**
* 윷판 그래프와 윷가락 (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;
export const NODE_NAMES: readonly string[] = [
'참먹이', '도', '개', '걸', '윷', '모', '뒷도', '뒷개', '뒷걸', '뒷윷',
'뒷모', '찌도', '찌개', '찌걸', '찌윷', '찌모', '날도', '날개', '날걸', '날윷',
'앞모도', '앞모개', '방', '속윷', '속모', '뒷모도', '뒷모개', '사려', '안찌',
];
export const THROW_STEPS: Record<Throw, number> = { do: 1, gae: 2, geol: 3, yut: 4, mo: 5, backdo: -1 };
export const THROW_KO: Record<Throw, string> = { do: '도', gae: '개', geol: '걸', yut: '윷', mo: '모', backdo: '빽도' };
/** Display order of the result chips. */
export const THROW_ORDER: Throw[] = ['backdo', 'do', 'gae', 'geol', 'yut', 'mo'];
/**
* 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 방).
* Returns GOAL when the piece leaves 참먹이 after a lap.
*/
export function step(node: NodeId | 'home', starting: boolean, prev: NodeId | null): NodeId | typeof GOAL {
if (node === 'home') return 1;
if (node === 0) return GOAL;
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.
* 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'): 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);
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<Throw, number> {
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,
};
}