Examples
Practical examples. Live demos.
Pick an example to open it full screen, with the live preview and its source side by side.
Pick an example to open it full screen, with the live preview and its source side by side.
Play the computer. when() adds each mark, and a CSS animation on its dash draws it in.
import 'nuclo' type Mark = 'X' | 'O' | null const LINES = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6], ] let board: Mark[] = Array(9).fill(null) const score = { you: 0, cpu: 0, draw: 0 } const winner = () => LINES.find(([a, b, c]) => board[a] && board[a] === board[b] && board[a] === board[c]) const full = () => board.every(Boolean) const over = () => !!winner() || full() const center = (i: number) => [(i % 3) * 70 + 35, Math.floor(i / 3) * 70 + 35] // Win if possible, else block, else the center, else any free cell. function computer() { const free = board.flatMap((m, i) => (m ? [] : [i])) const wins = (who: Mark) => free.find((i) => LINES.some((l) => l.includes(i) && l.every((j) => j === i || board[j] === who)), ) return wins('O') ?? wins('X') ?? (board[4] ? free[Math.floor(Math.random() * free.length)] : 4) } function play(i: number) { if (board[i] || over()) return board[i] = 'X' if (!over()) board[computer()] = 'O' const w = winner() if (w) score[board[w[0]] === 'X' ? 'you' : 'cpu']++ else if (full()) score.draw++ update() } // The line through the three winning cells, a little longer at both ends. function winLine() { const w = winner() if (!w) return '' const [x1, y1] = center(w[0]) const [x2, y2] = center(w[2]) const dx = Math.sign(x2 - x1) * 24 const dy = Math.sign(y2 - y1) * 24 return `M${x1 - dx},${y1 - dy} L${x2 + dx},${y2 + dy}` } const status = () => { const w = winner() if (w) return board[w[0]] === 'X' ? 'You win!' : 'The computer wins.' return full() ? 'A draw.' : 'Your move. You are X.' } // pathLength '1' makes every stroke 1 unit long, so a dash of 1 // is the whole stroke. Animating its offset from 1 to 0 draws it, // and a mark that when() adds starts drawing as soon as it is in. const draw = keyframes({ from: { raw: { 'stroke-dashoffset': 1 } }, to: { raw: { 'stroke-dashoffset': 0 } }, }) const drawn = css({ pointerEvents: 'none', animation: `${draw} 0.35s ease-out both` }) const drawnLate = css({ pointerEvents: 'none', animation: `${draw} 0.35s 0.3s ease-out both` }) const drawnLast = css({ pointerEvents: 'none', animation: `${draw} 0.35s 0.6s ease-out both` }) const gridDraw = css({ animation: `${draw} 0.6s ease-out both` }) const cell = css({ cursor: 'pointer', outline: 'none', raw: { fill: 'transparent' }, '&:hover': { raw: { fill: 'rgba(255, 63, 0, 0.08)' } }, '&:focus-visible': { raw: { fill: 'rgba(255, 63, 0, 0.14)' } }, }) const ink = { fill: 'none', 'stroke-width': 7, 'stroke-linecap': 'round', pathLength: '1', 'stroke-dasharray': '1', } as const export const TicTacToe = () => div( { style: { display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '18px' } }, svgSvg( { viewBox: '-5 -5 220 220', width: '220', height: '220', role: 'group', 'aria-label': 'Tic-tac-toe board', }, pathSvg(gridDraw, { d: 'M70,4 V206 M140,4 V206 M4,70 H206 M4,140 H206', fill: 'none', stroke: '#ddd', 'stroke-width': 3, 'stroke-linecap': 'round', pathLength: '1', 'stroke-dasharray': '1', }), ...Array.from({ length: 9 }, (_, i) => { const [x, y] = center(i) return gSvg( when(() => board[i] === 'X', pathSvg(drawn, ink, { stroke: '#ff3f00', d: `M${x - 17},${y - 17} L${x + 17},${y + 17} M${x + 17},${y - 17} L${x - 17},${y + 17}`, }), ).when(() => board[i] === 'O', circleSvg(drawnLate, ink, { stroke: '#2563eb', cx: x, cy: y, r: 19 }), ), rectSvg(cell, { x: x - 33, y: y - 33, width: 66, height: 66, rx: 8, tabindex: '0', role: 'button', 'aria-label': () => `Row ${Math.floor(i / 3) + 1}, column ${(i % 3) + 1}: ${board[i] ?? 'empty'}`, onClick: () => play(i), onKeyDown: (e) => { if (e.key !== 'Enter' && e.key !== ' ') return e.preventDefault() play(i) }, }), ) }), // the winning line draws in last when(() => !!winner(), pathSvg(drawnLast, ink, { stroke: '#222', 'stroke-width': 5, d: winLine }), ), ), div({ 'aria-live': 'polite', style: { fontWeight: '600' } }, status), div( { style: { fontSize: '0.75rem', color: '#8b949e' } }, () => `you ${score.you} · draws ${score.draw} · computer ${score.cpu}`, ), button('New game', { onClick: () => { board = Array(9).fill(null); update() }, }), )