]> git.leonardobizzoni.com Git - astar-visualizer/commitdiff
Fixed duplicate nodes and added replayability
authorLeonardoBizzoni <leo2002714@gmail.com>
Sun, 27 Jun 2021 07:19:39 +0000 (09:19 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Sun, 27 Jun 2021 07:19:39 +0000 (09:19 +0200)
src/main/java/Main/AStar.java
src/main/java/Main/ControlPanel.java
src/main/java/Main/Map.java
src/main/java/Main/PathfinderUtils.java
target/PathVisualizer-1.0-SNAPSHOT.jar
target/classes/Main/AStar.class
target/classes/Main/ControlPanel.class
target/classes/Main/Map.class
target/classes/Main/PathfinderUtils.class
target/surefire-reports/Main.AppTest.txt
target/surefire-reports/TEST-Main.AppTest.xml

index 6a60a95c041225ccf2af7f38d83ba3a44007d137..086bc044eaac89a3392e6e529811ba21179b3db4 100644 (file)
@@ -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);
index e5186ef86257751d952894bd30138763edfa5c60..30e7aa4c6327aa5aadc744f8f1e1701c89631da7 100644 (file)
@@ -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);
index 167847aa12992b1663190f3ea0e6c3ff087b10e7..cc962450c636b46df990313cb5374069cdf08e7d 100644 (file)
@@ -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");
         }
     }
index 528a005cff13e3d3532d0386e98eb8773cb8e0db..2b2744ad4b580b4922cd5ae87585f98610fe39e7 100644 (file)
@@ -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;
 
index 39668b14f55524d269800ce1d9d903ab3692a8ff..674d0e13e92634711300b474eeb3d686799d6789 100644 (file)
Binary files a/target/PathVisualizer-1.0-SNAPSHOT.jar and b/target/PathVisualizer-1.0-SNAPSHOT.jar differ
index e722d34d013d6539e90fc14e7062d48e62519e27..8676543f083443aff7a11be4c46d6808aca453a1 100644 (file)
Binary files a/target/classes/Main/AStar.class and b/target/classes/Main/AStar.class differ
index 920e67e59aa47f53584766312ba2b8e1fef391d6..865752d027b77df97fffc6001c651b2ae1adbce5 100644 (file)
Binary files a/target/classes/Main/ControlPanel.class and b/target/classes/Main/ControlPanel.class differ
index 28b921daf45d913656448e9b0545604e806d7037..fc2bb22d2a4d39132b909bf67233f496852a688a 100644 (file)
Binary files a/target/classes/Main/Map.class and b/target/classes/Main/Map.class differ
index c3ec61a485a76e38401acec470836359f9e571ee..ccf6ade5e1be0f3d697031f89fd493bf70913077 100644 (file)
Binary files a/target/classes/Main/PathfinderUtils.class and b/target/classes/Main/PathfinderUtils.class differ
index bdab1da3b2f7dfb74b92b57e5049b807f43802b3..1952558253f3d459ea67f584c7a832ef6d9f15fb 100644 (file)
@@ -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
index 396d6b02f0ad224eb6e38248afdd78f8db9584f4..518b40fe7b232fe59c0af778c5fff8a5090aa131 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="Main.AppTest" time="0.02" tests="1" errors="0" skipped="0" failures="0">
+<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="Main.AppTest" time="0.029" tests="1" errors="0" skipped="0" failures="0">
   <properties>
     <property name="awt.toolkit" value="sun.awt.X11.XToolkit"/>
     <property name="java.specification.version" value="11"/>
@@ -16,7 +16,7 @@
     <property name="sun.java.launcher" value="SUN_STANDARD"/>
     <property name="user.country" value="GB"/>
     <property name="sun.boot.library.path" value="/opt/openjdk-bin-11.0.11_p9/lib"/>
-    <property name="sun.java.command" value="/home/leo/Docs/Proj/PathVisualizer/target/surefire/surefirebooter12422851706423362175.jar /home/leo/Docs/Proj/PathVisualizer/target/surefire 2021-06-19T13-00-08_471-jvmRun1 surefire10461286948834527749tmp surefire_05278180969358337674tmp"/>
+    <property name="sun.java.command" value="/home/leo/Docs/Proj/PathVisualizer/target/surefire/surefirebooter8510599142121748610.jar /home/leo/Docs/Proj/PathVisualizer/target/surefire 2021-06-27T09-11-09_330-jvmRun1 surefire6120348368433176072tmp surefire_01333341478733871355tmp"/>
     <property name="jdk.debug" value="release"/>
     <property name="surefire.test.class.path" value="/home/leo/Docs/Proj/PathVisualizer/target/test-classes:/home/leo/Docs/Proj/PathVisualizer/target/classes:/home/leo/.m2/repository/junit/junit/4.11/junit-4.11.jar:/home/leo/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:"/>
     <property name="sun.cpu.endian" value="little"/>
     <property name="java.specification.name" value="Java Platform API Specification"/>
     <property name="java.vm.specification.vendor" value="Oracle Corporation"/>
     <property name="java.awt.graphicsenv" value="sun.awt.X11GraphicsEnvironment"/>
-    <property name="surefire.real.class.path" value="/home/leo/Docs/Proj/PathVisualizer/target/surefire/surefirebooter12422851706423362175.jar"/>
+    <property name="surefire.real.class.path" value="/home/leo/Docs/Proj/PathVisualizer/target/surefire/surefirebooter8510599142121748610.jar"/>
     <property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/>
     <property name="java.runtime.version" value="11.0.11+9"/>
     <property name="user.name" value="leo"/>
     <property name="path.separator" value=":"/>
-    <property name="os.version" value="5.12.10-gentoo-LeoKernel"/>
+    <property name="os.version" value="5.12.12-gentoo-LeoKernel"/>
     <property name="java.runtime.name" value="OpenJDK Runtime Environment"/>
     <property name="file.encoding" value="UTF-8"/>
     <property name="java.vm.name" value="OpenJDK 64-Bit Server VM"/>