-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathindex.js
314 lines (292 loc) · 8.7 KB
/
index.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
const holes = document.querySelectorAll('.hole');
const moles = document.querySelectorAll('.mole');
const startButton = document.querySelector('#start');
// TODO: Add the missing query selectors:
const score = document.querySelector('#score'); // Use querySelector() to get the score element
//Made changes to const score, added querySelector score.
const timerDisplay = document.querySelector('#timer'); // use querySelector() to get the timer element.
//Made changes to const timerDisplay, added querySelector timer.
let time = 0;
let timer;
let lastHole = 0;
let points = 0;
let difficulty = "normal";
/**
* Generates a random integer within a range.
*
* The function takes two values as parameters that limits the range
* of the number to be generated. For example, calling randomInteger(0,10)
* will return a random integer between 0 and 10. Calling randomInteger(10,200)
* will return a random integer between 10 and 200.
*
*/
function randomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
//ADDED THE RANDOMINTEGER FUNCTION, ALREADY IMPLEMENTED FOR ME, SELECTS RANDOM TIME THE MOLES COME OUT OF THEIR HOLES DURING THE GAME.
console.log("A random integer between 0 and 10");
console.log(randomInteger(0, 10));
console.log("Another random integer between 0 and 10");
console.log(randomInteger(0, 10));
console.log("A random number between 600 and 1200");
console.log(randomInteger(600, 1200));
//ADDED RANDON INTETGERS
/**
* Sets the time delay given a difficulty parameter.
*
* The function takes a `difficulty` parameter that can have three values: `easy`
* `normal` or `hard`. If difficulty is "easy" then the function returns a time delay
* of 1500 milliseconds (or 1.5 seconds). If the difficulty is set to "normal" it should
* return 1000. If difficulty is set to "hard" it should return a randomInteger between
* 600 and 1200.
*
* Example:
* setDelay("easy") //> returns 1500
* setDelay("normal") //> returns 1000
* setDelay("hard") //> returns 856 (returns a random number between 600 and 1200).
*
*/
function setDelay(difficulty) {
// TODO: Write your code here.
if (difficulty === "easy") {
return 1500;
// SETTING DIFFICULTY FOR EASY
} if (difficulty === "normal") {
return 1000;
// SETTING DIFFICULTY FOR NORMAL
} if (difficulty === "hard") {
return randomInteger(600, 1200);
//SETTING DIFFICULTY FOR HARD
}
}
/**
* Chooses a random hole from a list of holes.
*
* This function should select a random Hole from the list of holes.
* 1. generate a random integer from 0 to 8 and assign it to an index variable
* 2. get a random hole with the random index (e.g. const hole = holes[index])
* 3. if hole === lastHole then call chooseHole(holes) again.
* 4. if hole is not the same as the lastHole then keep track of
* it (lastHole = hole) and return the hole
*
* Example:
* const holes = document.querySelectorAll('.hole');
* chooseHole(holes) //> returns one of the 9 holes that you defined
*/
function chooseHole(holes) {
// TODO: Write your code here.
const index = randomInteger(0,8);
const hole = holes[index];
if (hole === lastHole) {
return chooseHole(holes);
}
lastHole = hole;
return hole;
}
/**
*
* Calls the showUp function if time > 0 and stops the game if time = 0.
*
* The purpose of this function is simply to determine if the game should
* continue or stop. The game continues if there is still time `if(time > 0)`.
* If there is still time then `showUp()` needs to be called again so that
* it sets a different delay and a different hole. If there is no more time
* then it should call the `stopGame()` function. The function also needs to
* return the timeoutId if the game continues or the string "game stopped"
* if the game is over.
*
* // if time > 0:
* // timeoutId = showUp()
* // return timeoutId
* // else
* // gameStopped = stopGame()
* // return gameStopped
*
*/
function gameOver() {
// TODO: Write your code here
if (time > 0){
let timeoutId = showUp();
return timeoutId;
} else {
let gameStopped = stopGame();
return gameStopped;
}
}
/**
*
* Calls the showAndHide() function with a specific delay and a hole.
*
* This function simply calls the `showAndHide` function with a specific
* delay and hole. The function needs to call `setDelay()` and `chooseHole()`
* to call `showAndHide(hole, delay)`.
*
*/
function showUp() {
let delay = setDelay(difficulty); // TODO: Update so that it uses setDelay()
const hole = chooseHole(holes); // TODO: Update so that it use chooseHole()
return showAndHide(hole, delay);
}
/**
*
* The purpose of this function is to show and hide the mole given
* a delay time and the hole where the mole is hidden. The function calls
* `toggleVisibility` to show or hide the mole. The function should return
* the timeoutID
*
*/
function showAndHide(hole, delay){
// TODO: call the toggleVisibility function so that it adds the 'show' class.
toggleVisibility(hole);
const timeoutID = setTimeout(() => {
// TODO: call the toggleVisibility function so that it removes the 'show' class when the timer times out.
toggleVisibility(hole);
gameOver();
}, 1000); // TODO: change the setTimeout delay to the one provided as a parameter
return timeoutID;
}
/**
*
* Adds or removes the 'show' class that is defined in styles.css to
* a given hole. It returns the hole.
*
*/
function toggleVisibility(hole){
// TODO: add hole.classList.toggle so that it adds or removes the 'show' class.
hole.classList.toggle('show');
return hole;
}
/**
*
* This function increments the points global variable and updates the scoreboard.
* Use the `points` global variable that is already defined and increment it by 1.
* After the `points` variable is incremented proceed by updating the scoreboard
* that you defined in the `index.html` file. To update the scoreboard you can use
* `score.textContent = points;`. Use the comments in the function as a guide
* for your implementation:
*
*/
function updateScore() {
// TODO: Write your code here
points++;
score.textContent = points;
return points;
}
/**
*
* This function clears the score by setting `points = 0`. It also updates
* the board using `score.textContent = points`. The function should return
* the points.
*
*/
function clearScore() {
// TODO: Write your code here
points = 0;
score.textContent = points;
return points;
}
/**
*
* Updates the control board with the timer if time > 0
*
*/
function updateTimer() {
// TODO: Write your code here.
// hint: this code is provided to you in the instructions.
if (time > 0) {
time -= 1;
timerDisplay.textContent = time;
}
return time;
}
/**
*
* Starts the timer using setInterval. For each 1000ms (1 second)
* the updateTimer function get called. This function is already implemented
*
*/
function startTimer() {
// TODO: Write your code here
setInterval(updateTimer, 1000);
return timer;
}
/**
*
* This is the event handler that gets called when a player
* clicks on a mole. The setEventListeners should use this event
* handler (e.g. mole.addEventListener('click', whack)) for each of
* the moles.
*
*/
function whack(event) {
// TODO: Write your code here
updateScore();
return points;
}
/**
*
* Adds the 'click' event listeners to the moles. See the instructions
* for an example on how to set event listeners using a for loop.
*/
function setEventListeners(){
// TODO: Write your code here
moles.forEach(
mole => mole.addEventListener('click', whack));
return moles;
}
/**
*
* This function sets the duration of the game. The time limit, in seconds,
* that a player has to click on the sprites.
*
*/
function setDuration(duration) {
time = duration;
return time;
}
/**
*
* This function is called when the game is stopped. It clears the
* timer using clearInterval. Returns "game stopped".
*
*/
function stopGame(){
// stopAudio(song); //optional
clearInterval(timer);
return "game stopped";
}
/**
*
* This is the function that starts the game when the `startButton`
* is clicked.
*
*/
function startGame(){
showUp();
points = 0;
clearScore();
setDuration(30);
startTimer();
setEventListeners();
return "game started";
}
startButton.addEventListener("click", startGame);
// Please do not modify the code below.
// Used for testing purposes.
window.randomInteger = randomInteger;
window.chooseHole = chooseHole;
window.setDelay = setDelay;
window.startGame = startGame;
window.gameOver = gameOver;
window.showUp = showUp;
window.holes = holes;
window.moles = moles;
window.showAndHide = showAndHide;
window.points = points;
window.updateScore = updateScore;
window.clearScore = clearScore;
window.whack = whack;
window.time = time;
window.setDuration = setDuration;
window.toggleVisibility = toggleVisibility;
window.setEventListeners = setEventListeners;