Skip to content

Commit 42d3571

Browse files
author
LoneWandererProductions
committed
small touchups.
1 parent 3987717 commit 42d3571

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

Pathfinder/Node.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,20 @@ internal Node(int x, int y, int g, int h, int movementCost, Node parent = null)
122122
/// </item>
123123
/// </list>
124124
/// </returns>
125-
public int CompareTo(Node other)
125+
public int CompareTo(Node? other)
126126
{
127-
return F.CompareTo(other.F);
127+
if (other is null) return 1;
128+
129+
// Primary Sort: Lowest F cost first
130+
int compare = F.CompareTo(other.F);
131+
132+
// Tie-breaker: If F is equal, pick the one closer to the goal (lower H)
133+
if (compare == 0)
134+
{
135+
compare = H.CompareTo(other.H);
136+
}
137+
138+
return compare;
128139
}
129140
}
130141
}

Pathfinder/Pathfinding.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Pathfinder
1515
/// <summary>
1616
/// Implementation of the A* pathfinder algorithm.
1717
/// </summary>
18-
public sealed partial class Pathfinding : IPathfinding
18+
public sealed class Pathfinding : IPathfinding
1919
{
2020
/// <summary>
2121
/// The diagonal cost
@@ -82,7 +82,7 @@ public PathfinderResults FindPath(int startX, int startY, int goalX, int goalY,
8282
while (openList.Count > 0)
8383
{
8484
var currentNode = openList.Min;
85-
openList.Remove(currentNode);
85+
_ = openList.Remove(currentNode);
8686

8787
if (currentNode.X == goalX && currentNode.Y == goalY)
8888
{
@@ -127,7 +127,7 @@ public PathfinderResults FindPath(int startX, int startY, int goalX, int goalY,
127127
/// </summary>
128128
/// <param name="goalNode">The goal node.</param>
129129
/// <returns>A list of nodes representing the path from start to goal.</returns>
130-
private List<Node> BuildPath(Node goalNode)
130+
private static List<Node> BuildPath(Node goalNode)
131131
{
132132
var path = new List<Node>();
133133
var currentNode = goalNode;

0 commit comments

Comments
 (0)