Skip to content

Commit 81cf9e8

Browse files
committed
Some formatting and testing isWall with === instead of ==
1 parent da40824 commit 81cf9e8

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

astar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ var astar = {
1919
}
2020
},
2121
heap: function() {
22-
return new BinaryHeap(function(node) {
23-
return node.f;
22+
return new BinaryHeap(function(node) {
23+
return node.f;
2424
});
2525
},
2626
search: function(grid, start, end, diagonal, heuristic) {

graph.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// javascript-astar
22
// http://github.com/bgrins/javascript-astar
33
// Freely distributable under the MIT License.
4-
// Includes Binary Heap (with modifications) from Marijn Haverbeke.
4+
// Includes Binary Heap (with modifications) from Marijn Haverbeke.
55
// http://eloquentjavascript.net/appendix2.html
66

77

8-
var GraphNodeType = {
9-
OPEN: 1,
10-
WALL: 0
8+
var GraphNodeType = {
9+
OPEN: 1,
10+
WALL: 0
1111
};
1212

1313
// Creates a Graph class used in the astar search algorithm.
@@ -16,7 +16,7 @@ function Graph(grid) {
1616

1717
for (var x = 0; x < grid.length; x++) {
1818
nodes[x] = [];
19-
19+
2020
for (var y = 0, row = grid[x]; y < row.length; y++) {
2121
nodes[x][y] = new GraphNode(x, y, row[y]);
2222
}
@@ -46,7 +46,7 @@ function GraphNode(x,y,type) {
4646
this.x = x;
4747
this.y = y;
4848
this.pos = {
49-
x: x,
49+
x: x,
5050
y: y
5151
};
5252
this.type = type;
@@ -57,7 +57,7 @@ GraphNode.prototype.toString = function() {
5757
};
5858

5959
GraphNode.prototype.isWall = function() {
60-
return this.type == GraphNodeType.WALL;
60+
return this.type === GraphNodeType.WALL;
6161
};
6262

6363

@@ -82,21 +82,21 @@ BinaryHeap.prototype = {
8282
// If there are any elements left, put the end element at the
8383
// start, and let it bubble up.
8484
if (this.content.length > 0) {
85-
this.content[0] = end;
86-
this.bubbleUp(0);
85+
this.content[0] = end;
86+
this.bubbleUp(0);
8787
}
8888
return result;
8989
},
9090
remove: function(node) {
9191
var i = this.content.indexOf(node);
92-
92+
9393
// When it is found, the process seen in 'pop' is repeated
9494
// to fill up the hole.
9595
var end = this.content.pop();
9696

9797
if (i !== this.content.length - 1) {
9898
this.content[i] = end;
99-
99+
100100
if (this.scoreFunction(end) < this.scoreFunction(node)) {
101101
this.sinkDown(i);
102102
}
@@ -140,22 +140,24 @@ BinaryHeap.prototype = {
140140
var length = this.content.length,
141141
element = this.content[n],
142142
elemScore = this.scoreFunction(element);
143-
143+
144144
while(true) {
145145
// Compute the indices of the child elements.
146146
var child2N = (n + 1) << 1, child1N = child2N - 1;
147147
// This is used to store the new position of the element,
148148
// if any.
149149
var swap = null;
150+
var child1Score;
150151
// If the first child exists (is inside the array)...
151152
if (child1N < length) {
152-
// Look it up and compute its score.
153-
var child1 = this.content[child1N],
153+
// Look it up and compute its score.
154+
var child1 = this.content[child1N];
154155
child1Score = this.scoreFunction(child1);
155156

156-
// If the score is less than our element's, we need to swap.
157-
if (child1Score < elemScore)
158-
swap = child1N;
157+
// If the score is less than our element's, we need to swap.
158+
if (child1Score < elemScore){
159+
swap = child1N;
160+
}
159161
}
160162

161163
// Do the same checks for the other child.

0 commit comments

Comments
 (0)