-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
49 lines (44 loc) · 1.46 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
let currentPlayer = "X";
let gameBoard = ["", "", "", "", "", "", "", "", ""];
let gameOver = false; // Add a variable to track game over status
function handleClick(index) {
if (!gameOver && !gameBoard[index]) { // Check if the game is not over
gameBoard[index] = currentPlayer;
renderBoard();
checkWinner();
currentPlayer = currentPlayer === "X" ? "O" : "X";
}
}
function renderBoard() {
const cells = document.querySelectorAll(".cell");
cells.forEach((cell, index) => {
cell.textContent = gameBoard[index];
});
}
function checkWinner() {
const winningCombinations = [
[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 combination of winningCombinations) {
const [a, b, c] = combination;
if (gameBoard[a] && gameBoard[a] === gameBoard[b] && gameBoard[a] === gameBoard[c]) {
document.getElementById("status").textContent = `Player ${gameBoard[a]} wins!`;
gameOver = true; // Set game over status to true
return;
}
}
if (!gameBoard.includes("")) {
document.getElementById("status").textContent = "It's a tie!";
gameOver = true; // Set game over status to true
}
}
function resetGame() {
currentPlayer = "X";
gameBoard = ["", "", "", "", "", "", "", "", ""];
gameOver = false; // Reset game over status
renderBoard();
document.getElementById("status").textContent = "";
}
renderBoard();