forked from daveaugustus/pig-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
142 lines (103 loc) · 4.67 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
//Global variables declaration
let score, roundScore, activePlayer, isGameOn;
let lastRoll;
// call of initializeVariable() function to initialise the game at the starting stage of the game
initializeVariable();
/*
An event listener is given to Roll dice button, rolling a dice works when the isGameOn function is true then it generates dice random no from 0-6
then it checks if the dice value is non 1. if dice value is non 1, one can keep rolling or hold score if 1 the entire round score becomes 0 and
controll goes to the next player.
*/
document.querySelector('.btn-roll').addEventListener('click', function(){
if(isGameOn){
let dice = Math.floor(Math.random() * 6) + 1;
let diceDOM = document.querySelector('.dice');
diceDOM.style.display = 'block';
diceDOM.src = 'dice-' + dice + '.png';
if(dice === 6 && lastRoll === 6){
score[activePlayer] = 0
document.querySelector('#score-'+activePlayer).textContent = score[activePlayer];
nextPlayer();
}else if(dice !== 1){
roundScore += dice;
document.querySelector('#current-' + activePlayer).textContent = roundScore;
}else{
nextPlayer();
}
lastRoll = dice;
}
});
/*
if a player clicks hold button the even listener will be called which will hold the current score and pass the controll to the next player if isGameOn
variable is true. It will also check if the score is >== the winning score, is so it will display the winner and set the variable isGameOn to false
else game will continue...
*/
document.querySelector('.btn-hold').addEventListener('click', function(){
if(isGameOn){
// Add current score to Global score
score[activePlayer] += roundScore;
// Update UI
document.querySelector('#score-' + activePlayer).textContent = score[activePlayer];
let userSetScore = document.querySelector('#final-score').value;
let winningScore;
if(userSetScore){
winningScore = userSetScore;
}else{
winningScore = 100;
}
// Check who won the game
if(score[activePlayer] >= winningScore){
document.querySelector('#name-'+ activePlayer).textContent = 'Winner!';
document.querySelector('.dice').style.display = 'none';
document.querySelector('.player-' + activePlayer + '-panel').classList.add('winner');
document.querySelector('.player-' + activePlayer + '-panel').classList.remove('active');
isGameOn = false;
}
else{
//Next Player
nextPlayer();
}
}
});
/*
nextplayer function does nothing but it gives the controll to the next player if one comes or tow consecutive sixes come. the ternary operator in the
function checks if activePlayer is 0 then activePlayer will be 1 else the activePlayer will remain zero because there are only two players active player
will toggle between zero and one
*/
function nextPlayer(){
activePlayer === 0 ? activePlayer = 1 : activePlayer = 0;
roundScore = 0;
document.getElementById('current-0').textContent = '0';
document.getElementById('current-1').textContent = '0';
document.querySelector('.player-0-panel').classList.toggle('active');
document.querySelector('.player-1-panel').classList.toggle('active');
document.querySelector('.dice').style.display = 'none';
}
/*
clicking new game leads to resetting all the functions and variables to the initial state
for that we there is an event listener in the following code which calls the initializeVariable() fucntion
after click of the button.
*/
document.querySelector('.btn-new').addEventListener('click', initializeVariable);
/*
function initializeVariable initializes all the variables and display documents to
it's initial state.
*/
function initializeVariable(){
score = [0,0];
roundScore = 0;
activePlayer = 0;
isGameOn = true;
document.querySelector('.dice').style.display = 'none';
document.getElementById('score-0').textContent = '0';
document.getElementById('score-1').textContent = '0';
document.getElementById('current-0').textContent = '0';
document.getElementById('current-1').textContent = '0';
document.getElementById('name-0').textContent = 'Player 1';
document.getElementById('name-1').textContent = 'Player 2';
document.querySelector('.player-0-panel').classList.remove('winner');
document.querySelector('.player-1-panel').classList.remove('winner');
document.querySelector('.player-0-panel').classList.remove('active');
document.querySelector('.player-1-panel').classList.remove('active');
document.querySelector('.player-0-panel').classList.add('active');
}