Skip to content

Commit e645241

Browse files
committed
Fixed a bug with the A* heuristics comparer.
1 parent 9838afc commit e645241

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

Assets/Scripts/TileGridController.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ protected virtual void Start()
3737
var start = m_grid[9, 3];
3838
var end = m_grid[3, 33];
3939

40-
this.CreateExpensiveArea(m_grid[1, 15], m_grid[18, 20], 10);
40+
this.CreateExpensiveArea(m_grid[0, 15], m_grid[18, 20], 10);
4141

42-
this.StartCoroutine(DisplayUniformCostSearch(start, end));
42+
this.StartCoroutine(DisplayAStarSearch(start, end, CalculateEuclideanDistance));
4343
}
4444

4545
private IEnumerator DisplayDepthFirstSearch(Tile start, Tile end)
@@ -221,10 +221,13 @@ private IEnumerator DisplayAStarSearch(Tile start, Tile end, Func<Tile, Tile, fl
221221
end.Color = m_endColor;
222222
end.SetText("X");
223223

224+
var costs = InitializePathCosts(m_grid);
225+
costs[start] = 0.0f;
226+
224227
Comparison<Tile> heuristicComparison = (a, b) =>
225228
{
226-
var aPriority = a.Weight + heuristic(a, end);
227-
var bPriority = b.Weight + heuristic(b, end);
229+
var aPriority = costs[a] + heuristic(a, end);
230+
var bPriority = costs[b] + heuristic(b, end);
228231

229232
return aPriority.CompareTo(bPriority);
230233
};
@@ -235,9 +238,6 @@ private IEnumerator DisplayAStarSearch(Tile start, Tile end, Func<Tile, Tile, fl
235238
var frontier = new PriorityQueue<Tile>(heuristicComparison); // Stable priority queue.
236239
frontier.Enqueue(start);
237240

238-
var costs = InitializePathCosts(m_grid);
239-
costs[start] = 0.0f;
240-
241241
while (frontier.Count > 0)
242242
{
243243
var current = frontier.Dequeue();

0 commit comments

Comments
 (0)