/// <inheritdoc /> public IList<Node> FindPath(Node start, Node end) { start.Parent = null; start.G = 0; start.H = 0; this.open.Enqueue(start, start.ID); while (this.open.Count > 0) { Node current = this.open.Dequeue(); current.IsVisited = true; this.closed.Add(current); if (current.Equals(end)) { IList<Node> path = Backtrace(current); this.ResetNodes(); return path; } foreach (Node neighbor in current.Children) { if (neighbor.IsBlocked || neighbor.IsVisited) { continue; } int gscore = current.G + this.findDistance(current, neighbor); if (this.open.Contains(neighbor)) { if (gscore < neighbor.G) { neighbor.Parent = current; neighbor.G = gscore; neighbor.H = this.heuristic(neighbor, end); this.open.UpdatePriority(neighbor, neighbor.F); } continue; } neighbor.Parent = current; neighbor.G = gscore; neighbor.H = this.heuristic(neighbor, end); this.open.Enqueue(neighbor, neighbor.F); } } this.ResetNodes(); return null; }
/// <summary> /// Initializes a new instance of the Node class with default values. /// </summary> /// <param name="id">The node ID.</param> /// <param name="isBlocked">If the node can be passed or not.</param> /// <param name="isVisited">If the node has been previously visited or not.</param> /// <param name="parent">The parent node.</param> /// <param name="h">The heuristic value.</param> /// <param name="g">The total cost so far.</param> public Node( int id, bool isBlocked = false, bool isVisited = false, Node parent = null, int h = 0, int g = 0) { this.id = id; this.IsBlocked = isBlocked; this.IsVisited = isVisited; this.Parent = parent; this.H = h; this.G = g; this.Children = new List<Node>(); }
private static void ResetNode(Node node) { node.IsVisited = false; }
private static IList<Node> Backtrace(Node node) { IList<Node> reversePath = new List<Node>(); while (node != null) { reversePath.Add(node); node = node.Parent; } IList<Node> path = new List<Node>(); for (int i = reversePath.Count - 1; i >= 0; i--) { path.Add(reversePath[i]); } return path; }
/// <inheritdoc /> public IList<Node> FindPath(Node start, Node end) { return this.pathfinder.FindPath(start, end); }