private void AddSuccessor(int x, int y, ArrayList successors)
{
int cost = _map.GetMapData(x, y);
if (cost == -1) return;
AStarNode2D node = new AStarNode2D(x, y, _map);
AStarNode2D p = (AStarNode2D)this.Parent;
while (p != null)
{
if (node.EqualTo(p)) return;
p = (AStarNode2D)p.Parent;
}
public override float Cost(AStarNode node)
{
AStarNode2D n = (AStarNode2D)node;
int xd = n.X - this.X;
int yd = n.Y - this.Y;
return (float)Math.Sqrt(xd * xd + yd * yd);
}
}
class MainEntryPoint
{
static void Main(string[] args)
{
Map map = new Map();
AStarNode2D start = new AStarNode2D(2, 2, map);
AStarNode2D goal = new AStarNode2D(6, 2, map);
AStarEngine engine = new AStarEngine();
ArrayList solution = new ArrayList();
if (engine.Execute(start, goal))
{
AStarNode2D node = (AStarNode2D)engine.ResultNode;
while (node != null)
{
solution.Insert(0, node);
node = (AStarNode2D)node.Parent;
}
Console.WriteLine("Path found:");
for (int i = 0; i < 10; i ++)
{
for (int j = 0; j < 10; j ++)
{
if (map.GetMapData(j, i) == -1)
Console.Write("X");
else
{
AStarNode2D nt = new AStarNode2D(j, i, map);
bool inSolution = false;
foreach(AStarNode2D n in solution)
{
if (n.EqualTo(nt))
{
inSolution = true;
break;
}
}
if (inSolution) Console.Write("S");
else Console.Write(".");
}
}
Console.WriteLine();
}
}
else
{
Console.WriteLine("Unable to find a path.");
}
ArrayList successors = new ArrayList();
node.Propagate(successors);
foreach(AStarNode sn in successors)
{
int no = findNodeIndex(_openList, sn);
int nc = findNodeIndex(_closedList, sn);
float newg = node.G + node.Cost(sn);
if (no >= 0 && ((AStarNode)_openList[no]).G <= newg)
continue;
if (nc >= 0 && ((AStarNode)_closedList[nc]).G <= newg)
continue;
if (nc >= 0) _closedList.RemoveAt(nc);
if (no >= 0)
{
AStarNode nt = (AStarNode)_openList[no];
nt.G = newg;
nt.H = nt.GoalDistEstimate(goal);
nt.Parent = node;
}
else
{
sn.G = newg;
sn.H = sn.GoalDistEstimate(goal);
sn.Parent = node;
_openList.Add(sn);
}
}
for (int i = 0; i < _openList.Count; i ++)
{
if ((node != null) && node.F > ((AStarNode)_openList[i]).F)
node = (AStarNode)_openList[i];
if (i == 0) node = (AStarNode)_openList[0];
}
if (node != null) _openList.Remove(node);
return node;
}
private int findNodeIndex(ArrayList list, AStarNode node)
{
for (int i = 0; i < list.Count; i ++)
{
if (((AStarNode)list[i]).EqualTo(node))
return i;
}