-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
78 lines (66 loc) · 1.8 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
let cells = document.querySelectorAll(".cell");
const statusText = document.getElementsByClassName("statusText")[0];
const restartBtn = document.getElementsByClassName("restartBtn")[0];
let currentPlayer = "x";
let gameIsOver = false;
let winningConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
const updateStatusText = () => {
statusText.innerHTML = `Player ${currentPlayer}'s turn`;
};
const checkForWinner = () => {
winningConditions.forEach((combination) => {
let check = combination.every(
(index) => cells[index].innerHTML.trim() === currentPlayer
);
if (check) {
statusText.innerHTML = `${currentPlayer} won`;
gameIsOver = true;
combination.forEach((index) => {
cells[index].classList.add("winning-cell");
cells[index].style.color = "blue";
});
}
});
};
const checkForDraw = () => {
if (![...cells].some((cell) => cell.innerHTML.trim() === "") && !gameIsOver) {
statusText.innerHTML = "It's a draw!";
gameIsOver = true;
}
};
updateStatusText();
cells = Array.from(cells);
cells.forEach((cell) => {
cell.addEventListener("click", () => {
if (gameIsOver || cell.innerHTML.trim() !== "") return;
cell.innerHTML = currentPlayer;
checkForWinner();
checkForDraw();
if (!gameIsOver) {
currentPlayer = currentPlayer === "x" ? "o" : "x";
updateStatusText();
}
});
});
restartBtn.addEventListener("click", () => {
restartGame();
});
const restartGame = () => {
cells.forEach((cell) => {
cell.innerHTML = "";
cell.classList.remove("winning-cell");
cell.style.color = "";
});
currentPlayer = "x";
gameIsOver = false;
updateStatusText();
};