47 lines
1.5 KiB
C#
47 lines
1.5 KiB
C#
namespace AdventOfCode.Core.Shared.A_Star
|
|
{
|
|
public class AStarGrid : Grid<AStarNode>
|
|
{
|
|
const int TILE_MOVE_COST = 10;
|
|
|
|
public AStarGrid(AStarNode[] data) : base(data)
|
|
{
|
|
}
|
|
|
|
public int MinimalSteps(AStarNode start, AStarNode destination)
|
|
{
|
|
List<AStarNode> closedNodes = new();
|
|
List<AStarNode> openNodes = new List<AStarNode> { start };
|
|
|
|
do
|
|
{
|
|
AStarNode current = openNodes.OrderBy(n => n.FCost).First();
|
|
openNodes.Remove(current);
|
|
current.CloseNode();
|
|
closedNodes.Add(current);
|
|
|
|
if (current == destination)
|
|
{
|
|
//done
|
|
return 1;
|
|
}
|
|
|
|
var neighbors = GetNeighbors(current, false);
|
|
neighbors = neighbors.Where(n => current.CanMoveTo(current));
|
|
|
|
// calc costs
|
|
foreach(AStarNode neighbor in neighbors)
|
|
{
|
|
neighbor.ParentNode = current;
|
|
// since we only move straight the move cost is always the same
|
|
neighbor.GCost = current.GCost + TILE_MOVE_COST;
|
|
neighbor.HCost = neighbor.DistanceTo(destination) * TILE_MOVE_COST;
|
|
neighbor.OpenNode();
|
|
openNodes.Add(neighbor);
|
|
}
|
|
}
|
|
while (true);
|
|
}
|
|
}
|
|
}
|