-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
232 lines (200 loc) · 7.83 KB
/
script.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
let x = document.querySelectorAll(".items")
let btnx = document.querySelector("#btnx")
let btno = document.querySelector("#btno")
let temp = 0
let won = document.querySelector("#won")
let play = document.querySelector(".play")
x.forEach(element => {
element.addEventListener("click",function(){
if(temp == 0){
element.innerHTML = "X"
btnx.style.backgroundColor = "transparent"
btno.style.backgroundColor = "#e2e8f0"
btno.style.color = "#1e293b"
btnx.style.color = "white"
// btnx.style.border = "2px solid #e2e8f0"
temp = 1
Gamelogic()
play.addEventListener("click",function(){
location.reload()
})
}
else{
element.innerHTML = "O"
btnx.style.backgroundColor = "#e2e8f0"
btno.style.backgroundColor = "transparent"
btnx.style.color = "#1e293b"
btno.style.color = "white"
// btno.style.border = "2px solid #e2e8f0"
temp = 0
Gamelogic()
}
})
});
function Gamelogic(){
let item1 = document.querySelector("#item1")
let item2 = document.querySelector("#item2")
let item3 = document.querySelector("#item3")
let item4 = document.querySelector("#item4")
let item5 = document.querySelector("#item5")
let item6 = document.querySelector("#item6")
let item7 = document.querySelector("#item7")
let item8 = document.querySelector("#item8")
let item9 = document.querySelector("#item9")
//X portion
// 3 conditions of item1
if(item1.innerText == 'X' && item2.innerText == 'X' && item3.innerText == 'X'){
won.innerHTML = "X Won"
item1.style.backgroundColor = "#cbdaf1"
item2.style.backgroundColor = "#cbdaf1"
item3.style.backgroundColor = "#cbdaf1"
play.style.display = "block"
}
if(item1.innerText == 'X' && item4.innerText == 'X' && item7.innerText == 'X'){
won.innerHTML = "X Won"
item1.style.backgroundColor = "#cbdaf1"
item4.style.backgroundColor = "#cbdaf1"
item7.style.backgroundColor = "#cbdaf1"
play.style.display = "block"
}
if(item1.innerText == 'X' && item5.innerText == 'X' && item9.innerText == 'X'){
won.innerHTML = "X Won"
item1.style.backgroundColor = "#cbdaf1"
item5.style.backgroundColor = "#cbdaf1"
item9.style.backgroundColor = "#cbdaf1"
play.style.display = "block"
}
// 2 Conditions of item2
if(item2.innerText == 'X' && item5.innerText == 'X' && item8.innerText == 'X'){
won.innerHTML = "X Won"
item2.style.backgroundColor = "#cbdaf1"
item5.style.backgroundColor = "#cbdaf1"
item8.style.backgroundColor = "#cbdaf1"
play.style.display = "block"
}
// One condition is common
// 3 conditions of item3
if(item3.innerText == 'X' && item6.innerText == 'X' && item9.innerText == 'X'){
won.innerHTML = "X Won"
item3.style.backgroundColor = "#cbdaf1"
item6.style.backgroundColor = "#cbdaf1"
item9.style.backgroundColor = "#cbdaf1"
play.style.display = "block"
}
if(item3.innerText == 'X' && item5.innerText == 'X' && item7.innerText == 'X'){
won.innerHTML = "X Won"
item3.style.backgroundColor = "#cbdaf1"
item5.style.backgroundColor = "#cbdaf1"
item7.style.backgroundColor = "#cbdaf1"
play.style.display = "block"
}
//One condition is common
// Conditions of 2nd row
if(item4.innerText == 'X' && item5.innerText == 'X' && item6.innerText == 'X'){
won.innerHTML = "X Won"
item4.style.backgroundColor = "#cbdaf1"
item5.style.backgroundColor = "#cbdaf1"
item6.style.backgroundColor = "#cbdaf1"
play.style.display = "block"
}
// item5's conditions are common in others
// item6's conditions are common in others
if(item7.innerText == 'X' && item8.innerText == 'X' && item9.innerText == 'X'){
won.innerHTML = "X Won"
item7.style.backgroundColor = "#cbdaf1"
item8.style.backgroundColor = "#cbdaf1"
item9.style.backgroundColor = "#cbdaf1"
play.style.display = "block"
}
// Remaining item's conditions are common in others
//O portion
if(item1.innerText == 'O' && item2.innerText == 'O' && item3.innerText == 'O'){
won.innerHTML = "O Won"
item1.style.backgroundColor = "#cbdaf1"
item2.style.backgroundColor = "#cbdaf1"
item3.style.backgroundColor = "#cbdaf1"
play.style.display = "block"
}
if(item1.innerText == 'O' && item4.innerText == 'O' && item7.innerText == 'O'){
won.innerHTML = "O Won"
item1.style.backgroundColor = "#cbdaf1"
item4.style.backgroundColor = "#cbdaf1"
item7.style.backgroundColor = "#cbdaf1"
play.style.display = "block" }
if(item1.innerText == 'O' && item5.innerText == 'O' && item9.innerText == 'O'){
won.innerHTML = "O Won"
item1.style.backgroundColor = "#cbdaf1"
item5.style.backgroundColor = "#cbdaf1"
item9.style.backgroundColor = "#cbdaf1"
play.style.display = "block" }
// 2 Conditions of item2
if(item2.innerText == 'O' && item5.innerText == 'O' && item8.innerText == 'O'){
won.innerHTML = "O Won"
item2.style.backgroundColor = "#cbdaf1"
item5.style.backgroundColor = "#cbdaf1"
item8.style.backgroundColor = "#cbdaf1"
play.style.display = "block"
}
// One condition is common
// 3 conditions of item3
if(item3.innerText == 'O' && item6.innerText == 'O' && item9.innerText == 'O'){
won.innerHTML = "O Won"
item3.style.backgroundColor = "#cbdaf1"
item6.style.backgroundColor = "#cbdaf1"
item9.style.backgroundColor = "#cbdaf1"
play.style.display = "block"
}
if(item3.innerText == 'O' && item5.innerText == 'O' && item7.innerText == 'O'){
won.innerHTML = "O Won"
item3.style.backgroundColor = "#cbdaf1"
item5.style.backgroundColor = "#cbdaf1"
item7.style.backgroundColor = "#cbdaf1"
play.style.display = "block" }
//One condition is common
// Conditions of 2nd row
if(item4.innerText == 'O' && item5.innerText == 'O' && item6.innerText == 'O'){
won.innerHTML = "O Won"
item4.style.backgroundColor = "#cbdaf1"
item5.style.backgroundColor = "#cbdaf1"
item6.style.backgroundColor = "#cbdaf1"
play.style.display = "block" }
// item5's conditions are common in others
// item6's conditions are common in others
if(item7.innerText == 'O' && item8.innerText == 'O' && item9.innerText == 'O'){
won.innerHTML = "O Won"
item7.style.backgroundColor = "#cbdaf1"
item8.style.backgroundColor = "#cbdaf1"
item9.style.backgroundColor = "#cbdaf1"
play.style.display = "block"
}
if(won.innerHTML == "X Won"){
x.forEach(element => {
if(element.innerHTML == "")
{
element.addEventListener("click",function(){
element.innerHTML = ""
btnx.style.backgroundColor = "transparent"
btno.style.backgroundColor = "#e2e8f0"
btno.style.color = "#1e293b"
btnx.style.color = "white"
location.reload()
})
}
});
}
if(won.innerHTML == "O Won"){
x.forEach(element => {
if(element.innerHTML == "")
{
element.addEventListener("click",function(){
element.innerHTML = ""
btnx.style.backgroundColor = "#e2e8f0"
btno.style.backgroundColor = "transparent"
btnx.style.color = "#1e293b"
btno.style.color = "white"
location.reload()
})
}
});
}
}