Files
joke-app/apps/web/src/games/omok/index.tsx
EJClaw ca8baf5cc8 모든 게임이 남는 화면 전체를 쓰도록: 게임별 폭 상한 제거, 판·카드·말을 --play-w/--play-h에 맞춰 키움
- 카드 게임: 펠트가 남는 높이를 채우고 손패·가운데 더미·자리 크기가 화면에 따라 커짐
- 판 게임: 체스·바둑·오목 등은 높이를 다 쓰고, 장기·쿼리도·젬블로는 넓은 화면에서 조작판을 판 옆으로
- 섯다: 자리를 판 크기에 맞춘 타원 위에 고르게 두고, 많을 때 자리 크기를 줄여 8명에서도 겹치지 않게
- 바퀴벌레 포커 6명·야단법석 달리기 5명 선수 뽑기가 PC에서 넘치지 않게
- 휴대폰·태블릿(1100px 아래) 배치는 그대로
- layout-survey: 컴퓨터 넣기 버튼 기다림, 넘친 높이(px) 표시

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

584 lines
28 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/omok.md §9). */
import { useEffect, useMemo, useRef, useState } from 'react';
import type { OmokOptions, OmokView, Pt } from '@bg/games/omok';
import type { PublicUser } from '@bg/shared/lite';
import { Avatar, ConfirmSheet, Sheet } from '../../components/ui';
import { TurnTimer, useServerNow } from '../../components/TurnTimer';
import { haptic, sfx } from '../../lib/feedback';
import { BOARD_MOTION_CSS, useAnimOver, useMoveDiff } from '../board-motion';
import { bump, useBanner } from '../card-fx';
import type { BoardProps, GameUI, OptionsProps } from '../types';
import { helpNow } from './help';
import { quickStart } from './quick';
import { fxMs } from '../../lib/motion';
type Color = 'black' | 'white';
const COLOR_KO: Record<Color, string> = { black: '흑', white: '백' };
const COLS = 'ABCDEFGHIJKLMNOPQRS';
const label = (size: number, p: Pt) => `${COLS[p.x]}${size - p.y}`;
const RULE_KO: Record<OmokOptions['ruleSet'], string> = { korean: '한국식', renju: '렌주룰', free: '자유룰' };
function stars(size: number): Pt[] {
const s = size === 19 ? [3, 9, 15] : [3, 7, 11];
return s.flatMap((x) => s.map((y) => ({ x, y })));
}
function Stone({ color, cx, cy, r, ghost }: { color: Color; cx: number; cy: number; r: number; ghost?: boolean }) {
return (
<g opacity={ghost ? 0.55 : 1}>
{!ghost && <ellipse cx={cx + r * 0.1} cy={cy + r * 0.16} rx={r} ry={r * 0.96} fill="#000" opacity={0.22} />}
<circle cx={cx} cy={cy} r={r} fill={color === 'black' ? '#151515' : '#fbfbf8'} stroke={color === 'black' ? '#000' : '#666'} strokeWidth={0.06} />
{color === 'black' ? (
<circle cx={cx - r * 0.3} cy={cy - r * 0.3} r={r * 0.28} fill="#fff" opacity={0.15} />
) : (
<circle cx={cx + r * 0.25} cy={cy + r * 0.25} r={r * 0.6} fill="#000" opacity={0.05} />
)}
</g>
);
}
/** A refused forbidden point (금수): a red mark pops up and "금수" floats away; the board gives a small shake. */
const OMOK_CSS = `
.om-forbid { transform-box: fill-box; transform-origin: center; animation: om-forbid .8s ease-out both; pointer-events: none; }
@keyframes om-forbid { 0% { transform: scale(.3); opacity: 0; } 18% { transform: scale(1.25); opacity: 1; } 32% { transform: scale(1); } 75% { opacity: 1; } 100% { opacity: 0; } }
.om-forbid-t { animation: om-forbid-t .9s ease-out both; pointer-events: none; }
@keyframes om-forbid-t { 0% { transform: translateY(0); opacity: 0; } 20% { opacity: 1; } 100% { transform: translateY(-.9px); opacity: 0; } }
/* PC: the board takes the whole play-area height minus the row that can show under it (--om-rows). */
@media (min-width: 1100px) { .board-wrap.om-fit { max-width: min(100%, calc(var(--play-h) - var(--om-rows, 0px))); } }
`;
function OmokBoard({ view: v, act, pending, tapConfirm }: BoardProps<OmokView>) {
const n = v.size;
const myTurn = v.phase === 'playing' && v.myColor === v.turn;
const [preview, setPreview] = useState<Pt | null>(null);
const [hover, setHover] = useState<Pt | null>(null);
const [cursor, setCursor] = useState<Pt>({ x: (n - 1) / 2, y: (n - 1) / 2 });
const [announce, setAnnounce] = useState('');
const [kbd, setKbd] = useState(false);
const svgRef = useRef<SVGSVGElement>(null);
const prevCount = useRef(v.moveCount);
const prevTurn = useRef(v.turn);
const [refused, setRefused] = useState<{ p: Pt; key: number } | null>(null);
const [banner, showBanner] = useBanner();
useEffect(() => {
if (!refused) return;
const t = setTimeout(() => setRefused(null), fxMs(950));
return () => clearTimeout(t);
}, [refused]);
// The game is over: "오목!" for five in a row (who won underneath), otherwise just who won.
const prevPhase = useRef(v.phase);
useEffect(() => {
const before = prevPhase.current;
prevPhase.current = v.phase;
if (before === v.phase || v.phase !== 'finished' || !v.result) return;
const w = v.result.winner;
const tone = w && w === v.myColor ? 'gold' : v.myColor && w ? 'dark' : 'blue';
if (v.winLine?.length) showBanner('오목!', tone, w ? `${COLOR_KO[w]} 승리` : undefined);
else showBanner(w ? `${COLOR_KO[w]} 승리` : '무승부', w ? tone : 'green', v.result.summary);
}, [v.phase]); // eslint-disable-line react-hooks/exhaustive-deps
const forbidden = useMemo(() => new Set(v.forbidden.map((p) => p.y * n + p.x)), [v.forbidden, n]);
// What the board was before the last change: a new stone sets down, stones taken back by 무르기 fade away.
const { before, forward } = useMoveDiff(v.board, v.moveCount);
const lifted = before && !forward ? before.flatMap((c, i) => (c !== 0 && v.board[i] === 0 ? [{ i, color: (c === 1 ? 'black' : 'white') as Color }] : [])) : [];
const fresh = !!before && forward;
const [liftedOver, liftedEnd] = useAnimOver(v.moveCount);
// Sounds, vibration and screen-reader announcements on new moves (mine, the opponent's and the computer's).
useEffect(() => {
if (v.moveCount > prevCount.current && v.lastMove) {
sfx.stone();
const who = v.moves[v.moves.length - 1]!.color;
setAnnounce(`${COLOR_KO[who]}이 ${label(n, v.lastMove)}에 두었어요.`);
} else if (v.moveCount < prevCount.current) sfx.whoosh();
if (v.phase === 'playing' && myTurn && (prevTurn.current !== v.turn || v.moveCount === 0) && v.moveCount !== prevCount.current) haptic(40);
prevCount.current = v.moveCount;
prevTurn.current = v.turn;
setPreview(null);
}, [v.moveCount, v.turn]); // eslint-disable-line react-hooks/exhaustive-deps
const canPlace = (p: Pt) =>
myTurn && !pending && v.board[p.y * n + p.x] === 0 && !forbidden.has(p.y * n + p.x) && (!v.firstMoveCenter || v.moveCount > 0 || (p.x === (n - 1) / 2 && p.y === (n - 1) / 2));
const place = (p: Pt) => {
act({ type: 'place', x: p.x, y: p.y });
setPreview(null);
setHover(null);
};
const tap = (p: Pt, mouse: boolean) => {
setCursor(p);
if (!myTurn || v.board[p.y * n + p.x] !== 0) return;
if (forbidden.has(p.y * n + p.x)) {
haptic([30, 30, 30]);
sfx.bad();
setAnnounce('여기는 금수 자리라 둘 수 없어요.');
setRefused({ p, key: Date.now() });
bump(svgRef.current, 'shake');
return;
}
// Mouse: one click places (hover already shows the preview). Touch: optional two-step confirm.
if (mouse || !tapConfirm) {
if (canPlace(p)) place(p);
return;
}
if (preview && preview.x === p.x && preview.y === p.y) place(p);
else setPreview(p);
};
const pointToCell = (e: React.PointerEvent<SVGSVGElement>): Pt | null => {
const svg = svgRef.current;
if (!svg) return null;
const pt = svg.createSVGPoint();
pt.x = e.clientX;
pt.y = e.clientY;
const loc = pt.matrixTransform(svg.getScreenCTM()!.inverse());
const x = Math.round(loc.x);
const y = Math.round(loc.y);
return x >= 0 && y >= 0 && x < n && y < n ? { x, y } : null;
};
const onKey = (e: React.KeyboardEvent) => {
setKbd(true);
const d: Record<string, [number, number]> = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] };
if (d[e.key]) {
e.preventDefault();
setCursor((c) => ({ x: Math.min(n - 1, Math.max(0, c.x + d[e.key]![0])), y: Math.min(n - 1, Math.max(0, c.y + d[e.key]![1])) }));
} else if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
// Same as a click: places, or explains a forbidden point.
tap(cursor, true);
}
};
const ghost = preview ?? (hover && canPlace(hover) ? hover : null);
const pad = 0.9;
return (
// Touch with tap-confirm shows [두기][취소] under the board: keep room for it.
<div className="board-wrap stack om-fit" style={{ gap: 10, '--om-rows': tapConfirm ? 'calc(var(--btn-h) + 10px)' : '0px' } as React.CSSProperties}>
<style>{BOARD_MOTION_CSS + OMOK_CSS}</style>
<svg
ref={svgRef}
viewBox={`${-pad} ${-pad} ${n - 1 + 2 * pad} ${n - 1 + 2 * pad}`}
style={{ width: '100%', display: 'block', background: 'var(--wood)', borderRadius: 12, touchAction: 'manipulation', cursor: myTurn ? 'pointer' : 'default', boxShadow: 'var(--shadow)' }}
onPointerMove={(e) => {
if (e.pointerType !== 'mouse' || !myTurn) return;
const p = pointToCell(e);
if (p?.x !== hover?.x || p?.y !== hover?.y) setHover(p);
}}
onPointerLeave={() => setHover(null)}
onPointerUp={(e) => {
setKbd(false);
const p = pointToCell(e);
if (p) tap(p, e.pointerType === 'mouse');
}}
tabIndex={0}
role="grid"
aria-label={`오목판 ${n}줄. 방향키로 움직이고 Enter로 둡니다. 현재 위치 ${label(n, cursor)}`}
onKeyDown={onKey}
>
{Array.from({ length: n }, (_, i) => (
<g key={i} stroke="var(--wood-line)" strokeWidth={i === 0 || i === n - 1 ? 0.06 : 0.035}>
<line x1={0} y1={i} x2={n - 1} y2={i} />
<line x1={i} y1={0} x2={i} y2={n - 1} />
</g>
))}
{Array.from({ length: n }, (_, i) => (
<g key={`c${i}`} fill="var(--wood-line)" fontSize={0.38} fontWeight={700} textAnchor="middle" dominantBaseline="central" opacity={0.75}>
<text x={i} y={-0.6}>
{COLS[i]}
</text>
<text x={-0.62} y={i}>
{n - i}
</text>
</g>
))}
{stars(n).map((p) => (
<circle key={`s${p.x}-${p.y}`} cx={p.x} cy={p.y} r={0.11} fill="var(--wood-line)" />
))}
{myTurn && v.hints?.urgent.map((p) => <circle key={`u${p.x}-${p.y}`} cx={p.x} cy={p.y} r={0.42} fill="none" stroke="#dc2626" strokeWidth={0.09} />)}
{myTurn && v.hints?.warn.map((p) => <circle key={`w${p.x}-${p.y}`} cx={p.x} cy={p.y} r={0.38} fill="none" stroke="#f59e0b" strokeWidth={0.07} strokeDasharray="0.15 0.1" />)}
{v.forbidden.map((p) => (
<g key={`f${p.x}-${p.y}`} stroke="#b91c1c" strokeWidth={0.08} opacity={0.8}>
<line x1={p.x - 0.2} y1={p.y - 0.2} x2={p.x + 0.2} y2={p.y + 0.2} />
<line x1={p.x + 0.2} y1={p.y - 0.2} x2={p.x - 0.2} y2={p.y + 0.2} />
</g>
))}
{v.board.map((c, i) => {
if (c === 0) return null;
// The newest stone is keyed by the move number so it sets down again even on a re-used point (after 무르기).
const isLast = fresh && v.lastMove !== null && i === v.lastMove.y * n + v.lastMove.x;
return (
<g key={isLast ? `m${v.moveCount}` : i} className={isLast ? 'bm-place' : undefined}>
<Stone color={c === 1 ? 'black' : 'white'} cx={i % n} cy={Math.floor(i / n)} r={0.46} />
</g>
);
})}
{!liftedOver &&
lifted.map(({ i, color }) => (
<g key={`u${v.moveCount}-${i}`} className="bm-gone" onAnimationEnd={liftedEnd}>
<Stone color={color} cx={i % n} cy={Math.floor(i / n)} r={0.46} />
</g>
))}
{v.lastMove && (
<g key={`lm${v.moveCount}`}>
{fresh && <circle className="bm-ring" cx={v.lastMove.x} cy={v.lastMove.y} r={0.5} fill="none" stroke="#ef4444" strokeWidth={0.06} />}
<circle className={fresh ? 'anim-pop' : undefined} style={{ animationDelay: '0.2s', transformBox: 'fill-box', transformOrigin: 'center' }} cx={v.lastMove.x} cy={v.lastMove.y} r={0.13} fill="#ef4444" />
</g>
)}
{(v.winLine ?? []).map((p, k) => (
<circle
key={`win${p.x}-${p.y}`}
className="bm-win"
style={{ '--bm-delay': `${k * 110}ms` } as React.CSSProperties}
cx={p.x}
cy={p.y}
r={0.5}
fill="none"
stroke="#22c55e"
strokeWidth={0.1}
/>
))}
{ghost && v.myColor && <Stone color={v.myColor} cx={ghost.x} cy={ghost.y} r={0.46} ghost />}
{refused && (
<g key={refused.key} aria-hidden>
<g className="om-forbid">
<circle cx={refused.p.x} cy={refused.p.y} r={0.46} fill="rgba(220,38,38,.25)" stroke="#dc2626" strokeWidth={0.08} />
<path d={`M ${refused.p.x - 0.22} ${refused.p.y - 0.22} L ${refused.p.x + 0.22} ${refused.p.y + 0.22} M ${refused.p.x + 0.22} ${refused.p.y - 0.22} L ${refused.p.x - 0.22} ${refused.p.y + 0.22}`} stroke="#dc2626" strokeWidth={0.12} strokeLinecap="round" />
</g>
<text
className="om-forbid-t"
x={refused.p.x}
y={refused.p.y - 0.75}
textAnchor="middle"
fontSize={0.48}
fontWeight={900}
fill="#dc2626"
stroke="#fff"
strokeWidth={0.06}
paintOrder="stroke"
>
금수
</text>
</g>
)}
{myTurn && kbd && <rect x={cursor.x - 0.5} y={cursor.y - 0.5} width={1} height={1} fill="none" stroke="var(--focus)" strokeWidth={0.06} />}
</svg>
<p className="sr-only" aria-live="polite">
{announce}
</p>
{banner}
{preview && myTurn && (
<div className="row" style={{ justifyContent: 'center' }}>
<button className="btn btn-primary" style={{ flex: 2 }} disabled={!canPlace(preview)} onClick={() => place(preview)}>
{`${label(n, preview)}에 두기`}
</button>
<button className="btn btn-secondary" style={{ flex: 1 }} onClick={() => setPreview(null)}>
취소
</button>
</div>
)}
</div>
);
}
/** Right-hand panel on PC (below the board on narrow screens). */
function OmokSide({ view: v, me, players, act, pending, deadline }: BoardProps<OmokView>) {
const [confirm, setConfirm] = useState<null | 'resign' | 'draw'>(null);
const incoming =
v.phase === 'playing' && v.myColor
? v.undo.pending && v.undo.pending.by !== v.myColor
? 'undo'
: v.draw.pendingBy && v.draw.pendingBy !== v.myColor
? 'draw'
: null
: null;
const outgoing = v.myColor && (v.undo.pending?.by === v.myColor || v.draw.pendingBy === v.myColor);
const undoLeft = v.myColor ? v.undo.remaining[v.myColor] : null;
const opp: Color = v.myColor === 'white' ? 'black' : 'white';
const mine: Color = v.myColor ?? 'black';
return (
<>
<PlayerBar view={v} color={v.myColor ? opp : 'black'} players={players} deadline={deadline} me={me} />
<PlayerBar view={v} color={v.myColor ? mine : 'white'} players={players} deadline={deadline} me={me} />
{v.phase === 'playing' && v.myColor && (
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr 1fr', gap: 8 }}>
{v.undo.allowed && (
<button
className="btn btn-secondary btn-sm"
disabled={!!outgoing || undoLeft === 0 || !v.moves.some((m) => m.color === v.myColor)}
onClick={() => act({ type: 'requestUndo' })}
title={undoLeft !== null ? `${undoLeft}번 남음` : '무제한'}
>
↩ 무르기{undoLeft !== null ? ` ${undoLeft}` : ''}
</button>
)}
{v.draw.allowed && (
<button className="btn btn-secondary btn-sm" disabled={!!outgoing} onClick={() => setConfirm('draw')}>
🤝 무승부
</button>
)}
<button className="btn btn-danger btn-sm" onClick={() => setConfirm('resign')}>
🏳 기권
</button>
</div>
)}
{outgoing && (
<div className="card row" style={{ justifyContent: 'space-between', padding: 10 }}>
<span className="small">상대의 답을 기다리는 중…</span>
<button className="btn btn-ghost btn-sm" onClick={() => act({ type: 'cancelOffer' })}>
요청 취소
</button>
</div>
)}
<MoveList view={v} />
{incoming && (
<Sheet onClose={() => {}} label="상대의 요청">
<h2>{incoming === 'undo' ? `상대가 ${v.undo.pending!.plies}수 무르기를 요청했어요` : '상대가 무승부를 제안했어요'}</h2>
<p className="muted">{incoming === 'undo' ? '수락하면 돌이 되돌아가요.' : '수락하면 이 판은 무승부로 끝나요.'}</p>
<div className="row">
<button className="btn btn-primary" style={{ flex: 1 }} disabled={pending} onClick={() => act(incoming === 'undo' ? { type: 'respondUndo', accept: true } : { type: 'respondDraw', accept: true })}>
수락
</button>
<button className="btn btn-secondary" style={{ flex: 1 }} disabled={pending} onClick={() => act(incoming === 'undo' ? { type: 'respondUndo', accept: false } : { type: 'respondDraw', accept: false })}>
거절
</button>
</div>
</Sheet>
)}
{confirm === 'resign' && (
<ConfirmSheet title="기권할까요?" body="기권하면 이 판은 상대가 이겨요." confirm="기권하기" danger onConfirm={() => act({ type: 'resign' })} onClose={() => setConfirm(null)} />
)}
{confirm === 'draw' && <ConfirmSheet title="무승부를 제안할까요?" confirm="제안하기" onConfirm={() => act({ type: 'offerDraw' })} onClose={() => setConfirm(null)} />}
</>
);
}
function MoveList({ view: v }: { view: OmokView }) {
const ref = useRef<HTMLOListElement>(null);
useEffect(() => {
ref.current?.scrollTo({ top: ref.current.scrollHeight });
}, [v.moveCount]);
if (v.moveCount === 0) return null;
return (
<details className="card wide-open" open style={{ padding: 12 }}>
<summary style={{ cursor: 'pointer', fontWeight: 700 }}>기보 ({v.moveCount}수)</summary>
<ol ref={ref} style={{ margin: '8px 0 0', paddingLeft: 0, listStyle: 'none', maxHeight: 132, overflow: 'auto', display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: '2px 8px', fontVariantNumeric: 'tabular-nums' }}>
{v.moves.map((m, i) => (
<li key={i} className="small">
<span className="muted">{i + 1}.</span> {m.color === 'black' ? '●' : '○'} {label(v.size, m.pt)}
{m.auto ? '*' : ''}
</li>
))}
</ol>
</details>
);
}
function PlayerBar({ view: v, color, players, deadline, me }: { view: OmokView; color: Color; players: Record<string, PublicUser>; deadline: number | null; me: string | null }) {
const id = v.players[color];
const u = players[id] ?? { id, nickname: '알 수 없음', avatar: null, kind: 'guest' as const };
const active = v.phase === 'playing' && v.turn === color;
const now = useServerNow(500);
const tc = v.timeControl;
let clock: React.ReactNode = null;
if (tc.kind === 'fischer') {
const ms = Math.max(0, v.remainingMs[color] - (active ? now - v.turnStartedAt : 0));
const s = Math.ceil(ms / 1000);
clock = (
<span style={{ fontVariantNumeric: 'tabular-nums', fontWeight: 700, fontSize: '1.15rem', color: active && s <= 10 ? 'var(--danger)' : undefined }}>
{Math.floor(s / 60)}:{String(s % 60).padStart(2, '0')}
</span>
);
} else if (active && tc.kind === 'perMove') {
clock = <TurnTimer deadline={deadline} totalMs={tc.perMoveSec * 1000} mine={id === me} />;
}
return (
<div
className="card row"
style={{ padding: '8px 12px', gap: 10, border: `3px solid ${active ? 'var(--primary)' : 'transparent'}`, transition: 'border-color 0.2s' }}
>
<svg width={28} height={28} viewBox="-1 -1 2 2" aria-hidden>
<Stone color={color} cx={0} cy={0} r={0.9} />
</svg>
<Avatar user={u} size={32} />
<div style={{ flex: 1, minWidth: 0 }}>
<div style={{ fontWeight: 700, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>
{u.nickname}
{id === me && <span className="muted small"> (나)</span>}
</div>
<div className="muted small">{COLOR_KO[color]}{active ? ' · 두는 중' : ''}</div>
</div>
{clock}
</div>
);
}
function statusText(v: OmokView, me: string | null, players: Record<string, PublicUser>): string {
if (v.phase === 'finished') return v.result?.summary ?? '게임이 끝났어요';
if (v.myColor && v.undo.pending && v.undo.pending.by !== v.myColor) return '상대가 무르기를 요청했어요';
if (v.myColor && v.draw.pendingBy && v.draw.pendingBy !== v.myColor) return '상대가 무승부를 제안했어요';
if (v.myColor === v.turn) {
if (v.moveCount === 0 && v.firstMoveCenter) return '내 차례예요. 첫 수는 가운데에 둬요';
return `내 차례예요 (${COLOR_KO[v.turn]}). 놓을 곳을 눌러 주세요`;
}
const nick = players[v.players[v.turn]]?.nickname ?? COLOR_KO[v.turn];
return v.myColor ? `${nick}님이 생각 중이에요…` : `${nick}님(${COLOR_KO[v.turn]}) 차례예요`;
}
// ---------------------------------------------------------------- options
function Seg<T extends string | number>({ value, options, onChange }: { value: T; options: [T, string][]; onChange: (v: T) => void }) {
return (
<div className="segmented">
{options.map(([k, l]) => (
<button key={String(k)} type="button" aria-pressed={value === k} onClick={() => onChange(k)}>
{l}
</button>
))}
</div>
);
}
const TIME_PRESETS: [string, string, Partial<OmokOptions['timeControl']>][] = [
['pm10', '한 수 10초', { kind: 'perMove', perMoveSec: 10 }],
['pm20', '한 수 20초', { kind: 'perMove', perMoveSec: 20 }],
['pm30', '한 수 30초', { kind: 'perMove', perMoveSec: 30 }],
['pm60', '한 수 60초', { kind: 'perMove', perMoveSec: 60 }],
['f3', '3분 + 2초', { kind: 'fischer', baseSec: 180, incSec: 2 }],
['f5', '5분 + 3초', { kind: 'fischer', baseSec: 300, incSec: 3 }],
['f10', '10분 + 5초', { kind: 'fischer', baseSec: 600, incSec: 5 }],
['none', '시간 제한 없음', { kind: 'none' }],
];
function timeKey(tc: OmokOptions['timeControl']): string {
if (tc.kind === 'none') return 'none';
if (tc.kind === 'perMove') return `pm${tc.perMoveSec}`;
return `f${Math.round(tc.baseSec / 60)}`;
}
function Options({ value: o, onChange }: OptionsProps<OmokOptions>) {
const [more, setMore] = useState(false);
const set = (patch: Partial<OmokOptions>) => onChange({ ...o, ...patch });
return (
<div className="stack" style={{ gap: 16 }}>
<div className="stack" style={{ gap: 6 }}>
<strong>규칙</strong>
<Seg value={o.ruleSet} options={[['korean', '한국식(쌍삼 금지)'], ['renju', '렌주룰'], ['free', '자유룰']]} onChange={(ruleSet) => set({ ruleSet })} />
</div>
<div className="stack" style={{ gap: 6 }}>
<strong>시간</strong>
<Seg
value={timeKey(o.timeControl)}
options={[
...TIME_PRESETS.map(([k, l]) => [k, l] as [string, string]),
...(TIME_PRESETS.some((p) => p[0] === timeKey(o.timeControl)) ? [] : [[timeKey(o.timeControl), `${timeLabel(o.timeControl)}(관리자 설정)`] as [string, string]]),
]}
onChange={(k) => {
const preset = TIME_PRESETS.find((p) => p[0] === k);
if (preset) set({ timeControl: { ...o.timeControl, ...preset[2] } });
}}
/>
</div>
<div className="stack" style={{ gap: 6 }}>
<strong>흑(먼저 두는 쪽)</strong>
<Seg
value={o.colorAssignment}
options={[['random', '무작위'], ['hostBlack', '방장이 흑'], ['hostWhite', '방장이 백'], ['alternate', '판마다 번갈아']]}
onChange={(colorAssignment) => set({ colorAssignment })}
/>
</div>
<button type="button" className="btn btn-ghost" onClick={() => setMore(!more)} aria-expanded={more}>
{more ? '▲ 자세한 설정 닫기' : '▼ 자세한 설정'}
</button>
{more && (
<>
<div className="stack" style={{ gap: 6 }}>
<strong>판 크기</strong>
<Seg value={o.boardSize} options={[[15, '15줄'], [19, '19줄']]} onChange={(boardSize) => set({ boardSize })} />
</div>
{o.ruleSet === 'free' && (
<div className="stack" style={{ gap: 6 }}>
<strong>자유룰 승리</strong>
<Seg value={o.freeWin} options={[['fivePlus', '5목 이상'], ['exactFive', '정확히 5목']]} onChange={(freeWin) => set({ freeWin })} />
</div>
)}
<div className="stack" style={{ gap: 6 }}>
<strong>무르기</strong>
<Seg value={o.undo} options={[['off', '안 씀'], ['1', '1번'], ['3', '3번'], ['unlimited', '무제한']]} onChange={(undo) => set({ undo })} />
</div>
{o.timeControl.kind === 'perMove' && (
<div className="stack" style={{ gap: 6 }}>
<strong>시간이 다 되면</strong>
<Seg value={o.timeoutPolicy} options={[['autoMove', '자동으로 두기'], ['lose', '시간패']]} onChange={(timeoutPolicy) => set({ timeoutPolicy })} />
</div>
)}
<div className="stack" style={{ gap: 6 }}>
<strong>첫 수 가운데</strong>
<Seg value={o.firstMoveCenter} options={[['auto', '렌주만'], ['on', '항상'], ['off', '안 함']]} onChange={(firstMoveCenter) => set({ firstMoveCenter })} />
</div>
<div className="stack" style={{ gap: 6 }}>
<strong>도움 표시</strong>
<Seg value={o.showForbidden ? 'on' : 'off'} options={[['on', '금수 자리 표시'], ['off', '금수 표시 안 함']]} onChange={(k) => set({ showForbidden: k === 'on' })} />
<Seg value={o.drawOffer ? 'on' : 'off'} options={[['on', '무승부 제안 허용'], ['off', '무승부 제안 안 함']]} onChange={(k) => set({ drawOffer: k === 'on' })} />
</div>
</>
)}
</div>
);
}
function timeLabel(tc: OmokOptions['timeControl']): string {
if (tc.kind === 'none') return '시간 제한 없음';
if (tc.kind === 'perMove') return `한 수 ${tc.perMoveSec}초`;
const m = Math.floor(tc.baseSec / 60);
const sec = tc.baseSec % 60;
return `${m}분${sec ? ` ${sec}초` : ''} + ${tc.incSec}초`;
}
function optionsSummary(o: OmokOptions): string {
const t = timeLabel(o.timeControl);
return [RULE_KO[o.ruleSet], `${o.boardSize}줄`, t].join(' · ');
}
function Rules({ options }: { options?: OmokOptions }) {
const rule = options?.ruleSet ?? 'korean';
return (
<div className="stack">
<p>흑과 백이 번갈아 한 수씩 판의 빈 교차점에 돌을 놓아요. 가로, 세로, 대각선 중 한 줄로 내 돌 <strong>5개</strong>를 먼저 이으면 이겨요. 흑이 먼저 둬요.</p>
{rule === 'korean' && (
<p>
<strong>한국식</strong>: 흑과 백 모두 <strong>쌍삼(열린 3을 두 개 동시에 만드는 수)</strong>을 둘 수 없어요. 6개 이상 이어지면 이긴 것이 아니에요.
</p>
)}
{rule === 'renju' && (
<p>
<strong>렌주룰</strong>: 흑만 쌍삼, 사사(4를 두 개 동시에), 6목 이상을 둘 수 없어요. 백은 제한이 없고 6목 이상도 이겨요. 첫 수는 가운데에 둬요.
</p>
)}
{rule === 'free' && (
<p>
<strong>자유룰</strong>: 금지된 수가 없어요. 설정에 따라 5개 이상이면 이기거나, 딱 5개일 때만 이겨요.
</p>
)}
<p>둘 수 없는 자리는 판에 빨간 ×로 표시돼요(설정에서 끌 수 있어요).{options?.threatHints ? ' 이 방은 상대가 곧 이길 수 있는 자리를 빨간 동그라미, 위험한 자리를 노란 점선으로 알려 줘요.' : ''}</p>
<p>휴대폰에서는 한 번 누르면 미리 보기 돌이 놓이고, 같은 곳을 한 번 더 누르거나 아래 [두기] 버튼을 누르면 확정돼요.</p>
</div>
);
}
const ui: GameUI<OmokView, OmokOptions> = {
Board: OmokBoard,
Side: OmokSide,
statusText,
isMyTurn: (v, me) => v.phase === 'playing' && !!v.myColor && v.players[v.turn] === me,
Options,
optionsSummary,
Rules,
quickStart,
helpNow,
};
export default ui;