Tic-Tac-Toe Game in JavaScript with electro4u

15 Oct 2023 Balmiki Mandal 0 JavaScript

Tic-Tac-Toe Game in JavaScript Step by Step

Tic-Tac-Toe is a classic two-player game where players take turns marking the spaces on a 3x3 grid. The first player to get three of their marks in a row, either horizontally, vertically, or diagonally, wins the game.

To implement Tic-Tac-Toe in JavaScript, you will need to create a HTML file for the game board and a JavaScript file for the game logic.

HTML

The HTML file will contain a table element with a 3x3 grid of cells. Each cell will be represented by a div element.

HTML
 
 
 
 
 
 
 
 
 
 
 

You can also add CSS to style the game board and make it look more appealing.

JavaScript

The JavaScript file will contain the game logic, such as tracking the current player's turn, checking for wins, and updating the game board.

JavaScript
// Define the current player's turn
var currentPlayer = "X";

// Create an array to represent the game board
var board = new Array(9);

// Initialize the game board
for (var i = 0; i < board.length; i++) {
  board[i] = "";
}

// Add event listeners to the game board cells
var boxes = document.querySelectorAll(".box");
for (var i = 0; i < boxes.length; i++) {
  boxes[i].addEventListener("click", function() {
    // Check if the cell is already filled
    if (board[this.id] === "") {
      // Place the current player's mark on the cell
      board[this.id] = currentPlayer;

      // Update the game board
      this.innerHTML = currentPlayer;

      // Check for a win
      var winner = checkForWin(board);
      if (winner) {
        // Display the winner
        alert(winner + " wins!");
      }

      // Switch to the next player's turn
      currentPlayer = (currentPlayer === "X") ? "O" : "X";
    }
  });
}

// Check for a win in the game board
function checkForWin(board) {
  // Check for horizontal wins
  for (var i = 0; i < 3; i++) {
    if (board[i * 3] === board[i * 3 + 1] && board[i * 3 + 1] === board[i * 3 + 2]) {
      return board[i * 3];
    }
  }

  // Check for vertical wins
  for (var i = 0; i < 3; i++) {
    if (board[i] === board[i + 3] && board[i + 3] === board[i + 6]) {
      return board[i];
    }
  }

  // Check for diagonal wins
  if (board[0] === board[4] && board[4] === board[8]) {
    return board[0];
  }
  if (board[2] === board[4] && board[4] === board[6]) {
    return board[2];
  }

  // If there is no winner, return null
  return null;
}

Once you have created the HTML and JavaScript files, you can open the HTML file in a web browser to play the game.

Additional Features

You can add additional features to your Tic-Tac-Toe game, such as:

  • Tracking wins and displaying the winner
  • Allowing players to choose their marks
  • Adding a difficulty level
  • Creating a multiplayer version of the game

With a little creativity, you can make your Tic-Tac-Toe game even more fun and engaging.

Top Resources Now:


Enroll Now:


[JavaScript 2023-2024: From Scratch to Advanced] "Start Supercharging Your Productivity!"

Contact Us:


  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.