I have a A* pathfinding class that I implemented myself with psuedocode as help. The program crashes when it is suppose to do a do-while loop that runs unitl target is found or the "OpenList" is empty. The problem seems to be with the OpenList, but I can't figure out what is wrong.
The list is an ArrayList and the loop runs while openList.Count is not 0. I did try to change the condition to just while pathFound == false, but it still crash.
What happens when it crash is I can't do anything with Unity and windows labels it as "Not responding".
My own speculation is that it somehow becomes an infinite loop, but I've checked and double checked all functions and they work the way I want. It works when I remove the do-while loop and all values are what I expect them to be.
Well, this is my code. Can anyone find something that might cause the problem?
do{
closedList.Add(current); //Switch to closed list
openList.Remove(current);
neighbors = GetNeighbors(current);
foreach (Node neighbor in neighbors){
if ((tileMap[neighbor.xpos, neighbor.zpos].Walkable()) && (!ClosedListContain(neighbor))){
if (!OpenListContains(neighbor)){///(!openList.Contains(neighbor)){ //
Debug.Log("Neighbor addede to openList");
openList.Add(neighbor); //openList now contains this node
neighbor.SetParent(current);
SetScores(neighbor);
}
else{
Debug.Log("No neighbor added");
Node neighborClone = neighbor;
SetScores(neighborClone);
if (neighborClone.G < neighbor.G){ //Current path to this node is quicker
neighbor.SetParent(current);
SetScores(neighbor);
}
}
}
}
if (ClosedListContain(goal)){
pathFound = true;
break;
}
} while(openList.Count != 0);
↧