Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Graph out of global scope and into astar.Graph #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ If you want just the A* search code (not the demo visualization), use code like

<script type='text/javascript' src='astar.js'></script>
<script type='text/javascript'>
var graph = new Graph([
var graph = new astar.Graph([
[1,1,1,1],
[0,1,1,0],
[0,0,1,1]
Expand All @@ -20,7 +20,7 @@ If you want just the A* search code (not the demo visualization), use code like
var result = astar.search(graph, start, end);
// result is an array containing the shortest path

var graphDiagonal = new Graph([
var graphDiagonal = new astar.Graph([
[1,1,1,1],
[0,1,1,0],
[0,0,1,1]
Expand All @@ -30,7 +30,7 @@ If you want just the A* search code (not the demo visualization), use code like
var resultWithDiagonals = astar.search(graphDiagonal, start, end);

// Weight can easily be added by increasing the values within the graph, and where 0 is infinite (a wall)
var graphWithWeight = new Graph([
var graphWithWeight = new astar.Graph([
[1,1,2,30],
[0,4,1.3,0],
[0,0,5,1]
Expand All @@ -50,7 +50,6 @@ The newest version of the algorithm using a Binary Heap. It is quite faster tha
http://www.briangrinstead.com/blog/astar-search-algorithm-in-javascript-updated
Binary Heap taken from http://eloquentjavascript.net/appendix2.html (license: http://creativecommons.org/licenses/by/3.0/)


## Running the test suite

[![Build Status](https://travis-ci.org/bgrins/javascript-astar.png?branch=master)](https://travis-ci.org/bgrins/javascript-astar)
Expand Down
6 changes: 3 additions & 3 deletions astar.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
} else {
var exports = definition();
window.astar = exports.astar;
window.Graph = exports.Graph;
}
})(function() {

Expand Down Expand Up @@ -381,9 +380,10 @@ BinaryHeap.prototype = {
}
};

astar.Graph = Graph;

return {
astar: astar,
Graph: Graph
astar: astar
};

});
4 changes: 2 additions & 2 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

Set up the demo page for the A* Search
*/
/* global Graph, astar, $ */
/* global astar, $ */

var WALL = 0,
performance = window.performance;
Expand Down Expand Up @@ -126,7 +126,7 @@ GraphSearch.prototype.initialize = function() {
nodes.push(nodeRow);
}

this.graph = new Graph(nodes);
this.graph = new astar.Graph(nodes);

// bind cell event, set start/wall positions
this.$cells = $graph.find(".grid_item");
Expand Down
12 changes: 6 additions & 6 deletions test/tests.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

/* global Graph, astar, ok, test, equal */
/* global astar, ok, test, equal */


test( "Sanity Checks", function() {
ok (typeof Graph !== "undefined", "Graph exists");
ok (typeof astar !== "undefined", "Astar exists");
ok (typeof astar.Graph !== "undefined", "Graph exists");
});

test( "Basic Horizontal", function() {
Expand Down Expand Up @@ -56,7 +56,7 @@ test( "Pathfinding", function() {
});

test( "Diagonal Pathfinding", function() {
var result1 = runSearch(new Graph([
var result1 = runSearch(new astar.Graph([
[1,1,1,1],
[0,1,1,0],
[0,0,1,1]
Expand Down Expand Up @@ -92,8 +92,8 @@ test( "Pathfinding to closest", function() {
});

function runSearch(graph, start, end, options) {
if (!(graph instanceof Graph)) {
graph = new Graph(graph);
if (!(graph instanceof astar.Graph)) {
graph = new astar.Graph(graph);
}
start = graph.grid[start[0]][start[1]];
end = graph.grid[end[0]][end[1]];
Expand Down Expand Up @@ -207,7 +207,7 @@ test( "GPS Pathfinding", function() {

// // https://gist.github.com/bgrins/581352
// function runBasic() {
// var graph = new Graph([
// var graph = new astar.Graph([
// [1,1,1,1],
// [0,1,1,0],
// [0,0,1,1]
Expand Down