public dribbelaerPath getPath(int X, int Y, int goalX, int goalY) { if (X == goalX && Y == goalY) return null; int maxCycles = (maxX * maxY) ^ 2; int Cycles = 0; dribbelaerPath Path = new dribbelaerPath(); this.Goal = new mapNode(goalX, goalY, 0, null, null, null); this.Start = new mapNode(X, Y, 0, null, Goal, null); Goal.Start = Goal; Start.Start = Start; Open.Add(Start); while (Open.Count > 0) { if (Cycles >= maxCycles) return null; else Cycles++; mapNode Current = (mapNode)Open.Pop(); if (Current.X == Goal.X && Current.Y == Goal.Y) { while (Current != null) { Path.Coords.Push(new Coord(Current.X, Current.Y)); Current = Current.Parent; } break; } else { foreach (mapNode Successor in Current.Successors(this)) { mapNode openNode = null; if (Open.Contains(Successor)) { openNode = (mapNode)Open[Open.IndexOf(Successor)]; if (Successor.totalCost > openNode.totalCost) continue; } mapNode closedNode = null; if (Closed.Contains(Successor)) { closedNode = (mapNode)Closed[Closed.IndexOf(Successor)]; if (Successor.totalCost > closedNode.totalCost) continue; } Open.Remove(Successor); Closed.Remove(Successor); Open.Push(Successor); } Closed.Add(Current); } } if (Path.Coords.Count == 0) return null; else return Path; }
public dribbelaerPath getPath(int X, int Y, int goalX, int goalY) { if (X == goalX && Y == goalY) { return(null); } int maxCycles = (maxX * maxY) ^ 2; int Cycles = 0; dribbelaerPath Path = new dribbelaerPath(); this.Goal = new mapNode(goalX, goalY, 0, null, null, null); this.Start = new mapNode(X, Y, 0, null, Goal, null); Goal.Start = Goal; Start.Start = Start; Open.Add(Start); while (Open.Count > 0) { if (Cycles >= maxCycles) { return(null); } else { Cycles++; } mapNode Current = (mapNode)Open.Pop(); if (Current.X == Goal.X && Current.Y == Goal.Y) { while (Current != null) { Path.Coords.Push(new Coord(Current.X, Current.Y)); Current = Current.Parent; } break; } else { foreach (mapNode Successor in Current.Successors(this)) { mapNode openNode = null; if (Open.Contains(Successor)) { openNode = (mapNode)Open[Open.IndexOf(Successor)]; if (Successor.totalCost > openNode.totalCost) { continue; } } mapNode closedNode = null; if (Closed.Contains(Successor)) { closedNode = (mapNode)Closed[Closed.IndexOf(Successor)]; if (Successor.totalCost > closedNode.totalCost) { continue; } } Open.Remove(Successor); Closed.Remove(Successor); Open.Push(Successor); } Closed.Add(Current); } } if (Path.Coords.Count == 0) { return(null); } else { return(Path); } }