A* Search Algorithm in JavaScript

Note that this code has been updated. I have an updated blog post detailing what changed. The new source code is available at https://github.com/bgrins/javascript-astar, and the code as of the original post is still available here: https://github.com/bgrins/javascript-astar/tree/0.0.1/original-implementation

View the online demo

I first implemented the A* algorithm for a research group I was in through school (Computer Graphics and Image Understanding). A* is a best-first, graph search algorithm. Some basic information can be found on the Wikipedia page for A* and the external links contained in it. Please refer to that page for general reference about the algorithm, I will simply explain in my own words how it works and how I got it working.

A* algorithm in JavaScript

A* algorithm in JavaScript

Why JavaScript?

Because it was easy to deploy!

Since I know JavaScript pretty well, and most of the examples you can find are in C, Java or a similar language that you cannot run without downloading source code or executables, I thought it would be a good idea to program it on an html page. This way, people could see what was going on and view the source very easily by using the online demo.

My hope was to build a page that could be extended with other search algorithms by separating the UI code (that generates a graph with walls and animates the path that is determined by an algorithm), and the algorithm that finds the path. Maybe I will get around to plugging in some more algorithms sometime and making it into a little resource for graph search algorithms.

How?

search.html

Just a basic html file that includes jQuery, the excellent JavaScript library, main.css, graph.js, and astar.js. Also, I have a JavaScript block that initializes the page.

graph.js

The point of this file is to build the graph, call the search function, and animate the results after the search has returned. It also has an option to show the debugging information created by the search algorithm. I won’t get too into the code here, since it distracts from the search algorithm.

Please take a look at it, be aware that there are some improvements I would make if I was to rewrite this today. There are some performance issues that could be addressed, and It modifies the Array.prototype to add on specific methods (findGraphNode and removeGraphNode) for search algorithms, which may not be ideal for bigger projects. For this little page, I’m not too worried about it, but if I do get around to adding in more algorithms, I will probably improve this code.

astar.js

This is the actual implementation of the algorithm. I will do my best to explain what is going on, but feel free to just look at the source of the example, or just download astar.js.

There are three functions that we keep track of for nodes that we look at:

  • g(x): The total cost of getting to that node (pretty straightforward). If we reach a node for the first time or reach a node in less time than it currently took, then update the g(x) to the cost to reach this node.
  • h(x): The estimated time to reach the finish from the current node. This is also called a heuristic. We online need to update this if it is not set already, since the distance to the finish will not change even if the path we took to arrive at a node changes. Note: There are many different ways to guess how far you are from the end, I use the Manhattan distance in this implementation.
  • f(x): Simply g(x) + h(x). The lower the f(x), the better. Think about it like this: the best node is one that takes the least total amount of time to arrive at and to get to the end. So, a node that took only 1 step to arrive at and 5 to get to the end is more ideal than one that took 10 to arrive and and only 1 to get to the end.

