create tic tac toe game in react using jsx files

function Square(props) {
  return (
    <button className="square" onClick={props.onClick}>
      {props.value}
    </button>
  );
}

class Board extends React.Component {
  renderSquare(i) {
    return (
      <Square
        value={this.props.squares[i]}
        onClick={() => this.props.onClick(i)}
      />
    );
  }

  render() {
    return (
      <div>
        <div className="board-row">
          {this.renderSquare(0)}
          {this.renderSquare(1)}
          {this.renderSquare(2)}
        </div>
        <div className="board-row">
          {this.renderSquare(3)}
          {this.renderSquare(4)}
          {this.renderSquare(5)}
        </div>
        <div className="board-row">
          {this.renderSquare(6)}
          {this.renderSquare(7)}
          {this.renderSquare(8)}
        </div>
      </div>
    );
  }
}

class Game extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      history: [
        {
          squares: Array(9).fill(null)
        }
      ],
      stepNumber: 0,
      xIsNext: true
    };
  }

  handleClick(i) {
    const history = this.state.history.slice(0, this.state.stepNumber + 1);
    const current = history[history.length - 1];
    const squares = current.squares.slice();
    if (calculateWinner(squares) || squares[i]) {
      return;
    }
    squares[i] = this.state.xIsNext ? "X" : "O";
    this.setState({
      history: history.concat([
        {
          squares: squares
        }
      ]),
      stepNumber: history.length,
      xIsNext: !this.state.xIsNext
    });
  }

  jumpTo(step) {
    this.setState({
      stepNumber: step,
      xIsNext: (step % 2) === 0
    });
  }

  render() {
    const history = this.state.history;
    const current = history[this.state.stepNumber];
    const winner = calculateWinner(current.squares);

    const moves = history.map((step, move) => {
      const desc = move ?
        'Go to move #' + move :
        'Go to game start';
      return (
        <li key={move}>
          <button onClick={() => this.jumpTo(move)}>{desc}</button>
        </li>
      );
    });

    let status;
    if (winner) {
      status = "Winner: " + winner;
    } else {
      status = "Next player: " + (this.state.xIsNext ? "X" : "O");
    }

    return (
      <div className="game">
        <div className="game-board">
          <Board
            squares={current.squares}
            onClick={i => this.handleClick(i)}
          />
        </div>
        <div className="game-info">
          <div>{status}</div>
          <ol>{moves}</ol>
        </div>
      </div>
    );
  }
}

// ========================================

ReactDOM.render(<Game />, document.getElementById("root"));

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;
}

3.8
10
Louel 110 points

                                        useEffect(()=&gt;{
        //checking winner row and col
        for (let i = 0; i &lt;= 2; i++){
            const idx = (i % 3) * 3 // -&gt; 0,3,6
            //check row
            if ( (table[idx] + table[idx+1] + table[idx+2] )=== 9 || (table[idx] + table[idx+1] + table[idx+2] ) === 15){
                setWinner([idx,idx+1,idx+2])
                gameOver()
            }
            //check col
            if ((table[i] + table[i+3] + table[i+6] )=== 9 || (table[i] + table[i+3] + table[i+6] ) === 15){
                setWinner([i,i+3,i+6])
                gameOver()
            }
        }
        //checking winner diagonal
        if ((table[0] + table[4] + table[8] ) === 15 || (table[0] + table[4] + table[8] ) === 9 ){
            setWinner([0, 4, 8])
            gameOver()
        }
        if ((table[2] + table[4] + table[6] ) === 9 || (table[2] + table[4] + table[6] ) ===15){
            setWinner([2, 4, 6])
            gameOver()
        }
        // check if table completed
        if (table.indexOf(0) === -1){
            gameOver()
        }
    }, [table])

