From: LeonardoBizzoni Date: Sun, 27 Jun 2021 07:19:39 +0000 (+0200) Subject: Fixed duplicate nodes and added replayability X-Git-Url: http://git.leonardobizzoni.com/?a=commitdiff_plain;h=c57c23571986a410bac02d9b7f72069cffdae8c9;p=astar-visualizer Fixed duplicate nodes and added replayability --- diff --git a/src/main/java/Main/AStar.java b/src/main/java/Main/AStar.java index 6a60a95..086bc04 100644 --- a/src/main/java/Main/AStar.java +++ b/src/main/java/Main/AStar.java @@ -24,18 +24,8 @@ public class AStar{ PathfinderUtils.closedNodes.add(parent); PathfinderUtils.openNodes.remove(parent); - if (parent.getX() == PathfinderUtils.endNode.getX() && parent.getY() == PathfinderUtils.endNode.getY()) { - ControlPanel.toggleRunBtn.setText("End"); - map.isFinished = true; - map.repaint(); - return; - } - - if (!map.isFinished) { + if (!map.isFinished) searchPath(parent); - } - else - System.exit(0); } public void calculateOpenNode(int nextX, int nextY, Node parent) { @@ -45,6 +35,12 @@ public class AStar{ return; if (nextX == PathfinderUtils.startNode.getX() && nextY == PathfinderUtils.startNode.getY()) return; + if (nextX == PathfinderUtils.endNode.getX() && nextY == PathfinderUtils.endNode.getY()) { + map.isFinished = true; + ControlPanel.toggleRunBtn.setText("Clear"); + map.repaint(); + return; + } Node openNode = new Node(nextX, nextY); openNode.setParentNode(parent); diff --git a/src/main/java/Main/ControlPanel.java b/src/main/java/Main/ControlPanel.java index e5186ef..30e7aa4 100644 --- a/src/main/java/Main/ControlPanel.java +++ b/src/main/java/Main/ControlPanel.java @@ -13,12 +13,14 @@ public class ControlPanel { public ControlPanel(Map map) { algo = new JComboBox<>(new String[] { "Select an algorithm", "A*", "Dijkstra", "Breadth-first search"}); algo.setVisible(true); + algo.setFocusable(false); toggleRunBtn = new JButton("Run"); toggleRunBtn.setVisible(true); toggleRunBtn.setMargin(new Insets(0, 0, 0, 0)); toggleRunBtn.setBackground(Color.white); toggleRunBtn.addActionListener(map); + toggleRunBtn.setFocusable(false); map.add(algo); map.add(toggleRunBtn); diff --git a/src/main/java/Main/Map.java b/src/main/java/Main/Map.java index 167847a..cc96245 100644 --- a/src/main/java/Main/Map.java +++ b/src/main/java/Main/Map.java @@ -16,8 +16,7 @@ import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; +import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.JFrame; @@ -98,7 +97,7 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi } // Draws closed nodes - g.setColor(Color.orange); + g.setColor(new Color(132, 255, 138)); for (Node node : PathfinderUtils.closedNodes) { g.fillRect(node.getX() + 1, node.getY() + 1, size - 1, size - 1); } @@ -112,10 +111,9 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi int posX = e.getX() - (e.getX() % size); int posY = e.getY() - (e.getY() % size); - // TODO fa cagare // Checks if start node and end node are the same - if (PathfinderUtils.endNode != null) { - if (PathfinderUtils.endNode.getX() == posX && PathfinderUtils.endNode.getY() == posY) { + if (PathfinderUtils.startNode != null && PathfinderUtils.endNode != null) { + if (PathfinderUtils.checkDuplicateNode(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,10 +134,9 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi int posX = e.getX() - (e.getX() % size); int posY = e.getY() - (e.getY() % size); - // TODO fa cagare // Checks if end node and start node are the same - if (PathfinderUtils.startNode != null) { - if (PathfinderUtils.startNode.getX() == posX && PathfinderUtils.startNode.getY() == posY) { + if (PathfinderUtils.startNode != null && PathfinderUtils.endNode != null) { + if (PathfinderUtils.checkDuplicateNode(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; @@ -161,7 +158,12 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi int posY = e.getY() - (e.getY() % size); Node barrierNode = new Node(posX, posY); - // TODO controlla se si sta creando una barriera su start o end + if(PathfinderUtils.startNode != null) + if(PathfinderUtils.checkDuplicateNode(barrierNode, PathfinderUtils.startNode)) + return; + if(PathfinderUtils.endNode != null) + if(PathfinderUtils.checkDuplicateNode(barrierNode, PathfinderUtils.endNode)) + return; PathfinderUtils.barriers.add(barrierNode); @@ -225,7 +227,6 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi switch (ControlPanel.algo.getItemAt(ControlPanel.algo.getSelectedIndex())) { case ("A*"): - System.out.println("A* selected"); new AStar(this).start(); break; @@ -234,11 +235,6 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi // AStar.start(); break; - case ("Greedy best-first search"): - System.out.println("Greedy best-first search selected"); - // AStar.start(); - break; - case ("Breadth-first search"): System.out.println("Breadth-first search selected"); // AStar.start(); @@ -249,11 +245,18 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi "Algorithm not selected", JOptionPane.ERROR_MESSAGE); return; } - - ControlPanel.toggleRunBtn.setText("Stop"); } - else { + else if(ControlPanel.toggleRunBtn.getText().equals("Clear")){ + PathfinderUtils.barriers.removeAll(PathfinderUtils.barriers); + PathfinderUtils.openNodes.removeAll(PathfinderUtils.openNodes); + PathfinderUtils.closedNodes.removeAll(PathfinderUtils.closedNodes); + PathfinderUtils.startNode = null; + PathfinderUtils.endNode = null; + + isFinished = false; + + this.repaint(); ControlPanel.toggleRunBtn.setText("Run"); } } diff --git a/src/main/java/Main/PathfinderUtils.java b/src/main/java/Main/PathfinderUtils.java index 528a005..2b2744a 100644 --- a/src/main/java/Main/PathfinderUtils.java +++ b/src/main/java/Main/PathfinderUtils.java @@ -24,6 +24,12 @@ public class PathfinderUtils { barriers.remove(id); } + static boolean checkDuplicateNode(Node a, Node b) { + if(a.getX() == b.getX() && a.getY() == b.getY()) + return true; + return false; + } + static void sort() { Node tmp; diff --git a/target/PathVisualizer-1.0-SNAPSHOT.jar b/target/PathVisualizer-1.0-SNAPSHOT.jar index 39668b1..674d0e1 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 e722d34..8676543 100644 Binary files a/target/classes/Main/AStar.class and b/target/classes/Main/AStar.class differ diff --git a/target/classes/Main/ControlPanel.class b/target/classes/Main/ControlPanel.class index 920e67e..865752d 100644 Binary files a/target/classes/Main/ControlPanel.class and b/target/classes/Main/ControlPanel.class differ diff --git a/target/classes/Main/Map.class b/target/classes/Main/Map.class index 28b921d..fc2bb22 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 c3ec61a..ccf6ade 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 bdab1da..1952558 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.02 s - in Main.AppTest +Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.029 s - in Main.AppTest diff --git a/target/surefire-reports/TEST-Main.AppTest.xml b/target/surefire-reports/TEST-Main.AppTest.xml index 396d6b0..518b40f 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,12 +32,12 @@ - + - +