-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge1.js
237 lines (167 loc) · 7.95 KB
/
challenge1.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
/*
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;
function newGame() {
gamePlaying = true;
previousRoll = undefined;
currentRoll = undefined;
resetName();
// Makes the dice disappear
document.querySelector('.dice').style.display = 'none';
// Hides the you rolled a one message
document.querySelector('.message0').style.display = 'none';
document.querySelector('.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;
}
newGame();
// // 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 && !(currentRoll === 6 && previousRoll === 6)) { // PLayer does not roll a one
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.querySelector('.message0').style.display = 'none';
document.querySelector('.message1').style.display = 'none';
} else if ((currentRoll !== 1 && (currentRoll === 6 && previousRoll === 6))) { //player does not roll a one // PLayer rolls double 6
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 DIPLAYED
document.querySelector('.message' + activePlayer).textContent = 'You rolled double 6';
document.querySelector('.message' + activePlayer).style.display = 'inline';
//
console.log('yes');
nextPlayer();
} 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('You rolled a one');
document.querySelector('.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();
// }
});
document.querySelector('.btn-hold').addEventListener('click', function () {
if (gamePlaying) {
//Add scores to global score // score[0] or score[1]
let globalScore = (scores[activePlayer] += roundScore);
document.querySelector('#score-' + activePlayer).textContent = globalScore;
if (scores[activePlayer] >= 100) {
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 {
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;
// }