diff --git a/src/main/java/ch/fritteli/maze/generator/algorithm/RandomDepthFirst.java b/src/main/java/ch/fritteli/maze/generator/algorithm/RandomDepthFirst.java index 2c1b3d2..a65b15e 100644 --- a/src/main/java/ch/fritteli/maze/generator/algorithm/RandomDepthFirst.java +++ b/src/main/java/ch/fritteli/maze/generator/algorithm/RandomDepthFirst.java @@ -6,6 +6,7 @@ import ch.fritteli.maze.generator.model.Position; import ch.fritteli.maze.generator.model.Tile; import io.vavr.control.Option; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Deque; import java.util.LinkedList; @@ -33,20 +34,11 @@ public class RandomDepthFirst { private void preDig() { final Position end = this.maze.getEnd(); - final Tile endTile = this.maze.getEndTile(); + final Direction direction = this.determineDirectionForDigging(end); - if (end.y() == 0) { - endTile.enableDiggingToOrFrom(Direction.TOP); - endTile.digFrom(Direction.TOP); - } else if (end.x() == 0) { - endTile.enableDiggingToOrFrom(Direction.LEFT); - endTile.digFrom(Direction.LEFT); - } else if (end.y() == this.maze.getHeight() - 1) { - endTile.enableDiggingToOrFrom(Direction.BOTTOM); - endTile.digFrom(Direction.BOTTOM); - } else if (end.x() == this.maze.getWidth() - 1) { - endTile.enableDiggingToOrFrom(Direction.RIGHT); - endTile.digFrom(Direction.RIGHT); + if (direction != null) { + final Tile endTile = this.maze.getEndTile(); + this.digFrom(endTile, direction); } this.positions.push(end); @@ -85,20 +77,38 @@ public class RandomDepthFirst { private void postDig() { final Position start = this.maze.getStart(); - final Tile startTile = this.maze.getStartTile(); - if (start.y() == 0) { - startTile.enableDiggingToOrFrom(Direction.TOP); - startTile.digTo(Direction.TOP); - } else if (start.x() == 0) { - startTile.enableDiggingToOrFrom(Direction.LEFT); - startTile.digTo(Direction.LEFT); - } else if (start.y() == this.maze.getHeight() - 1) { - startTile.enableDiggingToOrFrom(Direction.BOTTOM); - startTile.digTo(Direction.BOTTOM); - } else if (start.x() == this.maze.getWidth() - 1) { - startTile.enableDiggingToOrFrom(Direction.RIGHT); - startTile.digTo(Direction.RIGHT); + final Direction direction = this.determineDirectionForDigging(start); + if (direction != null) { + final Tile startTile = this.maze.getStartTile(); + this.digTo(startTile, direction); } } + + @Nullable + private Direction determineDirectionForDigging(@NotNull final Position position) { + if (position.y() == 0) { + return Direction.TOP; + } + if (position.x() == 0) { + return Direction.LEFT; + } + if (position.y() == this.maze.getHeight() - 1) { + return Direction.BOTTOM; + } + if (position.x() == this.maze.getWidth() - 1) { + return Direction.RIGHT; + } + return null; + } + + private void digFrom(@NotNull final Tile tile, @NotNull final Direction direction) { + tile.enableDiggingToOrFrom(direction); + tile.digFrom(direction); + } + + private void digTo(@NotNull final Tile tile, @NotNull final Direction direction) { + tile.enableDiggingToOrFrom(direction); + tile.digTo(direction); + } }