Tested DFS search algorithm.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package ca.recrown.islandsurvivalcraft.pathfinding;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.PriorityQueue;
|
||||
|
||||
public class DepthFirstSearch {
|
||||
@@ -7,13 +8,15 @@ public class DepthFirstSearch {
|
||||
private CoordinateValidatable validatable;
|
||||
private Node startNode;
|
||||
private Node endNode;
|
||||
private ArrayList<Node> checkedNodes;
|
||||
|
||||
public DepthFirstSearch(CoordinateValidatable validatable) {
|
||||
queue = new PriorityQueue<>();
|
||||
this.validatable = validatable;
|
||||
checkedNodes = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void setStartNode(int xGoal, int yGoal, int x, int y) {
|
||||
public void Setup(int xGoal, int yGoal, int x, int y) {
|
||||
startNode = new Node(null, xGoal, yGoal, x, y);
|
||||
}
|
||||
|
||||
@@ -34,7 +37,8 @@ public class DepthFirstSearch {
|
||||
endNode = null;
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
public boolean buildTree() {
|
||||
checkedNodes.clear();
|
||||
queue.clear();
|
||||
Node begin = startNode;
|
||||
queue.add(begin);
|
||||
@@ -51,8 +55,9 @@ public class DepthFirstSearch {
|
||||
}
|
||||
if (!n.check()) {
|
||||
for (int i = 0; i < n.child.length; i++) {
|
||||
validatable.validate(n.child[i].x, n.child[i].y);
|
||||
queue.add(n.child[i]);
|
||||
if (validatable.validate(n.child[i].x, n.child[i].y)) {
|
||||
queue.add(n.child[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,9 +102,24 @@ public class DepthFirstSearch {
|
||||
public boolean check() {
|
||||
if (!checked) {
|
||||
checked = true;
|
||||
if (checkedNodes.contains(this)) {
|
||||
return true;
|
||||
}
|
||||
checkedNodes.add(this);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof Node) {
|
||||
Node comp = (Node) obj;
|
||||
if (comp.x == this.x && comp.y == this.y) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@@ -140,8 +140,8 @@ public class IslandMapGenerator implements CoordinateValidatable {
|
||||
public boolean isSameIsland(int firstBlockX, int firstBlockZ, int secondBlockX, int secondBlockZ) {
|
||||
if (!isIsland(firstBlockX, firstBlockZ)) return false;
|
||||
if (!isIsland(secondBlockX, secondBlockZ)) return false;
|
||||
dfs.setStartNode(secondBlockX, secondBlockZ, firstBlockX, firstBlockZ);
|
||||
boolean res = dfs.isConnected();
|
||||
dfs.Setup(secondBlockX, secondBlockZ, firstBlockX, firstBlockZ);
|
||||
boolean res = dfs.buildTree();
|
||||
dfs.deleteTree();
|
||||
return res;
|
||||
}
|
||||
|
Reference in New Issue
Block a user