-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs.js
297 lines (256 loc) · 10.2 KB
/
bfs.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
// Runs BFS algorithm based on user's selections of start/end/barrier cells.
// Used in pathfinding page only.
// Graph class representing the grid.
class Graph {
constructor(r, c) {
this.numRows_ = r;
this.numCols_ = c;
this.grid_ = "<table>";
this.visited_ = [];
for (var row = 0; row < r; row++) {
this.grid_ += "<tr>";
this.visited_[row] = [];
for (var col = 0; col < c; col++) {
this.grid_ += '<td id="' + row + 'r' + 'c' + col + '"' + '></td>';
this.visited_[row][col] = false;
}
this.grid_ += "</tr>";
}
}
getGrid() {
return this.grid_;
}
getVisitedMatrix() {
return this.visited_;
}
getRows() {
return this.numRows_;
}
getCols() {
return this.numCols_;
}
} // Graph class.
// GridCell class representing the row,col values of a cell in the grid.
class GridCell {
constructor(r, c) {
this.row_ = r;
this.column_ = c;
}
getRow() {
return this.row_;
}
getColumn() {
return this.column_;
}
equals(other) {
return (this.row_ == other.getRow() && this.column_ == other.getColumn());
}
} // GridCell class.
// Array representation of a queue. First-in-first-out (FIFO).
class Queue {
constructor() {
this.q_ = [];
}
add(item) {
this.q_.push(item);
}
remove() {
if (this.isEmpty())
return "Underflow";
return this.q_.shift();
}
isEmpty() {
return this.size() == 0;
}
size() {
return this.q_.length;
}
} // Queue class.
// Search class which implements the BFS algorithm.
class Search {
constructor(visitedMatrix, startRC, endRC, numRows, numCols) {
this.isSuccessful_ = false;
var queue_ = new Queue();
this.edgeTo_ = new Map(); // "#r#c" -> GridCell(prevR, prevC)
this.states = new Queue(); // All the states explored.
queue_.add(startRC);
visitedMatrix[startRC.getRow()][startRC.getColumn()] = true;
var key = "" + startRC.getRow() + "r" + startRC.getColumn() + "c";
this.edgeTo_.set(key, startRC);
// Direction vectors.
var c = [0, 1, 0, -1, 1, 1, -1, -1];
var r = [-1, 0, 1, 0, -1, 1, -1, 1];
var isTrue = true;
while ((!queue_.isEmpty()) && isTrue) {
var currentRC = queue_.remove();
if (!currentRC.equals(startRC) && !currentRC.equals(endRC)) {
this.states.add(currentRC);
}
var currentR = currentRC.getRow();
var currentC = currentRC.getColumn();
for (var i = 0; i < 8; i++) {
var rNeighbor = currentR + r[i];
var cNeighbor = currentC + c[i];
if (!this.isSafe(visitedMatrix, rNeighbor, cNeighbor, numRows, numCols)) { continue; }
if (this.isGoal(rNeighbor, cNeighbor, endRC)) {
isTrue = false;
this.isSuccessful_ = true;
}
visited[rNeighbor][cNeighbor] = true;
var neighbor = new GridCell(rNeighbor, cNeighbor);
key = "" + rNeighbor + "r" + cNeighbor + "c";
this.edgeTo_.set(key, currentRC);
queue_.add(neighbor);
}
}
}
getPath() {
return this.edgeTo_;
}
isSuccessful() {
return this.isSuccessful_;
}
statesExplored() {
return this.states;
}
isSafe(visited, row, col, numRows, numCols) {
return (row >= 0) && (row < numRows) && (col >= 0) && (col < numCols) && (!visited[row][col]);
}
isGoal(row, col, endRC) {
return (row == endRC.getRow()) && (col == endRC.getColumn());
}
} // Search class.
// Flush first (i.e. reset everything).
document.getElementById("instructions").innerHTML = "> Choose where to start!";
$("#instructions").css({"float":"left", "font-weight":"590", "margin-bottom":"7px", "color":"green"});
document.getElementById("tableContainer").innerHTML = "";
document.getElementById("okClear").innerHTML = "";
// Create.
var graph = new Graph(20, 60);
var grid = graph.getGrid(); // Grid.
var visited = graph.getVisitedMatrix(); // Matrix initialized w/ 'false', for not visited.
var rows = graph.getRows(); // # of rows in the grid.
var cols = graph.getCols(); // # of cols in the grid.
var sRC; // The start cell's row,col values.
var eRC; // The end cell's row,col values.
$("#tableContainer").append(grid); // Add the grid to html.
// When 'Okay!' is clicked. Where the magic all happens.
function run() {
var bfs = new Search(visited, sRC, eRC, rows, cols);
$('td').off();
document.getElementById("instructions").innerHTML = "> Please wait until the path is found!";
$('#instructions').css('color', '#48D1CC');
var statesExplored = bfs.statesExplored();
var htmlText = "";
if (bfs.isSuccessful()) {
var path = bfs.getPath();
var t = setInterval(doSequence, 0.01);
function doSequence() {
if (!statesExplored.isEmpty()) {
// Display all the states explored.
var state = statesExplored.remove();
$("#" + state.getRow() + "r" + "c" + state.getColumn()).css('background-color', '#7FFFD4');
} else {
// Retrace/display the path.
var key = "" + eRC.getRow() + "r" + eRC.getColumn() + "c";
var step = path.get(key);
while (!step.equals(sRC)) {
$("#" + step.getRow() + "r" + "c" + step.getColumn()).css('background-color', '#e6e600');
key = "" + step.getRow() + "r" + step.getColumn() + "c";
step = path.get(key);
}
htmlText += "> Path found! Click 'Clear' to reset.";
document.getElementById("instructions").innerHTML = htmlText;
$('#instructions').css('color', 'black');
clearInterval(t);
}
}
} else {
var t = setInterval(doSequence, 0.01);
function doSequence() {
if (!statesExplored.isEmpty()) {
// Display all the states explored.
var state = statesExplored.remove();
$("#" + state.getRow() + "r" + "c" + state.getColumn()).css('background-color', '#7FFFD4');
} else {
htmlText += "> No possible path found. Click 'Clear' to reset and try again!";
document.getElementById("instructions").innerHTML = htmlText;
$('#instructions').css('color', 'black');
clearInterval(t);
}
}
}
}
// Wait for user to select the start/end/barrier. Once 'Okay!' is selected, run() is invoked.
$(function() {
var isMouseDown = false;
var eyeD;
var rc;
var counter = 0;
var htmlText = "";
// On desktops/laptops.
$('td').mousedown(function() {
isMouseDown = true;
if (counter < 1) {
// First selection is the start cell.
eyeD = $(this).attr('id'); // Get 'id' of the 'td' clicked.
// Extract the row and col from the 'id' of the 'td' clicked, and put them in an array.
rc = eyeD.split('rc').map(Number);
$(this).css('background-color', 'green');
$(this).off('mousedown');
$(this).off('mouseover');
counter++;
sRC = new GridCell(rc[0], rc[1]); // Record the start cell's row,col values.
htmlText += "> Now choose where to end. (Click 'Clear' to reset)";
document.getElementById("instructions").innerHTML = htmlText;
$('#okClear')
.append('<input id="reset" type="button" value="Clear" onClick="document.location.reload(false)">');
$('#instructions').css('color', 'blue');
} else if (counter < 2) {
// Second selection is the end cell.
eyeD = $(this).attr('id'); // Get 'id' of the 'td' clicked.
// Extract the row and col from the 'id' of the 'td' clicked, and put them in an array.
rc = eyeD.split('rc').map(Number);
$(this).css('background-color', 'blue');
$(this).off('mousedown');
$(this).off('mouseover');
counter++;
eRC = new GridCell(rc[0], rc[1]); // Record the end cell's row,col values.
htmlText = "> Now create the BARRIERS. ";
htmlText += "(Click 'Search' when done or 'Clear' to reset)";
document.getElementById("instructions").innerHTML = htmlText;
htmlText = '<input id="done" type="button" value="Search" style="background-color:#22272F;" ';
htmlText += 'onclick="run(); this.onclick=null;">';
$('#okClear').append(htmlText);
$('#instructions').css('color', 'red');
} else {
// Barrier selections.
eyeD = $(this).attr('id'); // Get 'id' of the 'td' clicked.
// Extract the row and col from the 'id' of the 'td' clicked, and put them in an array.
rc = eyeD.split('rc').map(Number);
$(this).addClass("highlighted");
$(this).off('mousedown');
counter++;
visited[rc[0]][rc[1]] = true; // Mark all barrier cells as 'true', for visited.
return false; // Prevent text selection.
}
});
// On desktops/laptops. For selecting multiple barrier cells at once (i.e. click and drag).
$('td').mouseover(function () {
if (isMouseDown && counter > 1) {
eyeD = $(this).attr('id'); // Get 'id' of the 'td' clicked.
// Extract the row and col from the 'id' of the 'td' clicked, and put them in an array.
rc = eyeD.split('rc').map(Number);
$(this).addClass("highlighted");
$(this).off('mouseover');
counter++;
visited[rc[0]][rc[1]] = true; // Mark all barrier cells as 'true', for visited.
}
}).bind("selectstart", function () {
return false;
});
// On desktops/laptops.
$('td').mouseup(function () {
isMouseDown = false;
});
});