Tested DFS search algorithm.

This commit is contained in:
Harrison Deng 2020-04-20 17:34:24 -05:00
parent a3024f6e91
commit 81fb18ab3d
4 changed files with 129 additions and 44 deletions

View File

@ -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;
}
}
}

View File

@ -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;
}

View File

@ -1,38 +0,0 @@
package ca.recrown.islandsurvivalcraft;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}

View File

@ -0,0 +1,103 @@
package ca.recrown.islandsurvivalcraft.pathfinding;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class DepthFirstSearchTest extends TestCase
{
private class Validator implements CoordinateValidatable {
private boolean[][] map;
public Validator(boolean[][] map) {
this.map = map;
}
@Override
public boolean validate(int x, int y) {
try {
return map[y][x];
} catch (IndexOutOfBoundsException e) {
return false;
}
}
}
private boolean[][] mapA = new boolean[][] {
{true, true},
{true, false},
{true, true}
};
private boolean[][] mapB = new boolean[][] {
{true, true, true, true},
{true, false, false, false},
{true, true, true, true}
};
private boolean[][] mapC = new boolean[][] {
{true, true, true, true},
{false, false, false, false},
{true, true, true, true}
};
private boolean[][] mapD = new boolean[][] {
{true, true, true, true},
{false, false, false, true},
{true, true, true, true},
{true, false, false, false},
};
/**
* Create the test case
*
* @param testName name of the test case
*/
public DepthFirstSearchTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( DepthFirstSearchTest.class );
}
@Override
protected void setUp() throws Exception {
super.setUp();
}
public void testDFSMapAValid()
{
DepthFirstSearch dfs = new DepthFirstSearch(new Validator(mapA));
dfs.Setup(1, 0, 1, 2);
assertTrue(dfs.buildTree());
}
public void testDFSMapBValid()
{
DepthFirstSearch dfs = new DepthFirstSearch(new Validator(mapB));
dfs.Setup(3, 2, 3, 0);
assertTrue(dfs.buildTree());
}
public void testDFSMapDValid()
{
DepthFirstSearch dfs = new DepthFirstSearch(new Validator(mapD));
dfs.Setup(0, 3, 0, 0);
assertTrue(dfs.buildTree());
}
public void testDFSInvalid()
{
DepthFirstSearch dfs = new DepthFirstSearch(new Validator(mapC));
dfs.Setup(3, 2, 3, 0);
assertFalse(dfs.buildTree());
}
}