-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossZero.js
144 lines (129 loc) · 4 KB
/
crossZero.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const div = document.getElementsByClassName('col')
let chance = 0
function play() {
if (chance % 2 === 0) {
return "X";
}
else {
return "O"
}
}
const popUp = document.querySelector('.popUp')
function correctvalue(value) {
return value === "X"
}
function correctvalue2(value) {
return value === "O"
}
function win(player) {
popUp.style.display = 'block'
console.log(chance, "inside win")
document.querySelector('.popUp h1').textContent = `${player} won`;
clearInterval(id)
}
function tie() {
popUp.style.display = 'block'
document.querySelector('.popUp h1').textContent = "Tie";
}
const playAgain = document.getElementById('playAgain')
playAgain.addEventListener('click', clear)
function clear() {
for (let element of div) {
element.querySelectorAll('h1')[0].textContent = ''
}
popUp.style.display = 'none'
chance = 0
array = []
row1 = {}
row2 = {}
row3 = {}
mainDiagonal = []
diagonal = []
column1 = []
column2 = []
column3 = []
id = setInterval(() => {
if (chance === 9) {
tie()
clearInterval(id)
}
}, 1)
}
let row1 = {}
let row2 = {}
let row3 = {}
let mainDiagonal = []
let diagonal = []
let column1 = []
let column2 = []
let column3 = []
let array = []
for (let element of div) {
element.addEventListener('click', (e) => {
if (e.target.querySelectorAll('h1')[0].textContent === '') {
e.target.querySelectorAll('h1')[0].textContent = play()
e.target.id[1] == 0 ? row1[e.target.id] = e.target.textContent : ''
e.target.id[1] == 1 ? row2[e.target.id] = e.target.textContent : ''
e.target.id[1] == 2 ? row3[e.target.id] = e.target.textContent : ''
array = [row1, row2, row3]
for (let i = 0; i <= 2; i++) {
let rownew = Object.values(array[i]);
if (rownew.length == 0) {
} else if (rownew.length >= 3) {
if (rownew.every(correctvalue)) {
win(rownew[0]);
} else if (rownew.every(correctvalue2)) {
win(rownew[0]);
}
}
}
mainDiagonal = [array[0]['a00'], array[1]['a11'], array[2]['a22']]
if (mainDiagonal.length === 3) {
if (mainDiagonal.every(correctvalue)) {
win(mainDiagonal[0])
} else if (mainDiagonal.every(correctvalue2)) {
win(mainDiagonal[0])
}
}
diagonal = [array[0]['a02'], array[1]['a11'], array[2]['a20']]
if (diagonal.length === 3) {
if (diagonal.every(correctvalue)) {
win(diagonal[0])
} else if (diagonal.every(correctvalue2)) {
win(diagonal[0])
}
}
column1 = [array[0]['a00'], array[1]['a10'], array[2]['a20']]
if (column1.length === 3) {
if (column1.every(correctvalue)) {
win(column1[0])
} else if (column1.every(correctvalue2)) {
win(column1[0])
}
}
column2 = [array[0]['a01'], array[1]['a11'], array[2]['a21']]
if (column2.length === 3) {
if (column2.every(correctvalue)) {
win(column2[0])
} else if (column2.every(correctvalue2)) {
win(column2[0])
}
}
column3 = [array[0]['a02'], array[1]['a12'], array[2]['a22']]
if (column3.length === 3) {
if (column3.every(correctvalue)) {
win(column3[0])
} else if (column3.every(correctvalue2)) {
win(column3[0])
}
}
chance++
}
})
}
let id = setInterval(() => {
if (chance === 9) {
tie()
clearInterval(id)
}
}, 1)