-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
50 lines (49 loc) · 1.56 KB
/
app.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
const cells = document.querySelectorAll(".cell")
const restart = document.querySelector("#restart")
const darkMode = document.querySelector("#darkMode")
let player = null
let value = null
cells.forEach(cell => {
cell.addEventListener("click", () => {
if (value === "O") {
player = "player 1"
value = "X"
cell.textContent = value
setTimeout(() => winner(value),100)
} else {
player = "player 2"
value = "O"
cell.textContent = value
setTimeout(() => winner(value), 100)
}
})
})
restart.addEventListener("click", () => {
location.reload(true)
})
function winner(valueUp) {
//controllo righe
for (let i = 0; i < 3; i++) {
if (
cells[i * 3].textContent === valueUp &&
cells[i * 3 + 1].textContent === valueUp &&
cells[i * 3 + 2].textContent === valueUp) {
alert(`winner ${player}`);
}
}
//controllo colonne
for (let j = 0; j < 3; j++) {
if (cells[j].textContent === valueUp &&
cells[j + 3].textContent === valueUp &&
cells[j + 6].textContent === valueUp) {
alert(`winner ${player}`);
}
}
//controllo diagonali
if (cells[0].textContent === valueUp && cells[4].textContent === valueUp && cells[8].textContent === valueUp) {
alert(`winner ${player}`);
}
if (cells[2].textContent === valueUp && cells[4].textContent === valueUp && cells[6].textContent === valueUp) {
alert(`winner ${player}`);
}
}