Preventing flooder from checking same node.

Added another flooder test.
This commit is contained in:
Harrison Deng 2020-04-27 23:17:54 -05:00
parent 85ec44a4d7
commit 7c073cdc6e
2 changed files with 22 additions and 6 deletions

View File

@ -6,7 +6,6 @@ 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;
@ -20,24 +19,26 @@ public class Flooder {
}
public void start() {
HashSet<Point2> checked = new HashSet<>();
LinkedList<Point2> queue = new LinkedList<>();
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);
if (checked.add(pE) && floodable.flood(pE)) queue.add(pE);
Point2 pW = new Point2(p.x - 1, p.y);
if (floodable.flood(pW)) queue.add(pW);
if (checked.add(pW) && floodable.flood(pW)) queue.add(pW);
Point2 pN = new Point2(p.x, p.y + 1);
if (floodable.flood(pN)) queue.add(pN);
if (checked.add(pN) && floodable.flood(pN)) queue.add(pN);
Point2 pS = new Point2(p.x, p.y - 1);
if (floodable.flood(pS)) queue.add(pS);
if (checked.add(pS) && floodable.flood(pS)) queue.add(pS);
}
}
}

View File

@ -99,4 +99,19 @@ public class FloodFillTest {
}
}
}
@Test
public void testFillMapBNonCorner() {
Flooder flooder = new Flooder(new Point2(43, 11), 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));
}
}
}
}
}