-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_test.js
462 lines (317 loc) · 12.6 KB
/
script_test.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// if (window.matchMedia("(orientation: portrait)").matches) {
// // you're in PORTRAIT mode
// alert("Please turn your device to landscape mode!");
// }
// code for working of dice
let dice = document.getElementById('dice')
dice.addEventListener('click', rollDice)
function generateRandom(min, max)
{
return Math.ceil(Math.random() * (max - min) + min)
}
let dice_img = document.getElementById('dice_img')
let degree = 180
let dice_value = 1
// creating HTMLAudioObjects using Audio() constructor
// const diceRollSound = new Audio('sounds/dice_roll-sound.mp3')
const ladderClimbSound = new Audio('sounds/ladder_climb-sound.mp3')
const snakeBiteSound = new Audio('sounds/snake_bite-sound.mp3')
const victorySound = new Audio('sounds/victory-sound.mp3')
const jumpSound = new Audio('sounds/piece_move-sound.mp3')
const confettiButton = document.getElementsByClassName('confetti-button')[0]
const message = document.getElementById('message')
const loading_animation = document.createElement('img')
loading_animation.src = "images/loading_animation.gif"
loading_animation.id = "loading-animation"
const query1 = window.matchMedia("(max-width: 1200px) and (orientation: landscape)")
const setAccToMediaQuery1 = (query, called_from) => {
if(called_from == "rollDice"){
if(query.matches){
message.style.paddingTop = "140px";
}
else{
message.style.paddingTop = "70px";
}
}
else if(called_from == "toggleMessage"){
if(query.matches){
message.style.paddingTop = "10px";
}
else{
message.style.paddingTop = "0px";
}
}
}
function rollDice()
{
dice.disabled = true
message.appendChild(loading_animation)
setAccToMediaQuery1(query1, "rollDice")
// creating a new object of the HTMLAudioObject everytime rollDice function is called,
// so that even if the sound was already playing, it is played from the start
new Audio('sounds/dice_roll-sound.mp3').play()
dice_value = generateRandom(0, 6)
dice_img.src = "images/dice_"+dice_value+".png"
dice.style.transform = "rotateX("+degree+"deg) rotateY("+degree+"deg)"
degree += 180
setTimeout(performAction, 1000)
}
// for testing purposes
// setInterval(() => {
// if(dice.disabled == false){
// rollDice();
// }
// }, 2000)
let red_player_pos = 1
let blue_player_pos = 1
// code to maintain chance of every player
let i = 1
// get the object reference of the red player icon
const red_player_icon = document.getElementById('red_player_icon')
// get the object reference of the blue player icon
const blue_player_icon = document.getElementById('blue_player_icon')
function toggleMessage()
{
setAccToMediaQuery1(query1, "toggleMessage")
if(i % 2 == 1)
{
message.innerText = "Blue Player's Turn"
message.style.color = "blue"
}
else
{
message.innerText = "Red Player's Turn"
message.style.color = "red"
}
}
async function performAction()
{
if(i % 2 == 1) // red player's turn
{
const target_pos = red_player_pos + dice_value
if(target_pos <= 100)
{
for(let pos = red_player_pos; pos < target_pos; pos++)
{
// create a promise that resolves after 500ms,
// so that the execution of the loop is paused until every 'setTimeout' delay is completed
// this is done to ensure there is no dom manipulation errors, which would have been
// produced due to the asyncronous nature of JavaScript
await new Promise(resolve => setTimeout(resolve, 500)) // wait for 500ms (0.5s)
removeFromAndMovePlayerTo(pos, pos+1)
// red_player_pos = pos
jumpSound.play()
}
// check if the player has reached the 100 position with the exact number of dice value
if(target_pos == 100)
{
// change the message, and its color
message.innerText = "Red Player Won!"
message.style.color = "red"
playerWon()
}
}
else
{
toggleMessage()
}
if(target_pos < 100)
{
// check for any snake or ladder at the target index
ActionIfSnakeOrLadder(target_pos)
if(red_player_pos !== 100)
{
// change the turn message, and its color
toggleMessage()
}
}
}
else // blue player's turn
{
const target_pos = blue_player_pos + dice_value
if(target_pos <= 100)
{
for(let pos = blue_player_pos; pos < target_pos; pos++)
{
// create a promise that resolves after 500ms,
// so that the execution of the loop is paused until every 'setTimeout' delay is completed
// this is done to ensure there is no dom manipulation errors, which would have been
// produced due to the asyncronous nature of JavaScript
await new Promise(resolve => setTimeout(resolve, 500))
removeFromAndMovePlayerTo(pos, pos+1)
// blue_player_pos = pos
jumpSound.play()
}
// check if the player has reached the 100 position with the exact number of dice value
if(target_pos == 100)
{
// change the message, and its color
message.innerText = "Blue Player Won!"
message.style.color = "blue"
playerWon()
}
}
else
{
toggleMessage()
}
if(target_pos < 100)
{
// check for any snake or ladder at the target index
ActionIfSnakeOrLadder(target_pos)
if(blue_player_pos !== 100)
{
// change the turn message, and its color
toggleMessage()
}
}
}
if(blue_player_pos != 100 && red_player_pos != 100)
{
dice.disabled = false
}
i++
}
// code to achieve the functionality of snakes and ladders
// creating a map to store the starting and ending points of the ladders as key-value pairs.
const ladders_map = new Map([[1, 38], [4, 14], [9, 31], [21, 42], [28, 84], [51, 67], [71,91], [80, 100]])
// creating a map to store the starting and ending points of the snakes as key-value pairs.
const snakes_map = new Map([[17, 7], [62, 19], [54, 34], [64, 60], [87, 24], [93, 73], [95, 75], [98, 79]])
function ActionIfSnakeOrLadder(cell_number)
{
console.log(cell_number)
if(ladders_map.has(cell_number)) // check if the cell has a ladder on it
{
const new_cell_number = Number(ladders_map.get(cell_number))
console.log("You just stepped on a ladder! Jumping to cell "+new_cell_number)
ladderClimbSound.play()
console.log("red player pos ",red_player_pos,"blue player pos ", blue_player_pos)
// climbUpOrSlideDown("ladder", cell_number, new_cell_number)
removeFromAndMovePlayerTo(cell_number, new_cell_number)
console.log("new red player pos ",red_player_pos,"new blue player pos ", blue_player_pos)
if(red_player_pos == 100)
{
// change the turn message, and its color
message.innerText = "Red Player Won!"
message.style.color = "red"
playerWon()
}
else if(blue_player_pos == 100)
{
// change the turn message, and its color
message.innerText = "Blue Player Won!"
message.style.color = "blue"
playerWon()
}
}
else if(snakes_map.has(cell_number)) // check if the cell has a snake on it
{
const new_cell_number = Number(snakes_map.get(cell_number))
console.log("You just stepped on a snake! Jumping to cell "+new_cell_number)
snakeBiteSound.play()
// climbUpOrSlideDown("snake", cell_number, new_cell_number)
removeFromAndMovePlayerTo(cell_number, new_cell_number)
}
}
// const snakesPathMap = new Map([17, [16, 5, 6]], [54, [48, 34]], [62, [58, 42, 43, 38, 23, 18, 19]], [64, [63, 62, 59, 60]], [87, [74, 67, 66, 55, 46, 45, 44, 36, 25, 24]], [93, [88, 72, 73]], [95, [87, 86, 75]], [98, [83, 78, 79]])
// async function climbUpOrSlideDown(snake_or_ladder, current_cell_number, target_cell_number)
// {
// if(i % 2 == 1) // if it was red player's turn
// {
// if(target_cell_number <= 100)
// {
// // check if the player has reached the 100 position with the exact number of dice value
// for(let pos = current_cell_number; pos < target_cell_number; pos++)
// {
// // create a promise that resolves after 500ms,
// // so that the execution of the loop is paused until every 'setTimeout' delay is completed
// // this is done to ensure there is no dom manipulation errors, which would have been
// // produced due to the asyncronous nature of JavaScript
// await new Promise(resolve => setTimeout(resolve, 500)) // wait for 500ms (0.5s)
// removeFromAndMovePlayerTo(pos, pos+1)
// // red_player_pos = pos
// jumpSound.play()
// }
// if(target_cell_number == 100)
// {
// // change the message, and its color
// message.innerText = "Red Player Won!"
// message.style.color = "red"
// playerWon()
// }
// }
// else
// {
// toggleMessage()
// }
// if(target_cell_number < 100)
// {
// // change the turn message, and its color
// toggleMessage()
// }
// }
// else // if it was blue player's turn
// {
// if(target_cell_number <= 100)
// {
// // check if the player has reached the 100 position with the exact number of dice value
// for(let pos = current_cell_number; pos < target_cell_number; pos++)
// {
// // create a promise that resolves after 500ms,
// // so that the execution of the loop is paused until every 'setTimeout' delay is completed
// // this is done to ensure there is no dom manipulation errors, which would have been
// // produced due to the asyncronous nature of JavaScript
// await new Promise(resolve => setTimeout(resolve, 500)) // wait for 500ms (0.5s)
// removeFromAndMovePlayerTo(pos, pos+1)
// // blue_player_pos = pos
// jumpSound.play()
// }
// if(target_cell_number == 100)
// {
// // change the message, and its color
// message.innerText = "Blue Player Won!"
// message.style.color = "blue"
// playerWon()
// }
// }
// else
// {
// toggleMessage()
// }
// if(target_cell_number < 100)
// {
// // change the turn message, and its color
// toggleMessage()
// }
// }
// }
function removeFromAndMovePlayerTo(current_cell_number, target_cell_number)
{
let player_icon // to store the reference of the player icon
if(i % 2 == 1) // if it was red player's turn
{
player_icon = document.getElementById('red_player_icon')
red_player_pos = target_cell_number
}
else // if it was blue player's turn
{
player_icon = document.getElementById('blue_player_icon')
blue_player_pos = target_cell_number
}
// get the reference of the current cell
let current_cell = document.getElementById('_'+current_cell_number)
// remove the respective icon from the current cell
current_cell.removeChild(player_icon)
// get the reference of the new cell
const target_cell = document.getElementById("_"+target_cell_number)
// add the respective player icon to the target cell
target_cell.appendChild(player_icon)
}
function playerWon()
{
message.style.paddingTop = "0px"
confettiButton.click()
victorySound.play()
dice.disabled = true
return
}