DFS can now take Point2 as parameters for its searches.

This commit is contained in:
Harrison Deng 2020-04-26 17:28:57 -05:00
parent e8340bfc91
commit df10a2c298
2 changed files with 18 additions and 13 deletions

View File

@ -2,21 +2,24 @@ package ca.recrown.islandsurvivalcraft.pathfinding;
import java.util.Objects;
public class DFSNode implements Comparable<DFSNode> {
import ca.recrown.islandsurvivalcraft.Types.Point2;
public class DFSNode extends Point2 implements Comparable<DFSNode> {
private DFSNode goal;
public DFSNode[] child;
private DFSNode parent;
private int x;
private int y;
public DFSNode(DFSNode parent, int x, int y, DFSNode goal) {
super(x, y);
child = new DFSNode[4];
this.x = x;
this.y = y;
this.parent = parent;
this.goal = goal;
}
public DFSNode(DFSNode parent, Point2 coords, DFSNode goal) {
this(parent, coords.x, coords.y, goal);
}
public int getX() {
return x;
}
@ -61,12 +64,4 @@ public class DFSNode implements Comparable<DFSNode> {
public int hashCode() {
return Objects.hash(x, y);
}
public void copyTo(DFSNode node) {
node.child = child;
node.parent = parent;
node.x = x;
node.y = y;
node.goal = goal;
}
}

View File

@ -6,6 +6,8 @@ import java.util.Queue;
import org.apache.commons.lang.NullArgumentException;
import ca.recrown.islandsurvivalcraft.Types.Point2;
public class DepthFirstSearch {
private Queue<DFSNode> queue;
private CoordinateValidatable coordValidatable;
@ -43,10 +45,18 @@ public class DepthFirstSearch {
startNode = new DFSNode(null, x, y, endNode);
}
public void setStartPosition(Point2 start) {
startNode = new DFSNode(null, start, endNode);
}
public void setEndPosition(int x, int y) {
endNode = new DFSNode(null, x, y, endNode);
}
public void setEndPosition(Point2 goal) {
endNode = new DFSNode(null, goal, endNode);
}
public DFSNode getFoundNode() {
return foundNode;
}