Here is some high level pseudocode of what is happening in the algorithm. Also see the Wikipedia pseudocode for another example.

  push startNode onto openList
  while(openList is not empty) {
     currentNode = find lowest f in openList
     if currentNode is final, return the successful path
     push currentNode onto closedList and remove from openList
     foreach neighbor of currentNode {
         if neighbor is not in openList {
                save g, h, and f then save the current parent
                add neighbor to openList
         }
         if neighbor is in openList but the current g is better than previous g {
                 save g and f, then save the current parent
         }
     }

Here is the JavaScript for the list implementation:

 
var astar = {
	init: function(grid) {
		for(var x = 0; x < grid.length; x++) {
			for(var y = 0; y < grid[x].length; y++) {
				grid[x][y].f = 0;
				grid[x][y].g = 0;
				grid[x][y].h = 0;
				grid[x][y].debug = "";
				grid[x][y].parent = null;
			}	
		}
	},
	search: function(grid, start, end) {
		astar.init(grid);
 
		var openList   = [];
		var closedList = [];
		openList.push(start);
 
		while(openList.length > 0) {
 
			// Grab the lowest f(x) to process next
			var lowInd = 0;
			for(var i=0; i<openList.length; i++) {
				if(openList[i].f < openList[lowInd].f) { lowInd = i; }
			}
			var currentNode = openList[lowInd];
 
			// End case -- result has been found, return the traced path
			if(currentNode.pos == end.pos) {
				var curr = currentNode;
				var ret = [];
				while(curr.parent) {
					ret.push(curr);
					curr = curr.parent;
				}
				return ret.reverse();
			}
 
			// Normal case -- move currentNode from open to closed, process each of its neighbors
			openList.removeGraphNode(currentNode);
			closedList.push(currentNode);
			var neighbors = astar.neighbors(grid, currentNode);
 
			for(var i=0; i<neighbors.length;i++) {
				var neighbor = neighbors[i];
				if(closedList.findGraphNode(neighbor) || neighbor.isWall()) {
					// not a valid node to process, skip to next neighbor
					continue;
				}
 
				// g score is the shortest distance from start to current node, we need to check if
				//	 the path we have arrived at this neighbor is the shortest one we have seen yet
				var gScore = currentNode.g + 1; // 1 is the distance from a node to it's neighbor
				var gScoreIsBest = false;
 
 
				if(!openList.findGraphNode(neighbor)) {
					// This the the first time we have arrived at this node, it must be the best
					// Also, we need to take the h (heuristic) score since we haven't done so yet
 
					gScoreIsBest = true;
					neighbor.h = astar.heuristic(neighbor.pos, end.pos);
					openList.push(neighbor);
				}
				else if(gScore < neighbor.g) {
					// We have already seen the node, but last time it had a worse g (distance from start)
					gScoreIsBest = true;
				}
 
				if(gScoreIsBest) {
					// Found an optimal (so far) path to this node.	 Store info on how we got here and
					//	just how good it really is...
					neighbor.parent = currentNode;
					neighbor.g = gScore;
					neighbor.f = neighbor.g + neighbor.h;
					neighbor.debug = "F: " + neighbor.f + "<br />G: " + neighbor.g + "<br />H: " + neighbor.h;
				}
			}
		}
 
		// No result was found -- empty array signifies failure to find path
		return [];
	},
	heuristic: function(pos0, pos1) {
		// This is the Manhattan distance
		var d1 = Math.abs (pos1.x - pos0.x);
		var d2 = Math.abs (pos1.y - pos0.y);
		return d1 + d2;
	},
	neighbors: function(grid, node) {
		var ret = [];
		var x = node.pos.x;
		var y = node.pos.y;
 
		if(grid[x-1] && grid[x-1][y]) {
			ret.push(grid[x-1][y]);
		}
		if(grid[x+1] && grid[x+1][y]) {
			ret.push(grid[x+1][y]);
		}
		if(grid[x][y-1] && grid[x][y-1]) {
			ret.push(grid[x][y-1]);
		}
		if(grid[x][y+1] && grid[x][y+1]) {
			ret.push(grid[x][y+1]);
		}
		return ret;
	}
};

And here is a faster implementation, using a Binary Heap instead of a list. This is a lot faster and also includes the option to search diagonally – 8 directional movement. Head over to the astar graph search project page to get the latest version of the code!

var astar = {
    init: function(grid) {
        for(var x = 0, xl = grid.length; x < xl; x++) {
            for(var y = 0, yl = grid[x].length; y < yl; y++) {
                var node = grid[x][y];
                node.f = 0;
                node.g = 0;
                node.h = 0;
                node.cost = 1;
                node.visited = false;
                node.closed = false;
                node.parent = null;
            }
        }
    },
    heap: function() {
        return new BinaryHeap(function(node) { 
            return node.f; 
        });
    },
    search: function(grid, start, end, diagonal, heuristic) {
        astar.init(grid);
        heuristic = heuristic || astar.manhattan;
        diagonal = !!diagonal;
 
        var openHeap = astar.heap();
 
        openHeap.push(start);
 
        while(openHeap.size() > 0) {
 
            // Grab the lowest f(x) to process next.  Heap keeps this sorted for us.
            var currentNode = openHeap.pop();
 
            // End case -- result has been found, return the traced path.
            if(currentNode === end) {
                var curr = currentNode;
                var ret = [];
                while(curr.parent) {
                    ret.push(curr);
                    curr = curr.parent;
                }
                return ret.reverse();
            }
 
            // Normal case -- move currentNode from open to closed, process each of its neighbors.
            currentNode.closed = true;
 
            // Find all neighbors for the current node. Optionally find diagonal neighbors as well (false by default).
            var neighbors = astar.neighbors(grid, currentNode, diagonal);
 
            for(var i=0, il = neighbors.length; i < il; i++) {
                var neighbor = neighbors[i];
 
                if(neighbor.closed || neighbor.isWall()) {
                    // Not a valid node to process, skip to next neighbor.
                    continue;
                }
 
                // The g score is the shortest distance from start to current node.
                // We need to check if the path we have arrived at this neighbor is the shortest one we have seen yet.
                var gScore = currentNode.g + neighbor.cost;
                var beenVisited = neighbor.visited;
 
                if(!beenVisited || gScore < neighbor.g) {
 
                    // Found an optimal (so far) path to this node.  Take score for node to see how good it is.
                    neighbor.visited = true;
                    neighbor.parent = currentNode;
                    neighbor.h = neighbor.h || heuristic(neighbor.pos, end.pos);
                    neighbor.g = gScore;
                    neighbor.f = neighbor.g + neighbor.h;
 
                    if (!beenVisited) {
                        // Pushing to heap will put it in proper place based on the 'f' value.
                        openHeap.push(neighbor);
                    }
                    else {
                        // Already seen the node, but since it has been rescored we need to reorder it in the heap
                        openHeap.rescoreElement(neighbor);
                    }
                }
            }
        }
 
        // No result was found - empty array signifies failure to find path.
        return [];
    },
    manhattan: function(pos0, pos1) {
        // See list of heuristics: http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html
 
        var d1 = Math.abs (pos1.x - pos0.x);
        var d2 = Math.abs (pos1.y - pos0.y);
        return d1 + d2;
    },
    neighbors: function(grid, node, diagonals) {
        var ret = [];
        var x = node.x;
        var y = node.y;
 
        // West
        if(grid[x-1] && grid[x-1][y]) {
            ret.push(grid[x-1][y]);
        }
 
        // East
        if(grid[x+1] && grid[x+1][y]) {
            ret.push(grid[x+1][y]);
        }
 
        // South
        if(grid[x] && grid[x][y-1]) {
            ret.push(grid[x][y-1]);
        }
 
        // North
        if(grid[x] && grid[x][y+1]) {
            ret.push(grid[x][y+1]);
        }
 
        if (diagonals) {
 
            // Southwest
            if(grid[x-1] && grid[x-1][y-1]) {
                ret.push(grid[x-1][y-1]);
            }
 
            // Southeast
            if(grid[x+1] && grid[x+1][y-1]) {
                ret.push(grid[x+1][y-1]);
            }
 
            // Northwest
            if(grid[x-1] && grid[x-1][y+1]) {
                ret.push(grid[x-1][y+1]);
            }
 
            // Northeast
            if(grid[x+1] && grid[x+1][y+1]) {
                ret.push(grid[x+1][y+1]);
            }
 
        }
 
        return ret;
    }
};

Conclusion

This A* search implementation could be used as a component to larger system (like a game – maybe tower defense or puzzle), or just for learning purposes. I have done my best to make the code understandable and to present the concepts in a way that would help someone who has never seen the algorithm before, or someone who is not very familiar with JavaScript.

Feel free to view the demo, or download the latest version of the astar.js file to mess around with it. Check out the javascript-astar project page on Github for the latest code and documentation

Tags: , , ,

55 Responses to “A* Search Algorithm in JavaScript”

  1. Jelle Says:

    You’re using A* instead of Dijkstra for some reason, I assume speed. So why do you loop through the open list instead of using a heap? Boom, N^2 instead of NlgN.

  2. brian Says:

    Jelle, thanks for the feedback. I built it using a linked list because it was the most straightforward way to understand the code. This was more of a learning exercise for myself with the hope of being useful to anyone else.

    However, I would be interested in optimizations. I am not aware of a JavaScript heap object that is provided from the standard library, but I found a couple of examples online in case anyone is interested.

    It would be interesting to implement that and compare the performance between the two.

  3. Zack J. Says:

    I’ve got an Actionscript implementation of A* that I’ve been cleaning up recently. I also added a smoothing step at the end to remove unnecessary nodes at the end; it makes it much more useful for game AI.

  4. brian Says:

    Zack, could you go into a little more detail about removing the unnecessary nodes or provide a link to your code so I can look at it?

    By the way, A* Pathfinding for Beginners, which you linked to from your site, is a great resource for anyone looking for more information about the algorithm.

  5. Zack J. Says:

    Here’s an example running. I haven’t posted it on the site or posted the source because I’m not sure what we’re going to do with it yet:

    http://pixelwelders.com/experiments/Pathfinder2/

    It could obviously be optimized even further, but this example shows the search process (yellow), and then the final path (blue line). It eliminates the stairstep and wall-hugging, as well as some other strange-looking paths that you sometimes get with A*.

  6. Justin Says:

    That’s neat!

    I have a tutorial and C++ implementation of A* which uses an STL priority queue and has various optimisations.

    http://code.google.com/p/a-star-algorithm-implementation/
    http://www.heyes-jones.com/astar.html

  7. Harvey Wasyliszyn Says:

    Hi, I found this article while searching for help with JavaScript. I’ve recently changed browsers from Chrome to Internet Explorer 7. Now I seem to have a issue with loading JavaScript. Everytime I browse website that requires Javascript, the page does not load and I get a “runtime error javascript.JSException: Unknown name”. I cannot seem to find out how to fix the problem. Any aid is greatly appreciated! Thanks

  8. Van Schutze Says:

    Thanks for sharing the information!

  9. Learn Drums Says:

    Hello, I read all your articles, keep them coming.

  10. Ronald Nannen Says:

    I am definitely bookmarking this page and sharing it with my friends.

  11. A-Star Algorithm using JavaScript « Blog by Gagan Says:

    [...] vector data are available.  One of these is finding shortest path for road network. I have taken Brian Grinstead JavaScript code for A-Star [...]

  12. A* Search Algorithm in JavaScript | Extra Future Says:

    [...] Looks like a good implementation, be sure to check out the demo. [...]

  13. Shortest Path with Geometric Network « Blog by Gagan Says:

    [...] This is based on A-Start Algorithm . A JavaScript  implementation of A- Star is nicely done by Brian Grinstead,  same code I have included in my project. Actually scalability of the graph and related [...]

  14. nota fiscal eletronica Says:

    How can i subscribe to your channel ? i really like to see more of what you can

  15. b Says:

    Nice work!! Your script is really nice. I’m digging through it now :) Thanks!

  16. Pathfinding | Tim's Blog Says:

    [...] http://www.briangrinstead.com/blog/asta … javascript [...]

  17. Brian Grinstead » Blog Archive » A* Search Algorithm in JavaScript (Updated) Says:

    [...] I first wrote the A* Search in JavaScript article I knew there were some things that I knew could be improved to make the pathfinding faster. [...]

  18. Locksmith Knoxville Says:

    For reasons uknown i’m ending up with a blank page whenever i make an attempt to post a comment,do you know the reason why its transpiring?i’m using oprea web-browser

  19. using jquery Says:

    Excellent read. Proper search algorithms are a difficult thing.

  20. Drusilla Nacke Says:

    I’d come to engage with you here. Which is not something I typically do! I enjoy reading a post that will make people think. Also, thanks for allowing me to speak my mind!

  21. David Says:

    I wish someone could teach me some JavaScript.

  22. Mixed Martial Arts Says:

    Thanks for your clear-cut explanation of the A* search algorithm. Cleared up some issues I’ve been having.

  23. as3isolib and A* Pathfinding | silverbase.net Says:

    [...] in integrating A* algorithm. One of them is by AJ Piergiovanni (Rocketman) and the other one is by Brian Grinstead (JavaScript). I recommend both of them as well as A* Pathfinding for Beginners by Patrick [...]

  24. Luca Says:

    Thank you! What’s about the copyright?

  25. Brian Says:

    Luca,
    The code is available under an MIT style license. All source code can be found at https://github.com/bgrins/javascript-astar.

  26. Age of empire browser versie (poc) | svenn.d Says:

    [...] gebruikt, de villagers mogen niet door objecten heen lopen toch ?! Ik heb hier gekozen voor het A*pathfinding algoritme. Een bijkomstig probleem is dat het pathfinding iedere stap moet herhaald worden, gezien het pad [...]

  27. as3isolib and A* Pathfinding | shiftArray() Says:

    [...] in integrating A* algorithm. One of them is by AJ Piergiovanni (Rocketman) and the other one is by Brian Grinstead (JavaScript). I recommend both of them as well as A* Pathfinding for Beginners by Patrick [...]

  28. felix Says:

    Hi im trying to build a html/javascript browser RTS game engine .. so the first pathfind javascript i found wa this one and it works well :)
    but before i searcht for finished pathfind scripts i have started my own witch is now almost finisht .. if you want to, you can add this here .. (if it is good enough ;) ) you can find it here http://ronco.packagecloud.com/path/
    your a* is also in this demo to compere..

  29. A* Search Algorithm in JavaScript | Just thoughts Says:

    [...] via Brian Grinstead » Blog Archive » A Search Algorithm in JavaScript. [...]

  30. genduet Says:

    how used A Search Algorithm in google maps,,,? can u help me? thanks a lot … :)

  31. Mentifex Says:

    Nice to find this AI-related JavaScript resource!

  32. host then profit Says:

    An impressive share, I simply given this onto a colleague who was doing a little analysis on this. And he in actual fact bought me breakfast because I discovered it for him.. smile. So let me reword that: Thnx for the deal with! However yeah Thnkx for spending the time to debate this, I feel strongly about it and love studying extra on this topic. If attainable, as you grow to be experience, would you mind updating your blog with extra particulars? It’s highly useful for me. Big thumb up for this blog submit!

  33. Junior Schrope Says:

    I just added this webpage to my google reader, fantastic stuff. Can’t get enough!

  34. Pokemon Tower Defense Says:

    Exellent read! I loved Tower Defense Games so much i made one based on the Pokemon TD game.

  35. autism recovery Says:

    autism recovery…

    [...]Brian Grinstead » Blog Archive » A* Search Algorithm in JavaScript[...]…

  36. Johnnie Furber Says:

    Strong work always good to come across a really thoughtful website – I’ve bookmarked it. Add article to my site.

  37. Michale Says:

    Submitted to Stumbleupon as well as got you with a plus 1!

  38. Loan Consolidation Plan Says:

    Hello, everyone, I just came here, nice to satisfy you, welcome to visit my site and space, happy to be friends to you, love your little Tom

  39. Spellfork Says:

    Hi to everyone! I’ve been trying out things with this astar implementation and it works really well. I was however wondering if there is someone who has used this code and extended it with weighted paths? To clarify what I mean: Say that all the squares in the grid have a weight that can differ and the search will try to find a successful path from point A to point B that has the least weight value based on the weights of the squares. Think of it as a game where movement on roads costs less than movement in mountains? Maybe there is another type of algorithm that is more suitable, so if anyone can provide some input to point me in the right direction I would be ever so thankful.

  40. Brian Says:

    Spellfork,
    This would not be too difficult to add in. Basically just allow the grid to have different costs (other than 1), the the line

    var gScore = currentNode.g + 1;

    would become something like:

    var gScore = currentNode.g + currentNode.cost;

    Where ‘cost’ is a property that is initialized in astar.init based on the input grid.

    Please add a comment to the following issue expressing that this would be useful to you: https://github.com/bgrins/javascript-astar/issues/3. We can talk more there about what it would take to get it added into this implementation.

  41. Spellfork Says:

    Thanks for your reply Brian, I will try and experiment with this for a bit and also head over to the github page.

  42. website Says:

    2 potato

  43. sarah Says:

    thanks for a wonderful i want to ask some help. i want to code astar in c.sharp on map(vector shape file). i don’t know how to do this? and where to start. i want to use postgis at backend. but i am confused in programming i couldn’t find any libraries etc

    kindly help me and guide me.

    Regards,

    Sarah

  44. cosmeticsaresafe.org Says:

    cosmeticsaresafe.org…

    [...]Brian Grinstead » Blog Archive » A* Search Algorithm in JavaScript[...]…

  45. Dan Says:

    This is great! I got it working… one thing… my Graph() prints out 10 x 15.. 10 rows downs.. 15 columns across.

    However, after 10×10… the right 5 on each row will not work.. here is what I am using… inside this paste.. I also included a link to an online example.

    How do I make it work past the right 5 columns?

    http://pastebin.com/KZGhD1zD

  46. Astar Graph Search – Variable Costs - Brian Grinstead Says:

    [...] been working on updating my A* Graph Search Algorithm (A* on Github) and toying with the idea of adding variable costs to the grid, also known as [...]

  47. 2D Game – Progress | LiamBateman.net Says:

    [...] A Star Path Finding Simple Inheritence [...]

  48. Can’t Implementing A* on IE? | Jisku.com Says:

    [...] I’m using the astar code from this website: http://www.briangrinstead.com/blog/astar-search-algorithm-in-javascript [...]

  49. Marcel Says:

    Hey there,
    this is a really interesting article, and also an interesting topic.
    I’m doing some path-finding experiments and i am facing a problem using your code.
    The returned result array is always undefined.
    The coordinates are correctly while pushed in the array, but after returning it, the array
    is undefined. I didn’t change your source :(

    Here is the snippet, where i am trying to get the coordinates:

    var graph = new Graph([
    [0,1,0,0,0,0,0,0,0], // 0 is open, 1 is wall
    [0,1,0,0,0,0,0,0,0],
    [0,1,0,0,0,0,0,0,0],
    [0,1,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0]
    ]);

    var start = graph.nodes[0][0];
    var end = graph.nodes[4][4];
    var result = astar.search(graph.nodes, start, end);

    alert(“Result: ” + result[1]);

    Does anyone know the error?

    Greetings,
    Marcel

  50. Sam Says:

    Hi there,

    at Marcel, did you try to use 1 for free and 0 for wall? ;)

    at brian, first of all great code! :)
    Are there any settings for the Euclidean find type?
    I mean like on http://devpro.it/examples/astar/index2.html
    there is euclidean and euclideanFree – mode, there is a difference between them.
    euclidean can´t move as free as euclideanFree which is much better for realistic move of units is a RTS game in my case.

    Thank you for the answer!
    Greetings
    Sam

  51. Brian Says:

    Sam,
    If you use the `diagonal` option it can move similarly to Euclidean Free. Basically, pass true as the 4th parameter to astar.search. By default it uses the Manhattan heuristic, but if you’d rather use Euclidean, you could write your own callback and pass it in as the 5th parameter.

  52. Sam Says:

    hey :) the basic code worked like “EuclideanFree”, so I extended the ifs for “Euclidean” like in the exsample post before
    im not 100% for mistakes but the test looks good ^^

    all you have to do now is to say “Euclidean” oder “EuclidianFree” at the 4th parameter

    if (diagonals == “Euclidean”) { //new mode

    // Southwest
    if (grid[x-1] && grid[x-1][y-1])
    {
    //push if no wall in the south and no wall in the west
    if(!(grid[x][y-1].closed || grid[x][y-1].isWall()) && !(grid[x-1][y].closed || grid[x-1][y].isWall()))
    {
    ret.push(grid[x-1][y-1]);
    }
    }

    // Southeast
    if(grid[x+1] && grid[x+1][y-1])
    {
    //push if no wall in the south and no wall in the east
    if (!(grid[x][y-1].closed || grid[x][y-1].isWall()) && !(grid[x+1][y].closed || grid[x+1][y].isWall()))
    {
    ret.push(grid[x+1][y-1]);
    }
    }

    // Northwest
    if(grid[x-1] && grid[x-1][y+1])
    {
    //push if no wall in the north and no wall in the west
    if (!(grid[x][y+1].closed || grid[x][y+1].isWall()) && !(grid[x-1][y].closed || grid[x-1][y].isWall()))
    {
    ret.push(grid[x-1][y+1]);
    }
    }

    // Northeast
    if(grid[x+1] && grid[x+1][y+1])
    {
    //push if no wall in the north and no wall in the east
    if (!(grid[x][y+1].closed || grid[x][y+1].isWall()) && !(grid[x+1][y].closed || grid[x+1][y].isWall()))
    {
    ret.push(grid[x+1][y+1]);
    }
    }

    }
    else if (diagonals == “EuclideanFree”) { //basic mode

    // Southwest
    if(grid[x-1] && grid[x-1][y-1]) {
    ret.push(grid[x-1][y-1]);
    }

    // Southeast
    if(grid[x+1] && grid[x+1][y-1]) {
    ret.push(grid[x+1][y-1]);
    }

    // Northwest
    if(grid[x-1] && grid[x-1][y+1]) {
    ret.push(grid[x-1][y+1]);
    }

    // Northeast
    if(grid[x+1] && grid[x+1][y+1]) {
    ret.push(grid[x+1][y+1]);
    }

    }

    return ret;
    }
    };

  53. Rachel Says:

    Hi

    Great code.

    2 Questions:

    How can I make the path line stay viable

    How can I automatically make the path display when I pass an “end” value?

    Thanks

  54. Giancarlo Says:

    It can be applied in a isometric grid, for games?

  55. as3isolib and A* Pathfinding | shiftarray.com Blog Archive Says:

    […] in integrating A* algorithm. One of them is by AJ Piergiovanni (Rocketman) and the other one is by Brian Grinstead (JavaScript). I recommend both of them as well as A* Pathfinding for Beginners by Patrick […]

Leave a Reply

Posting Code: Use html such as <pre lang='javascript'></pre>. See all supported languages.

*