Well, this is my updated code. It doesn't slow down, but no path appears.
public static IntPosition[] GetPath(BlockType[,] blocks, IntPosition start, IntPosition end, int threshhold)
{
List<Node> MapNodes = new List<Node>();
MapNodes.Add(new Node() { Position = start });
开发者_JAVA百科 bool[,] explored = new bool[blocks.GetLength(0), blocks.GetLength(1)];
explored[start.X, start.Y] = true;
int? endNode = null;
int index = 0;
while (endNode == null && index < threshhold)
{
List<Node> addNodes = new List<Node>();
foreach (Node n in MapNodes)
{
//left
if (n.Position.X - 1 >= 0)
if (explored[n.Position.X - 1, n.Position.Y] == false)
if (blocks[n.Position.X - 1, n.Position.Y] == BlockType.Open)
{
int i = addNodes.Count;
addNodes.Add(new Node() { Position = new IntPosition(n.Position.X - 1, n.Position.Y), ParentNode = n });
explored[n.Position.X - 1, n.Position.Y] = true;
if (addNodes[i].Position == end)
endNode = i;
}
//right
if (n.Position.X + 1 <= blocks.GetLength(0) - 1)
if (explored[n.Position.X + 1, n.Position.Y] == false)
if (blocks[n.Position.X + 1, n.Position.Y] == BlockType.Open)
{
int i = addNodes.Count;
addNodes.Add(new Node() { Position = new IntPosition(n.Position.X + 1, n.Position.Y), ParentNode = n });
explored[n.Position.X + 1, n.Position.Y] = true;
if (addNodes[i].Position == end)
endNode = i;
}
//up
if (n.Position.Y - 1 >= 0)
if (explored[n.Position.X, n.Position.Y - 1] == false)
if (blocks[n.Position.X, n.Position.Y - 1] == BlockType.Open)
{
int i = addNodes.Count;
addNodes.Add(new Node() { Position = new IntPosition(n.Position.X, n.Position.Y - 1), ParentNode = n });
explored[n.Position.X, n.Position.Y - 1] = true;
if (addNodes[i].Position == end)
endNode = i;
}
//down
if (n.Position.Y + 1 <= blocks.GetLength(1))
if (explored[n.Position.X, n.Position.Y + 1] == false)
if (blocks[n.Position.X, n.Position.Y + 1] == BlockType.Open)
{
int i = addNodes.Count;
addNodes.Add(new Node() { Position = new IntPosition(n.Position.X, n.Position.Y + 1), ParentNode = n });
explored[n.Position.X, n.Position.Y + 1] = true;
if (addNodes[i].Position == end)
endNode = i;
}
}
MapNodes.AddRange(addNodes);
index++;
}
if (endNode == null)
return new IntPosition[] { };
else
{
List<IntPosition> path = new List<IntPosition>();
path.Add(end);
Node CurrentNode = (MapNodes[(int)endNode].ParentNode);
bool endReached = false;
while (endReached == false)
{
path.Add(CurrentNode.Position);
if (CurrentNode.Position == start)
endReached = true;
else
CurrentNode = CurrentNode.ParentNode;
}
path.Reverse();
return path.ToArray();
}
}
public class IntPosition
{
public int X;
public int Y;
public IntPosition(int x, int y)
{
X = x;
Y = y;
}
public IntPosition() { X = 0; Y = 0; }
public override bool Equals(object obj)
{
return ((IntPosition)obj).X == X && ((IntPosition)obj).Y == Y;
}
}
There are a couple of issues with the code. Firstly, your X + 1 and Y + 1 checks have the comparison the wrong way around (greater-than instead of less-than). I suspect that's what causing the algorithm to fail.
Secondly, while I'm not familiar with the algorithm, I suspect you need to do something to ensure that nodes you've already check are not checked again in subsequent iterations. Also, you need to make sure you don't add nodes to MapNodes
that are already present. The combination of these is likely to be the cause of the slowness you're seeing, because it will result in exponential growth of MapNodes
with many redundant nodes.
Next to what Chris mentions, there's something else wrong with your usage of MapNodes
. It needs to be sorted, and contain all members, including all nodes you put on the list addNodes
. Caching all nodes which need to be added to MapNodes
is not a valid A* implementation. When you add a node to addNodes
, it should actually be added to MapNodes
and MapNodes
should then be sorted by F, which is a number associated with a node, which is the sum of the total cost from the startnode to that node and the value of a heuristic function evaluated for that node. There are multiple descriptions of heuristic functions on the internet, I suggest you try to read about that.
And btw, where's the heuristic in your code? An A* algorithm is just a slow Dijkstra algorithm without a heuristic. I'm afraid what you've implemented resembles Dijkstra more that A*.
And to answer your actual question: the algorithm probably doesn't yield a path because there are bugs, preventing the search algorithm from actually reaching the destination node.
精彩评论