While running Dijkstra's Algorithm to assign a direction to every tile and when an object land on the tile, follow the direction to a goal. I encountered an infinite loop (or I think it is). The following is my code:
while (openList.Count > 0)
{
Debug.Log("Running alg");
failsafe++;
if (failsafe > 1000) { break; }
Tile currentTile = openList[0];
openList.Remove(currentTile);
closedList.Add(currentTile);
List<Tile> neighbours = GetNeighbours(currentTile.localX, currentTile.localY, grid);
foreach (var neighbour in neighbours)
{
if (!closedList.Contains(neighbour))
{
neighbour.parentX = currentTile.localX;
neighbour.parentY = currentTile.localY;
openList.Add(neighbour);
}
}
}
Under these conditions, the failsafe triggered and broke the loop. The grid is a 11x11 grid and as such by my understanding should have never reached 1000 cycles.
When the failsafe triggers, it explored 9 tiles away from the goal and the rest are unexplored.
What am I doing wrong here?