Flood algorithm added and (basic) tests performed.

This commit is contained in:
2020-04-27 21:10:55 -05:00
parent 2a0f26f8bc
commit 85ec44a4d7
3 changed files with 159 additions and 0 deletions

View File

@@ -0,0 +1,102 @@
package ca.recrown.islandsurvivalcraft.floodfill;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Scanner;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import ca.recrown.islandsurvivalcraft.types.Point2;
@TestInstance(Lifecycle.PER_CLASS)
public class FloodFillTest {
public byte[][] mapA;
public byte[][] mapB;
private class Flood implements Floodable {
private final byte[][] map;
public Flood(byte[][] map) {
this.map = map;
}
@Override
public boolean flood(Point2 point) {
try {
if (map[point.y][point.x] < 1) return false;
} catch (IndexOutOfBoundsException e) {
return false;
}
map[point.y][point.x] = 0;
return true;
}
}
private byte[][] readMap(String filename) throws IOException {
byte[][] map = null;
InputStream stream = getClass().getClassLoader().getResourceAsStream((filename));
Scanner scanner = new Scanner(stream);
String line = null;
ArrayList<byte[]> rows = new ArrayList<>();
while (scanner.hasNextLine()) {
line = scanner.nextLine();
char[] chars = line.toCharArray();
byte[] rowBytes = new byte[chars.length];
for (int i = 0; i < chars.length; i++) {
rowBytes[i] = (byte) Character.getNumericValue(chars[i]);
}
rows.add(rowBytes);
}
scanner.close();
map = new byte[rows.size()][rows.get(0).length];
for (int row = 0; row < rows.size(); row++) {
map[row] = rows.get(row);
}
return map;
}
@BeforeEach
public void reset() {
try {
mapA = readMap("DFSTestMapLargeA.txt");
mapB = readMap("DFSTestMapLargeB.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testFillMapA() {
Flooder flooder = new Flooder(new Point2(0, 0), new Flood(mapA));
flooder.start();
for (int y = 0; y < mapA.length; y++) {
for (int x = 0; x < mapA[y].length; x++) {
assertEquals(0, mapA[y][x], String.format("At: %d, %d", x, y));
}
}
}
@Test
public void testFillMapB() {
Flooder flooder = new Flooder(new Point2(0, 0), new Flood(mapB));
flooder.start();
for (int y = 0; y < mapB.length; y++) {
for (int x = 0; x < mapB[y].length; x++) {
if (x > 21 && y > 15 && x < 27) {
assertTrue(mapB[y][x] > 0, String.format("At: %d, %d", x, y));
} else {
assertEquals(0, mapB[y][x], String.format("At: %d, %d", x, y));
}
}
}
}
}