Refactor a bit.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Manuel Friedli 2024-12-13 23:13:28 +01:00
parent b18e7fba9e
commit cf405fbc98
Signed by: manuel
GPG key ID: 41D08ABA75634DA1

View file

@ -6,6 +6,7 @@ import ch.fritteli.maze.generator.model.Position;
import ch.fritteli.maze.generator.model.Tile; import ch.fritteli.maze.generator.model.Tile;
import io.vavr.control.Option; import io.vavr.control.Option;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Deque; import java.util.Deque;
import java.util.LinkedList; import java.util.LinkedList;
@ -33,20 +34,11 @@ public class RandomDepthFirst {
private void preDig() { private void preDig() {
final Position end = this.maze.getEnd(); final Position end = this.maze.getEnd();
final Tile endTile = this.maze.getEndTile(); final Direction direction = this.determineDirectionForDigging(end);
if (end.y() == 0) { if (direction != null) {
endTile.enableDiggingToOrFrom(Direction.TOP); final Tile endTile = this.maze.getEndTile();
endTile.digFrom(Direction.TOP); this.digFrom(endTile, direction);
} 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);
} }
this.positions.push(end); this.positions.push(end);
@ -85,20 +77,38 @@ public class RandomDepthFirst {
private void postDig() { private void postDig() {
final Position start = this.maze.getStart(); final Position start = this.maze.getStart();
final Tile startTile = this.maze.getStartTile();
if (start.y() == 0) { final Direction direction = this.determineDirectionForDigging(start);
startTile.enableDiggingToOrFrom(Direction.TOP); if (direction != null) {
startTile.digTo(Direction.TOP); final Tile startTile = this.maze.getStartTile();
} else if (start.x() == 0) { this.digTo(startTile, direction);
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);
} }
} }
@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);
}
} }