From 4ed9d4d2724c5bc6a7be0a51487113c28891f5a5 Mon Sep 17 00:00:00 2001 From: Harrison Date: Wed, 22 Apr 2020 19:41:25 -0500 Subject: [PATCH] Tested DFS direction assisted search. --- .../pathfinding/DepthFirstSearch.java | 14 ++++++++---- .../pathfinding/DepthFirstSearchTest.java | 22 ++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/ca/recrown/islandsurvivalcraft/pathfinding/DepthFirstSearch.java b/src/main/java/ca/recrown/islandsurvivalcraft/pathfinding/DepthFirstSearch.java index e578cd7..0977301 100644 --- a/src/main/java/ca/recrown/islandsurvivalcraft/pathfinding/DepthFirstSearch.java +++ b/src/main/java/ca/recrown/islandsurvivalcraft/pathfinding/DepthFirstSearch.java @@ -14,6 +14,8 @@ public class DepthFirstSearch { private Node endNode; private HashSet checkedNodes; private int maxNodesSearched = -1; + private int foundX; + private int foundY; public DepthFirstSearch() { checkedNodes = new HashSet<>(); @@ -43,16 +45,16 @@ public class DepthFirstSearch { startNode = new Node(null, x, y); } - public void setEndPosition(int x, int y) { + public void setDirectionPosition(int x, int y) { endNode = new Node(null, x, y); } public int getEndX() { - return endNode.x; + return foundX; } public int getEndY() { - return endNode.y; + return foundY; } public void deleteTree() { @@ -118,7 +120,8 @@ public class DepthFirstSearch { n.child[2] = new Node(n, n.x, n.y + 1); n.child[3] = new Node(n, n.x, n.y - 1); } else { - endNode = n; + foundX = n.x; + foundY = n.y; return true; } if (checkedNodes.add(n)) { @@ -133,6 +136,9 @@ public class DepthFirstSearch { return false; } + public int getSearchedCount() { + return checkedNodes.size(); + } private class Node implements Comparable { public Node[] child; public Node parent; diff --git a/src/test/java/ca/recrown/islandsurvivalcraft/pathfinding/DepthFirstSearchTest.java b/src/test/java/ca/recrown/islandsurvivalcraft/pathfinding/DepthFirstSearchTest.java index 366a0a5..b1d1989 100644 --- a/src/test/java/ca/recrown/islandsurvivalcraft/pathfinding/DepthFirstSearchTest.java +++ b/src/test/java/ca/recrown/islandsurvivalcraft/pathfinding/DepthFirstSearchTest.java @@ -99,7 +99,7 @@ public class DepthFirstSearchTest { { dfs.setValidatable(new Validator(mapA)); dfs.setStartPosition(1, 2); - dfs.setEndPosition(1, 0); + dfs.setDirectionPosition(1, 0); assertTrue(dfs.buildPathToEndNode()); } @@ -108,7 +108,7 @@ public class DepthFirstSearchTest { { dfs.setValidatable(new Validator(mapB)); dfs.setStartPosition(3, 0); - dfs.setEndPosition(3, 2); + dfs.setDirectionPosition(3, 2); assertTrue(dfs.buildPathToEndNode()); } @@ -117,7 +117,7 @@ public class DepthFirstSearchTest { { dfs.setValidatable(new Validator(mapD)); dfs.setStartPosition(0, 0); - dfs.setEndPosition(0, 3); + dfs.setDirectionPosition(0, 3); assertTrue(dfs.buildPathToEndNode()); } @@ -126,7 +126,7 @@ public class DepthFirstSearchTest { { dfs.setValidatable(new Validator(mapC)); dfs.setStartPosition(3, 0); - dfs.setEndPosition(3, 2); + dfs.setDirectionPosition(3, 2); assertFalse(dfs.buildPathToEndNode()); } @@ -135,7 +135,7 @@ public class DepthFirstSearchTest { { dfs.setValidatable(new Validator(mapE)); dfs.setStartPosition(3, 0); - dfs.setEndPosition(3, 2); + dfs.setDirectionPosition(3, 2); assertFalse(dfs.buildPathToEndNode()); } @@ -211,4 +211,16 @@ public class DepthFirstSearchTest { assertFalse(dfs.findTarget(validator)); } + + @Test + public void testDFSFindEndNodeMapHWithAssistValid() { + Validator validator = new Validator(mapH); + dfs.setValidatable(validator); + dfs.setStartPosition(3, 0); + dfs.setDirectionPosition(42, 84); + + assertTrue(dfs.findTarget(validator)); + assertEquals(95, dfs.getEndX()); + assertEquals(49, dfs.getEndY()); + } }