forked from violet360/pathFInding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathFinding.js
288 lines (253 loc) · 5.83 KB
/
pathFinding.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
// bug report................
// cells not getting selected after restore is triggered
var algo = 0;
var nodes = [];
var cl = 0;
adj = {}; // adjacency list with key as cell_address a.k.a c_name (5-6) and value is a list of it's children
// travelling = [];
window.test = function(e)
{
algo = e.value;
console.log(algo);
}
function vanish()
{
document.querySelector(".dropdown").style.display="none";
document.querySelector(".initiate").remove();
}
function check_limit(x, y)
{
if(x>=0 && x<23 && y>=0 && y<60)
return true;
else
return false;
}
function create_neighbours(x, y, c_name)
{
var del_X = [-1, 0, 1, 0];
var del_Y = [0, 1, 0, -1];
var children = [];
for(var idx = 0; idx<4; idx++)
{
if(check_limit((x + del_X[idx]), (y + del_Y[idx])))
{
var store_x = x + del_X[idx];
var store_y = y + del_Y[idx];
var child = store_x.toString()+"-"+store_y.toString();
// adj[x.toString()+"-"+y.toString()].push(child);
// console.log(child);
children.push(child);
}
}
adj[c_name] = children;
}
function restore()
{
document.querySelector("#restore_btn").remove();
nodes = [];
for(var x=0; x<23;x++)
{
for(var y=0; y<60;y++)
{
var children = [];
var r = x.toString();
var c = y.toString();
var c_name = r+"-"+c;
var cell = document.getElementById(c_name);
// console.log(adj[c_name]);
cell.style.background = "cyan";
cell.setAttribute("class", "unvisited");
}
}
document.querySelector(".dropdown").style.display="inlinae-block";
var btn = document.createElement("button");
var obj = document.body.appendChild(btn);
obj.innerHTML = "initiate";
btn.setAttribute("onclick", "deal()");
btn.setAttribute("class", "initiate");
cl = 0;
}
function replenish()
{
var btn = document.createElement("button");
var obj = document.body.appendChild(btn);
obj.innerHTML = "restore";
btn.setAttribute("id", "restore_btn");
btn.setAttribute("onclick", "restore()");
}
function deal()
{
algo_obj = new path_finding();
if(nodes.length !=2 && algo == 0)
{
alert("select starting , ending cells and an algo");
return;
}
else if(nodes.length!=2)
{
alert("select starting and ending cells");
return;
}
else if(algo==0)
{
alert("select an algo");
return;
}
else if(algo==1)
{
travelling = [];
vanish();
algo_obj.dfs(nodes[0]);
// for(var t=0; t<travelling.length; t++)
// {
// console.log(travelling[t]);
// // print travveling array such that their is an interval of 2s and the node which you just visited is colored yellow.......
// }
algo_obj.trace();
replenish();
}
else if(algo==2)
{
vanish();
algo_obj.bfs();
replenish();
}
else if(algo==3)
{
vanish();
algo_obj.dk();
replenish();
}
}
//initialises the table after fuck it is pressed.....
function myFunction()
{
var btn = document.createElement("button");
var obj = document.body.appendChild(btn);
obj.innerHTML = "initiate";
btn.setAttribute("onclick", "deal()");
btn.setAttribute("class", "initiate");
document.querySelector("#start_up").remove();
var table = document.getElementById("myTable");
for(var x=0; x<23;x++)
{
var row = table.insertRow(x);
for(var y=0; y<60;y++)
{
var cell = row.insertCell(y);
cell.style.width = "15px";
cell.style.height = "15px";
cell.style.background = "cyan";
var r = x.toString();
var c = y.toString();
var c_name = r+"-"+c;
cell.setAttribute("class", "unvisited");
cell.setAttribute("id", c_name);
cell.setAttribute("onclick", "lit_up(\""+ c_name + "\")");
create_neighbours(x, y, c_name);
// console.log(adj[c_name]+" "+c_name);
}
}
}
function lit_up(i_d)
{
cl = cl + 1;
if(cl<3)
{
var cell_obj = document.getElementById(i_d);
if(cl==1)
{
cell_obj.style.background = "red";
nodes.push(i_d);
}
else if(cl==2)
{
cell_obj.style.background = "black";
nodes.push(i_d);
}
}
else
{
cl = 3;
}
for(var x=0; x<nodes.length; x++)
{
console.log(nodes[x]+" ");
}
}
class path_finding
{
parent = {};
constructor()
{
this.found = false;
}
dfs(src)
{
var x = document.getElementById(src);
if(src==nodes[1])
{
this.found = true;
console.log(11111);
return;
}
x.setAttribute("class", "visited");//changing class of src from unvisited to visited
var address = x.getAttribute("id");
// console.log("This is printed every time XD, chutiye");
setTimeout(function()
{
x.style.background = "yellow";
console.log("dfvdf");
}, 7000);
//the coloring is lagging and two adjacent cells are not colored with an interval if 2 sec........
// x.style.background = "yellow";
for(var idx=0; idx<adj[address].length; idx++)
{
var child = adj[address][idx];
// console.log(child);
if(document.getElementById(child).getAttribute("class") == "unvisited")
{
// travelling.push(child);
console.log(child);
parent[child] = src;
this.dfs(child);
if(this.found)
return;
}
}
// console.log("dfs, bitches!!!");
}
// void dfs(ll src)
// {
// used[src] = true;
// for(ll children:adj[src])
// {
// if(!used[children])
// {
// cout<<children<<endl;
// parent[children] = src;
// dfs(children);
// }
// }
// }
bfs()
{
console.log("bfs, bitches!!!");
}
dk()
{
console.log("dk, bitches!!!");
}
trace()
{
var sell = nodes[1];
var object = document.getElementById(sell);
while(sell!=nodes[0])
{
object = document.getElementById(sell);
setTimeout(function(){object.style.background = "green";}, 5000); //colors not retracing the path!!!
console.log(":::"+sell);
sell = parent[sell];
}
}
}