From: LeonardoBizzoni Date: Wed, 30 Jun 2021 09:57:49 +0000 (+0200) Subject: Finished A* X-Git-Url: http://git.leonardobizzoni.com/?a=commitdiff_plain;h=66b88d044ad5ef6559f7e695c5723ad10be5cf3c;p=astar-visualizer Finished A* --- diff --git a/src/main/java/Main/AStar.java b/src/main/java/Main/AStar.java index 086bc04..62aa128 100644 --- a/src/main/java/Main/AStar.java +++ b/src/main/java/Main/AStar.java @@ -5,6 +5,7 @@ public class AStar{ public AStar(Map map) { this.map = map; + PathfinderUtils.path.removeAll(PathfinderUtils.path); } public void start() { @@ -36,6 +37,9 @@ public class AStar{ if (nextX == PathfinderUtils.startNode.getX() && nextY == PathfinderUtils.startNode.getY()) return; if (nextX == PathfinderUtils.endNode.getX() && nextY == PathfinderUtils.endNode.getY()) { + PathfinderUtils.endNode.setParentNode(parent); + + PathfinderUtils.drawPath(); map.isFinished = true; ControlPanel.toggleRunBtn.setText("Clear"); map.repaint(); diff --git a/src/main/java/Main/Map.java b/src/main/java/Main/Map.java index cc96245..e63983d 100644 --- a/src/main/java/Main/Map.java +++ b/src/main/java/Main/Map.java @@ -91,16 +91,24 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi menu.renderMenu(); // Draws open nodes - g.setColor(Color.white); + g.setColor(new Color(69, 133, 136)); for (Node node : PathfinderUtils.openNodes) { g.fillRect(node.getX() + 1, node.getY() + 1, size - 1, size - 1); } // Draws closed nodes - g.setColor(new Color(132, 255, 138)); + g.setColor(new Color(77, 77, 77)); for (Node node : PathfinderUtils.closedNodes) { g.fillRect(node.getX() + 1, node.getY() + 1, size - 1, size - 1); } + + // if path is found start drawing the shortest path + if (isFinished) { + g.setColor(new Color(250, 189, 47)); + for (Node node : PathfinderUtils.path) { + g.fillRect(node.getX() + 1, node.getY() + 1, size - 1, size - 1); + } + } } // Drawing on the grid @@ -113,7 +121,7 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi // Checks if start node and end node are the same if (PathfinderUtils.startNode != null && PathfinderUtils.endNode != null) { - if (PathfinderUtils.checkDuplicateNode(PathfinderUtils.startNode, PathfinderUtils.endNode)) { + if (PathfinderUtils.isSameNode(PathfinderUtils.startNode, PathfinderUtils.endNode)) { JOptionPane.showMessageDialog(null, "End node and start node can't be the same node", "Same node error", JOptionPane.ERROR_MESSAGE); return; @@ -136,7 +144,7 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi // Checks if end node and start node are the same if (PathfinderUtils.startNode != null && PathfinderUtils.endNode != null) { - if (PathfinderUtils.checkDuplicateNode(PathfinderUtils.startNode, PathfinderUtils.endNode)) { + if (PathfinderUtils.isSameNode(PathfinderUtils.startNode, PathfinderUtils.endNode)) { JOptionPane.showMessageDialog(null, "End node and start node can't be the same node!", "SAME NODE ERROR", JOptionPane.ERROR_MESSAGE); return; @@ -159,10 +167,10 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi Node barrierNode = new Node(posX, posY); if(PathfinderUtils.startNode != null) - if(PathfinderUtils.checkDuplicateNode(barrierNode, PathfinderUtils.startNode)) + if(PathfinderUtils.isSameNode(barrierNode, PathfinderUtils.startNode)) return; if(PathfinderUtils.endNode != null) - if(PathfinderUtils.checkDuplicateNode(barrierNode, PathfinderUtils.endNode)) + if(PathfinderUtils.isSameNode(barrierNode, PathfinderUtils.endNode)) return; PathfinderUtils.barriers.add(barrierNode); diff --git a/src/main/java/Main/PathfinderUtils.java b/src/main/java/Main/PathfinderUtils.java index 2b2744a..20371d4 100644 --- a/src/main/java/Main/PathfinderUtils.java +++ b/src/main/java/Main/PathfinderUtils.java @@ -7,6 +7,7 @@ public class PathfinderUtils { static List barriers = new ArrayList<>(); static List openNodes = new ArrayList<>(); static List closedNodes = new ArrayList<>(); + static List path = new ArrayList<>(); static Node startNode, endNode; // Returns index of barrier if the coordinates are of a barrier node @@ -24,8 +25,8 @@ public class PathfinderUtils { barriers.remove(id); } - static boolean checkDuplicateNode(Node a, Node b) { - if(a.getX() == b.getX() && a.getY() == b.getY()) + static boolean isSameNode(Node a, Node b) { + if (a.getX() == b.getX() && a.getY() == b.getY()) return true; return false; } @@ -43,4 +44,23 @@ public class PathfinderUtils { } } } + + static void drawPath() { + if (path.size() == 0) { + Node node = endNode.getParentNode(); + + while (!isSameNode(node, startNode)) { + path.add(node); + + for (int i = 0; i < closedNodes.size(); i++) { + Node current = closedNodes.get(i); + + if (isSameNode(current, node)) { + node = current.getParentNode(); + break; + } + } + } + } + } } diff --git a/target/PathVisualizer-1.0-SNAPSHOT.jar b/target/PathVisualizer-1.0-SNAPSHOT.jar index 674d0e1..c080b58 100644 Binary files a/target/PathVisualizer-1.0-SNAPSHOT.jar and b/target/PathVisualizer-1.0-SNAPSHOT.jar differ diff --git a/target/classes/Main/AStar.class b/target/classes/Main/AStar.class index 8676543..01b3d6c 100644 Binary files a/target/classes/Main/AStar.class and b/target/classes/Main/AStar.class differ diff --git a/target/classes/Main/Map.class b/target/classes/Main/Map.class index fc2bb22..d729a41 100644 Binary files a/target/classes/Main/Map.class and b/target/classes/Main/Map.class differ diff --git a/target/classes/Main/PathfinderUtils.class b/target/classes/Main/PathfinderUtils.class index ccf6ade..a6ec000 100644 Binary files a/target/classes/Main/PathfinderUtils.class and b/target/classes/Main/PathfinderUtils.class differ diff --git a/target/surefire-reports/Main.AppTest.txt b/target/surefire-reports/Main.AppTest.txt index 1952558..c1e1e01 100644 --- a/target/surefire-reports/Main.AppTest.txt +++ b/target/surefire-reports/Main.AppTest.txt @@ -1,4 +1,4 @@ ------------------------------------------------------------------------------- Test set: Main.AppTest ------------------------------------------------------------------------------- -Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.029 s - in Main.AppTest +Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.019 s - in Main.AppTest diff --git a/target/surefire-reports/TEST-Main.AppTest.xml b/target/surefire-reports/TEST-Main.AppTest.xml index 518b40f..7752e56 100644 --- a/target/surefire-reports/TEST-Main.AppTest.xml +++ b/target/surefire-reports/TEST-Main.AppTest.xml @@ -1,5 +1,5 @@ - + @@ -16,7 +16,7 @@ - + @@ -32,7 +32,7 @@ - +