diff --git a/Pathfinding Visualizer/Assets/Scripts/AStar.cs b/Pathfinding Visualizer/Assets/Scripts/AStar.cs deleted file mode 100644 index 5d51e6d..0000000 --- a/Pathfinding Visualizer/Assets/Scripts/AStar.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class AStar : MonoBehaviour -{ - - LogicGrid world; - List obstacles; - List samples; - int heuristic; // 0 = h0 | - - public AStar(LogicGrid world, int heuristic) - { - this.world = world; - this.obstacles = world.obstacles; - this.samples = world.samples; - this.heuristic = heuristic; - } - - public void go() - { - - } -} diff --git a/Pathfinding Visualizer/Assets/Scripts/CellMesh.cs b/Pathfinding Visualizer/Assets/Scripts/CellMesh.cs index c4c6a42..9e62969 100644 --- a/Pathfinding Visualizer/Assets/Scripts/CellMesh.cs +++ b/Pathfinding Visualizer/Assets/Scripts/CellMesh.cs @@ -30,7 +30,7 @@ public class CellMesh : MonoBehaviour { // if(e.x == -1 && e.y == -1), grid is being reset - Debug.Log(e.x + " " + e.y); + //Debug.Log(e.x + " " + e.y); UpdateCellVisual(); } diff --git a/Pathfinding Visualizer/Assets/Scripts/LogicGrid.cs b/Pathfinding Visualizer/Assets/Scripts/LogicGrid.cs index c87514e..212eb91 100644 --- a/Pathfinding Visualizer/Assets/Scripts/LogicGrid.cs +++ b/Pathfinding Visualizer/Assets/Scripts/LogicGrid.cs @@ -18,12 +18,6 @@ public class LogicGrid // Debug Grid Array used for updating the text-objects private TextMesh[,] debugTextArray; - - // Coordinates for pathfinding - public int[] agent; - public List obstacles; - public List samples; - public event EventHandler OnGridValueChanged; public class OnGridValueChangedEventArgs : EventArgs { @@ -103,46 +97,6 @@ public class LogicGrid x = x, y = y }); - - // If deleting a cell's value, remove coord from set - if (value == 0) - { - - // make behavior to check for -1 values in coords - if (agent[0].Equals(x) && agent[1].Equals(y)) - { - agent[0] = -1; - agent[1] = -2; - } - - } - - // Add coordinate to obstacle array - if (value == 1) - { - int[][] coord = new int[2][]; - - coord[0][0] = x; - coord[1][0] = y; - - obstacles.Add(coord); - } - // Add coordinate to sample array - if (value == 2) - { - int[][] coord = new int[2][]; - - coord[0][0] = x; - coord[1][0] = y; - - samples.Add(coord); - } - // Add coordinate to obstacle array - if (value == 3) - { - agent[0] = x; - agent[1] = y; - } } } @@ -183,10 +137,6 @@ public class LogicGrid { gridArray = new int[width, height]; - agent = new int[2]; - obstacles = new List(); - samples = new List(); - if (OnGridValueChanged != null) OnGridValueChanged(this, new OnGridValueChangedEventArgs { diff --git a/Pathfinding Visualizer/Assets/Scripts/Main.cs b/Pathfinding Visualizer/Assets/Scripts/Main.cs index 6ebb37f..53bbec2 100644 --- a/Pathfinding Visualizer/Assets/Scripts/Main.cs +++ b/Pathfinding Visualizer/Assets/Scripts/Main.cs @@ -8,27 +8,29 @@ public class Main : MonoBehaviour [SerializeField] private CellMesh cellMesh; private LogicGrid world; - char placementMode; int placementValue; - bool astarSelected; + // Coordinates for pathfinding + public Vector2 agent = new Vector2(); + public List obstacles = new List(); + public List samples = new List(); + // Start is called before the first frame update void Start() { // Create world - int width = 100; - int height = 100; - float cellSize = 5f; - Vector3 origin = new Vector3(-110, -110); + int width = 5; + int height = 5; + float cellSize = 10f; + Vector3 origin = new Vector3(-10, -10); world = new LogicGrid(width, height, cellSize, origin); // Set default placement to Obstacle placementValue = 1; - placementMode = 'o'; cellMesh.SetGrid(world); @@ -45,21 +47,18 @@ public class Main : MonoBehaviour if (Input.GetKeyDown("o")) { Debug.Log("Placing Mode: Obstacle"); - placementMode = 'o'; placementValue = 1; } // Place Sample in cell if(Input.GetKeyDown("s")) { Debug.Log("Placing Mode: Sample"); - placementMode = 's'; placementValue = 2; } // Place agent in cell if (Input.GetKeyDown("a")) { Debug.Log("Placing Mode: Agent"); - placementMode = 'a'; placementValue = 3; } // Reset Canvas @@ -67,6 +66,11 @@ public class Main : MonoBehaviour { ResetGrid(); } + + if (Input.GetKeyDown("g")) + { + StartAlgorithm(); + } ////////////////////////// @@ -107,9 +111,50 @@ public class Main : MonoBehaviour public void StartAlgorithm() { - if(astarSelected) + Vector2 coord = new Vector2(); + + // Collect information about world + for (int x = 0; x < world.GetWidth(); x++) + for(int y = 0; y < world.GetHeight(); y++) + { + int index = x * world.GetHeight() + y; + int value = world.GetValue(x, y); + + Debug.Log("x: " + x + " y: " + y + " | value: " + value); + + // Add coordinate to obstacle array + if (value == 1) + { + //coord = new int[2][]; + + coord.x = x; + coord.y = y; + + if (!obstacles.Contains(coord)) + obstacles.Add(coord); + } + // Add coordinate to sample array + if (value == 2) + { + //coord = new int[2][]; + + coord.x = x; + coord.y = y; + + samples.Add(coord); + } + // Add coordinate to obstacle array + if (value == 3) + { + agent[0] = x; + agent[1] = y; + } + } + + + if ( true ) { - AStar astar = new AStar(world, 0); + Pathfinder astar = new Pathfinder(world, agent, obstacles, samples, 0, 0); astar.go(); } } diff --git a/Pathfinding Visualizer/Assets/Scripts/PathNode.cs b/Pathfinding Visualizer/Assets/Scripts/PathNode.cs index e65e1b2..8b13bfa 100644 --- a/Pathfinding Visualizer/Assets/Scripts/PathNode.cs +++ b/Pathfinding Visualizer/Assets/Scripts/PathNode.cs @@ -43,7 +43,7 @@ using UnityEngine; int[] onSample = new int[2]; // since this state is being currently visited (expanded), put in closed list - // SampleWorld.closed.add(this.getState()); + Pathfinder.closed.Add(this.getState()); //////////////////////////////////// // BEGIN CHECKING FOR VALID MOVES // @@ -115,14 +115,17 @@ using UnityEngine; public bool isOpen(int[] position) { // check that agent is not trying to move into an obstacle - for (int i = 0; i < SampleWorld.obstacles.size(); i++) + for (int i = 0; i < Pathfinder.obstacles.Count(); i++) { - if (Enumerable.SequenceEqual(SampleWorld.obstacles.get(i), position) == 0) + int[] coord = new int[2]; + coord[0] = Pathfinder.obstacles[i][0][0]; + coord[1] = Pathfinder.obstacles[i][1][0]; + if (Pathfinder.obstacles[i][0][0] == position[0] && Pathfinder.obstacles[i][1][0] == position[1]) return false; } // check that agent is not stepping out of the world - if (!((position[row] >= 0) && (position[row] <= SampleWorld.worldRows - 1) && (position[col] >= 0) && (position[col] <= SampleWorld.worldCols - 1))) + if (!((position[row] >= 0) && (position[row] <= Pathfinder.height - 1) && (position[col] >= 0) && (position[col] <= Pathfinder.width - 1))) return false; return true; @@ -216,7 +219,7 @@ using UnityEngine; } } // helper class for Node - class State + public class State { int[] agent; int[][] samples; @@ -237,9 +240,9 @@ using UnityEngine; public bool inClosed() { - for (int i = 0; i < SampleWorld.closed.size(); i++) + for (int i = 0; i < Pathfinder.closed.Count(); i++) { - if (this.equals(SampleWorld.closed.get(i))) + if (this.equals(Pathfinder.closed[i])) { return true; } diff --git a/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs b/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs new file mode 100644 index 0000000..6410626 --- /dev/null +++ b/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs @@ -0,0 +1,53 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Pathfinder : MonoBehaviour +{ + + // used to store all unique states that agent has visited (holds agent and sample coords) + public static List closed = new List(); + + LogicGrid world; + public static int[] agent; + public static List obstacles; + public static List samples; + + public static int algorithm; // 0 = A* | 1 = dfs | 2 = bfs + public static int heuristic; // 0 = h0 | 1 = h1 | 2 = h2 + + public static int height; + public static int width; + + + public Pathfinder(LogicGrid world, int[] a, List o, List s, int algo, int heu) + { + this.world = world; + height = world.GetHeight(); + width = world.GetWidth(); + + agent = a; + obstacles = o; + samples = s; + + algorithm = algo; + heuristic = heu; + } + + public void go() + { + + Debug.Log("Agent: " + agent[0] + "," + agent[1]); + + for(int i = 0; i < obstacles.Count; i++) + { + Debug.Log("Obstacle " + i + ": " + obstacles[i][0][0] + ", " + obstacles[i][0][1]); + } + + for (int i = 0; i < samples.Count; i++) + { + Debug.Log("Sample " + i + ": " + samples[i][0][0] + ", " + samples[i][0][1]); + } + + } +} diff --git a/Pathfinding Visualizer/Assets/Scripts/AStar.cs.meta b/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs.meta similarity index 100% rename from Pathfinding Visualizer/Assets/Scripts/AStar.cs.meta rename to Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs.meta