-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (45 loc) · 1.18 KB
/
index.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
let boxes = document.querySelectorAll(".box");
let resetBtn = document.querySelector("#resetbutton");
let turnO = true;
const winPatterns = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
function checkWinner() {
for (const pattern of winPatterns) {
const [a, b, c] = pattern;
if (boxes[a].innerText && boxes[a].innerText === boxes[b].innerText && boxes[a].innerText === boxes[c].innerText) {
return boxes[a].innerText; // Return the winner ('O' or 'X')
}
}
return null; // No winner yet
}
boxes.forEach((box) => {
box.addEventListener("click", () => {
console.log("Box was Clicked");
if (turnO) {
box.innerText = "O";
} else {
box.innerText = "X";
}
turnO = !turnO;
box.disabled = true;
const winner = checkWinner();
if (winner) {
alert(`${winner} is the winner!`);
resetGame();
}
});
});
resetBtn.addEventListener("click", resetGame);
function resetGame() {
boxes.forEach((box) => {
box.innerText = "";
box.disabled = false;
});
}