shahilsaha

Untitled

Jul 22nd, 2025
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | Gaming | 0 0
  1. public class AStar : BasePathfinding
  2. {
  3.     protected override PathResult FindPath(Node start, Node goal, HashSet<Node> allowedNodes = null)
  4.     {
  5.         List<Node> openList = new List<Node>();
  6.         HashSet<Node> closedList = new HashSet<Node>();
  7.  
  8.         openList.Add(start);
  9.         start.gCost = 0; // Cost of the start node is 0
  10.         start.hCost = HeuristicHelper.GetManhattanDistance(start, goal); // Heuristic cost from start to end
  11.         start.fCost = start.gCost + start.hCost; // Total cost of the path from start to end
  12.  
  13.         // while there are nodes in the open list
  14.         while (openList.Count > 0)
  15.         {
  16.             // Get the current Node with the lowest fCost
  17.             Node currentNode = HeuristicHelper.FindLowestF(openList);
  18.  
  19.             // if the node is there in the open list, move it to the closed list
  20.             if (openList.Contains(currentNode))
  21.                 openList.Remove(currentNode);
  22.  
  23.             closedList.Add(currentNode);
  24.  
  25.             // if the goal node is the current node, retrace the path and return it
  26.             if (currentNode == goal)
  27.             {
  28.                 var (path, totalCost) = HeuristicHelper.RetracePath(start, goal);
  29.                 return new PathResult
  30.                 {
  31.                     Path = path,
  32.                     PathLength = path.Count,
  33.                     PathCost = totalCost,
  34.                 };
  35.             }
  36.  
  37.             // Else, get all the neighbors of the current node
  38.             var neighbors = currentNode.GetNeighbors();
  39.  
  40.             // Loop through each neighbor
  41.             foreach (var neighbor in neighbors)
  42.             {
  43.                 // If the neighbor is already in the closed list or is blocked, skip it
  44.                 if (closedList.Contains(neighbor) || !HeuristicHelper.IsNodeAllowed(neighbor, allowedNodes))
  45.                     continue;
  46.  
  47.                 // Gets the distance from the current node to the neighbor
  48.                 float tentativeGCost =
  49.                     currentNode.gCost + HeuristicHelper.GetManhattanDistance(currentNode, neighbor);
  50.  
  51.                 // calculate the cost of the path to the neighbor
  52.                 if (tentativeGCost < neighbor.gCost || !openList.Contains(neighbor))
  53.                 {
  54.                     neighbor.gCost = tentativeGCost;
  55.                     neighbor.hCost = HeuristicHelper.GetManhattanDistance(neighbor, goal);
  56.                     neighbor.fCost = neighbor.gCost + neighbor.hCost;
  57.                     neighbor.parent = currentNode;
  58.  
  59.                     // Add it to the open list if it is not already there
  60.                     if (!openList.Contains(neighbor))
  61.                         openList.Add(neighbor);
  62.                 }
  63.             }
  64.         }
  65.  
  66.         // No path was found, return null
  67.         return null;
  68.     }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment