From c8715e3dbba33134d4b081ac3c389b5c6526609a Mon Sep 17 00:00:00 2001 From: Harrison Date: Wed, 22 Apr 2020 17:50:19 -0500 Subject: [PATCH] DFS target finding will now prioritize user given end node. Meaning if the general area of the target is known, DFS can use it. --- .../islandsurvivalcraft/pathfinding/DepthFirstSearch.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/ca/recrown/islandsurvivalcraft/pathfinding/DepthFirstSearch.java b/src/main/java/ca/recrown/islandsurvivalcraft/pathfinding/DepthFirstSearch.java index 2433db2..29d132d 100644 --- a/src/main/java/ca/recrown/islandsurvivalcraft/pathfinding/DepthFirstSearch.java +++ b/src/main/java/ca/recrown/islandsurvivalcraft/pathfinding/DepthFirstSearch.java @@ -1,7 +1,6 @@ package ca.recrown.islandsurvivalcraft.pathfinding; import java.util.HashSet; -import java.util.LinkedList; import java.util.Objects; import java.util.PriorityQueue; import java.util.Queue; @@ -17,6 +16,7 @@ public class DepthFirstSearch { public DepthFirstSearch() { checkedNodes = new HashSet<>(); + queue = new PriorityQueue<>(); } public DepthFirstSearch(CoordinateValidatable validatable) { @@ -53,7 +53,7 @@ public class DepthFirstSearch { if (validatable == null) throw new IllegalStateException("Need to set a validator."); if (startNode == null) throw new IllegalStateException("Need to set the starting position."); - queue = new PriorityQueue<>(); + queue.clear(); checkedNodes.clear(); Node begin = startNode; queue.add(begin); @@ -82,6 +82,7 @@ public class DepthFirstSearch { /** * Finds the end node. + * If an end node is set, it will try going in that direction first. * Will not set end node if one wasn't found, * therefore, previous end node data is kept. * @param targetValidator @@ -92,7 +93,7 @@ public class DepthFirstSearch { if (targetValidator == null) throw new NullArgumentException("targetValidator"); if (startNode == null) throw new IllegalStateException("Need to set the starting position."); - queue = new LinkedList<>(); + queue.clear(); checkedNodes.clear(); Node begin = startNode; queue.add(begin); @@ -134,6 +135,7 @@ public class DepthFirstSearch { @Override public int compareTo(Node o) { + if (endNode == null) return 0; return Math.round(distanceToGoal(endNode) - o.distanceToGoal(endNode)); }