-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
313 lines (211 loc) · 7.43 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
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
// 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)
}
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"
}
}
function performAction()
{
if(i % 2 === 1) // red player's turn
{
let target_cell = red_player_pos + dice_value;
// check if the player has reached the 100 position with the exact number of dice value
if(target_cell <= 100)
{
if(target_cell == 100)
{
// change the message, and its color
message.innerText = "Red Player Won!"
message.style.color = "red"
playerWon()
}
else
{
// change the message, and its color
toggleMessage()
}
const new_cell = removeFromAndMovePlayerTo(red_player_pos, target_cell)
jumpSound.play()
if(target_cell !== 100)
{
ActionIfSnakeOrLadder()
}
}
else
{
toggleMessage()
}
}
else // blue player's turn
{
let target_cell = blue_player_pos + dice_value;
// check if the player has reached the 100 position with the exact number of dice value
if(target_cell <= 100)
{
if(target_cell === 100)
{
// change the message, and its color
message.innerText = "Blue Player Won!"
message.style.color = "blue"
playerWon()
}
else
{
// change the turn message, and its color
toggleMessage()
}
const new_cell = removeFromAndMovePlayerTo(blue_player_pos, target_cell)
jumpSound.play()
if(target_cell !== 100)
{
ActionIfSnakeOrLadder(target_cell)
}
}
else
{
toggleMessage()
}
}
i++
if(blue_player_pos != 100 && red_player_pos != 100)
{
dice.disabled = false
}
}
// 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()
removeFromAndMovePlayerTo(cell_number, new_cell_number)
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()
removeFromAndMovePlayerTo(cell_number, new_cell_number)
}
}
function removeFromAndMovePlayerTo(prev_cell_number, new_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 = new_cell_number
}
else // if it was blue player's turn
{
player_icon = document.getElementById('blue_player_icon')
blue_player_pos = new_cell_number
}
// get the reference of the current cell
const prev_cell = document.getElementById('_'+prev_cell_number)
// remove the respective icon from the current cell
prev_cell.removeChild(player_icon)
// get the reference of the new cell
const new_cell = document.getElementById("_"+new_cell_number)
// add the respective player icon to the new cell
new_cell.appendChild(player_icon)
return new_cell
}
function playerWon()
{
message.style.paddingTop = "0px";
confettiButton.click()
victorySound.play()
dice.disabled = true
}