-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
57 lines (49 loc) · 1.54 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
50
51
52
53
54
55
56
57
const cells = document.querySelectorAll("[data-cell]");
const board = document.getElementById("board");
const messageElement = document.getElementById("message");
const restartButton = document.getElementById("restartButton");
let currentPlayer = "♡";
let gameActive = true;
let boardState = Array(9).fill(null);
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],
];
function handleCellClick(e) {
const cell = e.target;
const cellIndex = Array.from(cells).indexOf(cell);
if (boardState[cellIndex] !== null || !gameActive) return;
boardState[cellIndex] = currentPlayer;
cell.textContent = currentPlayer;
if (checkWin()) {
gameActive = false;
messageElement.textContent = `${currentPlayer} Wins!`;
} else if (boardState.every((cell) => cell !== null)) {
gameActive = false;
messageElement.textContent = "Draw!";
} else {
currentPlayer = currentPlayer === "♡" ? "⟡" : "♡";
}
}
function checkWin() {
return winningCombinations.some((combination) => {
return combination.every((index) => {
return boardState[index] === currentPlayer;
});
});
}
function restartGame() {
currentPlayer = "♡";
gameActive = true;
boardState = Array(9).fill(null);
cells.forEach((cell) => (cell.textContent = ""));
messageElement.textContent = "";
}
cells.forEach((cell) => cell.addEventListener("click", handleCellClick));
restartButton.addEventListener("click", restartGame);