-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wordsearch.js
490 lines (415 loc) · 14.7 KB
/
Wordsearch.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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
function goToPage(nextPage) // function to go to a different page
{
window.location.href = nextPage;
location.replace(nextPage);
}
//----------------------------------------------------------------------
let x1Input = document.querySelector("#x1"); // starting row
x1Input.addEventListener("input", numberEntered, false);
let y1Input = document.querySelector("#y1"); // starting column
y1Input.addEventListener("input", numberEntered, false);
let x2Input = document.querySelector("#x2"); // ending row
x2Input.addEventListener("input", numberEntered, false);
let y2Input = document.querySelector("#y2"); // ending column
y2Input.addEventListener("input", numberEntered, false);
let wordAndDirection;
var table;
var valid;
var correctWordCount = 0;
function numberEntered(e) { // numberentered
valid.value = isValid();
}
// database of words
// some words taken from https://7esl.com/5-letter-words/
var allWords = ["abate", "annual", "above", "accept", "again", "agent",
"appear", "assume", "bank", "begin", "behind", "benefit", "believe",
"behave", "budget", "camera", "catch", "cause", "central", "charge",
"choice", "color", "consume", "design", "detail", "dinner", "discuss", "disgust",
"easy", "early", "edge", "effort", "finish", "focus", "friend", "front",
"garden", "great", "ground", "growth", "happen", "half", "health",
"heavy", "identify", "human", "image", "include", "increase", "kind",
"kitchen", "large", "leader", "level", "local", "machine", "maintain",
"major", "meeting", "minute", "modern", "never", "none", "number",
"occur", "office", "order", "paper", "parent", "partner", "patient",
"player", "popular", "prevent", "protect", "provide", "quality", "quite",
"range", "raise", "rather", "reach", "ready", "realize", "require",
"sign", "simple", "small", "smile", "social", "source", "stock",
"suffer", "table", "teacher", "thought", "travel", "until", "value",
"voice", "watch", "weight", "whole", "whose", "worker", "writer", "young",
"adult", "apple", "award", "beach", "birth", "drama", "dream",
"dress", "drink", "enemy", "entry", "faith", "admit", "isopod", "worm",
"anger", "basis", "block", "board", "brain", "bread", "break", "brown", "buyer",
"chain", "chair", "chest", "chief", "child", "claim", "class",
"clock", "coach", "coast", "court", "cover", "cream", "crime", "cross",
"crowd", "crown", "cycle", "dance", "death", "depth", "doubt", "draft",
"drama", "drive", "earth", "error", "event", "fault", "field", "fight",
"final", "floor", "focus", "force", "frame", "frank", "front", "fruit",
"glass", "grant", "grass", "green", "group", "guide", "heart", "horse",
"hotel", "house", "index", "input", "issue", "judge", "layer", "light",
"limit", "lunch", "metal", "model", "money", "month", "motor", "mouth",
"music", "night", "noise", "north", "novel", "nurse", "offer",
"other", "owner", "panel", "party", "peace", "phase",
"phone", "piece", "pilot", "pitch", "place", "plane", "plant", "plate",
"point", "pound", "power", "press", "price", "pride", "prize", "proof",
"queen", "radio", "ratio", "reply", "right", "river", "round",
"route", "scale", "scene", "scope", "score", "sense", "shape", "share",
"sheep", "sheet", "shift", "shirt", "shock", "sight", "skill",
"sleep", "smile", "sound", "space", "speed", "spite", "sport", "squad",
"staff", "stage", "start", "state", "steam", "steel", "stock", "stone",
"store", "study", "stuff", "style", "sugar", "taste", "theme",
"thing", "title", "total", "touch", "tower", "track", "trade", "train",
"trend", "trial", "trust", "truth", "uncle", "union", "unity",
"video", "visit", "waste", "water", "while", "white",
"whole", "woman", "world", "youth", "slay", "because", "java", "bike",
"happy", "toast", "computer"];
var wordList = createWordList(allWords);
var wordListCopy = [];
function createWordList(database) { // creates a list of 5 random words
var arr = []; // temporary storage
var same = false;
for (let i = 0; i < 5; i++) {
arr[i] = database[Math.floor(Math.random() * database.length)].toLowerCase();
for (let j = 0; j < 5; j++) {
if (i != j && arr[i] == arr[j])
same = true;
}
if (same)
arr[i] = database[Math.floor(Math.random() * database.length)];
}
return arr;
}
function run() { // main running method
// creating a 2d array called table to be the wordsearch table
table = new Array(10); // create an empty array of length n
for (let i = 0; i < 10; i++) {
table[i] = new Array(20); // make each element an array
for (let j = 0; j < 20; j++) // setting each element to 0
table[i][j] = ".";
}
//inserting a word
var directions = ["vertical", "horizontal", "diagonal"]; // array of directions
printWordBank(wordList); // displays word bank on the screen
wordAndDirection = new Array(wordList.length); // pairs a random word with
// a random direction
for (let i = 0; i < wordList.length; i++) {
wordAndDirection[i] = new Array(2);
var word = wordList[i];
wordListCopy[i] = wordList[i];
var dir = directions[Math.floor(Math.random()*3)];
wordAndDirection[i][0] = word;
wordAndDirection[i][1] = dir;
table = insertWord(word, dir, table);
}
// putting random letters wherever there isnt a word
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 20; j++) {
if (table[i][j] == ".")
table[i][j] = String.fromCharCode(Math.floor(Math.random()*26+97));
}
}
displayTable(table);
}
function displayTable(table) { // displays the 2d array as a table
var cell = "";
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 20; j++) {
localStorage.setItem("c", "" + table[i][j]); // stores each value via local storage
document.getElementById("cell" + i + j).innerHTML = localStorage.getItem("c"); // puts on html
}
}
}
function pickNewWord(word) { // picks another word if a word doesnt fit
let i;
for(i = 0; i < wordList.length; i++)
{
if(word == wordList[i])
{
wordList[i] == allWords[Math.floor(Math.random * (allWords.length - 1))];
break;
}
}
return wordList[i];
}
function insertWord(word, dir, table) { // puts the word in the table
var insertTheWord = true; // boolean to check if another word is already
// in a certain area -- if insertTheWord is false, the function shouldn't
// put a new word in that area
if (dir == "horizontal") { // increments the row coords
var x = Math.floor(Math.random()*(20 - word.length));
var y = Math.floor(Math.random()*10)
for (let i = 0; i < word.length; i++) {
if (table[y][x + i] != ".")
insertTheWord = false;
}
if (insertTheWord) {
for (let i = 0; i < word.length; i++)
table[y][x + i] = word[i];
}
else
insertWord(word, dir, table)
return table;
}
if (dir == "vertical" && insertTheWord) { // increments column coords
var x = Math.floor(Math.random()*20);
var y = Math.floor(Math.random()*(10 - word.length));
for (let i = 0; i < word.length; i++) {
if (table[y + i][x] != ".")
insertTheWord = false;
}
if (insertTheWord) {
for (let i = 0; i < word.length; i++)
table[y + i][x] = word[i];
}
else
insertWord(word, dir, table)
return table;
}
if (dir == "diagonal" && insertTheWord) { // increments row and column
var x = Math.floor(Math.random()*(20 - word.length));
var y = Math.floor(Math.random()*(10 - word.length));
for (let i = 0; i < word.length; i++) {
if (table[y + i][x + i] != ".") {
insertTheWord = false;
word = pickNewWord(word);
}
}
if (insertTheWord) {
for (let i = 0; i < word.length; i++)
table[y + i][x + i] = word[i];
}
else
insertWord(word, dir, table)
return table;
}
}
//----------------------------------------------------------------------
function printWordBank(wordList) // prints the word back
{
document.getElementById("word1").innerHTML = wordList[0];
document.getElementById("word2").innerHTML = wordList[1];
document.getElementById("word3").innerHTML = wordList[2];
document.getElementById("word4").innerHTML = wordList[3];
document.getElementById("word5").innerHTML = wordList[4];
}
let x1 = Number(x1Input.value);
let x2 = Number(x2Input.value);
let y1 = Number(y1Input.value);
let y2 = Number(y2Input.value);
function checkWord() // checks the user's input
{
valid = isValid(); //checks if all are true or not
if(!valid)
{
errorModal();
} else {
let isCorrect = checkDirection(x1, x2, y1, y2);
if(isCorrect) {
highlight(x1, y1, x2, y2);
}
else
incorrectModal();
var allZero = true;
for (let i = 0; i < 5; i++) {
if (wordListCopy[i] != 0)
allZero = false;
}
if (allZero)
winModal();
}
}
function isValid()
{
let xStart = false;
let xEnd = false;
let yStart = false;
let yEnd = false;
let startBeforeEnd = false;
if(x1Input.value == Number(x1Input.value)) //idk what this does i just copied ur code
x1 = Number(x1Input.value);
if (x1 >= 1 && x1 <= 10) //checks if valid
xStart = true;
if(y1Input.value == Number(y1Input.value)) //repeat for others
y1 = Number(y1Input.value);
if (y1 >= 1 && y1 <= 20)
yStart = true;
if(x2Input.value == Number(x2Input.value))
x2 = Number(x2Input.value);
if (x2 >= 1 && x2 <= 10)
xEnd = true;
if(y2Input.value == Number(y2Input.value))
y2 = Number(y2Input.value);
if (y2 >= 1 && y2 <= 20)
yEnd = true;
if(x1 <= x2 && y1 <= y2) //checks if the start value comes before end value
startBeforeEnd = true;
if(xStart && xEnd && yStart && yEnd && startBeforeEnd)
return true;
return false;
}
// all modal code is based on https://www.w3schools.com/howto/howto_css_modals.asp
function errorModal()
{
// Get the modal
var modal = document.getElementById("myModal");
// Get the button that opens the modal
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// open the modal
modal.style.display = "block";
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
document.getElementById("message").innerHTML = "ERROR -- PLEASE CHECK THAT" //errormessage
+ "<br>" + "✧ ALL VALUES HAVE BEEN ENTERED CORRECTLY" + "<br>" +
"✧ ALL VALUES ARE WITHIN THE GIVEN RANGE" + "<br>" +
"✧ START VALUE COMES *BEFORE* END VALUE";
}
function incorrectModal()
{
// Get the modal
var modal = document.getElementById("myModal");
// Get the button that opens the modal
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// open the modal
modal.style.display = "block";
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
document.getElementById("message").innerHTML = "YOUR GUESS IS" +
" INCORRECT" + "<br>" + "PLEASE CHECK VALUES AND RE-ENTER";
}
function winModal()
{
// Get the modal
var modal = document.getElementById("myModal2");
// Get the button that opens the modal
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// open the modal
modal.style.display = "block";
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
document.getElementById("win-message").innerHTML = "YOU WIN!!!";
}
function checkDirection(x1, x2, y1, y2) // finds the direction of the word
{
let isCorrect = false;
for (let i = 0; i < wordAndDirection.length; i++) //checks if the word matches a word in wordList
{
if(wordAndDirection[i][1] == "horizontal")
isCorrect = checkHorizontal(wordAndDirection[i][0], x1, x2, y1, y2); //check these next
else if(wordAndDirection[i][1] == "vertical")
isCorrect = checkVertical(wordAndDirection[i][0], x1, x2, y1, y2);
else if(wordAndDirection[i][1] == "diagonal")
isCorrect = checkDiagonal(wordAndDirection[i][0], x1, x2, y1, y2);
if(isCorrect)
{
strikeThrough(i);
break;
}
}
return isCorrect;
}
function checkHorizontal(word, x1, x2, y1, y2) // checks horizontal words
{
let thisWord = "";
for (let i = y1 - 1; i < y2; i++) //checks y coords, x coords are constant
{
if(i >= 20)
return false;
thisWord += table[x1 - 1][i];
if(thisWord == word)
return true;
}
if(thisWord != word)
return false;
return true;
}
function checkVertical(word, x1, x2, y1, y2) // checks vertical words
{
let thisWord = "";
for (let i = x1 - 1; i < x2; i++) //checks x coords, y coords are constant
{
if(i >= 10)
return false;
thisWord += table[i][y1 - 1];
if(thisWord == word)
return true;
}
if(thisWord != word)
return false;
return true;
}
function checkDiagonal(word, x1, x2, y1, y2) // checks diagonal words
{
let thisWord = "";
for(let i = 0; i < word.length; i++)
{
if(((x1 + i - 1) >= 10) || ((y1 + i - 1) >= 20))
return false;
thisWord += table[x1 + i - 1][y1 + i - 1];
if(thisWord == word && x2 == (x1 + i) && y2 == (y1 + i))
return true;
}
return false;
}
function highlight(x1, y1, x2, y2) { // changes the color of a correct word on the table
let word = "";
//style.color and style.fontWeight idea from w3schools https://www.w3schools.com/js/js_htmldom_css.asp
if (y1 == y2) {
for (let i = x1-1; i < x2; i++) {
word += table[i][y1 - 1];
document.getElementById("cell" + i + (y1-1)).style.color = "rgb(236, 226, 208)";
document.getElementById("cell" + i + (y1-1)).style.fontWeight = "bold";
}
}
else if (x1 == x2) {
for (let i = y1-1; i < y2; i++) {
word += table[x1 - 1][i];
document.getElementById("cell" + (x1-1) + i).style.color = "rgb(236, 226, 208)";
document.getElementById("cell" + (x1-1) + i).style.fontWeight = "bold";
}
}
else {
let length = Math.abs(x1 - x2) + 1;
for (let i = 0; i < length; i++) {
word += table[x1 + i - 1][y1 + i - 1];
document.getElementById("cell" + (x1 + i - 1) + (y1 + i - 1)).style.color = "rgb(236, 226, 208)";
document.getElementById("cell" + (x1 + i - 1) + (y1 + i - 1)).style.fontWeight = "bold";
}
}
for (let i = 0; i < wordList.length; i++)
{
if(word == wordList[i]) {
wordListCopy[i] = "0";
strikeThrough(i);
}
}
}
function strikeThrough(wordIndex) // crosses out correct words in the word bank
{
document.getElementById("word" + (wordIndex + 1)).style.textDecoration = "line-through";
}