Island highlighting debug now works, albeit, not favorable.
Reworked DFS as well to suit a broader spectrum of needs. Reworked other classes for easier use.
This commit is contained in:
@@ -14,6 +14,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||
|
||||
import ca.recrown.islandsurvivalcraft.utilities.datatypes.Point2;
|
||||
import ca.recrown.islandsurvivalcraft.utilities.pathfinding.CoordinateTargetValidatable;
|
||||
import ca.recrown.islandsurvivalcraft.utilities.pathfinding.CoordinateValidatable;
|
||||
|
||||
@@ -31,26 +32,24 @@ public class DepthFirstSearchTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate(int x, int y) {
|
||||
public boolean validate(Point2 point) {
|
||||
try {
|
||||
return map[y][x] >= 1;
|
||||
return map[point.y][point.x] >= 1;
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCoordinateTarget(int x, int y) {
|
||||
public boolean isCoordinateTarget(Point2 point) {
|
||||
try {
|
||||
return map[y][x] == 2;
|
||||
return map[point.y][point.x] == 2;
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private DepthFirstSearch dfs;
|
||||
|
||||
private byte[][] mapA;
|
||||
private byte[][] mapB;
|
||||
private byte[][] mapC;
|
||||
@@ -62,7 +61,6 @@ public class DepthFirstSearchTest {
|
||||
|
||||
@BeforeAll
|
||||
public void setUp() throws IOException {
|
||||
dfs = new DepthFirstSearch();
|
||||
mapA = readMap("DFSTestMapSmallA.txt");
|
||||
mapB = readMap("DFSTestMapSmallB.txt");
|
||||
mapC = readMap("DFSTestMapSmallC.txt");
|
||||
@@ -100,130 +98,126 @@ public class DepthFirstSearchTest {
|
||||
@Test
|
||||
public void testDFSBuildPathToEndNodeMapAValid()
|
||||
{
|
||||
dfs.setValidatable(new Validator(mapA));
|
||||
dfs.setStartPosition(1, 2);
|
||||
dfs.setEndPosition(1, 0);
|
||||
assertTrue(dfs.buildPathToEndNode());
|
||||
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(1, 2), new Point2(1, 0));
|
||||
dfs.buildToGoal(new Validator(mapA), null);
|
||||
assertTrue(dfs.getResult() != null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDFSBuildPathToEndNodeMapBValid()
|
||||
{
|
||||
dfs.setValidatable(new Validator(mapB));
|
||||
dfs.setStartPosition(3, 0);
|
||||
dfs.setEndPosition(3, 2);
|
||||
assertTrue(dfs.buildPathToEndNode());
|
||||
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(3, 0), new Point2(3, 2));
|
||||
dfs.buildToGoal(new Validator(mapB), null);
|
||||
assertTrue(dfs.getResult() != null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDFSBuildPathToEndNodeMapDValid()
|
||||
{
|
||||
dfs.setValidatable(new Validator(mapD));
|
||||
dfs.setStartPosition(0, 0);
|
||||
dfs.setEndPosition(0, 3);
|
||||
assertTrue(dfs.buildPathToEndNode());
|
||||
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(0, 0), new Point2(0, 3));
|
||||
dfs.buildToGoal(new Validator(mapD), null);
|
||||
assertTrue(dfs.getResult() != null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDFSBuildPathToEndNodeMapCInvalid()
|
||||
{
|
||||
dfs.setValidatable(new Validator(mapC));
|
||||
dfs.setStartPosition(3, 0);
|
||||
dfs.setEndPosition(3, 2);
|
||||
assertFalse(dfs.buildPathToEndNode());
|
||||
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(3, 0), new Point2(3, 2));
|
||||
dfs.buildToGoal(new Validator(mapC), null);
|
||||
assertFalse(dfs.getResult() != null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDFSBuildPathToEndNodeMapEInvalid()
|
||||
{
|
||||
dfs.setValidatable(new Validator(mapE));
|
||||
dfs.setStartPosition(3, 0);
|
||||
dfs.setEndPosition(3, 2);
|
||||
assertFalse(dfs.buildPathToEndNode());
|
||||
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(3, 0), new Point2(3, 2));
|
||||
dfs.buildToGoal(new Validator(mapE), null);
|
||||
assertFalse(dfs.getResult() != null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDFSFindEndNodeMapBValid() {
|
||||
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(3, 0), new Point2(0, 0));
|
||||
Validator validator = new Validator(mapB);
|
||||
dfs.setValidatable(validator);
|
||||
dfs.setStartPosition(3, 0);
|
||||
|
||||
assertTrue(dfs.findTarget(validator));
|
||||
assertEquals(1, dfs.getFoundNode().getX());
|
||||
assertEquals(2, dfs.getFoundNode().getY());
|
||||
dfs.buildToGoal(validator, validator);
|
||||
assertTrue(dfs.getResult() != null);
|
||||
assertEquals(1, dfs.getResult().x);
|
||||
assertEquals(2, dfs.getResult().y);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDFSFindEndNodeMapDValid() {
|
||||
Validator validator = new Validator(mapD);
|
||||
dfs = new DepthFirstSearch(validator);
|
||||
dfs.setValidatable(validator);
|
||||
dfs.setStartPosition(0, 0);
|
||||
|
||||
assertTrue(dfs.findTarget(validator));
|
||||
assertEquals(0, dfs.getFoundNode().getX());
|
||||
assertEquals(3, dfs.getFoundNode().getY());
|
||||
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(0, 0), new Point2(0, 0));
|
||||
dfs.buildToGoal(validator, validator);
|
||||
assertTrue(dfs.getResult() != null);
|
||||
assertEquals(0, dfs.getResult().x);
|
||||
assertEquals(3, dfs.getResult().y);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDFSFindEndNodeMapHValid() {
|
||||
Validator validator = new Validator(mapH);
|
||||
dfs.setValidatable(validator);
|
||||
dfs.setStartPosition(3, 0);
|
||||
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(3, 0), new Point2(0, 0));
|
||||
dfs.buildToGoal(validator, validator);
|
||||
|
||||
assertTrue(dfs.findTarget(validator));
|
||||
assertEquals(95, dfs.getFoundNode().getX());
|
||||
assertEquals(49, dfs.getFoundNode().getY());
|
||||
assertTrue(dfs.getResult() != null);
|
||||
assertEquals(95, dfs.getResult().x);
|
||||
assertEquals(49, dfs.getResult().y);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDFSFindEndNodeMapHInvalidLimited() {
|
||||
Validator validator = new Validator(mapH);
|
||||
DepthFirstSearch temporary = new DepthFirstSearch(128, validator);
|
||||
temporary.setStartPosition(3, 0);
|
||||
assertFalse(temporary.findTarget(validator));
|
||||
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(3, 0), new Point2(0, 0), null, 128);
|
||||
dfs.buildToGoal(validator, validator);
|
||||
assertFalse(dfs.getResult() != null);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDFSFindEndNodeMapEInvalid() {
|
||||
Validator validator = new Validator(mapE);
|
||||
dfs = new DepthFirstSearch();
|
||||
dfs.setValidatable(validator);
|
||||
dfs.setStartPosition(0, 0);
|
||||
|
||||
assertFalse(dfs.findTarget(validator));
|
||||
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(0, 0), new Point2(0, 0), null, 128);
|
||||
dfs.buildToGoal(validator, validator);
|
||||
assertFalse(dfs.getResult() != null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDFSFindEndNodeMapFValid() {
|
||||
Validator validator = new Validator(mapF);
|
||||
dfs.setValidatable(validator);
|
||||
dfs.setStartPosition(0, 0);
|
||||
|
||||
assertTrue(dfs.findTarget(validator));
|
||||
assertEquals(26, dfs.getFoundNode().getX());
|
||||
assertEquals(32, dfs.getFoundNode().getY());
|
||||
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(0, 0), new Point2(0, 0), null, 1024);
|
||||
dfs.buildToGoal(validator, validator);
|
||||
assertTrue(dfs.getResult() != null);
|
||||
assertEquals(26, dfs.getResult().x);
|
||||
assertEquals(32, dfs.getResult().y);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDFSFindEndNodeMapGInvalid() {
|
||||
Validator validator = new Validator(mapG);
|
||||
dfs.setValidatable(validator);
|
||||
dfs.setStartPosition(0, 0);
|
||||
|
||||
assertFalse(dfs.findTarget(validator));
|
||||
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(0, 0), new Point2(0, 0), null, 128);
|
||||
dfs.buildToGoal(validator, validator);
|
||||
assertFalse(dfs.getResult() != null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDFSFindEndNodeMapHWithAssistValid() {
|
||||
Validator validator = new Validator(mapH);
|
||||
dfs.setValidatable(validator);
|
||||
dfs.setStartPosition(3, 0);
|
||||
dfs.setEndPosition(42, 84);
|
||||
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(3, 0), new Point2(95, 49), null, 0);
|
||||
dfs.buildToGoal(validator, validator);
|
||||
assertTrue(dfs.getResult() != null);
|
||||
assertEquals(95, dfs.getResult().x);
|
||||
assertEquals(49, dfs.getResult().y);
|
||||
}
|
||||
|
||||
assertTrue(dfs.findTarget(validator));
|
||||
assertEquals(95, dfs.getFoundNode().getX());
|
||||
assertEquals(49, dfs.getFoundNode().getY());
|
||||
@Test
|
||||
public void testDFSFindEndNodeMapHWithoutAssistValid() {
|
||||
Validator validator = new Validator(mapH);
|
||||
DepthFirstSearch dfs = new DepthFirstSearch(new Point2(3, 0), new Point2(0, 0), null, 0);
|
||||
dfs.buildToGoal(validator, validator);
|
||||
assertTrue(dfs.getResult() != null);
|
||||
assertEquals(95, dfs.getResult().x);
|
||||
assertEquals(49, dfs.getResult().y);
|
||||
}
|
||||
}
|
||||
|
@@ -62,7 +62,7 @@ public class IslandWorldMapTest {
|
||||
Point2 origin = null;
|
||||
for (int i = 0; i < GRIDSIZE; i++) {
|
||||
if (origin != null) {
|
||||
if (mapper.isSameIsland(origin.x, origin.y, i, 0)) {
|
||||
if (mapper.isSameIsland(origin, new Point2(i, 0))) {
|
||||
assertEquals(origin, mapper.findIslandOrigin(i, 0), String.format("Looking at (%d, 0)", i));
|
||||
} else {
|
||||
origin = null;
|
||||
@@ -80,7 +80,7 @@ public class IslandWorldMapTest {
|
||||
for (int y = 0; y < gridSize; y++) {
|
||||
for (int x = 0; x < gridSize; x++) {
|
||||
if (origin != null) {
|
||||
if (mapper.isSameIsland(origin.x, origin.y, x, y)) {
|
||||
if (mapper.isSameIsland(origin, new Point2(x, y))) {
|
||||
assertEquals(origin, mapper.findIslandOrigin(x, y), String.format("Looking at (%d, %d)", x, y));
|
||||
} else {
|
||||
origin = null;
|
||||
|
Reference in New Issue
Block a user