Added a target coordinate finder.
Implemented DFS name changes.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package ca.recrown.islandsurvivalcraft.pathfinding;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@@ -15,96 +16,143 @@ import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||
@TestInstance(Lifecycle.PER_CLASS)
|
||||
public class DepthFirstSearchTest {
|
||||
|
||||
private class Validator implements CoordinateValidatable {
|
||||
private boolean[][] map;
|
||||
private class Validator implements CoordinateValidatable, CoordinateTargetValidatable {
|
||||
private byte[][] map;
|
||||
|
||||
public Validator(boolean[][] map) {
|
||||
public Validator(byte[][] map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate(int x, int y) {
|
||||
try {
|
||||
return map[y][x];
|
||||
return map[y][x] >= 1;
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCoordinateTarget(int x, int y) {
|
||||
try {
|
||||
return map[y][x] == 2;
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
private DepthFirstSearch dfs;
|
||||
|
||||
private boolean[][] mapA = new boolean[][] {
|
||||
{true, true},
|
||||
{true, false},
|
||||
{true, true}
|
||||
private byte[][] mapA = new byte[][] {
|
||||
{1, 1},
|
||||
{1, 0},
|
||||
{1, 1}
|
||||
};
|
||||
|
||||
private boolean[][] mapB = new boolean[][] {
|
||||
{true, true, true, true},
|
||||
{true, false, false, false},
|
||||
{true, true, true, true}
|
||||
private byte[][] mapB = new byte[][] {
|
||||
{1, 1, 1, 1},
|
||||
{1, 0, 0, 0},
|
||||
{1, 2, 1, 1}
|
||||
};
|
||||
|
||||
private boolean[][] mapC = new boolean[][] {
|
||||
{true, true, true, true},
|
||||
{false, false, false, false},
|
||||
{true, true, true, true}
|
||||
private byte[][] mapC = new byte[][] {
|
||||
{1, 1, 1, 1},
|
||||
{0, 0, 0, 0},
|
||||
{1, 1, 1, 1}
|
||||
};
|
||||
|
||||
private boolean[][] mapD = new boolean[][] {
|
||||
{true, true, true, true},
|
||||
{false, false, false, true},
|
||||
{true, true, true, true},
|
||||
{true, false, false, false},
|
||||
private byte[][] mapD = new byte[][] {
|
||||
{1, 1, 1, 1},
|
||||
{0, 0, 0, 1},
|
||||
{1, 1, 1, 1},
|
||||
{2, 0, 0, 0},
|
||||
};
|
||||
|
||||
private boolean[][] mapE = new boolean[][] {
|
||||
{true, true, true, true},
|
||||
{false, false, false, false},
|
||||
{true, true, true, true},
|
||||
{true, false, false, false},
|
||||
private byte[][] mapE = new byte[][] {
|
||||
{1, 1, 1, 1},
|
||||
{0, 0, 0, 0},
|
||||
{1, 1, 1, 1},
|
||||
{1, 0, 2, 0},
|
||||
};
|
||||
|
||||
@BeforeAll
|
||||
protected void setUp() {
|
||||
dfs = new DepthFirstSearch();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDFSMapAValid()
|
||||
public void testDFSBuildPathToEndNodeMapAValid()
|
||||
{
|
||||
DepthFirstSearch dfs = new DepthFirstSearch(new Validator(mapA));
|
||||
dfs.Setup(1, 0, 1, 2);
|
||||
assertTrue(dfs.buildTree());
|
||||
dfs.setValidatable(new Validator(mapA));
|
||||
dfs.setStartPosition(1, 2);
|
||||
dfs.setEndPosition(1, 0);
|
||||
assertTrue(dfs.buildPathToEndNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDFSMapBValid()
|
||||
public void testDFSBuildPathToEndNodeMapBValid()
|
||||
{
|
||||
DepthFirstSearch dfs = new DepthFirstSearch(new Validator(mapB));
|
||||
dfs.Setup(3, 2, 3, 0);
|
||||
assertTrue(dfs.buildTree());
|
||||
dfs.setValidatable(new Validator(mapB));
|
||||
dfs.setStartPosition(3, 0);
|
||||
dfs.setEndPosition(3, 2);
|
||||
assertTrue(dfs.buildPathToEndNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDFSMapDValid()
|
||||
public void testDFSBuildPathToEndNodeMapDValid()
|
||||
{
|
||||
DepthFirstSearch dfs = new DepthFirstSearch(new Validator(mapD));
|
||||
dfs.Setup(0, 3, 0, 0);
|
||||
assertTrue(dfs.buildTree());
|
||||
dfs.setValidatable(new Validator(mapD));
|
||||
dfs.setStartPosition(0, 0);
|
||||
dfs.setEndPosition(0, 3);
|
||||
assertTrue(dfs.buildPathToEndNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDFSMapCInvalid()
|
||||
public void testDFSBuildPathToEndNodeMapCInvalid()
|
||||
{
|
||||
DepthFirstSearch dfs = new DepthFirstSearch(new Validator(mapC));
|
||||
dfs.Setup(3, 2, 3, 0);
|
||||
assertFalse(dfs.buildTree());
|
||||
dfs.setValidatable(new Validator(mapC));
|
||||
dfs.setStartPosition(3, 0);
|
||||
dfs.setEndPosition(3, 2);
|
||||
assertFalse(dfs.buildPathToEndNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDFSMapEInvalid()
|
||||
public void testDFSBuildPathToEndNodeMapEInvalid()
|
||||
{
|
||||
DepthFirstSearch dfs = new DepthFirstSearch(new Validator(mapE));
|
||||
dfs.Setup(3, 2, 3, 0);
|
||||
assertFalse(dfs.buildTree());
|
||||
dfs.setValidatable(new Validator(mapE));
|
||||
dfs.setStartPosition(3, 0);
|
||||
dfs.setEndPosition(3, 2);
|
||||
assertFalse(dfs.buildPathToEndNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDFSFindEndNodeMapBValid() {
|
||||
Validator validator = new Validator(mapB);
|
||||
dfs.setValidatable(validator);
|
||||
dfs.setStartPosition(3, 0);
|
||||
|
||||
assertTrue(dfs.findEndNode(validator));
|
||||
assertEquals(1, dfs.getEndX());
|
||||
assertEquals(2, dfs.getEndY());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDFSFindEndNodeMapDValid() {
|
||||
Validator validator = new Validator(mapD);
|
||||
dfs.setValidatable(validator);
|
||||
dfs.setStartPosition(0, 0);
|
||||
|
||||
assertTrue(dfs.findEndNode(validator));
|
||||
assertEquals(0, dfs.getEndX());
|
||||
assertEquals(3, dfs.getEndY());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDFSFindEndNodeMapEInvalid() {
|
||||
Validator validator = new Validator(mapE);
|
||||
dfs.setValidatable(validator);
|
||||
dfs.setStartPosition(0, 0);
|
||||
|
||||
assertFalse(dfs.findEndNode(validator));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user