feat(P5B.T4): TicTacToeBoard + ConnectFourBoard render-only components
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import {
|
||||
C4_COLS,
|
||||
C4_ROWS,
|
||||
c4DropRow,
|
||||
c4WinningCells,
|
||||
type Cell,
|
||||
} from '@chat-app/shared/chat';
|
||||
|
||||
interface Props {
|
||||
board: Cell[];
|
||||
myPlayerIdx: 0 | 1 | null;
|
||||
disabled: boolean;
|
||||
onMove: (column: number) => void;
|
||||
}
|
||||
|
||||
export function ConnectFourBoard({ board, myPlayerIdx, disabled, onMove }: Props) {
|
||||
const winCells = c4WinningCells(board);
|
||||
const winSet = new Set<number>(winCells ?? []);
|
||||
|
||||
return (
|
||||
<div
|
||||
className="mx-auto w-full max-w-2xl rounded-2xl bg-sky-900/40 p-3"
|
||||
role="grid"
|
||||
aria-label="Vier-Gewinnt"
|
||||
>
|
||||
<div
|
||||
className="grid gap-1.5"
|
||||
style={{ gridTemplateColumns: 'repeat(' + C4_COLS + ', minmax(0, 1fr))' }}
|
||||
>
|
||||
{Array.from({ length: C4_ROWS * C4_COLS }, (_, idx) => {
|
||||
const cell = board[idx];
|
||||
const col = idx % C4_COLS;
|
||||
const dropTo = c4DropRow(board, col);
|
||||
const canClickColumn = !disabled && dropTo >= 0 && myPlayerIdx !== null;
|
||||
const inWin = winSet.has(idx);
|
||||
return (
|
||||
<button
|
||||
key={idx}
|
||||
type="button"
|
||||
onClick={() => canClickColumn && onMove(col)}
|
||||
disabled={!canClickColumn}
|
||||
aria-label={'Spalte ' + (col + 1) + (cell !== null ? ' belegt' : '')}
|
||||
className={
|
||||
'flex aspect-square items-center justify-center rounded-full border-2 transition ' +
|
||||
(inWin
|
||||
? 'border-emerald-300 bg-emerald-400 shadow-[0_0_12px_rgba(110,231,183,0.7)]'
|
||||
: cell === 0
|
||||
? 'border-rose-300 bg-rose-500'
|
||||
: cell === 1
|
||||
? 'border-amber-300 bg-amber-400'
|
||||
: canClickColumn
|
||||
? 'cursor-pointer border-sky-700 bg-sky-950 hover:bg-sky-900'
|
||||
: 'cursor-not-allowed border-sky-800 bg-sky-950 opacity-80')
|
||||
}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import { type Cell, tttWinningLine } from '@chat-app/shared/chat';
|
||||
|
||||
interface Props {
|
||||
board: Cell[];
|
||||
myPlayerIdx: 0 | 1 | null;
|
||||
disabled: boolean;
|
||||
onMove: (cell: number) => void;
|
||||
}
|
||||
|
||||
const MARK = ['×', '○'] as const;
|
||||
|
||||
export function TicTacToeBoard({ board, myPlayerIdx, disabled, onMove }: Props) {
|
||||
const winLine = tttWinningLine(board);
|
||||
const winSet = new Set<number>(winLine ?? []);
|
||||
|
||||
return (
|
||||
<div
|
||||
className="mx-auto grid w-full max-w-md grid-cols-3 gap-2 p-4"
|
||||
style={{ aspectRatio: '1 / 1' }}
|
||||
role="grid"
|
||||
aria-label="Tic-Tac-Toe"
|
||||
>
|
||||
{board.map((cell, i) => {
|
||||
const filled = cell !== null;
|
||||
const inWin = winSet.has(i);
|
||||
const canClick = !disabled && !filled && myPlayerIdx !== null;
|
||||
return (
|
||||
<button
|
||||
key={i}
|
||||
type="button"
|
||||
onClick={() => canClick && onMove(i)}
|
||||
disabled={!canClick}
|
||||
aria-label={'Feld ' + (i + 1) + (filled ? ' belegt' : ' frei')}
|
||||
className={
|
||||
'flex aspect-square items-center justify-center rounded-xl border-2 text-5xl font-bold transition ' +
|
||||
(inWin
|
||||
? 'border-emerald-400 bg-emerald-400/20 text-emerald-200'
|
||||
: filled
|
||||
? cell === 0
|
||||
? 'border-rose-500/60 bg-rose-500/10 text-rose-300'
|
||||
: 'border-sky-500/60 bg-sky-500/10 text-sky-300'
|
||||
: canClick
|
||||
? 'cursor-pointer border-line bg-surface-2 text-fg-muted hover:bg-surface-3 hover:text-fg'
|
||||
: 'cursor-not-allowed border-line bg-surface-2 text-fg-muted opacity-60')
|
||||
}
|
||||
>
|
||||
{filled ? MARK[cell as 0 | 1] : ''}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user