-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
207 lines (185 loc) · 6.18 KB
/
script.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
let words = ["datalists","overseas","input","index", "styles",];
let rows =9;
let columns=9;
var board;
window.onload = function(){
showTheSearchWords();
setWordSearchBoard();//pick any row and any cell and if in horizontal line or vertical line there exists empty gridCells whose count is words[i].length then add those word
fillUpEmptyGridCells();//fill up the empty grid cells
}
function showTheSearchWords(){
const displayContainer = document.querySelector(".words-list");
let text = "<ul>";
let length = words.length;
let ul = document.createElement("ul");
for(let i=0;i<length;i++)
{
let li = document.createElement("li");
li.innerHTML= words[i];
li.classList.add(`${words[i]}`);
ul.appendChild(li);
}
displayContainer.appendChild(ul);
}
let wordSearch = document.querySelector(".search-container");
function setWordSearchBoard(){
board=[];
for(let i=0;i<rows;i++)
{
let rows=[];
for(let j=0;j<columns;j++)
{
rows.push(' ');
let gridCell = document.createElement("div");
gridCell.id = i+"id"+j;
gridCell.classList.add("grid-cell");
gridCell.setAttribute("onmousedown","mouseDownFunction(this)");
gridCell.setAttribute("onmouseover","mouseOverFunction(this)");
gridCell.setAttribute("onmouseup","mouseLeaveFunction(this)");
wordSearch.append(gridCell);
}
board.push(rows);
}
addletters();
}
function addletters(){
let wordsLength = words.length;
let i=0;
while(i<wordsLength)//for every word in words do the below and increment i only when word[i] is put on grid
{
let randomrow = Math.floor(Math.random()*rows);
let randomColumn = Math.floor(Math.random()*columns);
if(board[randomrow][randomColumn]== " ")//if fails then loops until any one row and column is " "
{
if(checkIfExists(words[i],randomrow,randomColumn,0,1)==true)
{
addThelettersHorizontally(randomrow , randomColumn,words[i],columns);
i++;
continue; //if condition is satified stop the current loop
}
if(checkIfExists(words[i],randomrow,randomColumn,1,0)==true)
{
addThelettersVertically( randomrow,randomColumn,words[i],rows);
i++;
continue;
}
}
}
}
function addThelettersHorizontally(row,column,word,length){
console.log("lette")
let w=0;
for(let i=column;i<columns;i++)
{
if(w<word.length)
{
let gridCell = document.getElementById(row+"id"+i);
gridCell.textContent= word[w].toUpperCase();
board[row][i]=gridCell.textContent;
w+=1;
}
}
}
//randomly pick any row and any column , and from that row and that column check if horizontally there exists word[i].length empty grid cells . Consider words.length is 4 and randomRow is 2 and randomcolumn is 3 now we check if from board[2][3] we check if empty grid cells exists .if exists then from [2][3] start adding the letter in grids (for horizontal)
function checkIfExists(word , r, c, i ,j){
let count=1;
let wordlength = word.length;
while(r>=0 && c>=0 && r<rows && c<columns && board[r][c]== " ")
{
if(wordlength<=count)
{
return true;
}
count++;
r+=i;
c+=j;
}
return false;
}
function addThelettersVertically(row,column,word,length){
let w=0;
for(let i=row;i<rows;i++)
{
if(w<word.length)
{
let gridCell = document.getElementById(i+"id"+column); //i+column because we are moving vertically so i is same and column changes
gridCell.textContent= word[w].toUpperCase();
board[i][column]=gridCell.textContent;
w+=1;
}
}
}
function fillUpEmptyGridCells(){
for(let i=0;i<rows;i++)
{
for(let j=0;j<columns;j++)
{
if(board[i][j]==" ")
{
let index = Math.floor(Math.random()*26);
index = 65+index;//uppercase 97-lowercase
let gridCell = document.getElementById(i+"id"+j);
gridCell.textContent= String.fromCharCode(index);
board[i][j]=gridCell.textContent;
}
}
}
}
let selectedWord = [];
let clicking = false;
function mouseDownFunction(event)
{
clicking=true;//when user first clicks the gridcell make a flag as true which indicates the user started to search and add that to []
if(!selectedWord.includes(event.id))
{
event.classList.add("active")
selectedWord.push(event.id);
}
console.log(selectedWord);
}
function mouseOverFunction(event){
if(clicking==true)//to which ever gridcell user hovers add that to []
{
if(!selectedWord.includes(event.id))
{
event.classList.add("active")
selectedWord.push(event.id);
}
}
}
function mouseLeaveFunction(event){
clicking=false;
let length = selectedWord.length;
let text = "";
for(let i=0;i<length;i++)
{
let gridCells = selectedWord[i].split("id");
text+=board[gridCells[0]][gridCells[1]];
}
if(words.includes(text.toLowerCase()))
{
let grids = document.querySelectorAll(".grid-cell");
grids.forEach((grid)=>{
grid.classList.contains("active")? grid.classList.add("found"):"";
});
let word = document.querySelector(`.${text.toLowerCase()}`);
console.log(word);
word.classList.add("linethrough");
}
else{
let grids = document.querySelectorAll(".grid-cell");
grids.forEach((grid)=>{
grid.classList.contains("active")? grid.classList.remove("active"):"";
});
}
text="";
selectedWord=[];
}
document.addEventListener("mousemove",function(e)
{
const container = wordSearch.getBoundingClientRect();
if(e.clientX < container.left || e.clientX > container.right || e.clientY < container.top || e.clientY > container.bottom)
{
selectedWord=[];
}
})