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; 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; private DFSNode goal;
public DFSNode[] child; public DFSNode[] child;
private DFSNode parent; private DFSNode parent;
private int x;
private int y;
public DFSNode(DFSNode parent, int x, int y, DFSNode goal) { public DFSNode(DFSNode parent, int x, int y, DFSNode goal) {
super(x, y);
child = new DFSNode[4]; child = new DFSNode[4];
this.x = x;
this.y = y;
this.parent = parent; this.parent = parent;
this.goal = goal; this.goal = goal;
} }
public DFSNode(DFSNode parent, Point2 coords, DFSNode goal) {
this(parent, coords.x, coords.y, goal);
}
public int getX() { public int getX() {
return x; return x;
} }
@ -61,12 +64,4 @@ public class DFSNode implements Comparable<DFSNode> {
public int hashCode() { public int hashCode() {
return Objects.hash(x, y); 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 org.apache.commons.lang.NullArgumentException;
import ca.recrown.islandsurvivalcraft.Types.Point2;
public class DepthFirstSearch { public class DepthFirstSearch {
private Queue<DFSNode> queue; private Queue<DFSNode> queue;
private CoordinateValidatable coordValidatable; private CoordinateValidatable coordValidatable;
@ -43,10 +45,18 @@ public class DepthFirstSearch {
startNode = new DFSNode(null, x, y, endNode); 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) { public void setEndPosition(int x, int y) {
endNode = new DFSNode(null, x, y, endNode); endNode = new DFSNode(null, x, y, endNode);
} }
public void setEndPosition(Point2 goal) {
endNode = new DFSNode(null, goal, endNode);
}
public DFSNode getFoundNode() { public DFSNode getFoundNode() {
return foundNode; return foundNode;
} }