-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
862 lines (794 loc) · 32.1 KB
/
app.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
//need this id for setInterval() to be
let rolling
// create function that rolls dice
const diceRoll = () => {
let diceNum
// begin dice rolling if clicked on, this will probably change when i add more to the flow of the game. this is more for testing purp
if (dice.innerText === ''){
rolling = setInterval(()=>{dice.innerText = Math.floor(Math.random() * 10)}, 100)
//make mario appear under dice
document.querySelector('.testPlayer').style.backgroundImage = 'url("https://i.etsystatic.com/16581340/r/il/fbb2d9/1423191149/il_1588xN.1423191149_2ygc.jpg")'
buttons[0].innerText = 'Roll'
//make items button disappear
buttons[1].style.visibility = 'hidden'
}
// if the dice is rolling, grab the value inside and console log it and stop rolling
else if (dice.innerText !== ''){
//grab the player under the dice and add the jump animation
document.querySelector('.testPlayer').classList.add('playerJump')
//make the dice roll at the top of the roll
setTimeout(() => {
document.querySelector('.dice').classList.add('diceRoll')
clearInterval(rolling)
console.log(dice.innerText)
diceNum = dice.innerText
}, 800)
//delay enough time for the move total to be clearly seen before character moves
setTimeout(() => {
dice.innerText = ''
document.querySelector('.testPlayer').style.backgroundImage = ''
}, 2000)
setTimeout(() => {
if (diceNum > 0){
move(mario, mario.position, diceNum)
} else if (diceNum === '0'){
computerRoll()
}
//make player options disappear
gameInfo.style.visibility = 'hidden'
buttons[0].style.visibility = 'hidden'
buttons[0].innerText = 'Move'
buttons[1].style.visibility = 'hidden'
document.querySelector('p').style.visibility = 'hidden'
}, 2000)
//remove the animation classes
setTimeout(() => {
document.querySelector('.testPlayer').classList.remove('playerJump')
document.querySelector('.dice').classList.remove('diceRoll')
}, 3000)
}
}
class Player {
constructor(position = 1, money = 0, stars = 0){
this.position = position
this.money = money
this.stars = stars
this.items = []
}
}
const mario = new Player()
const donkeyKong = new Player()
//this function will run when the game is loaded to assign squares with certain properties
//array of + money squares
const plusMoney = []
//array of - money squares
const minusMoney = []
//position of star
let starPos
//position of store
let storePos
//boolean to keep track of which turn
let playerMove = true
//number of turns
let turns = 0
const tileStyle = (pos, styleClass) => {
if (pos < 6){
let tileID = '#a' + `${pos}`
let tile = document.querySelector(tileID)
tile.classList.add(styleClass)
} else if (pos < 10){
let tileID = '#b' + `${pos}`
let tile = document.querySelector(tileID)
tile.classList.add(styleClass)
} else if (pos < 17){
let tileID = '#c' + `${pos}`
let tile = document.querySelector(tileID)
tile.classList.add(styleClass)
}
}
const boardSetup = () => {
//create 4 + money squares
for (let i = 0; i < 4; i++){
let pos = Math.floor(Math.random() * 15 + 1)
while(plusMoney.includes(pos)){
pos = Math.floor(Math.random() * 15 + 1)
}
plusMoney.push(pos)
//change styling of tiles
tileStyle(pos, 'plusMoney')
}
//create 4 - money squares, cannot overlap with + money
for (let i = 0; i < 4; i++){
let pos = Math.floor(Math.random() * 15 + 1)
while(plusMoney.includes(pos) || minusMoney.includes(pos)){
pos = Math.floor(Math.random() * 15 + 1)
}
minusMoney.push(pos)
tileStyle(pos, 'minusMoney')
}
//star tile that does not overlap
let foundStar = false
while (foundStar === false){
let pos = Math.floor(Math.random() * 15 + 1)
if (plusMoney.includes(pos) === false && minusMoney.includes(pos) === false){
starPos = pos
foundStar = true
}
}
tileStyle(starPos, 'star')
let foundStore = false
while (foundStore === false){
let pos = Math.floor(Math.random() * 15 + 1)
if (plusMoney.includes(pos) === false && minusMoney.includes(pos) === false && starPos !== pos){
storePos = pos
foundStore = true
}
}
tileStyle(storePos, 'store')
}
boardSetup()
const updateInfo = (character) => {
if (character === 'mario'){
player1Info.innerText = `Mario \n Coins: ${mario.money} \n Stars: ${mario.stars}`
} else if (character === 'donkeyKong'){
player2Info.innerText = `Donkey Kong \n Coins: ${donkeyKong.money} \n Stars: ${donkeyKong.stars}`
}
}
//function that tests what square a player lands on
const squareCheck = (square) => {
if (plusMoney.includes(square)){
mario.money += 10
gifBox.style.backgroundImage = 'url("https://media.tenor.com/images/35b07a9e7f2746fce6048419053d435c/tenor.gif")'
setTimeout(() => {
gifBox.style.backgroundImage = ""
}, 1500)
} else if (minusMoney.includes(square)){
if (mario.money - 2 < 0){
mario.money = 0
} else {
mario.money -= 2
}
gifBox.style.backgroundImage = 'url("https://i.kym-cdn.com/photos/images/newsfeed/001/149/747/a0a.gif")'
setTimeout(() => {
gifBox.style.backgroundImage = ""
}, 1500)
}
player1Info.innerText = `Mario \n Coins: ${mario.money} \n Stars: ${mario.stars}`
//trigger computer move when the final square is tested
computerRoll()
}
//function that checks if player passes a star
//needs to clearInterval and restart it with remaining moves when player makes a decision
//grab dice and event listener
const dice = document.querySelector('.dice')
dice.addEventListener('click', diceRoll)
//game info
const gameInfo = document.querySelector('.gameInfo')
//player 1 info
const player1Info = document.querySelector('#p1')
const player1 = document.querySelector('.player1')
//player 2 info
const player2Info = document.querySelector('#p2')
const player2 = document.querySelector('.player2')
//buttons
const buttons = document.querySelectorAll('button')
//gif box
const gifBox = document.querySelector('.gifBox')
buttons[0].addEventListener('click', diceRoll)
// buttons[1].addEventListener('click', chooseItem)
//variables necessary for move function
let startX
let endX
let startY
let endY
let position = 1
//variable to allow for restarting movement in star/store check functions
let remainingSquares
//player object
//create this variable (ID for moving setInterval) in global scope so that i can manipulate in other functions
let moving
const move = (character, position, numOfSquares) => {
if (character === mario){
startX = parseInt(window.getComputedStyle(player1).getPropertyValue('grid-column-start'))
endX = parseInt(window.getComputedStyle(player1).getPropertyValue('grid-column-end'))
startY = parseInt(window.getComputedStyle(player1).getPropertyValue('grid-row-start'))
endY = parseInt(window.getComputedStyle(player1).getPropertyValue('grid-row-end'))
remainingSquares = numOfSquares
//need this conditional to prevent the star/item functions from making an additional move when remainingSquares = 0
if (remainingSquares !== 0){
moving =setInterval(() => {
if (position < 5){
startX++
endX++
player1.style.gridColumnStart = `${startX}`
player1.style.gridColumnEnd = `${endX}`
position++
starAndStoreCheck(position)
} else if (position < 9){
startY++
endY++
player1.style.gridRowStart = `${startY}`
player1.style.gridRowEnd = `${endY}`
position++
starAndStoreCheck(position)
} else if (position < 13){
startX--
endX--
player1.style.gridColumnStart = `${startX}`
player1.style.gridColumnEnd = `${endX}`
position++
starAndStoreCheck(position)
} else if (position < 16){
startY--
endY--
player1.style.gridRowStart = `${startY}`
player1.style.gridRowEnd = `${endY}`
position++
starAndStoreCheck(position)
} else {
startX = 1
endX = 1
startY = 1
endY = 1
player1.style.gridColumnStart = `${startX}`
player1.style.gridColumnEnd = `${endX}`
player1.style.gridRowStart = `${startY}`
player1.style.gridRowEnd = `${endY}`
position = 1
starAndStoreCheck(position)
}
numOfSquares--
remainingSquares--
if (character === mario){
mario.position = position
} else if (character === donkeyKong){
donkeyKong.position = position
}
if (numOfSquares <= 0){
clearInterval(moving)
if (character === mario){
mario.position = position
} else if (character === donkeyKong){
donkeyKong.position = position
}
if (position === storePos || position === starPos){
starAndStoreCheck(position, true)
} else {
squareCheck(position)
}
}
}, 500)
}
}
}
const computerStoreStarCheck = () => {
let pause
if (donkeyKong.position === starPos){
if (donkeyKong.money >= 20){
//show donkey kong banana gif
gifBox.style.backgroundImage = 'url("https://www.armortechs.com/upload/image/dkc_banana_hoard.gif")'
donkeyKong.money -= 20
donkeyKong.stars += 1
// get rid of banana gif
setTimeout(() => {
gifBox.style.backgroundImage = ""
}, 2000)
pause = true
}
} else if (donkeyKong.position === storePos){
if (donkeyKong.money >= 5){
//show donkey kong banana gif
gifBox.style.backgroundImage = 'url("https://i.gifer.com/wUH.gif")'
donkeyKong.money -= 5
donkeyKong.items.push('greenShell')
fillInventory('donkeyKong')
//get rid of gif
setTimeout(() => {
gifBox.style.backgroundImage = ""
}, 2000)
//make sure donkey kong will use the item at the start of their turn
pause = true
}
}
updateInfo('donkeyKong')
return pause
}
const computerSquareCheck = () => {
if (plusMoney.includes(donkeyKong.position)){
donkeyKong.money += 10
gifBox.style.backgroundImage = 'url("https://media.tenor.com/images/b1b1756bffd985a7ff26e91a21e09804/tenor.gif")'
setTimeout(() => {
gifBox.style.backgroundImage = ""
}, 1500)
} else if (minusMoney.includes(donkeyKong.position)){
if (donkeyKong.money - 2 < 0) {
donkeyKong.money = 0
} else {
donkeyKong.money -= 2
}
gifBox.style.backgroundImage = 'url("https://thumbs.gfycat.com/IllustriousAlertFieldspaniel-max-1mb.gif")'
setTimeout(() => {
gifBox.style.backgroundImage = ""
}, 1500)
}
updateInfo('donkeyKong')
}
//function to see if donkey kong has any items, and if he does, use one
const computerItemCheck = () => {
if (donkeyKong.items.includes('greenShell')){
gifBox.style.backgroundImage = 'url("https://64.media.tumblr.com/tumblr_m1k2zhACXQ1qcnzaso1_500.gifv")'
if (mario.money - 10 <= 0){
mario.money = 0
} else {
mario.money -= 10
}
donkeyKong.items.pop()
let items = document.querySelectorAll('.DKitems')
for (let i = items.length - 1; i >= 0; i--){
if (items[i].classList.contains('greenShell')){
items[i].classList.remove('greenShell')
donkeyKong.items.pop()
i = -1
updateInfo('donkeyKong')
}
}
console.log('donkey kong uses an item')
setTimeout(() => {
gifBox.style.backgroundImage = ""
}, 1500)
}
updateInfo('donkeyKong')
updateInfo('mario')
}
let dkMoving
const computerRoll = () => {
computerItemCheck()
//make donkey kong appear beneath dice
document.querySelector('.testPlayer').style.backgroundImage = "url('https://d3gqasl9vmjfd8.cloudfront.net/0b2aa3ba-f820-4ed0-8969-281d5dbf7507.png')"
//make dice roll
let rolling
let diceNum
//make dice roll
rolling = setInterval(()=>{dice.innerText = Math.floor(Math.random() * 10)}, 100)
setTimeout(() => {
//make donkey kong jump
document.querySelector('.testPlayer').classList.add('playerJump')
}, 1000)
setTimeout(() => {
dice.classList.add('diceRoll')
clearInterval(rolling)
diceNum = dice.innerText
computerMove(diceNum)
}, 1800)
//clear dice text, donkey kong image, animation classes
setTimeout(() => {
dice.innerText = ''
document.querySelector('.testPlayer').classList.remove('playerJump')
dice.classList.remove('diceRoll')
document.querySelector('.testPlayer').style.backgroundImage = ""
}, 3000)
//delay move for after all of this complete
}
const computerMove = (diceNum) => {
setTimeout(() => {
console.log('Donkey kong rolled a ', diceNum)
startX = parseInt(window.getComputedStyle(player2).getPropertyValue('grid-column-start'))
endX = parseInt(window.getComputedStyle(player2).getPropertyValue('grid-column-end'))
startY = parseInt(window.getComputedStyle(player2).getPropertyValue('grid-row-start'))
endY = parseInt(window.getComputedStyle(player2).getPropertyValue('grid-row-end'))
//variable to decide if donkey kong needs to pause
let pause
if (diceNum !== '0'){
console.log('dk diceNum not equal to 0')
dkMoving = setInterval(() => {
if (donkeyKong.position < 5){
startX++
endX++
player2.style.gridColumnStart = `${startX}`
player2.style.gridColumnEnd = `${endX}`
donkeyKong.position++
//computer store check?
pause = computerStoreStarCheck()
} else if (donkeyKong.position < 9){
startY++
endY++
player2.style.gridRowStart = `${startY}`
player2.style.gridRowEnd = `${endY}`
donkeyKong.position++
//computer store check?
pause = computerStoreStarCheck()
} else if (donkeyKong.position < 13){
startX--
endX--
player2.style.gridColumnStart = `${startX}`
player2.style.gridColumnEnd = `${endX}`
donkeyKong.position++
pause = computerStoreStarCheck()
// computer store check?
} else if (donkeyKong.position < 16){
startY--
endY--
player2.style.gridRowStart = `${startY}`
player2.style.gridRowEnd = `${endY}`
donkeyKong.position++
pause = computerStoreStarCheck()
//computer store check?
} else {
startX = 1
endX = 1
startY = 1
endY = 1
player2.style.gridColumnStart = `${startX}`
player2.style.gridColumnEnd = `${endX}`
player2.style.gridRowStart = `${startY}`
player2.style.gridRowEnd = `${endY}`
donkeyKong.position = 1
//computer store check?
pause = computerStoreStarCheck()
}
diceNum--
if (pause){
clearInterval(dkMoving)
setTimeout(() => {
computerMove(diceNum)
}, 2000)
}
if (diceNum <= 0){
clearInterval(dkMoving)
//computer store and square check?
computerSquareCheck()
gameInfo.style.visibility = 'visible'
buttons[0].style.visibility = 'visible'
buttons[1].style.visibility = 'visible'
document.querySelector('p').style.visibility = 'visible'
if (turns === 20){
buttons[0].style.visibility = 'hidden'
buttons[1].style.visibility = 'hidden'
}
}
}, 500)
turns++
//put end game process hear
console.log('dk diceNum: ', diceNum)
console.log('Turns: ', turns)
if (turns >= 20){
//compare amount of stars for win
if (mario.stars > donkeyKong.stars){
document.querySelector('p').innerText = 'Game Over, Mario wins!'
gifBox.style.backgroundImage = 'url("https://thumbs.gfycat.com/GrippingDependentIvorybilledwoodpecker-size_restricted.gif")'
setTimeout(() => {
gifBox.style.backgroundImage = ""
}, 3000)
} else if (mario.stars < donkeyKong.stars){
document.querySelector('p').innerText = 'Game Over, Donkey Kong wins!'
gifBox.style.backgroundImage = 'url("https://i.kym-cdn.com/photos/images/newsfeed/000/829/805/b76.gif")'
setTimeout(() => {
gifBox.style.backgroundImage = ""
}, 3000)
} else {
document.querySelector('p').innerText = "Game Over, it's a tie!"
gifBox.style.backgroundImage = 'url("https://img3.goodfon.com/wallpaper/nbig/c/1c/donkey-kong-mario-pivo-bochka.jpg")'
setTimeout(() => {
gifBox.style.backgroundImage = ""
}, 3000)
}
}
//make player options reappear, whether or not dk rolled a 0
} else {
gameInfo.style.visibility = 'visible'
buttons[0].style.visibility = 'visible'
buttons[1].style.visibility = 'visible'
document.querySelector('p').style.visibility = 'visible'
}
}, 1500)
}
const starAndStoreCheck = (pos, lastMove = false) => {
if (pos === starPos){
console.log('starcheck')
//stop movement
clearInterval(moving)
//can i determine how many more squares left to move from here?
document.querySelector('p').innerText = "Buy a star for 20 coins?"
document.querySelector('p').style.visibility = 'visible'
gameInfo.style.visibility = 'visible'
buttons[0].innerText = 'Yes'
buttons[1].innerText = 'No'
gameInfo.visibility = 'visible'
buttons[0].style.visibility = 'visible'
buttons[1].style.visibility = 'visible'
//need to remove existing event listeners and place new ones, then put back old event listener when done with this logic
buttons[0].removeEventListener('click', diceRoll)
buttons[1].removeEventListener('click', chooseItem)
if (lastMove === true){
//need to restart movement in buyStar, declineStar functions
buttons[0].addEventListener('click', buyStarThenCPMove)
buttons[1].addEventListener('click', declineStarThenCPMove)
} else if (lastMove === false){
//need to restart movement in buyStar, declineStar functions
buttons[0].addEventListener('click', buyStar)
buttons[1].addEventListener('click', declineStar)
}
// if (lastMove === true){
// computerRoll()
// }
} else if (pos === storePos){
console.log('storeCheck')
clearInterval(moving)
document.querySelector('p').innerText = 'Buy an item?'
document.querySelector('p').style.visibility = 'visible'
gameInfo.style.visibility = 'visible'
buttons[0].innerText = 'Green Shell - 5 Coins'
buttons[1].innerText = 'No'
buttons[0].style.visibility = 'visible'
buttons[1].style.visibility = 'visible'
//event listener swap that allows for purchase of items
buttons[0].removeEventListener('click', diceRoll)
buttons[1].removeEventListener('click', chooseItem)
if (lastMove === false){
//need to restart movement in buyItem, declineItem functions
buttons[0].addEventListener('click', buyItem)
buttons[1].addEventListener('click', declineItem)
} else if (lastMove === true){
buttons[0].addEventListener('click', buyItemThenCPMove)
buttons[1].addEventListener('click', declineItemThenCPMove)
}
// if (lastMove === true){
// computerRoll()
// }
}
}
const buyStarThenCPMove = () => {
if (mario.money >= 20){
mario.money -= 20
mario.stars += 1
//revert game info box
changeStarToMove(true)
updateInfo('mario')
move(mario, mario.position, remainingSquares)
gifBox.style.backgroundImage = 'url("https://media3.giphy.com/media/iEs4yXq5rVlL2/200.gif")'
setTimeout(() => {
document.querySelector('p').innerText = 'Your move'
document.querySelector('p').style.visibility = 'hidden'
gifBox.style.backgroundImage = ""
move(mario, mario.position, remainingSquares)
}, 1500)
} else {
document.querySelector('p').innerText = 'Not enough cash!'
document.querySelector('p').style.visibility = 'visible'
changeStarToMove(true)
gifBox.style.backgroundImage = 'url("https://thumbs.gfycat.com/MellowParallelClingfish-size_restricted.gif")'
setTimeout(() => {
document.querySelector('p').innerText = 'Your move'
document.querySelector('p').style.visibility = 'hidden'
gifBox.style.backgroundImage = ""
move(mario, mario.position, remainingSquares)
}, 1500)
}
gameInfo.style.visibility = 'hidden'
buttons[0].style.visibility = 'hidden'
buttons[1].style.visibility = 'hidden'
computerRoll()
}
const declineStarThenCPMove = () => {
changeStarToMove(true)
// move(mario, mario.position, remainingSquares)
gameInfo.style.visibility = 'hidden'
buttons[0].style.visibility = 'hidden'
buttons[1].style.visibility = 'hidden'
computerRoll()
}
const buyItemThenCPMove = () => {
if (mario.money >= 5) {
buttons[0].removeEventListener('click', buyItemThenCPMove)
mario.money -= 5
mario.items.push('greenShell')
//function to update inventory div
fillInventory('mario')
//change buttons and event listeners
changeItemToMove(true)
move(mario, mario.position, remainingSquares)
} else {
document.querySelector('p').innerText = 'Not enough cash!'
document.querySelector('p').style.visibility = 'visible'
changeItemToMove(true)
gifBox.style.backgroundImage = 'url("https://thumbs.gfycat.com/MellowParallelClingfish-size_restricted.gif")'
setTimeout(() => {
document.querySelector('p').innerText = 'Your move'
document.querySelector('p').style.visibility = 'hidden'
gifBox.style.backgroundImage = ""
move(mario, mario.position, remainingSquares)
}, 1500)
}
updateInfo('mario')
gameInfo.style.visibility = 'hidden'
buttons[0].style.visibility = 'hidden'
buttons[1].style.visibility = 'hidden'
computerRoll()
}
const declineItemThenCPMove = () => {
changeItemToMove(true)
move(mario, mario.position, remainingSquares)
gameInfo.style.visibility = 'hidden'
document.querySelector('p').style.visibility = 'hidden'
buttons[0].style.visibility = 'hidden'
buttons[1].style.visibility = 'hidden'
computerRoll()
}
//use green shell if available
const chooseItem = () => {
let items = document.querySelectorAll('.items')
for (let i = items.length - 1; i >= 0; i--){
if (items[i].classList.contains('greenShell')){
gifBox.style.backgroundImage = 'url("https://pa1.narvii.com/6357/276364aba4b4f524766d1252bd53386cd4792917_00.gif")'
if (donkeyKong.money - 10 >= 0){
donkeyKong.money -= 10
} else {
donkeyKong.money = 0
}
items[i].classList.remove('greenShell')
mario.items.pop()
i = -1
updateInfo('donkeyKong')
setTimeout(() => {
gifBox.style.backgroundImage = ""
}, 1500)
diceRoll()
}
}
}
//some utility functions to change info in game info box
const changeStarToMove = (cpMove) => {
if (cpMove === false){
buttons[0].removeEventListener('click', buyStar)
buttons[1].removeEventListener('click', declineStar)
} else if (cpMove === true){
buttons[0].removeEventListener('click', buyStarThenCPMove)
buttons[1].removeEventListener('click', declineStarThenCPMove)
}
buttons[0].addEventListener('click', diceRoll)
buttons[1].addEventListener('click', chooseItem)
buttons[0].innerText = 'Move'
buttons[1].innerText = 'Item'
// document.querySelector('p').style.visibility = 'hidden'
gameInfo.style.visibility = 'hidden'
buttons[0].style.visibility = 'hidden'
buttons[1].style.visibility = 'hidden'
}
const buyStar = () => {
if (mario.money >= 20){
mario.money -= 20
mario.stars += 1
//revert game info box
changeStarToMove(false)
updateInfo('mario')
move(mario, mario.position, remainingSquares)
gifBox.style.backgroundImage = 'url("https://media3.giphy.com/media/iEs4yXq5rVlL2/200.gif")'
setTimeout(() => {
gifBox.style.backgroundImage = ""
document.querySelector('p').innerText = 'Your move'
document.querySelector('p').style.visibility = 'hidden'
}, 1500)
} else {
document.querySelector('p').innerText = 'Not enough cash!'
document.querySelector('p').style.visibility = 'visible'
changeStarToMove(false)
gifBox.style.backgroundImage = 'url("https://thumbs.gfycat.com/MellowParallelClingfish-size_restricted.gif")'
setTimeout(() => {
document.querySelector('p').innerText = 'Your move'
document.querySelector('p').style.visibility = 'hidden'
gifBox.style.backgroundImage = ""
move(mario, mario.position, remainingSquares)
}, 1500)
}
gameInfo.style.visibility = 'hidden'
buttons[0].style.visibility = 'hidden'
buttons[1].style.visibility = 'hidden'
}
const declineStar = () => {
changeStarToMove(false)
move(mario, mario.position, remainingSquares)
document.querySelector('p').innerText = 'Your move'
gameInfo.style.visibility = 'hidden'
document.querySelector('p').style.visibility = 'hidden'
buttons[0].style.visibility = 'hidden'
buttons[1].style.visibility = 'hidden'
}
const changeItemToMove = (cpMove) => {
if (cpMove === false){
buttons[0].removeEventListener('click', buyItem)
buttons[1].removeEventListener('click', declineItem)
} else if (cpMove === true){
buttons[0].removeEventListener('click', buyItemThenCPMove)
buttons[1].removeEventListener('click', declineItemThenCPMove)
}
buttons[0].addEventListener('click', diceRoll)
buttons[1].addEventListener('click', chooseItem)
buttons[0].innerText = 'Move'
buttons[1].innerText = 'Item'
gameInfo.style.visibility = 'hidden'
buttons[0].style.visibility = 'hidden'
buttons[1].style.visibility = 'hidden'
}
const buyItem = () => {
if (mario.money >= 5) {
mario.money -= 5
mario.items.push('greenShell')
//function to update inventory div
fillInventory('mario')
//change buttons and event listeners
changeItemToMove(false)
move(mario, mario.position, remainingSquares)
document.querySelector('p').innerText = 'Your move'
document.querySelector('p').style.visibility = 'hidden'
} else {
document.querySelector('p').innerText = 'Not enough cash!'
document.querySelector('p').style.visibility = 'visible'
changeItemToMove(false)
gifBox.style.backgroundImage = 'url("https://thumbs.gfycat.com/MellowParallelClingfish-size_restricted.gif")'
setTimeout(() => {
gifBox.style.backgroundImage = ""
document.querySelector('p').innerText = 'Your move'
document.querySelector('p').style.visibility = 'hidden'
move(mario, mario.position, remainingSquares)
}, 1500)
}
updateInfo('mario')
gameInfo.style.visibility = 'hidden'
buttons[0].style.visibility = 'hidden'
buttons[1].style.visibility = 'hidden'
}
const fillInventory = (character) => {
if (character === 'mario'){
let items = document.querySelectorAll('.items')
console.log(items[0].classList)
for (let i = 0; i < items.length; i++){
if (items[i].classList.contains('greenShell') !== true){
items[i].classList.add('greenShell')
i = items.length
}
}
} else if (character === 'donkeyKong'){
let items = document.querySelectorAll('.DKitems')
console.log(items)
for (let i = 0; i < items.length; i++){
if (items[i].classList.contains('greenShell') !== true){
items[i].classList.add('greenShell')
i = items.length
}
}
}
}
const declineItem = () => {
changeItemToMove(false)
move(mario, mario.position, remainingSquares)
document.querySelector('p').innerText = 'Your move'
document.querySelector('p').style.visibility = 'hidden'
gameInfo.style.visibility = 'hidden'
buttons[0].style.visibility = 'hidden'
buttons[1].style.visibility = 'hidden'
}
const turnOptions = () => {
// change text in game info box
document.querySelector('p').innerText = 'Your Turn!'
gameInfo.style.visibility = 'visible'
buttons[0].style.visibility = 'visible'
buttons[1].style.visibility = 'visible'
// make the Move and Item buttons appear
for (let i = 0; i < buttons.length; i++){
buttons[i].style.visibility = 'visible'
}
}
//function to display game start gif
const gameStart = () => {
gifBox.style.backgroundImage = 'url("https://www.lukiegames.com/assets/images/N64/n64_mario_party_p_4p6j0j.jpg")'
setTimeout(() => {
gifBox.style.backgroundImage = ''
}, 2000)
}
window.addEventListener('DOMContentLoaded', () => {
gameStart()
setTimeout(() => {
turnOptions()
}, 2000)
})
//function to display up to three items where the move and item buttons usually are