-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.js
57 lines (49 loc) · 1.44 KB
/
js.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 message = document.querySelector(".message");
const restartButton = document.querySelector(".restart-button");
let currentPlayer = "X";
let isGameActive = true;
const winningCombos = [
[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 checkWinner() {
for (const combo of winningCombos) {
const [a, b, c] = combo;
if (cells[a].textContent && cells[a].textContent === cells[b].textContent && cells[a].textContent === cells[c].textContent) {
message.textContent = `${currentPlayer} wins!`;
isGameActive = false;
}
}
}
function checkDraw() {
if ([...cells].every(cell => cell.textContent)) {
message.textContent = "It's a draw!";
isGameActive = false;
}
}
function handleClick() {
if (!isGameActive || this.textContent) return;
this.textContent = currentPlayer;
this.style.backgroundColor = "#ccc";
checkWinner();
checkDraw();
currentPlayer = currentPlayer === "X" ? "O" : "X";
}
function restartGame() {
cells.forEach(cell => {
cell.textContent = "";
cell.style.backgroundColor = "#eee";
});
message.textContent = "";
isGameActive = true;
currentPlayer = "X";
}
cells.forEach(cell => cell.addEventListener("click", handleClick));
restartButton.addEventListener("click", restartGame);