-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
298 lines (287 loc) · 17.2 KB
/
main.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
var itemsinline1 = ['1','2','3']
var itemsinline2 = ['','','']
var itemsinline3 = ['','','']
const numberofmovesdisplay = document.getElementById("numberofmovesentry")
var placebtn1 = document.getElementById("line1btn")
var placebtn2 = document.getElementById("line2btn")
var placebtn3 = document.getElementById("line3btn")
const disc1 = document.getElementById("1")
const disc2 = document.getElementById("2")
const disc3 = document.getElementById("3")
var selectState = false
var totalDiscs = [disc1,disc2,disc3]
var placeBTNs = [placebtn1,placebtn2,placebtn3]
const line1 = document.getElementById("line1container")
const line2 = document.getElementById("line2container")
const line3 = document.getElementById("line3container")
var selecteddisc = null
var number_of_moves = totalDiscs.length*placeBTNs.length
const winSTATE = ["1","2","3"]
const wonTEXT = document.getElementById("wontext")
function HoldDisc(discid){
selecteddisc = document.getElementById(discid)
selecteddisc.setAttribute("isSelected","true")
selecteddisc.style.background = "green"
selectState = true
console.log("DISC "+discid+" Selected!")
totalDiscs.forEach(discin => {
if(discin.id == discid){
discin.setAttribute("isSelected","true")
selectState = true
}
else{
discin.style.background = "blue"
discin.setAttribute("isSelected","false")
}
});
}
function winCheck(){
if(itemsinline3[0] == winSTATE[0]){
if(itemsinline3[1] == winSTATE[1]){
if(itemsinline3[2] == winSTATE[2]){
alert("U WON!")
wonTEXT.style.display = "block"
}
}
}
}
function placeDisc(line){
console.log(selecteddisc) // Displays the selected disc
if(selecteddisc == null){ // If any disc is not selected then alert it
alert("NO DISC SELECTED!")
}
else{
if(number_of_moves == 0){ // If there are no moves left then alert
alert("NO MOVES LEFT!")
}
else{
if(line == 'line1'){ // If Line is 1 then
var selected_disc_container = selecteddisc.getAttribute("disccontainerlinename")
if(selected_disc_container == line){
alert("YOU CAN'T PLACE THE SAME DISC ON THE SAME LINE!")
}
else{
if(selected_disc_container == "line1"){
if((itemsinline1[itemsinline1.indexOf(selecteddisc.id)-1] =="") || (itemsinline1[itemsinline1.indexOf(selecteddisc.id)-1] == undefined)){
line1.appendChild(selecteddisc) // Insert the selected disc!
number_of_moves = number_of_moves - 1
numberofmovesdisplay.innerText = number_of_moves
console.log(number_of_moves)
selecteddisc.setAttribute("disccontainerlinename","line1")
var currentIndex = itemsinline1.indexOf(selecteddisc.id)
itemsinline1[currentIndex] = ""
if(itemsinline1[2] == ''){
itemsinline1[2] = selecteddisc.id
}
else if(itemsinline1[1] == ''){
itemsinline1[1] = selecteddisc.id
}
else if(itemsinline1[0] == ''){
itemsinline1[0] = selecteddisc.id
}
}
else{
alert("There is a disc above!")
}
}
else if(selected_disc_container == "line2"){
if((itemsinline2[itemsinline2.indexOf(selecteddisc.id)-1] == "") || (itemsinline2[itemsinline2.indexOf(selecteddisc.id)-1] == undefined)){
line1.appendChild(selecteddisc) // Insert the selected disc!
number_of_moves = number_of_moves - 1
numberofmovesdisplay.innerText = number_of_moves
console.log(number_of_moves)
selecteddisc.setAttribute("disccontainerlinename","line1")
var currentIndex = itemsinline2.indexOf(selecteddisc.id)
itemsinline2[currentIndex] = ""
if(itemsinline1[2] == ''){
itemsinline1[2] = selecteddisc.id
}
else if(itemsinline1[1] == ''){
itemsinline1[1] = selecteddisc.id
}
else if(itemsinline1[0] == ''){
itemsinline1[0] = selecteddisc.id
}
}
else{
alert("There is a disc above!")
}
}
else if(selected_disc_container == "line3"){
if((itemsinline3[itemsinline3.indexOf(selecteddisc.id)-1] == "") || (itemsinline3[itemsinline3.indexOf(selecteddisc.id)-1] == undefined)){
line1.appendChild(selecteddisc) // Insert the selected disc!
number_of_moves = number_of_moves - 1
numberofmovesdisplay.innerText = number_of_moves
console.log(number_of_moves)
selecteddisc.setAttribute("disccontainerlinename","line1")
var currentIndex = itemsinline3.indexOf(selecteddisc.id)
itemsinline3[currentIndex] = ""
if(itemsinline1[2] == ''){
itemsinline1[2] = selecteddisc.id
}
else if(itemsinline1[1] == ''){
itemsinline1[1] = selecteddisc.id
}
else if(itemsinline1[0] == ''){
itemsinline1[0] = selecteddisc.id
}
}
}
else{
alert("There is a disc above!")
}
}
}
if(line == 'line2'){
var selected_disc_container = selecteddisc.getAttribute("disccontainerlinename")
if(selected_disc_container == line){
alert("YOU CAN'T PLACE THE SAME DISC ON THE SAME LINE!")
}
else{
if(selected_disc_container == "line1"){
if((itemsinline1[itemsinline1.indexOf(selecteddisc.id)-1] == "") || (itemsinline1[itemsinline1.indexOf(selecteddisc.id)-1] == undefined)){
line2.appendChild(selecteddisc) // Insert the selected disc!
number_of_moves = number_of_moves - 1
numberofmovesdisplay.innerText = number_of_moves
console.log(number_of_moves)
selecteddisc.setAttribute("disccontainerlinename","line2")
var currentIndex = itemsinline1.indexOf(selecteddisc.id)
itemsinline1[currentIndex] = ""
if(itemsinline2[2] == ''){
itemsinline2[2] = selecteddisc.id
}
else if(itemsinline2[1] == ''){
itemsinline2[1] = selecteddisc.id
}
else if(itemsinline2[0] == ''){
itemsinline2[0] = selecteddisc.id
}
}
else{
alert("There is a disc above!")
}
}
else if(selected_disc_container == "line2"){
if((itemsinline2[itemsinline2.indexOf(selecteddisc.id)-1] == "") || (itemsinline2[itemsinline2.indexOf(selecteddisc.id)-1] == undefined)){
line2.appendChild(selecteddisc) // Insert the selected disc!
number_of_moves = number_of_moves - 1
numberofmovesdisplay.innerText = number_of_moves
console.log(number_of_moves)
selecteddisc.setAttribute("disccontainerlinename","line2")
var currentIndex = itemsinline2.indexOf(selecteddisc.id)
itemsinline2[currentIndex] = ""
if(itemsinline2[2] == ''){
itemsinline2[2] = selecteddisc.id
}
else if(itemsinline2[1] == ''){
itemsinline2[1] = selecteddisc.id
}
else if(itemsinline2[0] == ''){
itemsinline2[0] = selecteddisc.id
}
}
else{
alert("There is a disc above!")
}
}
else if(selected_disc_container == "line3"){
if((itemsinline3[itemsinline3.indexOf(selecteddisc.id)-1] == "") || (itemsinline3[itemsinline3.indexOf(selecteddisc.id)-1] == undefined)){
line2.appendChild(selecteddisc) // Insert the selected disc!
number_of_moves = number_of_moves - 1
numberofmovesdisplay.innerText = number_of_moves
console.log(number_of_moves)
selecteddisc.setAttribute("disccontainerlinename","line2")
var currentIndex = itemsinline3.indexOf(selecteddisc.id)
itemsinline3[currentIndex] = ""
if(itemsinline2[2] == ''){
itemsinline2[2] = selecteddisc.id
}
else if(itemsinline2[1] == ''){
itemsinline2[1] = selecteddisc.id
}
else if(itemsinline2[0] == ''){
itemsinline2[0] = selecteddisc.id
}
}
else{
alert("There is a disc above!")
}
}
}
}
if(line == 'line3'){
var selected_disc_container = selecteddisc.getAttribute("disccontainerlinename")
if(selected_disc_container == line){
alert("YOU CAN'T PLACE THE SAME DISC ON THE SAME LINE!")
}
else{
if(selected_disc_container == "line1"){
if((itemsinline1[itemsinline1.indexOf(selecteddisc.id)-1] == "") || (itemsinline1[itemsinline1.indexOf(selecteddisc.id)-1] == undefined)){
line3.appendChild(selecteddisc) // Insert the selected disc!
number_of_moves = number_of_moves - 1
numberofmovesdisplay.innerText = number_of_moves
console.log(number_of_moves)
selecteddisc.setAttribute("disccontainerlinename","line3")
var currentIndex = itemsinline1.indexOf(selecteddisc.id)
itemsinline1[currentIndex] = ""
if(itemsinline3[2] == ''){
itemsinline3[2] = selecteddisc.id
}
else if(itemsinline3[1] == ''){
itemsinline3[1] = selecteddisc.id
}
else if(itemsinline3[0] == ''){
itemsinline3[0] = selecteddisc.id
}
}
else{
alert("There is a disc above!")
}
}
else if(selected_disc_container == "line2"){
if((itemsinline2[itemsinline2.indexOf(selecteddisc.id)-1] == "") || (itemsinline2[itemsinline2.indexOf(selecteddisc.id)-1] == undefined)){
line3.appendChild(selecteddisc) // Insert the selected disc!
number_of_moves = number_of_moves - 1
numberofmovesdisplay.innerText = number_of_moves
console.log(number_of_moves)
selecteddisc.setAttribute("disccontainerlinename","line3")
var currentIndex = itemsinline2.indexOf(selecteddisc.id)
itemsinline2[currentIndex] = ""
if(itemsinline3[2] == ''){
itemsinline3[2] = selecteddisc.id
}
else if(itemsinline3[1] == ''){
itemsinline3[1] = selecteddisc.id
}
else if(itemsinline3[0] == ''){
itemsinline3[0] = selecteddisc.id
}
}
else{
alert("There is a disc above!")
}
}
else if(selected_disc_container == "line3"){
if((itemsinline3[itemsinline3.indexOf(selecteddisc.id)-1] == "") || (itemsinline3[itemsinline3.indexOf(selecteddisc.id)-1] == undefined)){
line3.appendChild(selecteddisc) // Insert the selected disc!
number_of_moves = number_of_moves - 1
numberofmovesdisplay.innerText = number_of_moves
console.log(number_of_moves)
selecteddisc.setAttribute("disccontainerlinename","line3")
var currentIndex = itemsinline3.indexOf(selecteddisc.id)
itemsinline3[currentIndex] = ""
if(itemsinline3[2] == ''){
itemsinline3[2] = selecteddisc.id
}
else if(itemsinline3[1] == ''){
itemsinline3[1] = selecteddisc.id
}
else if(itemsinline3[0] == ''){
itemsinline3[0] = selecteddisc.id
}
}
else{
alert("There is a disc above!")
}}}}}}
winCheck()
}