Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
400 lines (318 loc) · 11.4 KB
/
Copy pathscript.js
File metadata and controls
400 lines (318 loc) · 11.4 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
########################################
Global variable declarations here
########################################
*/
//global answers array
let answerWords = [];
let correctWords = []; //generates new array with correctly guessed words
//global variable for random letters generated
let letters = "";
//Init score
let score = 0;
let gameRunning = false;
//Page elements
const inputEl = $("#search-bar");
const scoreEl = document.querySelector(".score-display");
const timeEl = document.querySelector(".time-display");
const gifBoxContainer = document.querySelector(".gifCardStorageBox");
const startBtn = document.querySelector(".startBtn");
const preGameEl = document.querySelector(".preGame");
const duringGameEl = document.querySelector(".during-game");
const afterGameModal = $(".modal.after-game");
const playAgainBtn = document.querySelector(".play-again-btn");
const endGameBtn = document.querySelector(".end-game-btn");
const clearBtn = document.querySelector("#clear-btn");
const boggleButtons = $(".boggleButton");
//Modal elements
const finalScoreEl = document.querySelector(".final-score");
const wordsGuessedEl = document.querySelector(".words-guessed");
const wordsUnguessedEl = document.querySelector(".words-unguessed");
const percentGuessedEl = document.querySelector(".percentage-words");
const unguessedWordsEl = document.querySelector(".unguessed-words");
/*
########################################
Function declarations start here
########################################
*/
//Function to query boggle api and then return answer to global answerwords array
//By Ben
const queryBoggleAPI = (letters) => {
//Set query URL
let queryURL = `https://codebox-boggle-v1.p.rapidapi.com/${letters}`;
//Make api call
$.ajax({
url: queryURL,
method: "GET",
headers: {
//Auth headers
"x-rapidapi-host": "codebox-boggle-v1.p.rapidapi.com",
"x-rapidapi-key": "022db4a757msh43b4c58fa247ec3p1f954ejsn28f1e76c07fb"
},
//Function to run on successful response
success: (response) => {
//Testing
// console.log(response);
//output.innerText = "";
//For each word in response array convert to lowercase then push to global answer words array
response.forEach(word => {
answerWords.push(word.toLowerCase());
//Testing
//output.innerText += word.toLowerCase() + "\n";
});
//Testing
// console.log(answerWords);
},
//Function to run on error
error: (xhr, status, error) => {
// console.log(`status: ${status}, Error: ${error}`);
}
});
}
//Timer object
//By Ben F
const timer = {
//Default start seconds
startSecs: 180,
//Tracks the current
currSecs: 180,
increment: 1000,
timerRunning: false,
//Start timer Fn
start: function () {
//set timer running to true
this.timerRunning = true;
//interval function to run until finish
var intervalFn = setInterval(() => {
//if the timer is running continue
if (this.timerRunning) {
//decrement
this.currSecs -= 1;
//Display on page
this.display();
//if reached 0 stop timer and call endgame fn
if (this.currSecs <= 0) {
this.timerRunning = false;
//call end game events after small delay otherwise display isn't updated
setTimeout(endGame, 4);
}
} else {
//if timer is not running clears interval
clearInterval(intervalFn);
}
}, this.increment);
},
//stop timer function
stop: function () {
this.timerRunning = false;
},
//reset timer function
reset: function (seconds = this.startSecs) {
this.startSecs = seconds;
this.currSecs = this.startSecs;
this.display();
},
//Fn to display secs to page
display: function () {
// console.log(this.currSecs);
let mins = Math.floor(this.currSecs / 60);
let secs = this.currSecs % 60;
secs = secs < 10 ? "0" + secs : secs;
formattedString = mins + ":" + secs;
// timeEl.innerText = this.currSecs;
timeEl.innerText = formattedString;
}
}
// creating a function for start button and to reveal boggle containers
//By Nima
const startGame = (event) => {
preGameEl.classList.add("hidden");
duringGameEl.classList.remove("hidden");
//Starting page: instruction page to be hidden
// startpage.classList.add("hidden");
letters = randomLetterGenerator();
score = 0;
scoreEl.innerText = 0;
gifBoxContainer.innerHTML = "";
correctWords = [];
timer.reset();
queryBoggleAPI(letters);
BoggleBlocks(letters);
timer.start();
gameRunning = true;
};
//Generates letters used in boggle from availble letters
//By Claire
const randomLetterGenerator = () => {
var boggleCombinations = ["AAEEGN", "ELRTTY", "AOOTTW", "ABBJOO", "EHRTVW", "CIMOTU", "DISTTY", "EIOSST", "DELRVY", "ACHOPS", "HIMNQU", "EEINSU", "EEGHNW", "AFFKPS", "HLNNRZ", "DEILRX"];
//Choose a random string from the array
var randomLetters = "";
//Generate 0 to 16. runs for loop 16 times.
for (let i = 0; i < 16; i++) {
//picks string from 1- 16 (different dice)
var randomIndex = Math.floor(Math.random() * boggleCombinations.length);
//pulls out string. set to current string.
var currentString = boggleCombinations[randomIndex];
// random letter from string between 0-5. Saved to letter variable
var letter = currentString[Math.floor(Math.random() * currentString.length)];
//console.log(letter);
//append letter onto random letter string.
randomLetters += letter;
//remove from array
boggleCombinations.splice(randomIndex, 1)
//console.log(boggleCombinations)
};
// console.log(randomLetters);
//return the string.
return randomLetters;
}
//Function that fills page elements with letters passed into it
//By Claire
const BoggleBlocks = (letters) => {
for (let i = 0; i < 16; i++) {
let boggleCube = document.getElementById(`boggleBox${i}`);
// console.log(boggleCube);
let letter = letters[i];
if (letter == "Q") {
letter = "QU";
}
// console.log(letter);
boggleCube.innerText = letter;
}
}
//End the Game function
//By Ben F
const endGame = () => {
timer.stop();
gameRunning = false;
//Fill modal elements
finalScoreEl.innerText = score;
wordsGuessedEl.innerText = correctWords.length;
wordsUnguessedEl.innerText = answerWords.length;
percentGuessedEl.innerText = Math.round((correctWords.length / (answerWords.length + correctWords.length)) * 100) + "%";
unguessedWordsEl.innerHTML = "";
answerWords.forEach(word => {
unguessedWordsEl.innerHTML += `<p>${word.toUpperCase()}</p>`
})
afterGameModal.modal({
onHide: () => {
duringGameEl.classList.add("hidden");
preGameEl.classList.remove("hidden");
}
}).modal("show");
}
//Function to remove guessed words from available words
//By Ben C
//Creates new array with correctly guessed words removed - also creates a new array of corect words
function remove(enteredWord) {
let index = answerWords.indexOf(enteredWord);
// console.log(index);
if (index !== -1) {
answerWords.splice(index, 1);
correctWords.push(enteredWord);
}
}
//Input Validator
//By Ben C
const validateInput = (event) => {
if (!gameRunning) {
return;
}
// console.log("Test");
let enteredWord = inputEl.val().toLowerCase();
// console.log("before", answerWords.length);
if (answerWords.includes(enteredWord)) {
remove(enteredWord);
// console.log("after", answerWords.length);
// console.log(enteredWord);
inputEl.val("");
createCard(enteredWord);
//Increase score
let wordScore = 0;
let wordlength = enteredWord.length;
if (wordlength < 3) {
wordScore = 0;
} else if (wordlength < 4) { // 3
wordScore = 1;
} else if (wordlength < 5) { // 4
wordScore = 2;
} else if (wordlength < 6) { // 5
wordScore = 3;
} else if (wordlength < 7) { // 6
wordScore = 4;
} else if (wordlength < 8) { // 7
wordScore = 5;
} else {
wordScore = 6;
}
score += wordScore;
scoreEl.innerText = score;
}
}
//Create cards with Gifs and word in them
//By Ben F and Claire
const createCard = (word) => {
//note that the variable (and the output from the boggle is called "word")
var queryURL = `https://api.giphy.com/v1/gifs/search?q=${word}&limit=1&rating=g&api_key=kqQyG8Y7gjqsyjEcFmZd3qBhbj2KBn5i`;
$.ajax({
url: queryURL,
method: "GET"
})
.then(function (response) {
// console.log(response);
let gifURL = response.data[0].images.original.url ? response.data[0].images.original.url : "https://media3.giphy.com/media/xNBcChLQt7s9a/giphy.gif?cid=ecf05e47dded58718835a05dcae59e8c6c0ac476f4903097&rid=giphy.gif";
let wordTitle = word.toUpperCase();
let cardHTML = `
<div class="six wide mobile five wide tablet four wide computer column">
<div class="ui fluid card gifCard">
<div >
<img class="ui image"
src="${gifURL}">
</div>
<div class="content">
<h3 class="header">${wordTitle}</h3>
</div>
</div>
</div>
`
let currentHTML = gifBoxContainer.innerHTML;
let newHtml = cardHTML + currentHTML;
gifBoxContainer.innerHTML = newHtml;
});
// console.log(word);
// console.log("Create card for word " + word);
}
//Function to run when play again btn clicked
//By Ben F
const playAgain = (event) => {
afterGameModal.modal("hide");
duringGameEl.classList.add("hidden");
preGameEl.classList.remove("hidden");
}
//Function to print out all available words for testing, only meant to be typed into console
//By Ben F
const godMode = () => {
console.table(answerWords);
}
/*
########################################
Code to run on page load
########################################
*/
/*
########################################
Event Listeners here
########################################
*/
inputEl.on("keyup", validateInput);
startBtn.addEventListener("click", startGame);
playAgainBtn.addEventListener("click", playAgain);
endGameBtn.addEventListener("click", endGame);
boggleButtons.on("click", function() {
let clickedLetter = $(this).text();
inputEl.val(inputEl.val() + clickedLetter);
validateInput();
});
clearBtn.addEventListener("click", () => {
inputEl.val("");
})