3.8 (10 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
create tic tac toe game in react using jsx files tic tac toe react using different components tic tac toe react using different componentds reactjs realtime tic tac toe tic-tac-toe multiplayer game in react learn react tic tac toe tic tac toe in reat tic tac toe using reactjs explained tic tac toe sample design react tict tac toe raeact tic tac toe sample with react tic tak toe game react tic-tac-toe game react react tik-toe game React tic tac toe coderbyte react tic tac toe coderbyte solution making tic tac toe in react tic tac toe game using react js2021 tic tac toe game using react js React Tic Tac Toe using react dom learning react tic tac toe something about react tic tac toe tic tac toe react native tic tak toe react building tic tac toe using reactjs react docs tic tac toe tic tac toe game in react js from scratch tic tac toe react js simple tic tac toe game in react js building from scratch tic tac toe react js app React.js Tic Tac Toe Game tic tac toe class react js create tic tac toe game react create tic tac toe game reacty reactjs tic tac toe game Multiplayer tic tac toe React tic tac toe react code tic tac toe game using react tic tac toe react .js simple tic tac toe react.js tic tac toe game with react node how to build simple tic tac toe game with react Tic tac toe program in React js tic tac toe game implementation in react tic tac toe react.js multiplayer tic tac toe with react creating tic tac toe in react how to make tic tac toe in react tic toe game using react js react js tic tac toe tic tac toe game node js react tic tac toe game nodejs react tic tac toe react project how to build tic tac toe in react tic tac toe react ru tic tac toe react rus how to make a tic tac toe game react js tic tac toe game react build tic tac toe react tic tact toe react app tictactoe react react app tic tac toe react tictactoe react tic-tac-toe tutorial react tic tac toe example tic tac toe reactjs tutorial tic tac toe doesnt show react tutorial react tictactoe api tic tac toe game in react class components tic tac toe game in react tictactoe game in react tictac toe react tic tac toe jsx tic tok toe game using react make a tic tac toe game in react functional react demo not showing tick tac toe board how to use 's in react tic-tac-toe react react tic tac toe app tic toe exammple react js description method react react for dummies react get started react js cool tutorial react js tutorial beginner &lt;i&gt; in react Pick your starting point in react.js learn reacy JS how to write react react basics react tutorials react tutorial for beginners react js tutorials how can we understand react documentation explainer walk though react js learn react from scratch react learn react js for dummies learn reactjs react frontend js operations react getting started react js foe beginners original in react react full tutorial reactjs to react javascript simple way of understating react intro react beginning setting up react class react app learn react tutorial react game tutorial reactjs getting started using jsx as boarding in react learn react js javascript react code filler react front end tutorial learn react react tictactoe docs tik tak toe react tick tak toe react tic tok toe in react tic tac toe react tutorial reactdoc tic tac toe tic tac teo game react tic tac toe game from react simple tic tac toe for 2 player react making tictactoe in react walk through tic tac toe n*n eact reactjs tictactoe small game code in react.js walkthrough tic tac toe react with user name choose create 'start game button' react create 'start game' button react create 'start game' btn react create 'start game btn react game start button and routing react react game start button and routing creating a tic tac toe react app react tic tak toe game tic tic toe react TIC TAC TOE REACT GAME react tic tac toe game react tic tac toe where to put game functionality tic tac toe react how to check if 3 x how to change x and o on tic tac toe react react tac toe npx create-react-app tic-tac-to tic tac toe reactjs web app tic toc in reactjs reactjs.org tic-tac-toe complete excercise how to format a tic tac toe game in reactjs tic tac toe with react simple react game tutorial React tutorial - tic-tac-toe bullies react to decorate Tic Tacs react tic tac toe time travel tic tac toe game in react js react tic tac to game how is right to make react game? react tic tac toe component react square component reactjs tic tac toe tic tac toe in react with moadl tic tac toe game code in react js tic tac toe react js nnode react tic tac toe tutorial simple toc react react create app allow user to make and squares react tutorial tic tac toe functioanl component react website tictac tutorial react create square component tic tac toe iwth react react tic tac toe tutorial react native tic tac toe how to make a tic tac toe using react tic tac toe in react tic tac toe using reactjs tic-tac-toe react js tic tac toe in react js tic tac toe in reactjs react documentation tic tac toe react tic tac toe reactjs button game react tutorial tic tac toe tic tac toe react tic tac toe game react js tic tac toe react example javascript Array tic tact toe react react tictactoe exmple tic tac toe reactjs react code for tic tak toe
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source