Flood algorithm added and (basic) tests performed.

This commit is contained in:
Harrison Deng 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,14 @@
package ca.recrown.islandsurvivalcraft.floodfill;
import ca.recrown.islandsurvivalcraft.types.Point2;
public interface Floodable {
/**
* Called for every block. If it shouldn't be flooded, return false.
* Code to execute for flooding should be placed here.
* @param x the x coordinate.
* @param y the y coordinate.
* @return if this was a successful flooding.
*/
public boolean flood(Point2 point);
}

View File

@ -0,0 +1,43 @@
package ca.recrown.islandsurvivalcraft.floodfill;
import java.util.HashSet;
import java.util.LinkedList;
import ca.recrown.islandsurvivalcraft.types.Point2;
public class Flooder {
private final HashSet<Point2> checked = new HashSet<>();
private final Point2 start;
private final Floodable floodable;
public Flooder(Point2 start, Floodable floodable) {
this.start = start;
this.floodable = floodable;
}
public Flooder(int xStart, int yStart, Floodable floodable) {
this(new Point2(xStart, yStart), floodable);
}
public void start() {
if (!checked.add(start)) return;
if (!floodable.flood(start)) return;
LinkedList<Point2> queue = new LinkedList<>();
queue.add(start);
while (!queue.isEmpty()) {
Point2 p = queue.pop();
Point2 pE = new Point2(p.x + 1, p.y);
if (floodable.flood(pE)) queue.add(pE);
Point2 pW = new Point2(p.x - 1, p.y);
if (floodable.flood(pW)) queue.add(pW);
Point2 pN = new Point2(p.x, p.y + 1);
if (floodable.flood(pN)) queue.add(pN);
Point2 pS = new Point2(p.x, p.y - 1);
if (floodable.flood(pS)) queue.add(pS);
}
}
}

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