Hi,
I’ve been working on this for several hours now and it is time to head to bed. I decided to post up my current attempt at writing A* where the cost is 0 for all nodes.
Currently, when I run this, the solution (which is the open list), contains every single node that the algorithm looked at. I am going by this description of the algorithm:
http://ai-depot.com/Tutorial/PathFinding.html
Though either I am not understanding this properly or that example is flawed…
I know my algorithm is reaching the indicated node because it leaves a single node untouched…
So I need to know how to remove unwanted nodes from the list as I go.
Here is my code for the algorithm. If you require the entire program let me know.
The filled part tells the program to draw a filled rectangle as opposed to an outline of one.
Thanks for any assistance.
public static void pathFind(PathWindow window)
{
Node startNode = window.getColumns().get(0).getNodeList().get(0);
int cols = window.getColumns().size();
int rows = window.getColumns().get(0).getNodeList().size();
Node endNode = window.getColumns().get(cols-1).getNodeList().get(rows-1);
LinkedList<Node> openList = new LinkedList<Node>();
openList.add(startNode);
while(openList.size() > 0)
{
Node currentNode = openList.poll();
//currentNode.setFilled(true);
//If we are at the end.
if(currentNode.equals(endNode))
{
//Add the current node back in since we removed it above.
openList.add(currentNode);
break;
}
//Successors
if(currentNode.getAboveNode() != null)
{
if(openList.contains(currentNode.getAboveNode()) == false)
{
openList.add(currentNode);
openList.add(currentNode.getAboveNode());
}
}
if(currentNode.getUnderNode() != null)
{
if(openList.contains(currentNode.getUnderNode()) == false)
{
openList.add(currentNode);
openList.add(currentNode.getUnderNode());
}
}
if(currentNode.getLeftNode() != null)
{
if(openList.contains(currentNode.getLeftNode()) == false)
{
openList.add(currentNode);
openList.add(currentNode.getLeftNode());
}
}
if(currentNode.getRightNode() != null)
{
if(openList.contains(currentNode.getRightNode()) == false)
{
openList.add(currentNode);
openList.add(currentNode.getRightNode());
}
}
}
System.out.println("openlist is:\n" + openList);
//set all these nodes to filled.
for(int i = 0;i<openList.size();i++)
{
openList.get(i).setFilled(true);
}
window.repaint();
}