The Wayback Machine - https://web.archive.org/web/20241217190221/https://www.geeksforgeeks.org/simple-tic-tac-toe-game-using-javascript/
Open In App

Simple Tic-Tac-Toe Game using JavaScript

Last Updated : 22 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

We will create a Tic-Tac-Toe Game using JavaScript. We will be using validation checks to implement the game features. The UI is designed using CSS so it is easy to create. In the game, Player-1 starts playing the game and both players make their moves in consecutive turns. The player who makes a straight 3-block chain wins the game. This game is built on the front-end using simple logic and validation checks only.   

Play Tic-Tac-Toe Game Online

Prerequisites 

Approach

  • Dynamic Board Creation: Instead of hardcoding the grid, we dynamically generate the 3×3 grid using JavaScript.
  • Game State Management: We use an array to track the board state, reducing DOM dependency for game logic.
  • Reusable Functions: Functions handle actions like win detection, board setup, and game reset to avoid redundancy.
  • Winning Combinations: We define an array of all possible winning patterns for simplicity.

To enhance your JavaScript abilities and build more interactive projects, explore our JavaScript Course, which includes hands-on projects like game development to help you apply your knowledge.

Example: Write the following code in respective files.

  • index.html: This file contains the structure of the project.
  • styles.css: This file contains the styling of the application.
  • main.js: This file contains the logic of the game.
HTML
<html>

<head>
    <link rel="stylesheet" type="text/css" href="styles.css">
</head>

<body>
    <div id="main">
        <h1>TIC TAC TOE</h1>
        <p id="ins">
            Tap on a box to play.<br><br>
            <b>Player X</b> starts first, followed by <b>Player O</b>.
        </p>
        <div id="board" class="board"></div>
        <br>
        <button id="reset" class="reset">RESET</button>
        <p id="message" class="message"></p>
    </div>
    <script src="main.js"></script>
</body>

</html>
CSS
body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f8f9fa;
}

#main {
    text-align: center;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 20px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
    border-radius: 10px;
    background-color: #ffffff;
}

.board {
    display: grid;
    grid-template-columns: repeat(3, 100px);
    grid-gap: 5px;
    margin-top: 20px;
}

.cell {
    width: 100px;
    height: 100px;
    border: 1px solid gray;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    cursor: pointer;
}

.cell.taken {
    pointer-events: none;
}

.message {
    margin-top: 20px;
    font-size: 18px;
    font-weight: bold;
}

.reset {
    margin-top: 20px;
    padding: 10px 20px;
    font-size: 16px;
    cursor: pointer;
    background-color: whitesmoke;
    border: 1px solid dodgerblue;
    color: dodgerblue;
    border-radius: 8px;
}
JavaScript
const TicTac = {
    cPlayer: "X", // Tracks current player (X or O)
    state: Array(9).fill(null), // Board state (null for empty cells)
    gameOver: false, // Indicates if the game is over

    // Initialize the game
    init() {
        this.cBoard();
        document
            .getElementById("reset")
            .addEventListener("click", () => this.reset());
    },

    // Create the game board dynamically
    cBoard() {
        const board = document.getElementById("board");
        board.innerHTML = ""; // Clear previous board
        this.state.forEach((_, i) => {
            const cell = document.createElement("div");
            cell.classList.add("cell");
            cell.dataset.index = i;
            board.appendChild(cell);
        });
        board.addEventListener("click", (e) => this.handleClick(e)); // Handle clicks on the board
        this.uMessage(`Player ${this.cPlayer}'s turn`);
    },

    // Handle a cell click
    handleClick(e) {
        const cell = e.target;
        const i = cell.dataset.index;

        // Ignore clicks if game is over or cell is taken
        if (this.gameOver || !cell.classList.contains("cell") || this.state[i])
            return;

        // Update board state and UI
        this.state[i] = this.cPlayer;
        cell.textContent = this.cPlayer;
        cell.classList.add("taken");

        // Check for winner or tie
        const winCombo = this.checkWin();
        if (winCombo) {
            this.highlight(winCombo);
            this.uMessage(`Player ${this.cPlayer} wins!`);
            this.gameOver = true;
        } else if (this.state.every((cell) => cell)) {
            this.uMessage("It's a tie!");
            this.gameOver = true;
        } else {
            // Switch players
            this.cPlayer = this.cPlayer === "X" ? "O" : "X";
            this.uMessage(`Player ${this.cPlayer}'s turn`);
        }
    },

    // Check if there's a winning combination
    checkWin() {
        const wins = [
            [0, 1, 2],
            [3, 4, 5],
            [6, 7, 8], // Rows
            [0, 3, 6],
            [1, 4, 7],
            [2, 5, 8], // Columns
            [0, 4, 8],
            [2, 4, 6], // Diagonals
        ];
        return wins.find((combo) =>
            combo.every((i) => this.state[i] === this.cPlayer)
        );
    },

    // Highlight winning cells
    highlight(combo) {
        combo.forEach((i) => {
            document.getElementById("board").children[i].style.color = "red";
        });
    },

    // Reset the game
    reset() {
        this.state = Array(9).fill(null);
        this.cPlayer = "X";
        this.gameOver = false;
        this.cBoard();
    },

    // Update the game status message
    uMessage(msg) {
        document.getElementById("message").textContent = msg;
    },
};

// Start the game
TicTac.init();

Output

tic-tac-toe-game

Simple Tic-Tac-Toe Game using JavaScript

How It Works

  • Game Initialization: The game board is dynamically generated using the createBoard function. Each cell is clickable, and its index is stored for state management.
  • Player Interaction: Players take turns clicking cells to mark them as “X” or “O”. After each move, the game checks for a winner or tie using the checkWin function.
  • Win Detection: Winning combinations are pre-defined, and the game evaluates the board state to detect a winner. Winning cells are highlighted in red.
  • Reset: The reset button clears the board, state, and game status, allowing a new game to start.


Next Article

Similar Reads

three90RightbarBannerImg