X & O Game with react: EDIT FILE: App.js
Sun Jun 23 2024 11:21:34 GMT+0000 (Coordinated Universal Time)
Saved by @Black_Shadow #react.js #+jsx
### 1. Import Statements ```javascript import { useState } from "react"; import "./App.css"; ``` - **useState**: This is a React hook that allows you to add state to functional components. - **"./App.css"**: This imports the CSS file for styling the components. ### 2. Square Component ```javascript function Square({ value, onSquareClick }) { return ( <button className="square" onClick={onSquareClick}> {value} </button> ); } ``` - **Square**: A functional component representing each square on the Tic-Tac-Toe board. - **props**: - **value**: The value ("X", "O", or null) displayed in the square. - **onSquareClick**: A function to handle click events on the square. ### 3. Board Component ```javascript function Board({ xIsNext, squares, onPlay }) { function handleClick(i) { if (calculateWinner(squares) || squares[i]) { return; } const nextSquares = squares.slice(); if (xIsNext) { nextSquares[i] = "X"; } else { nextSquares[i] = "O"; } onPlay(nextSquares); } const winner = calculateWinner(squares); let status; if (winner) { status = "Winner: " + winner; } else { status = "Next player: " + (xIsNext ? "X" : "O"); } return ( <> <div className="status">{status}</div> <div className="board-row"> <Square value={squares[0]} onSquareClick={() => handleClick(0)} /> <Square value={squares[1]} onSquareClick={() => handleClick(1)} /> <Square value={squares[2]} onSquareClick={() => handleClick(2)} /> </div> <div className="board-row"> <Square value={squares[3]} onSquareClick={() => handleClick(3)} /> <Square value={squares[4]} onSquareClick={() => handleClick(4)} /> <Square value={squares[5]} onSquareClick={() => handleClick(5)} /> </div> <div className="board-row"> <Square value={squares[6]} onSquareClick={() => handleClick(6)} /> <Square value={squares[7]} onSquareClick={() => handleClick(7)} /> <Square value={squares[8]} onSquareClick={() => handleClick(8)} /> </div> </> ); } ``` - **Board**: A functional component representing the game board. - **props**: - **xIsNext**: A boolean indicating if the next player is "X". - **squares**: An array representing the current state of the board. - **onPlay**: A function to handle updating the state of the board. - **handleClick(i)**: Handles the logic for clicking a square. - If there's already a winner or the square is filled, it returns early. - Otherwise, it updates the square with "X" or "O" based on **xIsNext** and calls **onPlay** with the new state. - **winner**: The result of **calculateWinner** function. - **status**: A string showing either the winner or the next player. ### 4. Game Component ```javascript export default function Game() { const [history, setHistory] = useState([Array(9).fill(null)]); const [currentMove, setCurrentMove] = useState(0); const xIsNext = currentMove % 2 === 0; const currentSquares = history[currentMove]; function handlePlay(nextSquares) { const nextHistory = [...history.slice(0, currentMove + 1), nextSquares]; setHistory(nextHistory); setCurrentMove(nextHistory.length - 1); } function jumpTo(nextMove) { setCurrentMove(nextMove); } const moves = history.map((squares, move) => { let description; if (move > 0) { description = "Go to move #" + move; } else { description = "Go to game start"; } return ( <li key={move}> <button onClick={() => jumpTo(move)}>{description}</button> </li> ); }); return ( <div className="game"> <div className="game-board"> <Board xIsNext={xIsNext} squares={currentSquares} onPlay={handlePlay} /> </div> <div className="game-info"> <ol>{moves}</ol> </div> </div> ); } ``` - **Game**: The main component managing the overall state and logic of the game. - **state**: - **history**: An array tracking the state of the board after each move. - **currentMove**: The index of the current move. - **xIsNext**: Determines if the next player is "X". - **currentSquares**: The state of the board at the current move. - **handlePlay(nextSquares)**: Updates the history and current move when a move is made. - **jumpTo(nextMove)**: Updates the current move, allowing the user to jump to any previous move. - **moves**: A list of buttons allowing the user to navigate to any move in the game's history. ### 5. calculateWinner Function ```javascript function 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; } ``` - **calculateWinner**: Determines if there's a winner by checking all possible winning combinations. - **squares**: The current state of the board. - **lines**: An array of arrays representing the winning combinations. - It returns "X" or "O" if there's a winner, or **null** if there's no winner yet. ### Summary This code implements a complete Tic-Tac-Toe game in React, featuring: - State management for the game board and move history. - Logic to determine the winner. - UI components for the board, squares, and game info. - Ability to navigate through the history of moves.
From my self
Comments