-
Notifications
You must be signed in to change notification settings - Fork 18
/
spelling_bee.js
353 lines (307 loc) · 9.46 KB
/
spelling_bee.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
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
var validWords=[];
var letters = "";
var discoveredWords =[];
var totalScore = 0;
var pangram = "";
var centerLetter = "";
var cursor = true;
var numFound = 0;
var maxscore = 0;
setInterval(() => {
if(cursor) {
document.getElementById('cursor').style.opacity = 0;
cursor = false;
}else {
document.getElementById('cursor').style.opacity = 1;
cursor = true;
}
}, 600);
//makes http request to an awi api endpoint that triggers a lambda function to return today's letters/words
//today's words and letters are generated by a lambda function from the valid_words.json dictionary
function get_valid_words(){
const url='https://uxxjtb4jz0.execute-api.us-east-1.amazonaws.com/default/FindValidWords';
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.setRequestHeader("Content-type", "text/plain");
request.onreadystatechange = function(){
try {
var data = JSON.parse(this.response);
//3 is LOADING, 4 is DONE
if(request.readyState == 3 && request.status == 200){
console.log(data)
letters = data['letters'];
validWords = data['possible_words'];
pangram = data['pangram'];
maxscore = data['maxscore'];
initialize_letters();
initialize_score();
console.log(validWords);
}
}
catch (e){
console.log('error')
};
};
request.send();
}
function initialize_score(){
document.getElementById("maxscore").innerHTML = String(maxscore);
}
//Creates the hexagon grid of 7 letters with middle letter as special color
function initialize_letters(){
var hexgrid = document.getElementById('hexGrid')
for(var i=0; i<letters.length; i++){
var char = letters[i];
var pElement = document.createElement("P");
pElement.innerHTML = char;
var aElement = document.createElement("A");
aElement.className = "hexLink";
aElement.href = "#";
aElement.appendChild(pElement);
aElement.addEventListener('click', clickLetter(char), false);
var divElement = document.createElement('DIV');
divElement.className = "hexIn";
divElement.appendChild(aElement);
var hexElement = document.createElement("LI");
hexElement.className = "hex";
hexElement.appendChild(divElement);
if(i==3){
aElement.id = "center-letter";
centerLetter = letters[i];
}
hexgrid.appendChild(hexElement);
}
}
Array.prototype.shuffle = function() {
let input = this;
for (let i = input.length-1; i >=0; i--) {
let randomIndex = Math.floor(Math.random()*(i+1));
let itemAtIndex = input[randomIndex];
input[randomIndex] = input[i];
input[i] = itemAtIndex;
}
return input;
}
function shuffleLetters() {
letters.shuffle()
//get center letter back to letter[3]
var centerIndex = letters.indexOf(centerLetter);
if(letters[3] != centerLetter) {
var temp = letters[3];
letters[3] = centerLetter;
letters[centerIndex] = temp;
}
var hexgrid = document.getElementById('hexGrid')
while (hexgrid.firstChild) {
hexgrid.removeChild(hexgrid.firstChild);
}
initialize_letters()
/*
//fill in shuffled letters into hex grid
for(var i=0; i<letters.length; i++) {
var char = letters[i];
var hexLetterElement = document.getElementsByClassName("hexLink");
hexLetterElement[i].removeChild(hexLetterElement[i].firstChild);
var pElement = document.createElement("P");
pElement.innerHTML = char;
hexLetterElement[i].appendChild(pElement);
}*/
}
//Validate whether letter typed into input box was from one of 7 available letters
// document.getElementById("testword").addEventListener("keydown", function(event){
// if(!letters.includes(event.key.toUpperCase())){
// alert('Invalid Letter Typed')
// event.preventDefault();
// }
// }
// )
//When letter is clicked add it to input box
var clickLetter = function(letter){
return function curried_func(e){
var tryword = document.getElementById("testword");
tryword.innerHTML = tryword.innerHTML + letter.toLowerCase();
}
}
//Deletes the last letter of the string in the textbox
function deleteLetter(){
var tryword = document.getElementById("testword");
var trywordTrimmed = tryword.innerHTML.substring(0, tryword.innerHTML.length-1);
tryword.innerHTML = trywordTrimmed
if(!checkIncorrectLetters(trywordTrimmed)) {
tryword.style.color = 'black';
}
}
function wrongInput(selector){
$(selector).fadeIn(1000);
$(selector).fadeOut(500);
$("#cursor").hide();
$( "#testword" ).effect("shake", {times:2.5}, 450, function(){
clearInput();
$("#cursor").show();
} );
}
function rightInput(selector){
$(selector).fadeIn(1500).delay(500).fadeOut(1500);
clearInput();
}
function clearInput(){
$("#testword").empty();
}
function showPoints(pts){
$(".points").html("+" + pts);
}
//check if the word is valid and clear the input box
//word must be at least 4 letters
//word must contain center letter
//word can't already be found
function submitWord(){
var tryword = document.getElementById('testword');
var centerLetter = document.getElementById('center-letter').firstChild.innerHTML;
let score = 0;
var isPangram = false;
var showScore = document.getElementById("totalScore");
if(tryword.innerHTML.length < 4){
wrongInput("#too-short");
}else if(discoveredWords.includes(tryword.innerHTML.toLowerCase())){
wrongInput("#already-found");
}else if(!tryword.innerHTML.toLowerCase().includes(centerLetter.toLowerCase())){
wrongInput("#miss-center");
}else if(validWords.includes(tryword.innerHTML.toLowerCase())){
var isPangram = checkPangram(tryword.innerHTML);
score = calculateWordScore(tryword.innerHTML, isPangram);
addToTotalScore(score);
console.log("totalscore: " + totalScore);
showDiscoveredWord(tryword.innerHTML);
numFound++;
document.getElementById("numfound").innerHTML = numFound;
document.getElementById("score").innerHTML = totalScore;
var l = tryword.innerHTML.length;
if(isPangram){
rightInput("#pangram");
showPoints(17);
}else if(l < 5){
rightInput("#good");
showPoints(1);
}else if(l<7){
rightInput("#great");
showPoints(l);
}else{
rightInput("#amazing");
showPoints(l);
}
}else{
wrongInput("#invalid-word");
}
}
//if word was valid, display it
//if all words are found end game.
function showDiscoveredWord(input){
var discText = document.getElementById("discoveredText");
discoveredWords.push(input.toLowerCase());
discoveredWords.sort()
while(discText.firstChild){
discText.removeChild(discText.firstChild);
}
var numFound = discoveredWords.length;
var numCol = Math.ceil(numFound/6);
var w = 0;
for(var c=0; c<numCol; c++){
var list = document.createElement("UL");
list.id= "discovered-words"+c;
list.style.cssText = "padding:5px 10px; font-weight:100; ";
discText.appendChild(list);
var n = 6;
if(c == numCol-1){
if(numFound%6 ==0){
if(numFound==0){
n = 0
}
else{
n=6;
}
}else{
n = numFound%6;}
}
for(var i=0; i<n; i++){
var listword = document.createElement("LI");
var pword = document.createElement("P");
pword.innerHTML = discoveredWords[w];
listword.appendChild(pword);
list.appendChild(listword);
w++;
}
}
if (numFound == validWords.length){
alert("You have found all of the possible words! Thanks for playing");
}
}
//adds input "score" to the total score of user
function addToTotalScore(score) {
totalScore += score;
}
//calculates the score of input "input" and also adjusts if "input" is a pangram
function calculateWordScore(input, isPangram) {
let len = input.length;
let returnScore = 1;
if(len > 4) {
if(isPangram) {
returnScore = len + 7;
}else{
returnScore = len;
}
}
console.log('score ' + returnScore)
return returnScore;
}
//checks if "input" word is a pangram
function checkPangram(input) {
var i;
var containsCount = 0;
var containsAllLetters = false;
for(i = 0; i < 7; i++) {
if(input.includes(letters[i])) {
containsCount++;
}
}
if(containsCount == 7) {
containsAllLetters = true;
}
console.log("isPangram?: " + containsAllLetters);
return containsAllLetters;
// console.log(input.value);
// if(input==pangram){
// return true;
// }
return false;
}
function checkIncorrectLetters(input) {
var i;
var badLetterCount = 0;
for(i = 0; i < input.length; i++) {
if(!letters.includes(input[i])) {
badLetterCount++;
}
}
if(badLetterCount > 0) {
return true;
}
return false;
}
//takes keyboard event from user and determines what should be done
function input_from_keyboard(event) {
var tryword = document.getElementById("testword");
if(event.keyCode == 13) {
submitWord();
}
if(event.keyCode == 8) {
deleteLetter();
}
//validation for just alphabet letters input
if(event.keyCode >= 97 && event.keyCode <= 122 ||
event.keyCode >=65 && event.keyCode <=90) {
tryword.innerHTML = tryword.innerHTML+ String.fromCharCode(event.keyCode).toLowerCase();
if(checkIncorrectLetters(tryword.innerHTML)) {
tryword.style.color = 'grey';
}
}
}