-
Notifications
You must be signed in to change notification settings - Fork 2
/
4Loopgame.js
99 lines (74 loc) · 1.78 KB
/
4Loopgame.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
let turn = 0
function changeTurn() {
turn = 1 - turn
}
const winCombo = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
class Box {
constructor(win, arr) {
this.win = 0
this.arr = ["", "", "", "", "", "", "", "", ""]
}
checkWin() {
for (let i = 0; i < 8; i++) {
if ((this.arr[winCombo[i][0]] == this.arr[winCombo[i][1]]) && (this.arr[winCombo[i][1]] == this.arr[winCombo[i][2]])) {
if ((this.arr[winCombo[i][0]] !== "") && (this.arr[winCombo[i][1]] !== "") && (this.arr[winCombo[i][2]] !== "")) {
return true
}
}
} return false
}
}
let Box1 = new Box(0, [])
document.querySelector(".game-box").onclick = function (e) {
let cell = e.target.id
if (Number(cell) > 0) {
Box1.arr[cell - 1] = turn
if (Box1.checkWin()) {
alert("WIN")
}
changeTurn()
}
}
document.querySelectorAll(".game-box").forEach(function (box) {
box.onclick = function (e) {
if (turn) {
this.classList.add("red")
} else {
this.classList.add("blue")
}
console.log(this.classList)
}
})
// for (let i = 0; i < 8; i++) {
// document.querySelector("body").innerHTML += `
// <div class="game-box">
// <div class="small-box" id="1">
// </div>
// <div class="small-box" id="2">
// </div>
// <div class="small-box" id="3">
// </div>
// <div class="small-box" id="4">
// </div>
// <div class="small-box" id="5">
// </div>
// <div class="small-box" id="6">
// </div>
// <div class="small-box" id="7">
// </div>
// <div class="small-box" id="8">
// </div>
// <div class="small-box" id="9">
// </div>
// </div>
// `
// }