-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge2.js
315 lines (223 loc) · 10.6 KB
/
challenge2.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
/*
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 previousRoll, currentRoll;
let winningScore = [100, 0];
function print(message) {
console.log(message);
}
newGame();
// let winningScore = document.getElementsByClassName('winning-score').value;
// function clicked () {
//What happens when the button is clicked
// function setScore() {
// let winningScore = document.getElementById('score-input').value;
// console.log(winningScore);
// }
// Sets the winning score to either default winningScore[0] or user input winningScore[1]
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';
})
// 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;
previousRoll = undefined;
currentRoll = undefined;
resetName();
// Makes the dice disappear
document.querySelector('.dice').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
let dice = Math.floor(Math.random() * 6 + 1);
// let dice = 6;
previousRoll = currentRoll;
currentRoll = dice;
// console.log('The previous roll was ' + previousRoll);
// console.log('The current roll is ' + currentRoll);
// 2. Display the dice
let diceDOM = document.querySelector('.dice');
diceDOM.style.display = 'block';
diceDOM.src = 'dice-' + dice + '.png';
// Checks to see if two 6's have been rolled
// if (currentRoll === 6 && previousRoll === 6) {
// console.log('yes');
// scores[activePlayer] = 0;
// }
// 3. Display the round score only if the round score was not 1
if (currentRoll !== 1) { // PLayer does not roll a one
// If the current roll is not a one but the previous roll and current roll are 6 // this if statement executes
if ((currentRoll === 6 && previousRoll === 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';
console.log('Active player is ' + activePlayer + ' You rolled a 6 & 6. The previous roll was ' + previousRoll + 'The current roll is ' + currentRoll);
scores[activePlayer] = 0;
document.querySelector('#score-' + activePlayer).textContent = '0';
// IF PLAYER ROLLS DOUBLE 6 - A MESSAGE IS DISPLAYED
//
console.log('You rolled a double 6');
// 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 6 & 6. The previous roll was ' + previousRoll + 'The current roll is ' + currentRoll);
// 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';
}
} else {// if active player rolls a one
// Previous roll and current roll resets to 0
// previousRoll = 0;
// currentRoll = 0;
// Displays a message that says you rolled a 1
console.log('Active player is ' + activePlayer + ' You didn\'t roll a 6 & 6. You rolled a one. The previous roll was ' + previousRoll + 'The current roll is ' + currentRoll);
document.getElementById('message' + activePlayer).style.display = 'inline'
// Changes the active player
nextPlayer();
}
}
// // If score of the active player is more than 100 and the roll dice button is clicked // Start a new game
// if (scores[activePlayer] >= 10) {
// newGame();
// }
});
// 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() {
previousRoll = 0;
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;
// Swapping the active class
document.querySelector('.player-0-panel').classList.toggle('active');
document.querySelector('.player-1-panel').classList.toggle('active');
document.querySelector('.dice').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);
/****************************************
CODE CHALLENGES
****************************************/
//Challenge 1
//1. Player rolls 6
// two variables - last roll and current roll
// current round score = current roll + last roll + any previous rolls
//if previous roll === 6 && current roll === 6 // player score[activePlayer] = 0; // DONE
// if not then active score should be
//2. player rolls 6 again
//3. Player loses all their score
//
// document.querySelector('#btn-hold').addEventListener('click', function () {
// // Figure out current player
// // Add scores to global score // score[0] or score[1]
// score[activePlayer] += roundScore;
// // Change player to other player
// })
// if (dice !== '1') {
// // Add score
// roundScore += dice;
// document.querySelector('#current-' + activePlayer).textContent = roundScore;
// } else if (dice === '1') {
// //Next player
// roundScore = 0;
// activePlayer === 0 ? activePlayer = 1 : activePlayer = 0;
// }