diff --git a/src/main/java/ca/recrown/islandsurvivalcraft/pathfinding/DepthFirstSearch.java b/src/main/java/ca/recrown/islandsurvivalcraft/pathfinding/DepthFirstSearch.java index da49186..a0c9b01 100644 --- a/src/main/java/ca/recrown/islandsurvivalcraft/pathfinding/DepthFirstSearch.java +++ b/src/main/java/ca/recrown/islandsurvivalcraft/pathfinding/DepthFirstSearch.java @@ -1,5 +1,6 @@ package ca.recrown.islandsurvivalcraft.pathfinding; +import java.util.ArrayList; import java.util.PriorityQueue; public class DepthFirstSearch { @@ -7,13 +8,15 @@ public class DepthFirstSearch { private CoordinateValidatable validatable; private Node startNode; private Node endNode; + private ArrayList checkedNodes; public DepthFirstSearch(CoordinateValidatable validatable) { queue = new PriorityQueue<>(); this.validatable = validatable; + checkedNodes = new ArrayList<>(); } - public void setStartNode(int xGoal, int yGoal, int x, int y) { + public void Setup(int xGoal, int yGoal, int x, int y) { startNode = new Node(null, xGoal, yGoal, x, y); } @@ -34,7 +37,8 @@ public class DepthFirstSearch { endNode = null; } - public boolean isConnected() { + public boolean buildTree() { + checkedNodes.clear(); queue.clear(); Node begin = startNode; queue.add(begin); @@ -51,8 +55,9 @@ public class DepthFirstSearch { } if (!n.check()) { for (int i = 0; i < n.child.length; i++) { - validatable.validate(n.child[i].x, n.child[i].y); - queue.add(n.child[i]); + if (validatable.validate(n.child[i].x, n.child[i].y)) { + queue.add(n.child[i]); + } } } } @@ -97,9 +102,24 @@ public class DepthFirstSearch { public boolean check() { if (!checked) { checked = true; + if (checkedNodes.contains(this)) { + return true; + } + checkedNodes.add(this); return false; } return true; } + + @Override + public boolean equals(Object obj) { + if (obj instanceof Node) { + Node comp = (Node) obj; + if (comp.x == this.x && comp.y == this.y) { + return true; + } + } + return false; + } } } \ No newline at end of file diff --git a/src/main/java/ca/recrown/islandsurvivalcraft/worldgen/IslandMapGenerator.java b/src/main/java/ca/recrown/islandsurvivalcraft/worldgen/IslandMapGenerator.java index 7b1ee4a..5020b50 100644 --- a/src/main/java/ca/recrown/islandsurvivalcraft/worldgen/IslandMapGenerator.java +++ b/src/main/java/ca/recrown/islandsurvivalcraft/worldgen/IslandMapGenerator.java @@ -140,8 +140,8 @@ public class IslandMapGenerator implements CoordinateValidatable { public boolean isSameIsland(int firstBlockX, int firstBlockZ, int secondBlockX, int secondBlockZ) { if (!isIsland(firstBlockX, firstBlockZ)) return false; if (!isIsland(secondBlockX, secondBlockZ)) return false; - dfs.setStartNode(secondBlockX, secondBlockZ, firstBlockX, firstBlockZ); - boolean res = dfs.isConnected(); + dfs.Setup(secondBlockX, secondBlockZ, firstBlockX, firstBlockZ); + boolean res = dfs.buildTree(); dfs.deleteTree(); return res; } diff --git a/src/test/java/ca/recrown/islandsurvivalcraft/AppTest.java b/src/test/java/ca/recrown/islandsurvivalcraft/AppTest.java deleted file mode 100644 index abec6b8..0000000 --- a/src/test/java/ca/recrown/islandsurvivalcraft/AppTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package ca.recrown.islandsurvivalcraft; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Unit test for simple App. - */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } - - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } - - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } -} diff --git a/src/test/java/ca/recrown/islandsurvivalcraft/pathfinding/DepthFirstSearchTest.java b/src/test/java/ca/recrown/islandsurvivalcraft/pathfinding/DepthFirstSearchTest.java new file mode 100644 index 0000000..cebaac3 --- /dev/null +++ b/src/test/java/ca/recrown/islandsurvivalcraft/pathfinding/DepthFirstSearchTest.java @@ -0,0 +1,103 @@ +package ca.recrown.islandsurvivalcraft.pathfinding; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class DepthFirstSearchTest extends TestCase +{ + + private class Validator implements CoordinateValidatable { + private boolean[][] map; + + public Validator(boolean[][] map) { + this.map = map; + } + + @Override + public boolean validate(int x, int y) { + try { + return map[y][x]; + } catch (IndexOutOfBoundsException e) { + return false; + } + } + } + + private boolean[][] mapA = new boolean[][] { + {true, true}, + {true, false}, + {true, true} + }; + + private boolean[][] mapB = new boolean[][] { + {true, true, true, true}, + {true, false, false, false}, + {true, true, true, true} + }; + + private boolean[][] mapC = new boolean[][] { + {true, true, true, true}, + {false, false, false, false}, + {true, true, true, true} + }; + + private boolean[][] mapD = new boolean[][] { + {true, true, true, true}, + {false, false, false, true}, + {true, true, true, true}, + {true, false, false, false}, + }; + + /** + * Create the test case + * + * @param testName name of the test case + */ + public DepthFirstSearchTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( DepthFirstSearchTest.class ); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + } + + public void testDFSMapAValid() + { + DepthFirstSearch dfs = new DepthFirstSearch(new Validator(mapA)); + dfs.Setup(1, 0, 1, 2); + assertTrue(dfs.buildTree()); + } + + public void testDFSMapBValid() + { + DepthFirstSearch dfs = new DepthFirstSearch(new Validator(mapB)); + dfs.Setup(3, 2, 3, 0); + assertTrue(dfs.buildTree()); + } + public void testDFSMapDValid() + { + DepthFirstSearch dfs = new DepthFirstSearch(new Validator(mapD)); + dfs.Setup(0, 3, 0, 0); + assertTrue(dfs.buildTree()); + } + public void testDFSInvalid() + { + DepthFirstSearch dfs = new DepthFirstSearch(new Validator(mapC)); + dfs.Setup(3, 2, 3, 0); + assertFalse(dfs.buildTree()); + } +}