A* search algorithm: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|C sharp}}: Regularize header markup to recommended on category page) |
mNo edit summary |
||
Line 1,845: | Line 1,845: | ||
import java.util.ArrayList; |
import java.util.ArrayList; |
||
import java.util.Collections; |
import java.util.Collections; |
||
import java.util.PriorityQueue; |
|||
import java.util.Comparator; |
|||
import java.util.LinkedList; |
|||
import java.util.Queue; |
|||
class AStar { |
class AStar { |
||
Line 1,917: | Line 1,922: | ||
} |
} |
||
/* |
/* |
||
**Expanding function nodes to check if nodes are illegal moves |
|||
** Looks in a given List<> for a node |
|||
** |
** |
||
⚫ | |||
** @return (bool) NeightborInListFound |
|||
*/ |
|||
public void expandAStar(int[][] maze, int xstart, int ystart, boolean diag){ |
|||
Queue<Mazecoord> exploreNodes = new LinkedList<mazecoord>(); |
|||
if(maze[stateNode.getR()][stateNode.getC()] == 2){ |
|||
if(isNodeILegal(stateNode, stateNode.expandDirection())){ |
|||
exploreNodes.add(stateNode.expandDirection()); |
|||
} |
|||
} |
|||
/* |
|||
** Calculate distance for goal two methods shown. |
|||
** |
|||
** |
|||
*/ |
*/ |
||
public void AStarSearch(){ |
|||
private static boolean findNeighborInList(List<Node> array, Node node) { |
|||
this.start.setCostToGoal(this.start.calculateCost(this.goal)); |
|||
return array.stream().anyMatch((n) -> (n.x == node.x && n.y == node.y)); |
|||
this.start.setPathCost(0); |
|||
⚫ | |||
this.start.setAStartCost(this.start.getPathCost() + this.start.getCostToGoal()); |
|||
Coord intialNode = this.start; |
|||
Coord stateNode = intialNode; |
|||
frontier.add(intialNode); |
|||
//explored<Queue> is empty |
|||
while (true){ |
|||
if(frontier.isEmpty()){ |
|||
System.out.println("fail"); |
|||
System.out.println(explored.size()); |
|||
System.exit(-1); |
|||
} |
|||
} |
|||
/* |
/* |
||
** Second method. |
|||
** Calulate distance between this.now and xend/yend |
|||
** |
** |
||
** |
** |
||
*/ |
*/ |
||
private double distance(int dx, int dy) { |
private double distance(int dx, int dy) { |