public void findPath(){ closed.add(start); AStarNode currentNode = start; while(!closed.contains(end)){ for(int x = -1; x <= 1; x++){ for(int y = -1; y <= 1; y++){ if(this.isValidNode(currentNode.getX() + x, currentNode.getY() + y)){ AStarNode temp = board[currentNode.getX() + x][currentNode.getY() + y]; if(!closed.contains(temp) && temp.isWalkable()){ if(!open.contains(currentNode)){ temp.setParent(currentNode); temp.setScore(currentNode, end); temp.setColor(Color.yellow); board[temp.getX()][temp.getY()] = temp; open.add(temp); } } } } } currentNode = open.first(); open.remove(open.first()); board[currentNode.getX()][currentNode.getY()] = currentNode; closed.add(currentNode); if(open.isEmpty()){ break; } } for(AStarNode node : closed){ board[node.getX()][node.getY()].setColor(Color.BLACK); } this.repaint(); }
This is a picture of a case where the path is found
Here is a picture where the path fails and freezes up
*Edit*
Pictures don't seem to be working so here are links
Working: http://img18.imageshack.us/i/workingastar.png/
Not Working: http://img408.imageshack.us/i/notworkingastar.png/