这里是清单执行JavaScript:
- 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;
- }
};
这里是一个快速的实现,使用二进制堆而不是一个列表。这是快了很多,还包括选择搜索对角–8定向运动。去一图搜索项目页面以获取最新的代码版本!
- 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;
- }
- };