-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
167 lines (120 loc) · 5.73 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
// Decalring all the necessary variables
var activePlayer, scores, gamePlaying, roundScore, dice1, dice2, winningScore, backImgToss, backgroundMusic, diceRollToss, diceRollSfx, player1, player2;
// Reloading Game
reload()
initiate ()
// ROLL DICE Button
document.querySelector('#roll-dice').addEventListener('click', function () {
if (gamePlaying) {
diceRollSfx = new Audio
diceRollSfx.src = 'Dice roll - 2.3gp.mp4'
diceRollSfx.volume = .2
diceRollSfx.play()
document.querySelector('#dice1').style.display = 'block';
document.querySelector('#dice2').style.display = 'block'
dice1 = Math.floor(Math.random() * 6) + 1
dice2 = Math.floor(Math.random() * 6) + 1
document.getElementById('dice1').src = 'dice-' + dice1 + '.png';
document.getElementById('dice2').src = 'dice-' + dice2 + '.png'
if (dice1 === 6 && dice2 === 6) {
scores[activePlayer] = 0;
roundScore = 0
document.querySelector('#current-' + activePlayer).textContent = roundScore;
document.querySelector('#score-' + activePlayer).textContent = scores[activePlayer]
nextPlayer ()
} else if (dice1 === 1 || dice2 === 1) {
roundScore = 0;
document.querySelector('#current-' + activePlayer).textContent = roundScore
nextPlayer ()
} else {
roundScore += (dice1 + dice2);
document.querySelector('#current-' + activePlayer).textContent = roundScore
}
}
})
// HOLD Button
document.querySelector('#hold').addEventListener('click', function () {
if (gamePlaying) {
if (document.querySelector('#final-score').value != 0) {
winningScore = document.querySelector('#final-score').value
}
scores[activePlayer] += roundScore
document.querySelector('#score-' + activePlayer).textContent = scores[activePlayer]
document.querySelector('#dice1').style.display = 'none';
document.querySelector('#dice2').style.display = 'none';
roundScore = 0;
document.querySelector('#current-' + activePlayer).textContent = roundScore
if (scores[activePlayer] >= winningScore) {
gamePlaying = false;
document.querySelector('#name-' + activePlayer).textContent = 'Winner!';
}
nextPlayer()
}
})
// NEW GAME Button
document.querySelector('#new-game').addEventListener('click', initiate)
function reload () {
document.querySelector('#next-1').addEventListener('click', function() {
document.querySelector('#rules-container').style.display = 'none';
document.querySelector('#player-names-container').style.display = 'initial';
})
document.querySelector('#next-2').addEventListener('click', function () {
player1 = document.querySelector('#player-1-name').value.toUpperCase()
player2 = document.querySelector('#player-2-name').value.toUpperCase()
document.querySelector('#name-0').textContent = player1
document.querySelector('#name-1').textContent = player2
if (player1 == '' || player2 == '') {
return document.querySelector('#name-validator').textContent = 'You cannot keep the player\'s names empty'
} else if (player1.length <= 2 || player2.length <= 2) {
return document.querySelector('#name-validator').textContent = 'The names should be at least 3 characters long!'
}
else if (player1 == player2) {
return document.querySelector('#name-validator').textContent = 'You cannot play with players with the same name. Hint: You can use a Number to make the names different'
} else {
document.querySelector('#player-names-container').style.display = 'none';
document.querySelector('.wrapper').style.display = 'initial'
}
})
}
// Game restarting Function
function initiate () {
scores = [0, 0];
roundScore = 0;
activePlayer = 0;
winningScore = 100
gamePlaying = true;
backgroundMusic = new Audio;
backgroundMusic.src = 'back-audio.3gp';
backgroundMusic.volume = 0.1;
backgroundMusic.play()
document.body.style.backgroundImage = "url('Back - 1.jpg')"
document.querySelector('#current-0').textContent = '0';
document.querySelector('#current-1').textContent = 0;
document.querySelector('#score-0').textContent = 0;
document.querySelector('#score-1').textContent = 0;
document.querySelector('#name-0').textContent = player1;
document.querySelector('#name-1').textContent = player2;
document.querySelector('#player-panel-0').classList.add('active');
document.querySelector('#player-panel-1').classList.remove('active');
document.querySelector('#final-score').value = 100;
document.querySelector('.active-sign-0').style.display = 'initial';
document.querySelector('.active-sign-1').style.display = 'none';
document.querySelector('#dice1').style.display = 'none';
document.querySelector('#dice2').style.display = 'none';
}
// Next player's turn Function
function nextPlayer () {
if (gamePlaying) {
activePlayer == 0 ? activePlayer = 1 : activePlayer = 0;
roundScore = 0
document.querySelector('#player-panel-0').classList.toggle('active')
document.querySelector('#player-panel-1').classList.toggle('active')
if (activePlayer === 0) {
document.querySelector('.active-sign-0').style.display = 'initial';
document.querySelector('.active-sign-1').style.display = 'none';
} else {
document.querySelector('.active-sign-1').style.display = 'initial';
document.querySelector('.active-sign-0').style.display = 'none';
}
}
}