Files
joke-app/packages/games/src/quoridor/board.ts
EJClaw 6390e3e64c 게임 4개 원래 이름으로, .5 + NPMplus 배포 구성
- 보석 상인 → 스플렌더, 벽 쌓기 길찾기 → 쿼리도, 숫자 타일 → 루미큐브, 주사위 땅따먹기 → 부루마불
- deploy/docker-compose.proxy.yml: Caddy 없이 앱만(.5:2970), HTTPS·헤더는 NPMplus
- deploy/npmplus-advanced.conf: Caddyfile과 같은 보안 헤더
- 문서: 2026-10-06 결정, 배포 7절 (e2e 구십구 0 카드 방향 버튼 반영 포함)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 22:16:32 +09:00

223 lines
8.8 KiB
TypeScript
Raw 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.
/**
* 쿼리도 판 계산 (docs/games/quoridor.md §3, §5): 좌표, 벽 충돌, 말 이동, BFS 경로.
* 칸 (c, r) 0~8, r=0이 1행(아래). 벽 앵커 (c, r) 0~7은 칸 (c,r)(c+1,r)(c,r+1)(c+1,r+1)의 가운데 교차점.
*/
export type Seat = 'S' | 'W' | 'N' | 'E';
export interface Cell {
c: number;
r: number;
}
export type Orientation = 'h' | 'v';
export interface Wall {
c: number;
r: number;
o: Orientation;
}
/** 8×8 앵커별 점유, 인덱스 r*8+c. */
export interface WallIndex {
h: boolean[];
v: boolean[];
}
export type JumpTrigger = 'wallEdgePawn' | 'wallOnly';
export const SIZE = 9;
export const ANCHORS = 8;
const CELLS = SIZE * SIZE;
export const SEAT_KO: Record<Seat, string> = { S: '남', W: '서', N: '북', E: '동' };
export const START: Record<Seat, Cell> = { S: { c: 4, r: 0 }, W: { c: 0, r: 4 }, N: { c: 4, r: 8 }, E: { c: 8, r: 4 } };
/** 목표 줄: 남 → 9행, 북 → 1행, 서 → i열, 동 → a열. */
export const isGoal = (seat: Seat, c: number, r: number): boolean =>
seat === 'S' ? r === SIZE - 1 : seat === 'N' ? r === 0 : seat === 'W' ? c === SIZE - 1 : c === 0;
export const emptyWallIndex = (): WallIndex => ({ h: new Array<boolean>(64).fill(false), v: new Array<boolean>(64).fill(false) });
export const onBoard = (c: number, r: number): boolean => c >= 0 && r >= 0 && c < SIZE && r < SIZE;
export const anchorInRange = (c: number, r: number): boolean => Number.isInteger(c) && Number.isInteger(r) && c >= 0 && r >= 0 && c < ANCHORS && r < ANCHORS;
export const cellIdx = (c: number, r: number): number => r * SIZE + c;
export const sameCell = (a: Cell, b: Cell): boolean => a.c === b.c && a.r === b.r;
export const cellLabel = (p: Cell): string => `${'abcdefghi'[p.c]}${p.r + 1}`;
export const wallLabel = (w: Wall): string => `${w.o === 'h' ? '가로' : '세로'} ${cellLabel(w)}`;
const hasH = (w: WallIndex, c: number, r: number): boolean => c >= 0 && r >= 0 && c < ANCHORS && r < ANCHORS && w.h[r * ANCHORS + c]!;
const hasV = (w: WallIndex, c: number, r: number): boolean => c >= 0 && r >= 0 && c < ANCHORS && r < ANCHORS && w.v[r * ANCHORS + c]!;
/** (c,r)에서 (dc,dr) 방향 이웃으로 가는 길이 벽에 막혔는지. 판 안 여부는 호출하는 쪽이 본다. */
export function blocked(w: WallIndex, c: number, r: number, dc: number, dr: number): boolean {
if (dr === 1) return hasH(w, c, r) || hasH(w, c - 1, r);
if (dr === -1) return hasH(w, c, r - 1) || hasH(w, c - 1, r - 1);
if (dc === 1) return hasV(w, c, r) || hasV(w, c, r - 1);
return hasV(w, c - 1, r) || hasV(w, c - 1, r - 1);
}
/** 북·남·동·서 순서(결정적). */
export const DIRS: readonly [number, number][] = [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
];
/** 기존 벽과 겹치는지(§5.3 조건 3): 같은 방향 반 칸 겹침, 같은 앵커 십자 교차. */
export function overlaps(w: WallIndex, c: number, r: number, o: Orientation): boolean {
if (o === 'h') return hasH(w, c - 1, r) || hasH(w, c, r) || hasH(w, c + 1, r) || hasV(w, c, r);
return hasV(w, c, r - 1) || hasV(w, c, r) || hasV(w, c, r + 1) || hasH(w, c, r);
}
export function setWall(w: WallIndex, wall: Wall, on: boolean): void {
(wall.o === 'h' ? w.h : w.v)[wall.r * ANCHORS + wall.c] = on;
}
// BFS 작업 공간(호출마다 stamp로 초기화 → 할당 없음). 순수 함수 결과에는 영향 없음.
const queue = new Int16Array(CELLS);
const seen = new Int32Array(CELLS);
const dist = new Int16Array(CELLS);
const parent = new Int16Array(CELLS);
let stamp = 0;
/** 말은 무시하고 벽만 보는 최단 거리(칸 수). 길이 없으면 -1. §5.3 경로 검사. */
export function pathLength(w: WallIndex, start: Cell, seat: Seat): number {
if (isGoal(seat, start.c, start.r)) return 0;
stamp++;
let head = 0;
let tail = 0;
const s0 = cellIdx(start.c, start.r);
queue[tail++] = s0;
seen[s0] = stamp;
dist[s0] = 0;
while (head < tail) {
const cur = queue[head++]!;
const c = cur % SIZE;
const r = (cur - c) / SIZE;
for (const [dc, dr] of DIRS) {
const nc = c + dc;
const nr = r + dr;
if (!onBoard(nc, nr) || blocked(w, c, r, dc, dr)) continue;
const ni = nr * SIZE + nc;
if (seen[ni] === stamp) continue;
seen[ni] = stamp;
dist[ni] = dist[cur]! + 1;
if (isGoal(seat, nc, nr)) return dist[ni]!;
queue[tail++] = ni;
}
}
return -1;
}
/** 출발 칸 다음부터 목표 줄의 칸까지 최단 경로(결정적). 길이 없으면 null. */
export function shortestPath(w: WallIndex, start: Cell, seat: Seat): Cell[] | null {
if (isGoal(seat, start.c, start.r)) return [];
stamp++;
let head = 0;
let tail = 0;
const s0 = cellIdx(start.c, start.r);
queue[tail++] = s0;
seen[s0] = stamp;
parent[s0] = -1;
while (head < tail) {
const cur = queue[head++]!;
const c = cur % SIZE;
const r = (cur - c) / SIZE;
for (const [dc, dr] of DIRS) {
const nc = c + dc;
const nr = r + dr;
if (!onBoard(nc, nr) || blocked(w, c, r, dc, dr)) continue;
const ni = nr * SIZE + nc;
if (seen[ni] === stamp) continue;
seen[ni] = stamp;
parent[ni] = cur;
if (isGoal(seat, nc, nr)) {
const out: Cell[] = [];
for (let i = ni; i !== s0; i = parent[i]!) out.push({ c: i % SIZE, r: Math.floor(i / SIZE) });
return out.reverse();
}
queue[tail++] = ni;
}
}
return null;
}
export interface Pawn {
seat: Seat;
pos: Cell;
}
/** §5.2 이동 가능 칸(중복 없음, 결정적 순서). `pawns`는 판에 있는 모든 말(나 포함). */
export function legalPawnMoves(w: WallIndex, pawns: readonly Pawn[], me: Cell, trigger: JumpTrigger): Cell[] {
const occ = new Set(pawns.map((p) => cellIdx(p.pos.c, p.pos.r)));
const out: Cell[] = [];
const seenOut = new Set<number>();
const add = (c: number, r: number) => {
const i = cellIdx(c, r);
if (!seenOut.has(i)) {
seenOut.add(i);
out.push({ c, r });
}
};
for (const [dc, dr] of DIRS) {
const nc = me.c + dc;
const nr = me.r + dr;
if (!onBoard(nc, nr) || blocked(w, me.c, me.r, dc, dr)) continue;
if (!occ.has(cellIdx(nc, nr))) {
add(nc, nr);
continue;
}
// 인접 칸에 말 Q: 곧게 뛰어넘기 → 안 되면 조건에 따라 대각.
const jc = nc + dc;
const jr = nr + dr;
const jOn = onBoard(jc, jr);
const wallBehind = jOn && blocked(w, nc, nr, dc, dr);
if (jOn && !wallBehind && !occ.has(cellIdx(jc, jr))) {
add(jc, jr);
continue;
}
if (trigger === 'wallOnly' && !wallBehind) continue;
for (const [pc, pr] of dc === 0 ? [[1, 0], [-1, 0]] : [[0, 1], [0, -1]]) {
const tc = nc + pc!;
const tr = nr + pr!;
if (onBoard(tc, tr) && !blocked(w, nc, nr, pc!, pr!) && !occ.has(cellIdx(tc, tr))) add(tc, tr);
}
}
return out;
}
/** 이동할 수 없는 칸의 이유(한국어). */
export function moveRefusal(w: WallIndex, pawns: readonly Pawn[], me: Cell, to: Cell, trigger: JumpTrigger): string {
if (!onBoard(to.c, to.r)) return '판 밖이에요.';
if (sameCell(me, to)) return '지금 있는 칸이에요.';
const occ = new Set(pawns.map((p) => cellIdx(p.pos.c, p.pos.r)));
if (occ.has(cellIdx(to.c, to.r))) return '다른 말이 있는 칸이에요.';
const dc = to.c - me.c;
const dr = to.r - me.r;
const ad = Math.abs(dc) + Math.abs(dr);
if (ad === 1) return blocked(w, me.c, me.r, dc, dr) ? '벽이 가로막고 있어요.' : '그 칸으로는 갈 수 없어요.';
if (Math.abs(dc) === 1 && Math.abs(dr) === 1) {
// 대각: 옆에 말이 있고 곧게 넘을 수 있으면 대각은 안 됨.
for (const [sc, sr] of [
[dc, 0],
[0, dr],
] as const) {
if (!occ.has(cellIdx(me.c + sc, me.r + sr)) || blocked(w, me.c, me.r, sc, sr)) continue;
const jc = me.c + 2 * sc;
const jr = me.r + 2 * sr;
if (onBoard(jc, jr) && !blocked(w, me.c + sc, me.r + sr, sc, sr) && !occ.has(cellIdx(jc, jr))) {
return '곧게 뛰어넘을 수 있을 때는 대각선으로 갈 수 없어요.';
}
if (trigger === 'wallOnly' && !(onBoard(jc, jr) && blocked(w, me.c + sc, me.r + sr, sc, sr))) {
return '이 방은 넘을 말 뒤에 벽이 있을 때만 대각선으로 갈 수 있어요.';
}
const mc = me.c + sc;
const mr = me.r + sr;
if (blocked(w, mc, mr, to.c - mc, to.r - mr)) return '벽이 가로막고 있어요.';
}
return '대각선으로는 상대 말을 넘을 때만 갈 수 있어요.';
}
if (ad === 2 && (dc === 0 || dr === 0)) {
const sc = Math.sign(dc);
const sr = Math.sign(dr);
if (!occ.has(cellIdx(me.c + sc, me.r + sr))) return '한 번에 한 칸씩만 움직일 수 있어요.';
return '벽이 가로막고 있어요.';
}
if (ad === 3 && (dc === 0 || dr === 0)) return '말은 한 번에 하나만 뛰어넘을 수 있어요.';
return '그 칸으로는 갈 수 없어요.';
}