import React, { useState } from 'react'; const Square = ({ value, onClick }) => ( ); const Board = ({ squares, onClick }) => (
{[0, 1, 2].map(row => (
{[0, 1, 2].map(col => ( onClick(row * 3 + col)} /> ))}
))}
); const TicTacToe = () => { const [board, setBoard] = useState(Array(9).fill(null)); const [xIsNext, setXIsNext] = useState(true); const calculateWinner = (squares) => { 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], ]; for (let i = 0; i < lines.length; i++) { const [a, b, c] = lines[i]; if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) { return squares[a]; } } return null; }; const handleClick = (i) => { if (calculateWinner(board) || board[i]) return; const newBoard = [...board]; newBoard[i] = xIsNext ? 'X' : 'O'; setBoard(newBoard); setXIsNext(!xIsNext); }; const winner = calculateWinner(board); let status; if (winner) { status = `Winner: ${winner}`; } else if (board.every(square => square)) { status = 'Draw!'; } else { status = `Next player: ${xIsNext ? 'X' : 'O'}`; } const resetGame = () => { setBoard(Array(9).fill(null)); setXIsNext(true); }; return (

Tic Tac Toe

{status}
); }; export default TicTacToe;