-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
61 lines (59 loc) · 1.6 KB
/
code.js
File metadata and controls
61 lines (59 loc) · 1.6 KB
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
let wincomb = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 4, 8],
[2, 4, 6],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8]
];
let reset = document.querySelector("#reset");
reset.addEventListener("click", res);
let boxes = document.querySelectorAll(".box");
let count = 0;
let win = document.querySelector("#winner");
let img = document.querySelector("img");
let turno = true;
let flag = false;
boxes.forEach((box) => {
box.addEventListener('click', function () {
if (flag || box.innerText !== "") return;
if (turno) {
box.innerText = 'O';
turno = false;
} else {
box.innerText = 'X';
turno = true;
}
box.disabled = true;
count++;
for (let pattern of wincomb) {
if (
boxes[pattern[0]].innerText !== "" &&
boxes[pattern[0]].innerText === boxes[pattern[1]].innerText &&
boxes[pattern[1]].innerText === boxes[pattern[2]].innerText
) {
win.innerText = "Player " + boxes[pattern[0]].innerText + " wins";
img.style.display = "block";
flag = true;
return;
}
}
if (count === 9 && !flag) {
win.innerText = "Draw -_-";
flag = true;
}
});
});
function res() {
boxes.forEach((box) => {
box.innerText = "";
box.disabled = false;
});
win.innerText = "";
img.style.display = "none";
count = 0;
flag = false;
turno = true;
}