index.html
<!DOCTYPE html> <html lang="en"> Â <head> Â Â <meta charset="UTF-8" /> Â Â <meta name="viewport" content="width=device-width, initial-scale=1.0" /> Â Â <title>Tic-Tac-Toe Game</title> Â Â <link rel="stylesheet" href="style.css" /> Â </head> Â <body> Â Â <div class="msg-container hide"> Â Â Â <p id="msg">Winner</p> Â Â Â <button id="new-btn">New Game</button> Â Â </div> Â Â <main> Â Â Â <h1>Game</h1> Â Â Â <h2>Created By - programmercoding.com </h2> Â Â Â <div class="container"> Â Â Â Â <div class="game"> Â Â Â Â Â <button class="box"></button> Â Â Â Â Â <button class="box"></button> Â Â Â Â Â <button class="box"></button> Â Â Â Â Â <button class="box"></button> Â Â Â Â Â <button class="box"></button> Â Â Â Â Â <button class="box"></button> Â Â Â Â Â <button class="box"></button> Â Â Â Â Â <button class="box"></button> Â Â Â Â Â <button class="box"></button> Â Â Â Â </div> Â Â Â </div> Â Â Â <button id="reset-btn">Reset Game</button> Â Â </main> Â Â <script src="app.js"></script> Â </body> </html>
Style.css
* {
  margin: 0;
  padding: 0;
 }
 body {
  background-color: #7f9a39;
  text-align: center;
 }
 .container {
  height: 70vh;
  display: flex;
  justify-content: center;
  align-items: center;
 }
 h2 {
  color: white;
 }
 .game {
  height: 60vmin;
  width: 60vmin;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 1.5vmin;
 }
 .box {
  height: 18vmin;
  width: 18vmin;
  border-radius: 1rem;
  border: none;
  box-shadow: 0 0 1rem rgba(0, 0, 0, 0.3);
  font-size: 8vmin;
  color: #f90707a7;
  background-color: #0bbde5;
 }
 #reset-btn {
  padding: 1rem;
  font-size: 1.25rem;
  background-color: #191913;
  color: #fff;
  border-radius: 1rem;
  border: none;
 }
 #new-btn {
  padding: 1rem;
  font-size: 1.25rem;
  background-color: #191913;
  color: #fff;
  border-radius: 1rem;
  border: none;
 }
 #msg {
  color: #ffffc7;
  font-size: 5vmin;
 }
 .msg-container {
  height: 100vmin;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  gap: 4rem;
 }
 .hide {
  display: none;
 }
app.js
let boxes = document.querySelectorAll(".box"); let resetBtn = document.querySelector("#reset-btn"); let newGameBtn = document.querySelector("#new-btn"); let msgContainer = document.querySelector(".msg-container"); let msg = document.querySelector("#msg"); let turnO = true; //playerX, playerO let count = 0; //To Track Draw const winPatterns = [ Â [0, 1, 2], Â [0, 3, 6], Â [0, 4, 8], Â [1, 4, 7], Â [2, 5, 8], Â [2, 4, 6], Â [3, 4, 5], Â [6, 7, 8], ]; const resetGame = () => { Â turnO = true; Â count = 0; Â enableBoxes(); Â msgContainer.classList.add("hide"); }; boxes.forEach((box) => { Â box.addEventListener("click", () => { Â Â if (turnO) { Â Â Â //playerO Â Â Â box.innerText = "O"; Â Â Â turnO = false; Â Â } else { Â Â Â //playerX Â Â Â box.innerText = "X"; Â Â Â turnO = true; Â Â } Â Â box.disabled = true; Â Â count++; Â Â let isWinner = checkWinner(); Â Â if (count === 9 && !isWinner) { Â Â Â gameDraw(); Â Â } Â }); }); const gameDraw = () => { Â msg.innerText = `Game was a Draw.`; Â msgContainer.classList.remove("hide"); Â disableBoxes(); }; const disableBoxes = () => { Â for (let box of boxes) { Â Â box.disabled = true; Â } }; const enableBoxes = () => { Â for (let box of boxes) { Â Â box.disabled = false; Â Â box.innerText = ""; Â } }; const showWinner = (winner) => { Â msg.innerText = `Congratulations, Winner is ${winner}`; Â msgContainer.classList.remove("hide"); Â disableBoxes(); }; const checkWinner = () => { Â for (let pattern of winPatterns) { Â Â let pos1Val = boxes[pattern[0]].innerText; Â Â let pos2Val = boxes[pattern[1]].innerText; Â Â let pos3Val = boxes[pattern[2]].innerText; Â Â if (pos1Val != "" && pos2Val != "" && pos3Val != "") { Â Â Â if (pos1Val === pos2Val && pos2Val === pos3Val) { Â Â Â Â showWinner(pos1Val); Â Â Â Â return true; Â Â Â } Â Â } Â } }; newGameBtn.addEventListener("click", resetGame); resetBtn.addEventListener("click", resetGame);
Output is:
