-
Notifications
You must be signed in to change notification settings - Fork 0
/
animate-aStar.js
56 lines (40 loc) · 1.72 KB
/
animate-aStar.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
function animateAStar(startCell, goalCell, grid) {
const howManyCells = grid.rows * grid.cols;
const closedSet = {};
const openSet = {};
openSet[startCell.index] = startCell;
const cameFrom = {};
const gScore = initScores(howManyCells);
gScore[startCell.index] = 0;
const fScore = initScores(howManyCells);
fScore[startCell.index] = heuristic_cost_estimate(startCell, goalCell, grid.cols);
const res = (function _x_(){
if(Object.keys(openSet).length <= 0) return -1;
const cellIndexWithLowestFScore = lowestFScore(openSet, fScore);
const currentCell = grid.get(cellIndexWithLowestFScore);
if (currentCell === undefined) return console.error('why');
if (currentCell.index === goalCell.index) {
return reconstruct_path(cameFrom, currentCell);
}
delete openSet[currentCell.index];
closedSet[currentCell.index] = currentCell;
const currentCellNeighbors = currentCell.neighbors();
for (let neighbor of currentCellNeighbors) {
if (neighbor instanceof BoundryCell) continue;
if(neighbor.isWall) continue;
if (closedSet[neighbor.index] !== undefined)
continue;
neighbor.isSeen = true;
if (openSet[neighbor.index] === undefined)
openSet[neighbor.index] = neighbor;
const tentative_gScore = gScore[currentCell.index] + dist_between(currentCell, neighbor);
if (tentative_gScore >= gScore[neighbor.index])
continue;
cameFrom[neighbor.index] = currentCell;
gScore[neighbor.index] = tentative_gScore;
fScore[neighbor.index] = gScore[neighbor.index] + heuristic_cost_estimate(neighbor, goalCell, grid.cols);
}
setTimeout(() => _x_() , 10);
}());
return res || new Error("Ooops");
}