Create a React-based Tic Tac Toe game.

Epoch 1: Setting up the Base


<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8" />
<title>React Tic Tac Toe</title>
<div id="root"></div>


import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './App.css';

ReactDOM.render(<App />, document.getElementById('root'));


.board-row {
display: flex;

.square {
width: 60px;
height: 60px;
border: 1px solid black;
text-align: center;
font-size: 24px;
cursor: pointer;

Epoch 2: Creating the Main Application Logic


import React, { useState } from 'react';

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

function Board() {
// State for squares and which player's turn it is
const [squares, setSquares] = useState(Array(9).fill(null));
const [isXNext, setIsXNext] = useState(true);

// Handle click on squares
const handleClick = (i) => {
const squaresCopy = squares.slice();

// If square already filled or game won, return
if (squaresCopy[i] || calculateWinner(squares)) {

// Mark square and switch player
squaresCopy[i] = isXNext ? 'X' : 'O';

// Render square component
const renderSquare = (i) => {
return <Square value={squares[i]} onClick={() => handleClick(i)} />;

const winner = calculateWinner(squares);
let status;
if (winner) {
status = 'Winner: ' + winner;
} else {
status = 'Next player: ' + (isXNext ? 'X' : 'O');

return (
<div className="status">{status}</div>
<div className="board-row">
<div className="board-row">
<div className="board-row">

function App() {
return (
<div className="App">
<h1>React Tic Tac Toe</h1>
<Board />

export default App;

// Helper function to calculate game winner
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;

Epoch 3: Documenting the Code

Documentation of App.js:
Square Function Component:
Accepts properties, specifically value and an onClick function.
Returns a button with the given value, that will trigger the onClick function when clicked.
Board Function Component:
Maintains the state of the game, which includes the state of each square (squares) and which player's turn it is (isXNext).
handleClick function is called when a square is clicked, which will update the game's state accordingly.
renderSquare function returns a Square component with the appropriate properties set.
The game's status (which player's turn, or who won) is displayed above the game board.
App Function Component:
This is the top-level component, which renders the title and the Board component.
calculateWinner function:
A helper function that checks if there's a winner.
It checks all possible winning combinations and returns 'X', 'O', or null (no winner yet).
To play the game, you will click on the squares to place your move ('X' or 'O'). The game will display the current player's turn or the winner when the game concludes.
Remember, to run this, you will need a React environment. You can set up a new React project using Create React App and replace the given App.js, index.js, and index.html with the above code.

How do I run this as a react app in visual studio code

To run the given React app in Visual Studio Code (VS Code), follow these steps:

1. Setting Up the Environment:

Install Node.js and npm: If you haven't already, download and install Node.js, which comes with npm (Node Package Manager), from
Install Visual Studio Code: If you haven't installed VS Code, get it from

2. Setting Up the React App:

Open Terminal in VS Code: Open VS Code, then go to the terminal by selecting View > Terminal from the top menu, or simply press Ctrl + ~.
Create a New React App: In the terminal, type:
npx create-react-app my-tic-tac-toe
This will create a new React app named "my-tic-tac-toe".
Navigate to Your Project:
cd my-tic-tac-toe
Replace the Default App.js Content: Now, open the App.js file in the src directory of your project using the VS Code explorer. Replace its content with the React code you provided earlier.

3. Running the App:

Install Necessary CSS (Optional): If your code references any specific CSS files or other dependencies that aren't provided, make sure to add them. The code you provided references some CSS classes, so you might want to style them in the App.css file in the src directory.
Run the App: Go back to the terminal in VS Code and run:
npm start
This will start the development server. Shortly after, your default web browser should open with the Tic Tac Toe game running.

4. Additional Tips:

React Developer Tools: Consider adding the React Developer Tools extension to your browser. It's an invaluable tool for inspecting your React components and observing component state and props in real-time.
Compatible with your device
Adds React debugging tools to the Chrome Developer Tools. Created from revision 5eda682f8 on 10/3/2023.
React Developer Tools is a Chrome DevTools extension for the open-source React JavaScript library. It allows you to inspect the React component hierarchies in the Chrome Developer Tools.

You will get two new tabs in your Chrome DevTools: "⚛️ Components" and "⚛️ Profiler".

The Components tab shows you the root React components that were rendered on the page, as well as the subcomponents that they ended up rendering.

By selecting one of the components in the tree, you can inspect and edit its current props and state in the panel on the right. In the breadcrumbs you can inspect the selected component, the component that created it, the component that created that one, and so on.

If you inspect a React element on the page using the regular Elements tab, then switch over to the React tab, that element will be automatically selected in the React tree.

The Profiler tab allows you to record performance information.

This extension requires permissions to access the page's React tree, but it does not transmit any data remotely. It is fully open source, and you can find its source code at

VS Code Extensions: There are numerous extensions available for VS Code to enhance React development:
ES7 React/Redux/GraphQL/React-Native snippets: Provides you with code snippets for React.
Prettier - Code formatter: Automatically formats your code to keep it consistent.
Bracket Pair Colorizer: Helps visually match brackets.
Remember, each time you make changes to your React code, the browser will automatically refresh, showing the updated version of your app due to the hot-reloading feature of create-react-app. Happy coding!

Can I just move the files I made before

If you've already created some React files and just want to incorporate them into a new create-react-app setup, you can move them. Here's how to do it:
Set Up a New React Project (if not done already):
npx create-react-app my-tic-tac-toe cd my-tic-tac-toe
Move Your Files:
Navigate to your previous project folder.
Copy the necessary files you've created. In this case, it might be just the App.js and possibly an associated CSS file or any other components you've made.
Go to the my-tic-tac-toe/src/ directory in your new project.
Paste your files here. If you're copying over the App.js, it will replace the default one, which is okay if you intend to replace it with your own.
Install Additional Dependencies (if any): If your old project had any specific npm packages installed, you'll need to install those as well. Use:
npm install package-name
Adjust Imports: Ensure that the paths in your import statements are correct. If you've moved files around, the paths may need adjustments.
Run Your Project:
npm start
Your project should now run with the files you've created previously.
Note: One thing to keep in mind is that create-react-app has a specific configuration and structure. If your previous setup had specific configurations or utilized a different build tool or directory structure, there might be more steps to integrate it fully. However, if it was a simple React component or two, moving the files as described should suffice.


Let's create a cheerful and sizable Tic Tac Toe board.
Square Styling:
Make the squares bigger.
Add bright colors.
Use a thick border to make the squares prominent.
Style the X and O to be bold and cheerful.
Board Styling:
Use flexbox to ensure squares are aligned properly.
Add some space between the squares for better visibility.
Here's a suggested CSS:
/* Overall App styling */
.App {
font-family: 'Arial', sans-serif;
text-align: center;
background-color: #FFEBEE; /* A light pink background */
padding: 50px;
border-radius: 15px;
box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.1);

h1 {
color: #E91E63; /* Cheerful pink */

/* Board and Square styling */
.board-row {
display: flex;
justify-content: center;
margin-bottom: 15px;

.square {
width: 100px;
height: 100px;
background-color: #FFCDD2; /* A cheerful light red */
border: 3px solid #F48FB1; /* Cheerful pink border */
font-size: 24px;
font-weight: bold;
cursor: pointer;
transition: background-color 0.3s;

/* Center text in button */
display: flex;
justify-content: center;
align-items: center;

.square:hover {
background-color: #FFABAB; /* A slightly darker red on hover */

/* Colors for X and O */
.square.X {
color: #1565C0; /* A bright blue for X */

.square.O {
color: #4CAF50; /* A bright green for O */

.status {
margin-bottom: 20px;
font-size: 20px;
color: #E91E63; /* Cheerful pink for status text */

This CSS makes use of cheerful colors, bigger square buttons for the game, and a shadow around the main game container for a slight 3D effect.
The X and O symbols have different cheerful colors to differentiate between them and add more cheer to the game board.
Include this CSS in your App.css file or whichever CSS file you're using, and it should give your Tic Tac Toe board a cheerful appearance!
** Make sure you are importing this css file into app.js

If the squares are not showing the new CSS, there might be a few reasons. Let's diagnose and solve this issue step-by-step:

1. File Reference:

Ensure that you added the CSS to the correct file. If you placed the styles in App.css, make sure you have the following import at the top of your App.js:
javascriptCopy code
import './App.css';

2. CSS Specificity:

Sometimes, other styles might be overriding your new styles due to higher specificity or because they are loaded after your styles. You can use browser's developer tools (right-click on an element and select "Inspect" or "Inspect Element") to see which styles are applied to your squares. If your styles are being overridden, you may need to increase their specificity.

3. Cached Files:

Web browsers cache files for faster loading. It might be possible that the browser is loading a cached version of your CSS. Clear your browser's cache or try hard refreshing the page. For most browsers, a hard refresh can be done using Ctrl + Shift + R.

4. Check for Errors:

It's possible there might be a syntactical error in the CSS, which prevents the browser from reading subsequent styles. Check the CSS for any missing braces, semicolons, or other potential errors.

5. External CSS Libraries:

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.