DFS target finding will now prioritize user given end node.

Meaning if the general area of the target is known, DFS can use it.
This commit is contained in:
Harrison Deng 2020-04-22 17:50:19 -05:00
parent 690abbdbe0
commit c8715e3dbb

View File

@ -1,7 +1,6 @@
package ca.recrown.islandsurvivalcraft.pathfinding; package ca.recrown.islandsurvivalcraft.pathfinding;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList;
import java.util.Objects; import java.util.Objects;
import java.util.PriorityQueue; import java.util.PriorityQueue;
import java.util.Queue; import java.util.Queue;
@ -17,6 +16,7 @@ public class DepthFirstSearch {
public DepthFirstSearch() { public DepthFirstSearch() {
checkedNodes = new HashSet<>(); checkedNodes = new HashSet<>();
queue = new PriorityQueue<>();
} }
public DepthFirstSearch(CoordinateValidatable validatable) { public DepthFirstSearch(CoordinateValidatable validatable) {
@ -53,7 +53,7 @@ public class DepthFirstSearch {
if (validatable == null) throw new IllegalStateException("Need to set a validator."); if (validatable == null) throw new IllegalStateException("Need to set a validator.");
if (startNode == null) throw new IllegalStateException("Need to set the starting position."); if (startNode == null) throw new IllegalStateException("Need to set the starting position.");
queue = new PriorityQueue<>(); queue.clear();
checkedNodes.clear(); checkedNodes.clear();
Node begin = startNode; Node begin = startNode;
queue.add(begin); queue.add(begin);
@ -82,6 +82,7 @@ public class DepthFirstSearch {
/** /**
* Finds the end node. * 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, * Will not set end node if one wasn't found,
* therefore, previous end node data is kept. * therefore, previous end node data is kept.
* @param targetValidator * @param targetValidator
@ -92,7 +93,7 @@ public class DepthFirstSearch {
if (targetValidator == null) throw new NullArgumentException("targetValidator"); if (targetValidator == null) throw new NullArgumentException("targetValidator");
if (startNode == null) throw new IllegalStateException("Need to set the starting position."); if (startNode == null) throw new IllegalStateException("Need to set the starting position.");
queue = new LinkedList<>(); queue.clear();
checkedNodes.clear(); checkedNodes.clear();
Node begin = startNode; Node begin = startNode;
queue.add(begin); queue.add(begin);
@ -134,6 +135,7 @@ public class DepthFirstSearch {
@Override @Override
public int compareTo(Node o) { public int compareTo(Node o) {
if (endNode == null) return 0;
return Math.round(distanceToGoal(endNode) - o.distanceToGoal(endNode)); return Math.round(distanceToGoal(endNode) - o.distanceToGoal(endNode));
} }