Skip to content
Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones

Categories

  • Automobiles (12)
    • Cars (7)
  • Blog (103)
    • Poems (2)
    • Space (2)
  • Command (2)
  • Education (2)
  • Entertainment (4)
  • Gadgets (9)
    • Phones (8)
      • Android Phones (4)
  • HTML Templates (11)
  • IT Training Institutes (1)
  • Lifestyle (4)
  • News (51)
  • Others (23)
  • Programming (296)
    • API (16)
    • CSS (83)
    • Database (4)
    • Hosting (1)
    • HTML (37)
    • JavaScript (117)
      • JQuery (27)
      • ReactJS (7)
    • PHP (116)
  • Python (3)
  • recipes (1)
  • SEE Result (1)
  • Server (3)
  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones
Close

Search

JavaScript

Easy Tic-Tac-Toe Game using JavaScript

By Admin
April 2, 2024 2 Min Read
0

Tic-Tac-Toe is a classic paper-and-pencil game often played by children and adults alike. It’s a two-player game played on a 3×3 grid. The players typically use ‘X’ and ‘O’ symbols to mark their moves on the grid, taking turns to do so. The objective of the game is to form a line of three of your symbols either horizontally, vertically, or diagonally on the grid.

Tic-tac-toeThe game starts with an empty grid, and players alternate turns placing their symbol in an empty cell. The first player to successfully create a line of three of their symbols wins the game. If all cells on the grid are filled without any player achieving a winning line, the game ends in a draw.

Tic Tac Toe is often used as a teaching tool for introducing the concepts of strategy, planning, and decision-making in a simple and accessible format. It’s also frequently implemented as a programming exercise due to its straightforward rules and structure, making it a great beginner project for learning game development or programming languages like JavaScript, Python, or Java.

You may also like : Design a Chess Board using HTML and CSS

Follow this video for complete guidance :

https://www.youtube.com/watch?v=T-65_AZDdx0&ab_channel=LetsTryThis%21

Tic Tac Toe, also known as noughts and crosses or Xs and Os, has a long history and its origins are not entirely clear. The game is simple in nature, requiring only a grid and two different symbols for players, making it likely that variations of the game have been played for centuries in different cultures.

One theory suggests that Tic-Tac-Toe may have originated in ancient Egypt, as early forms of the game have been found carved into the roofs of ancient Egyptian temples. Other theories propose that the game could have originated in ancient Rome or Greece, where similar grid-based games were played.

The modern version of Tic Tac Toe that we know today became popular in the 20th century, especially among schoolchildren, due to its simplicity and ease of play. It has since become a ubiquitous game, played both on paper and electronically, and has been implemented in various forms in computer software and online platforms. Despite its simplicity, Tic-Tac-Toe remains a timeless and engaging game enjoyed by people of all ages around the world.

Full Source Code for Tic-Tac-Toe Game:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Tic-Tac-Toe Game using JavaScript</title>
  <style type="text/css">
    .board{
      display: grid;
      grid-template-columns: repeat(3, 100px);
      grid-template-rows: repeat(3, 100px);
    }
    .cell{
      border:1px solid black;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 2em;
      cursor: pointer;
    }
  </style>
</head>
<body>
  <div class="board" id="board"></div>

  <script type="text/javascript">
    const board = document.getElementById('board');
    let currentPlayer = 'X';
    const cells = [];

    for(let i=0;i<9;i++){
      const cell = document.createElement('div');
      cell.classList.add('cell');
      cell.addEventListener('click',handleClick);
      board.appendChild(cell);
      cells.push(cell);
    }

    function handleClick(e){
      const cell = e.target;
      if(cell.textContent === ''){
        cell.textContent = currentPlayer;
        currentPlayer = currentPlayer === 'X'?'0':'X';
      }
    }
  </script>
</body>
</html>






Tags:

gametik tac toe
Author

Admin

Follow Me
Other Articles
Previous

Mastering Time Management like Pro: Balance Your Workload Effectively

Next

Easy Football Lineup Selection with jQuery UI Drag Drop

No Comment! Be the first one.

Leave a Reply

Your email address will not be published. Required fields are marked *

FIFA World Cup 2026 Predict and Win by SportsGuff

Recent Posts

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide
  • Chiranjibi Adhikari Appointed Acting President of CAN Federation
  • How to Design a Student Marksheet Using HTML and CSS

Tags

adsense ai animate animation animation using HTML and CSS API blog calculator chatgpt Cryptocurrency CSS css animation design Email Facebook featured filemanager file manager free template google htaccess HTML image Instagram interview javascript JQuery jquery ui NADA AutoShow NADA Auto Show 2024 password PHP Progressive Web App PWA QR random react reactjs Rotate travel Twitter vpn youthforum youthsforum youtube

About Us

At Youths Forum, we are passionate about sharing knowledge that empowers students, educators, professionals, and technology enthusiasts.

Our Mission

Our mission is simple: to make technology and education accessible, understandable, and beneficial for everyone. We strive to create content that helps our readers learn new skills and stay updated with industry developments.

RSS RSS

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You Admin
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide Admin
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide Admin

Quick Links

  • Stock Market Live
  • Parliament Election 2082
Copyright 2026 — Youths Forum. All rights reserved. Blogsy WordPress Theme