forked from jambis-prg/real-time-pathfinding-simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.js
More file actions
38 lines (32 loc) · 813 Bytes
/
Copy pathBFS.js
File metadata and controls
38 lines (32 loc) · 813 Bytes
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
class BFS extends PathAlgorithm {
constructor() {
super();
this.queue = [];
}
init(grid, start, goal) {
super.init(grid, start, goal);
this.grid.clear();
this.queue = [start];
this.grid.visit(start, -1);
}
step() {
if (this.queue.length === 0 || this.finished) {
this.finished = true;
return true;
}
let current = this.queue.shift();
if (current === this.goal) {
this.finished = true;
return true;
}
let neighbors = this.grid.neighbors(current);
for (let neighbor of neighbors) {
if (!this.grid.hasVisited(neighbor)) {
this.grid.visit(neighbor, current);
this.queue.push(neighbor);
}
}
this.grid.frontier = [...this.queue]; // Copia os itens da fila para o grid
return false;
}
}