-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
1179 lines (985 loc) · 43.8 KB
/
index.html
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
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!--
Project: Snake and Ladder
Author: Harsh Vishwakarma
Email: [email protected]
Description: This is a game designed using HTML, CSS & JavaScipt.
In Start you have to choose Number of players and then you have to
choose in which manner snakes and ladder must appear,
Options available are Orgainzed and Random,
If you choose Organized then you have to select a board from different boards.
Or if you choose Random then you have to select number of snakes from 1-8 or you can make them random,
similarly you have to select number of ladder or you can make them random.
After that You have to Enter the Names and Color Preference for All the Players.
after doing all such things The Game will begin.
All players will get their chance one by one, If player gets 6 he will get another chance and
If Player comes in position of start of ladder it promotes to end of ladder or
If Player comes in position of head of snake it demotes to tail of ladder
You can Turn on/Off Music anytime and Check ScoreBoard of game
if any player reaches 100 he will won the and will not get any chance in the further game
After all player reaches 100 game will Show ScoreCard and game will End.
-->
<!DOCTYPE html>
<html>
<head>
<!-- Inline CSS for Game -->
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#canvasdiv {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
height: 100vh;
position: relative;
background-image: url('img/background.jpg');
background-position: center;
background-size: cover;
/* background: #ffff88; */
}
#startingZone {
width: 500px;
position: relative;
}
#test {
width: 500px;
position: relative;
}
#test>img {
position: absolute;
top: 0px;
transition: 1s ease;
}
#setting {
position: absolute;
/* float: right; */
top: 512px;
right: 0px;
transition: 1s ease;
}
#setting>button,
#setting>button>img {
border-radius: 50%;
outline: none;
padding: 2px;
cursor: pointer;
}
#startingZone img {
position: absolute;
/* top: 500px; */
transition: 1s ease;
}
#startingZone img:nth-child(1) {
left: 0px;
}
#startingZone img:nth-child(2) {
left: 20px;
}
#startingZone img:nth-child(3) {
left: 40px;
}
#startingZone img:nth-child(4) {
left: 60px;
}
.hidden {
display: none;
/* opacity: 0; */
}
#screen {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
color: black;
background: #ffff88;
flex-direction: row;
overflow:scroll;
}
#screen>img{
flex-grow: 3;
max-width: 350px;
}
#box{
flex-grow: 4;
max-width: 400px;
}
.alertbox {
display: flex;
justify-content: center;
align-items: center;
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100%;
background: rgba(0, 0, 0, 1);
}
.playerbox {
position: fixed;
height: 200px;
width: 200px;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.playerbox span {
font-size: 1.6em;
}
.playerbox button,
.playerbox button img {
border: none;
border-radius: 20px;
outline: none;
transition: top 0.5s ease;
}
.playerbox .activebutton:hover {
position: relative;
top: -2px;
box-shadow: 0px 5px 4px black;
cursor: pointer;
}
.topleft {
top: 0;
left: 0;
}
.bottomleft {
bottom: 0;
left: 0;
}
.topright {
top: 0;
right: 0;
}
.bottomright {
bottom: 0;
right: 0;
}
.disabled {
background: grey !important;
color: white;
}
@media screen and (max-width:800px) {
.playerbox span{
font-size: 0.8em;
}
}
</style>
<link rel="shortcut icon" href="img/favicon.png" type="image/x-icon">
<title>Snake & Ladder | Games | Harsh Vishwakarma</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- W3.CSS -->
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<!-- JQUERY -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<!-- Canvas Div -->
<div id="canvasdiv">
<!-- Test Div for Snakes and Ladders -->
<div id="test">
<!-- Snakes -->
<img src="img/snake01.png" class="hidden" id="snake1" data-down="4" data-left="0" data-side="1" data-bottom="4"
data-right="2" width="130px" data-headx="0">
<img src="img/snake03.png" class="hidden" id="snake2" data-down="3" data-left="0" data-side="2" data-bottom="3"
data-right="2" width="115px" data-headx="0">
<img src="img/snake04.png" class="hidden" id="snake3" data-down="3" data-left="0" data-side="1" data-bottom="3"
data-right="2" width="115px" data-headx="0">
<img src="img/snake05.png" class="hidden" id="snake4" data-down="2" data-left="0" data-side="0" data-bottom="2"
data-right="2" width="135px" data-headx="0">
<img src="img/snake06.png" class="hidden" id="snake5" data-down="2" data-left="0" data-side="0" data-bottom="2"
data-right="2" width="135px" data-headx="0">
<img src="img/snake07.png" class="hidden" id="snake6" data-down="4" data-left="0" data-side="0" data-bottom="4"
data-right="0" width="45px" data-headx="0">
<img src="img/snake01.png" class="hidden" id="snake7" data-down="4" data-left="0" data-side="1" data-bottom="4"
data-right="2" width="130px" data-headx="0">
<img src="img/snake03.png" class="hidden" id="snake8" data-down="3" data-left="0" data-side="2" data-bottom="3"
data-right="2" width="115px" data-headx="0">
<!-- <img src="img/snake02.png" class="hidden" id="snake2" data-down="5" data-left="2" data-side="-2" data-bottom="5" data-right="0" width="115px" data-headx="2"> -->
<!-- <img src="img/snake08.png" class="hidden" id="snake8" data-down="5" data-left="2" data-side="-2" data-bottom="5" data-right="0" width="115px" data-headx="2"> -->
<!-- Ladders -->
<img src="img/ladder01.png" id="ladder1" class="hidden" data-down="3" data-side="-1" data-left="2"
data-right="0" width="100px" data-headx="1">
<img src="img/ladder02.png" id="ladder2" class="hidden" data-down="3" data-side="0" data-left="0"
data-right="0" width="35px" data-headx="0">
<img src="img/ladder03.png" id="ladder3" class="hidden" data-down="2" data-side="1" data-left="0"
data-right="2" width="100px" data-headx="0">
<img src="img/ladder04.png" id="ladder4" class="hidden" data-down="1" data-side="-1" data-left="1"
data-right="0" width="69px" data-headx="1">
<img src="img/ladder05.png" id="ladder5" class="hidden" data-down="4" data-side="0" data-left="0"
data-right="0" width="50px" data-headx="0">
<img src="img/ladder06.png" id="ladder6" class="hidden" data-down="2" data-side="-1" data-left="2"
data-right="0" width="70px" data-headx="1">
<img src="img/ladder07.png" id="ladder7" class="hidden" data-down="2" data-side="0" data-left="0"
data-right="0" width="35px" data-headx="0">
<img src="img/ladder08.png" id="ladder8" class="hidden" data-down="1" data-side="0" data-left="0"
data-right="0" width="30px" data-headx="0">
<img src="img/ladder09.png" id="ladder9" class="hidden" data-down="2" data-side="-1" data-left="1"
data-right="0" width="100px" data-headx="1">
<img src="img/ladder10.png" id="ladder10" class="hidden" data-down="3" data-side="1" data-left="0"
data-right="1" width="100px" data-headx="0">
<!-- Settings | Play/Pause Sound and Show Score Card -->
<div id="setting">
<button onclick="playPauseMusic()"><img id="sound" src="img/sound-on.png" alt="Sound-Off"
width="30px"></button>
<button onclick="showScoreCard()"><img id="score" src="img/score.jpg" alt="Sound-Off" width="30px"></button>
</div>
</div>
<!-- Staring Zone where all players will Start -->
<div id="startingZone">
<!-- <img src="img/player1.png" alt="Red Player" width="50px">
<img src="img/player2.png" alt="Red Player" width="50px">
<img src="img/player3.png" alt="Red Player" width="50px">
<img src="img/player4.png" alt="Red Player" width="50px"> -->
</div>
<!-- Canvas | The Actual Board -->
<canvas id="myCanvas" width="500" height="500" style="border:5px solid #8d6e63;">
Your browser does not support the canvas element.
</canvas>
<!-- <div class="w3-panel w3-green w3-display-container">
<span onclick="this.parentElement.style.display='none'" class="w3-button w3-large w3-display-topright">×</span>
<h3>Warning!</h3>
<p>Yellow often indicates a warning that might need attention.</p>
</div> -->
</div>
<!-- <div id="id01" class="w3-modal" onclick="this.style.display='none'">
<div class="w3-modal-content w3-animate-zoom">
<header class="w3-container w3-green">
<span onclick="document.getElementById('id01').style.display='none'"
class="w3-button w3-display-topright">×</span>
<h2>Modal Header</h2>
<p>Modal Footer</p>
</header>
</div>
</div> -->
<!-- <div class=" playerbox topleft disabled">
<span>Player 1</span>
<button id="add" name="text" class="input button"><img id="gifbox" src="img/dice/rolldice.jpg"
width="100px"></button>
</div>
<div class="playerbox w3-green bottomleft">
<span>Player 1</span>
<button id="add" name="text" class="input button"><img id="gifbox" src="img/dice/rolldice.jpg"
width="100px"></button>
</div>
<div class="playerbox w3-blue bottomright">
<span>Player 1</span>
<button id="add" name="text" class="input button"><img id="gifbox" src="img/dice/rolldice.jpg"
width="100px"></button>
</div>
<div class="playerbox w3-yellow topright">
<span>Player 1</span>
<button id="add" name="text" class="input button"><img id="gifbox" src="img/dice/rolldice.jpg"
width="100px"></button>
</div> -->
<!-- <//screen> -->
<div id="screen" class="">
<img src="img/snl.png" alt="">
<div id="box">
<center id="center-box">
<h3>Select no. of Players</h3>
<select id="noofplayer" class="w3-select" name="option">
<option value="0" disabled selected>Choose your option</option>
<option value="1">1 Player</option>
<option value="2">2 Players</option>
<option value="3">3 Players</option>
<option value="4">4 Players</option>
</select>
<h3>Preference for Snake & Ladders Position</h3>
<input onchange="updatePreference()" class="w3-radio" id="org" type="radio" name="gamepreference"
value="organized">
<label for="org">Organized</label>
<input onchange="updatePreference()" class="w3-radio" id="rand" type="radio" name="gamepreference"
value="random">
<label for="rand">Random</label>
<div id="selection-organized" style="display: none;">
<h3>Choose Board</h3>
<input class="w3-radio" id="board1" type="radio" name="board" value="0">
<label for="board1"><img src="img/board1.jpg" width="100px" alt="Board 1"></label>
<input class="w3-radio" id="board2" type="radio" name="board" value="1">
<label for="board2"><img src="img/board2.jpg" width="100px" alt="Board 2"></label>
</div>
<div id="selection-random" style="display: none;">
<h3>No. of Snakes</h3>
<span id="snake-range-value">4</span>
<input class="w3-range" min="1" max="7" value="4"
onchange="document.getElementById('snake-range-value').innerHTML=this.value" id="range-snake"
type="range" name="range-snake" value="organized">
<input type="checkbox" name="checkbox-snake" id="checkbox-snake">
<label for="checkbox-snake">Random</label>
<h3>No. of Ladder</h3>
<span id="ladder-range-value">5</span>
<input class="w3-range" min="1" max="10" value="5"
onchange="document.getElementById('ladder-range-value').innerHTML=this.value" id="range-ladder"
type="range" name="range-ladder" value="random">
<input type="checkbox" name="checkbox-ladder" id="checkbox-ladder">
<label for="checkbox-ladder">Random</label>
</div>
<br><br><button onclick="updatePlayer()" class="w3-button w3-blue w3-round-xlarge">Next</button>
</center>
</div>
</div>
<script>
//Declaring all Variables
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var boardSize = 500;
var aspectRatio = document.body.clientWidth / document.body.clientHeight;
if(aspectRatio<=0.6){
var newBoardSize = 0.92 * document.body.clientWidth;
var playerBoxSize = (document.body.clientHeight - (newBoardSize*1.4))/2;
}else if(aspectRatio>=1.5){
var newBoardSize = 0.8 * document.body.clientHeight;
var playerBoxSize = (document.body.clientWidth - (newBoardSize * 1.5)) / 2;
}
else{
if(aspectRatio<=1){
var newBoardSize = 0.65 * document.body.clientWidth;
var playerBoxSize = (document.body.clientWidth - (newBoardSize*1.4)) / 2;
}else{
var newBoardSize = 0.65 * document.body.clientHeight;
var playerBoxSize = (document.body.clientWidth - (newBoardSize*1.4)) / 2;
}
}
// $('#myCanvas').height(newBoardSize+"px");
// $('#test').width(newBoardSize+"px");
var boardColumn = 10;
var boxSize = boardSize / boardColumn;
var oddBackground = "#2ab7ca";
var evenBackground = "#fed766";
var oddText = "#fff";
var evenText = "#000";
// var noOfSnakes = rand(1, 8);
// var noOfLadder = rand(1, 10);
var noOfSnakes = 0;
var noOfLadder = 0;
var snakeHead = [];
var snakeTail = [];
var ladderHead = [];
var ladderTail = [];
var noOfPlayer = 0;
var winnerNum = 1;
var gamePreference = "";
var boardNumber = 0;
var scoreNum = 1;
var positions = [["topleft"],
["topleft", "bottomright"],
["topleft", "bottomleft", "bottomright"],
["topleft", "bottomleft", "bottomright", "topright"]];
var colors = [];
var playerNames = [];
var playersPos = [];
var rollDice = new sound("sound/rolldice.mp3");
var snakeSound = new sound("sound/snake.mp3");
var ladderSound = new sound("sound/ladder.mp3");
var moveSound = new sound("sound/move.mp3");
var sixSound = new sound("sound/six.mp3");
var winnerSound = new sound("sound/winner.mp3");
var gameMusic = new sound("sound/bgm.mp3");
var gameSound = true;
var arrangements = [
{
snakeHead: [99, 65, 40, 25, 48, 78, 85],
ladderHead: [51, 59, 57, 17, 93, 90, 30, 80]
},
{
snakeHead: [62, 86, 98, 38],
ladderHead: [47, 60, 72, 93, 49, 66]
},
{
snakeHead: [62, 86, 98, 38],
ladderHead: [47, 60, 72, 93, 49, 66]
}]
// Function to disable Range when Snake Checkbox is active
$("#checkbox-snake").click(function () {
// If Snake-checkbox is checked
if ($(this).is(":checked")) {
$("#range-snake").prop('disabled', true);
$("#snake-range-value").hide();
} else {
$("#range-snake").prop('disabled', false);
$("#snake-range-value").show();
}
});
// Function to disable Range when Ladder Checkbox is active
$("#checkbox-ladder").click(function () {
// If Ladder-checkbox is checked
if ($(this).is(":checked")) {
$("#range-ladder").prop('disabled', true);
$("#ladder-range-value").hide();
} else {
$("#range-ladder").prop('disabled', false);
$("#ladder-range-value").show();
}
});
// Function to change data on Selection of Organized/Random
function updatePreference() {
// console.log("Iam in the function");
$('#selection-organized').slideUp();
$('#selection-random').slideUp();
// If gamepreference is organized
if ($('input[name="gamepreference"]:checked').val() == 'organized') {
$('#selection-organized').slideDown();
} else {
$('#selection-random').slideDown();
}
}
// Update noOfPlayer, gamePreference, boardNumber, noOfSnakes, noOfLadder
function updatePlayer() {
// If noOfPlayer is not Selected
if ($('#noofplayer').val() == null) {
// alert($('input[name="gamepreference"]:checked').val());
alert("Please Select No. of Players");
}
// If gamepreference is not selected
else if ($('input[name="gamepreference"]:checked').val() == undefined) {
alert("Please Select any Preference \nOrganized or Random");
}
// If board is not selected
else if ($('input[name="gamepreference"]:checked').val() == 'organized' && $('input[name="board"]:checked').val() == undefined) {
alert("Please Select a Board");
} else {
gamePreference = $('input[name="gamepreference"]:checked').val();
// If game preference is organized
if (gamePreference == 'organized') {
boardNumber = $('input[name="board"]:checked').val();
noOfLadder = arrangements[boardNumber]['ladderHead'].length;
noOfSnakes = arrangements[boardNumber]['snakeHead'].length;
} else {
// If Snake-Checkbox is checked
if ($('input[name="checkbox-snake"]').is(':checked')) {
noOfSnakes = rand(1, 8);
} else {
noOfSnakes = Number($('input[name="range-snake"]').val());
}
// If Ladder-Checkbox is checked
if ($('input[name="checkbox-ladder"]').is(':checked')) {
noOfLadder = rand(1, 10);
} else {
noOfLadder = Number($('input[name="range-ladder"]').val());
}
}
// assigning noOfPlayer
noOfPlayer = $('#noofplayer').val();
var string = "";
for (let i = 0; i < noOfPlayer; i++) {
string += '<input id="name-player' + (i + 1) + '" class="w3-input w3-border w3-round" type="text" placeholder="Player-' + (i + 1) + ' Name">\
<select class="w3-select" id="color-player'+ (i + 1) + '">\
<option value="0" selected disabled>Player-'+ (i + 1) + ' Color</option>\
<option value="red">Red</option>\
<option value="green">Green</option>\
<option value="blue">Blue</option>\
<option value="yellow">Yellow</option>\
</select><br><br>';
}
// changing center-box's content after storing data of all variables
$('#center-box').html(string + '<button onclick="updateNameColor()" class="w3-button w3-blue w3-round-xlarge">Start Game</button>');
// function for having only one color choice
// $("select").change(function (e) {
// var $s = $(e.target);
// $("select").not($s).find("option[value=" + $s.val() + "]").remove();
// });
}
}
//function for updating Player NAmes and desired colors
function updateNameColor() {
var found = false;
for (var i = 0; i < noOfPlayer; i++) {
// If player name was not Entered
if ($('#name-player' + (i + 1)).val() == "") {
alert("Please Enter Name of Player" + (i + 1))
found = true;
break;
}
// IF Color was not selected
else if (document.getElementsByClassName('w3-select')[i].value == "0") {
alert("Please Choose Color for Player" + (i + 1))
found = true;
break;
}
}
if (!found) {
for (var i = 0; i < noOfPlayer; i++) {
playerNames[i] = $('#name-player' + (i + 1)).val();
colors[i] = document.getElementsByClassName('w3-select')[i].value;
}
// hiding screen to start game
$('#screen').hide();
// Background music
$('body').append('<audio id="game-music" loop>\
<source src="sound/bgminloop.mp3" type="audio/mp3">\
Your browser does not support the audio element.\
</audio>');
document.getElementById('game-music').play();
//Initializing the Actual Game
startGame();
}
}
// Function to Play or pause Background Music
function playPauseMusic() {
// If its already running
if (gameSound) {
document.getElementById('game-music').pause();
$('#sound').prop('src', 'img/sound-off.png');
gameSound = false;
} else {
document.getElementById('game-music').play();
$('#sound').prop('src', 'img/sound-on.png');
gameSound = true;
}
}
// Function to give chance to specific player by playerNum
function giveChanceTo(playerNum) {
// If playerNum is greater than number of player then set playerNum to 1
if (playerNum > noOfPlayer) {
playerNum = 1;
}
// If he is already a winner {Player reached 100} give chance to another
if (playersPos[playerNum - 1] >= 100) {
giveChanceTo(playerNum + 1);
console.log("This user is winner")
} else {
// Disabling all player box
for (let i = 1; i <= noOfPlayer; i++) {
$("#playerbox" + i).removeClass("w3-" + colors[i - 1]);
$("#playerbox" + i + " button").removeClass("activebutton");
$("#playerbox" + i + " button").prop('disabled', true);
$("#playerbox" + i).addClass("disabled");
}
// MAking PlayerNum's Player Box Active
$("#playerbox" + playerNum).addClass("w3-" + colors[playerNum - 1]);
$("#playerbox" + playerNum + " button").addClass("activebutton");
$("#playerbox" + playerNum + " button").prop('disabled', false);
$("#playerbox" + playerNum).removeClass("disabled");
}
}
function doChance(playerNum) {
// disabling dice button
$("#playerbox" + playerNum + " button").prop("disabled", true);
var val = rand(1, 7); //taking value from 1 to 6 {7 is not excluded}
var path = "img/dice/" + val + ".jpg"; // path to dice image
// Rolling dice for 2 second
var now = new Date().getTime();
$("#playerbox" + playerNum + " button img").attr("src", "img/dice/roll.gif")
rollDice.play();
setTimeout(harsh, 2000,)
function harsh() {
console.log(val);
$("#playerbox" + playerNum + " button img").attr("src", path);
gameSound = moveSound;
playersPos[playerNum - 1] += val;
// If it is moving out of 100
if (playersPos[playerNum - 1] > 100) {
playersPos[playerNum - 1] -= val;
giveChanceTo(playerNum + 1);
return 0;
}
// If he wins {pos is 100}
else if (playersPos[playerNum - 1] == 100) {
gameSound = winnerSound;
// playersPos[playerNum - 1] = 110;
$("#playerbox" + playerNum + " button img").attr("src", "img/dice/winner" + winnerNum + ".jpg");
alertbox(playerNum, colors[playerNum - 1], "Winner", playerNames[playerNum - 1] + " Won the Game & got " + romanize(winnerNum) + " position");
if (winnerNum==noOfPlayer) {
gameSound.play();
showScoreCard();
return 0;
}
winnerNum += 1;
}
movePlayer(playerNum, playersPos[playerNum - 1]);
//detecting Ladder
if (ladderTail.includes(playersPos[playerNum - 1])) {
for (let p = 0; p < ladderTail.length; p++) {
const element = ladderTail[p];
if (element == playersPos[playerNum - 1]) {
gameSound = ladderSound;
playersPos[playerNum - 1] = ladderHead[p];
movePlayer(playerNum, playersPos[playerNum - 1]);
break;
}
}
console.log("ladder detected")
}
//detecting Snakes
if (snakeHead.includes(playersPos[playerNum - 1])) {
for (let p = 0; p < snakeTail.length; p++) {
const element = snakeHead[p];
if (element == playersPos[playerNum - 1]) {
gameSound = snakeSound;
playersPos[playerNum - 1] = snakeTail[p];
movePlayer(playerNum, playersPos[playerNum - 1]);
break;
}
}
console.log("snake detected")
}
// If Player gets 6
if (val == 6) {
if(gameSound!= snakeSound || gameSound != ladderSound || gameSound!=winnerSound){
gameSound = sixSound;
}
giveChanceTo(playerNum);
} else {
giveChanceTo(playerNum + 1);
}
gameSound.play();
}
}
// Function to display a alert {especially Winner}
function alertbox(num, color, title, message) {
$("body").prepend('<div id="id0' + num + '" class="w3-modal alertbox" style="background:rgba(0,0,0,0.9);" onclick="this.style.display=\'none\'"> \
<div class="w3-modal-content w3-animate-zoom w3-'+ color + '" style="padding:50px;text-align:center">\
<span onclick="document.getElementById(\'id0'+ num + '\').style.display=\'none\'"\
class="w3-button w3-display-topright">×</span>\
<h2>'+ title + '</h2>\
<p>'+ message + '</p>\
</div>\
</div>');
document.getElementById('id0' + num).style.display = 'block';
}
// Function to Show Score Card
function showScoreCard() {
var playersPosCopy = playersPos.slice();
var playersPosDup = playersPos.slice();
playersPosCopy.sort((a,b) => a-b);
playersPosCopy.reverse();
printArr(playersPos);
printArr(playersPosDup);
printArr(playersPosCopy);
var string = '';
for (let i = 0; i < noOfPlayer; i++) {
var x = playersPosDup.indexOf(playersPosCopy[i]);
// alert(playersPosCopy[i]+" "+x);
string += '<div class="w3-' + colors[x] + ' w3-padding-large w3-margin-top">' + romanize(i + 1) + ' - ' + playerNames[x] + ' is on ' + playersPos[x] + '</div>';
playersPosDup[x] = -1;
}
$("body").append('<div id="id' + scoreNum + '" class="w3-modal alertbox" style="background:rgba(0,0,0,0.9);" onclick="this.style.display=\'none\'"> \
<div class="w3-modal-content w3-animate-zoom w3-white" style="padding:50px;text-align:center">\
<center><h2>Score Card</h2></center>\
<span onclick="document.getElementById(\'id'+ scoreNum + '\').style.display=\'none\'"\
class="w3-button w3-display-topright">×</span>'+ string + '</div></div>');
document.getElementById('id' + scoreNum).style.display = 'block';
scoreNum += 1;
}
// Function for Sounds
function sound(src) {
this.sound = document.createElement("audio");
this.sound.src = src;
this.sound.setAttribute("preload", "auto");
this.sound.setAttribute("controls", "none");
this.sound.style.display = "none";
document.body.appendChild(this.sound);
this.play = function () {
this.sound.play();
}
this.stop = function () {
this.sound.pause();
}
this.inLoop = function () {
this.sound.loop = true;
}
}
//Initialiing the Game
function startGame() {
makeBoard();
// giving Chance to Player 1 in the beginning
giveChanceTo(1);
}
//Making Board
function makeBoard() {
var n = boardColumn * boardColumn;
//MAking Actual Board
for (let i = 1; i <= boardColumn; i++) {
for (let j = 1; j <= boardColumn; j++) {
if ((i % 2 == 0 && j % 2 == 1) || (i % 2 == 1 && j % 2 == 0)) {
ctx.fillStyle = evenBackground;
} else {
ctx.fillStyle = oddBackground;
}
ctx.fillRect(boxSize * j - boxSize, boxSize * i - boxSize, (boxSize * j), boxSize * i);
if ((i % 2 == 0 && j % 2 == 1) || (i % 2 == 1 && j % 2 == 0)) {
ctx.fillStyle = evenText;
} else {
ctx.fillStyle = oddText;
}
if (i % 2 == 0) {
var number = n + j - boardColumn;
} else {
var number = n - j + 1;
}
ctx.fillText(number, (boxSize * j) - boxSize + 10, (boxSize * i) - boxSize + 15);
}
n -= boardColumn;
}
boardSize = newBoardSize;
boxSize = boardSize / boardColumn;
//Plotting Snakes
for (var i = 1; i <= noOfSnakes; i++) {
if (gamePreference == 'organized') {
var pos = arrangements[boardNumber]["snakeHead"][i - 1];
} else {
var pos = getSnakePos(i);
}
renderSnake(i, pos);
snakeHead.push(pos);
snakeTail.push(getSnakeTail(i, pos));
function getSnakePos(snakeNum) {
var pos = rand(31, 99);
var snakeRight = Number($("#snake" + snakeNum).attr("data-right"));
var snakeBottom = Number($("#snake" + snakeNum).attr("data-bottom"));
//Shifting from Bottom and Left
var bottom = Math.ceil(pos / boardColumn) - 1;
if (snakeBottom >= bottom) { pos += boardColumn; }
//for left to right
if (Math.ceil(pos / boardColumn) % 2 != 0) {
var right = boardColumn - (pos - ((Math.ceil(pos / boardColumn) - 1) * boardColumn));
if (snakeRight >= right) { pos -= snakeRight; }
// console.log("pos = " + pos + " right=" + right + " snakeRight=" + snakeRight);
}
//for right to left
else {
var right = (pos % boardColumn) - 1;
if (snakeRight >= right) { pos += snakeRight; }
}
if (snakeHead.includes(pos)) {
return getSnakePos(snakeNum);
} else if (snakeTail.includes(pos)) {
return getSnakePos(snakeNum);
} else if (snakeHead.includes(getSnakeTail(snakeNum, pos))) {
return getSnakePos(snakeNum);
} else if (snakeTail.includes(getSnakeTail(snakeNum, pos))) {
return getSnakePos(snakeNum);
}
else {
return pos;
}
// console.log("pos = " + pos + " right=" + right + " snakeRight=" + snakeRight);
}
function getSnakeTail(snakeNum, pos) {
var bottom = 0;
var snakeRight = Number($("#snake" + snakeNum).attr("data-right"));
var snakeSide = Number($("#snake" + snakeNum).attr("data-side"));
var snakeDown = Number($("#snake" + snakeNum).attr("data-bottom"));
if (snakeDown % 2 == 0) {
//for left to right
if (Math.ceil(pos / boardColumn) % 2 != 0) {
bottom = pos - (snakeDown * boardColumn) + snakeSide;
}
//for right to left
else {
bottom = pos - (snakeDown * boardColumn) - snakeSide;
}
} else {
//for left to right
if (Math.ceil(pos / boardColumn) % 2 != 0) {
var left = pos - ((Math.ceil(pos / boardColumn) - 1) * boardColumn) - 1;
bottom = pos - ((snakeDown - 1) * boardColumn) - snakeSide - (2 * left) - 1;
}
//for right to left
else {
var right = pos - ((Math.ceil(pos / boardColumn) - 1) * boardColumn) - 1;
bottom = pos - ((snakeDown - 1) * boardColumn) + snakeSide - (2 * right) - 1;
// console.log("right :" + right + " pos :" + pos + " bottom :" + bottom);
}
}
return bottom;
}
}
//Plotting Ladder
for (var i = 1; i <= noOfLadder; i++) {
// var pos = getLadderPos(i);
if (gamePreference == 'organized') {
var pos = arrangements[boardNumber]["ladderHead"][i - 1];
} else {
var pos = getLadderPos(i);
}
renderLadder(i, pos);
ladderHead.push(pos);
ladderTail.push(getLadderTail(i, pos));
function getLadderPos(ladderNum) {
var pos = rand(31, 99);
var ladderRight = Number($("#ladder" + ladderNum).attr("data-right"));
var ladderLeft = Number($("#ladder" + ladderNum).attr("data-left"));
var ladderBottom = Number($("#ladder" + ladderNum).attr("data-down"));
//Shifting from Bottom and Left
var bottom = Math.ceil(pos / boardColumn) - 1;
if (ladderBottom >= bottom) { pos += boardColumn; }
//for left to right
if (Math.ceil(pos / boardColumn) % 2 != 0) {
var right = boardColumn - (pos - ((Math.ceil(pos / boardColumn) - 1) * boardColumn));
if (ladderRight >= right) { pos -= ladderRight; }
var left = pos - ((Math.ceil(pos / boardColumn) - 1) * boardColumn) - 1;
console.log("pos = " + pos + " left=" + left + " ladderRight=" + ladderRight);
if (ladderLeft >= left) { pos += ladderLeft; }
}
//for right to left
else {
var right = (pos % boardColumn) - 1;
if (ladderRight >= right) { pos += ladderRight; }
var left = boardColumn - (pos - ((Math.ceil(pos / boardColumn) - 1) * boardColumn));
if (ladderLeft >= left) { pos += ladderLeft; }
}