Tik Tac Toe

ReactReactIntermediate
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this challenge you will learn how to develop a tic-tac-toe board game using react basic concepts with little JS programming logic around that.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL react(("`React`")) -.-> react/FundamentalsGroup(["`Fundamentals`"]) react(("`React`")) -.-> react/AdvancedConceptsGroup(["`Advanced Concepts`"]) react(("`React`")) -.-> react/StateManagementGroup(["`State Management`"]) react(("`React`")) -.-> react/StylingGroup(["`Styling`"]) react/FundamentalsGroup -.-> react/jsx("`JSX`") react/FundamentalsGroup -.-> react/event_handling("`Handling Events`") react/FundamentalsGroup -.-> react/conditional_render("`Conditional Rendering`") react/AdvancedConceptsGroup -.-> react/hooks("`React Hooks`") react/StateManagementGroup -.-> react/use_state_reducer("`Using useState and useReducer`") react/StylingGroup -.-> react/css_in_react("`CSS in React`") subgraph Lab Skills react/jsx -.-> lab-67594{{"`Tik Tac Toe`"}} react/event_handling -.-> lab-67594{{"`Tik Tac Toe`"}} react/conditional_render -.-> lab-67594{{"`Tik Tac Toe`"}} react/hooks -.-> lab-67594{{"`Tik Tac Toe`"}} react/use_state_reducer -.-> lab-67594{{"`Tik Tac Toe`"}} react/css_in_react -.-> lab-67594{{"`Tik Tac Toe`"}} end

Tik Tac Toe

To get started, open the editor. You can see the following files from your editor.

├── public
├── src
│   ├── components
│   │   ├── common
│   │   │   └── Utils.js
│   │   ├── Board.js
│   │   ├── Game.js
│   │   └── Square.js
│   ├── App.css
│   ├── App.js
│   ├── App.test.js
│   ├── index.css
│   ├── index.js
│   ├── logo.svg
│   ├── reportWebVitals.js
│   └── setupTests.js
├── package-lock.json
└── package.json

Requirements

  • To install the project dependencies, use the following command:

    npm i
  • Please complete this challenge in the src/components/Game.js file.

  • Use the useState hook to define three state variables: board, xTurn, and winner.

    • board represents the state of the game board. It is initialized as an array of 9 elements, where each element is initially set to null.
    • xTurn is a boolean flag indicating whether it is currently X's turn.
    • winner stores the result of the calculateWinner function, which determines the winner based on the current state of the board.
  • The handleClick function is called when a square on the game board is clicked.

    • It creates a copy of the current board state using the spread operator ([...board]) and assigns it to tmpBoard.
    • If there is already a winner (winner is truthy) or the clicked square is already marked (tmpBoard[i] is truthy), the function returns without making any changes.
    • Otherwise, it updates the value of the clicked square in tmpBoard to either "X" or "O" based on the value of xTurn.
    • The updated tmpBoard is then set as the new value for the board state using setBoard, and xTurn is toggled using setXTurn.

Example

Once you have completed the code, run it with the following command:

npm start

The finished result is as follows:

finished

Summary

Congratulations! You have completed the Tik Tac Toe You can practice more challenge in LabEx to improve your skills.

Other React Tutorials you may like