-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge3.js
255 lines (186 loc) · 9.13 KB
/
challenge3.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
/*
GAME RULES:
- The game has 2 players, playing in rounds
- In each turn, a player rolls a dice as many times as he whishes. Each result get added to his ROUND score
- BUT, if the player rolls a 1, all his ROUND score gets lost. After that, it's the next player's turn
- The player can choose to 'Hold', which means that his ROUND score gets added to his GLBAL score. After that, it's the next player's turn
- The first player to reach 100 points on GLOBAL score wins the game
*/
// Defining the scores variable
let scores, roundScore, activePlayer, gamePlaying;
let currentRoll;
let winningScore = [100, 0];
let dice1, dice2;
function print(message) {
console.log(message);
}
newGame();
function setScore() {
// if(document.getElementById('score-input').value ===
if (document.getElementById('score-input').value > 0) {
winningScore[1] = document.getElementById('score-input').value;
console.log(winningScore[1]);
} else {
winningScore[1] = winningScore[0];
console.log('The input field is empty. Default score has been set: ' + winningScore[1]);
}
}
// Submit button that hides the from user input fields
document.getElementById('submit-button').addEventListener('click', function () {
setScore();
document.getElementById('winning-score-text').style.display = 'none';
document.getElementById('score-input').style.display = 'none';
document.getElementById('submit-button').style.display = 'none';
gamePlaying = true;
document.getElementById('instructions').style.display = 'none';
// Shows the two dice
document.getElementById('dice1').style.display = 'inline';
document.getElementById('dice2').style.display = 'inline';
});
// functionthat shows the form user input fieldsat teh start of a new game
function getWinningScore() {
if (gamePlaying) {
gamePlaying = false;
document.getElementById('winning-score-text').style.display = 'block';
document.getElementById('score-input').style.display = 'block';
document.getElementById('submit-button').style.display = 'block';
document.getElementById('instructions').style.display = 'block';
} else { // if gamePlaying is false
gamePlaying = true;
getWinningScore();
}
// Turn on the forms off and Gets the final score of the game from the user
};
function newGame() {
getWinningScore();
print('New game');
winningScore[1] = 0;
winningScore[1] = document.getElementById('score-input').value = '';
gamePlaying = false;
currentRoll = undefined;
resetName();
// Makes the dice disappear
document.getElementById('dice1').style.display = 'none';
document.getElementById('dice2').style.display = 'none';
// Hides the you rolled a one message
document.getElementById('message0').style.display = 'none';
document.getElementById('message1').style.display = 'none';
// Resets the values of the variables
scores = [0, 0];
roundScore = 0;
activePlayer = 0;
// Makes the scores all
document.getElementById('score-0').textContent = '0';
document.getElementById('score-1').textContent = '0';
document.getElementById('current-0').textContent = '0';
document.getElementById('current-1').textContent = '0';
// Removes the winner class
document.querySelector('.player-0-panel').classList.remove('winner');
document.querySelector('.player-1-panel').classList.remove('winner');
// Adds the visual indicator to the class player 1
document.querySelector('.player-0-panel').classList.add('active');
document.querySelector('.player-1-panel').classList.remove('active');
document.querySelector('.player-' + activePlayer + '-panel').classList.remove('winner');
// Makes pLayer 1 the active player
activePlayer === 0 ? activePlayer = 0 : activePlayer = 0;
}
// // Uisng JS to change/ read the html
// var x = document.querySelector('#score-0').textContent;
// console.log(x);
// Selecting the button
// Creating what happens when the button is clicked
document.querySelector('.btn-roll').addEventListener('click', function () {
if (gamePlaying) {
// 1. We need a random number from two dice
dice1 = Math.floor(Math.random() * 6 + 1);
dice2 = Math.floor(Math.random() * 6 + 1);
// let dice = 6;
// dice = dice1 + dice2;
currentRoll = dice1 + dice2;
// 2. Display the dice1
let diceDOM1 = document.getElementById('dice1');
diceDOM1.style.display = 'block';
diceDOM1.src = 'dice-' + dice1 + '.png';
// Display dice2
let diceDOM2 = document.getElementById('dice2');
diceDOM2.style.display = 'block';
diceDOM2.src = 'dice-' + dice2 + '.png';
// Checks to see if two 6's have been rolled
// 3. Display the round score only if the round score was not 1
if (dice1 === 1 || dice2 === 1) { // PLayer rolls a one
// If the current roll is not a one but the previous roll and current roll are 6 // this if statement executes
console.log('Active player is ' + activePlayer + ' You rolled a one. Dice 1 was ' + dice1 + ' & Dice 2 was ' + dice2);
document.getElementById('message' + activePlayer).style.display = 'inline'
// Changes the active player
nextPlayer();
} else {// does not roll a one
if ((dice1 === 6 && dice2 === 6)) { //player does not roll a one // PLayer rolls double 6
document.getElementById('message' + activePlayer).textContent = 'You rolled a double 6!';
document.getElementById('message' + activePlayer).style.display = 'inline';
// IF PLAYER ROLLS DOUBLE 6 - A MESSAGE IS DISPLAYED
console.log('Active player is ' + activePlayer + ' You rolled a double 6. Dice 1 was ' + dice1 + ' & Dice 2 was ' + dice2);
scores[activePlayer] = 0;
document.querySelector('#score-' + activePlayer).textContent = '0';
// Changes turn to next Player
nextPlayer();
} else { // if it's not a 6 and 6 run this default line
console.log('Active player is ' + activePlayer + ' You didn\'t roll a double 6. Dice 1 was ' + dice1 + ' Dice 2 was ' + dice2);
// adds the dice value to the round score
roundScore += currentRoll;
// This displays the value of the round score to
document.querySelector('#current-' + activePlayer).textContent = roundScore;
// Hides the You rolled a one message
document.getElementById('message0').style.display = 'none';
document.getElementById('message1').style.display = 'none';
}
}
}
});
// When hold button is pressed
document.querySelector('.btn-hold').addEventListener('click', function () {
//If gamePlaying is true
if (gamePlaying) {
//Add round scores of active player to global score // score[0] or score[1]
let globalScore = (scores[activePlayer] += roundScore);
document.querySelector('#score-' + activePlayer).textContent = globalScore;
// Checks if player's score is greater than the winningScore
if (scores[activePlayer] >= winningScore[1]) {
document.querySelector('.player-' + activePlayer + '-panel').classList.add('winner');
console.log('Winner');
document.querySelector('#name-' + activePlayer).textContent = 'You Won!';
document.querySelector('.dice').style.display = 'none';
document.querySelector('.player-' + activePlayer + '-panel').classList.remove('active');
document.querySelector('.player-' + activePlayer + '-panel').classList.add('winner');
gamePlaying = false;
} else { // Otherwise pass the turn to the next player
console.log('Player ' + activePlayer + ' clicked Hold. Your score of ' + roundScore + ' has been saved');
nextPlayer();
}
}
});
// This changes the player
function nextPlayer() {
currentRoll = 0;
// Your round score becomes zero
roundScore = 0;
// Your round score is displayed as Zero
document.querySelector('#current-' + activePlayer).textContent = roundScore;
// Changes the active player
activePlayer === 0 ? activePlayer = 1 : activePlayer = 0;
// If a you rolled one message is displayed - hide the message
if (document.getElementById('message' + activePlayer).style.display === 'inline') {
document.getElementById('message' + activePlayer).style.display = 'none'
}
// Swapping the active class
document.querySelector('.player-0-panel').classList.toggle('active');
document.querySelector('.player-1-panel').classList.toggle('active');
// Hides the dice
document.getElementById('dice1').style.display = 'none';
document.getElementById('dice2').style.display = 'none';
}
function resetName() {
document.querySelector('#name-0').textContent = 'Player 1';
document.querySelector('#name-1').textContent = 'Player 2';
}
// Calls the new game function and starts a new game
document.querySelector('.btn-new').addEventListener('click', newGame);