diff --git a/Pathfinding Visualizer/Assets/Scripts/PathNode.cs b/Pathfinding Visualizer/Assets/Scripts/PathNode.cs index 8682070..9cd37fe 100644 --- a/Pathfinding Visualizer/Assets/Scripts/PathNode.cs +++ b/Pathfinding Visualizer/Assets/Scripts/PathNode.cs @@ -16,8 +16,8 @@ using UnityEngine; public char lastMove; // for output/animation public int distanceTraveled; // for calculating f(n) value public double heuristic; - public static double fn; // distance traveled + heuristic value - bool canSample; // for expansion + public double fn; // distance traveled + heuristic value + //bool canSample; // for expansion // construct node :) public Node(Node parent, Vector2Int agent, List samples, char lastMove, int distanceTraveled, double heuristic) @@ -50,8 +50,8 @@ using UnityEngine; //////////////////////////////////// // store coordinates for all potential moves to be checked - Vector2Int up = new Vector2Int( this.agent.y - 1, this.agent.x ); - Vector2Int down = new Vector2Int( this.agent.y + 1, this.agent.x ); + Vector2Int up = new Vector2Int( this.agent.y + 1, this.agent.x ); + Vector2Int down = new Vector2Int( this.agent.y - 1, this.agent.x ); Vector2Int left = new Vector2Int( this.agent.y, this.agent.x - 1 ); Vector2Int right = new Vector2Int( this.agent.y, this.agent.x + 1 ); @@ -117,14 +117,17 @@ using UnityEngine; // check that agent is not trying to move into an obstacle for (int i = 0; i < Pathfinder.obstacles.Count(); i++) { - if (Pathfinder.obstacles[i].x == position.x && Pathfinder.obstacles[i].y == position.y) + if (Pathfinder.obstacles[i].x.Equals(position.x) && Pathfinder.obstacles[i].y.Equals(position.y)) return false; } - // check that agent is not stepping out of the world - if (!((position.y >= 0) && (position.y <= Pathfinder.height - 1) && (position.x >= 0) && (position.x <= Pathfinder.width - 1))) + if ((position.y < 0) || (position.y > Pathfinder.height - 1) || (position.x < 0) || (position.x > Pathfinder.width - 1)) return false; + // check that agent is not stepping out of the world + // if (!((position.y >= 0) && (position.y <= Pathfinder.height - 1) && (position.x >= 0) && (position.x <= Pathfinder.width - 1))) + // return false; + return true; } diff --git a/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs b/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs index 212d6d2..b7f820e 100644 --- a/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs +++ b/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs @@ -60,6 +60,7 @@ public class Pathfinder Node initialState = new Node(null, agent, samples, '0', 0, 0); List result = new List(); + // Run AStar if (algorithm == 0) { result = StartAStar(initialState); @@ -96,6 +97,7 @@ public class Pathfinder // check whether we are in goal state if (currentNode.samples.Count() == 0) { + Debug.Log("Gottem"); while (currentNode.parent != null) { solution.Add(currentNode); @@ -108,7 +110,7 @@ public class Pathfinder // if not, expand children and continue List children = currentNode.expand(-1); - if (heuristic.Equals("h0")) + if (heuristic == 0) h0(children); /* else if (heuristic.Equals("h1")) h1(children); @@ -124,6 +126,20 @@ public class Pathfinder return solution; } + +/* private Node GetLowestNode(List NodeList) + { + Node lowest = NodeList[0]; + for (int i = 1; i < NodeList.Count; i++) + { + if (NodeList[i].fn < lowest.fn) + { + lowest = NodeList[i]; + } + } + return lowest; + }*/ + // HEURISTICS FOR ASTAR public static List h0(List children) { diff --git a/Pathfinding Visualizer/Assets/UI Elements/UI/Scripts/nMain.cs b/Pathfinding Visualizer/Assets/UI Elements/UI/Scripts/nMain.cs index cd4c5df..936ce1b 100644 --- a/Pathfinding Visualizer/Assets/UI Elements/UI/Scripts/nMain.cs +++ b/Pathfinding Visualizer/Assets/UI Elements/UI/Scripts/nMain.cs @@ -150,7 +150,6 @@ public class nMain : MonoBehaviour if (true) { Pathfinder astar = new Pathfinder(world, agent, obstacles, samples, algorithm, 0); - astar.go(); } }