Minor refactoring.

This commit is contained in:
Manuel Friedli 2020-10-04 23:11:47 +02:00
parent 88d86ed186
commit 5959200bed
4 changed files with 22 additions and 21 deletions

View file

@ -1,21 +1,10 @@
package ch.fritteli.labyrinth.model; package ch.fritteli.labyrinth.model;
import lombok.NonNull;
import java.util.function.UnaryOperator;
public enum Direction { public enum Direction {
TOP(position -> position.withY(position.getY() - 1)), TOP,
BOTTOM(position -> position.withY(position.getY() + 1)), BOTTOM,
LEFT(position -> position.withX(position.getX() - 1)), LEFT,
RIGHT(position -> position.withX(position.getX() + 1)); RIGHT;
@NonNull
private final UnaryOperator<Position> translation;
Direction(@NonNull final UnaryOperator<Position> translation) {
this.translation = translation;
}
public Direction invert() { public Direction invert() {
switch (this) { switch (this) {
@ -30,8 +19,4 @@ public enum Direction {
} }
throw new IllegalStateException("Programming error: Not all enum values covered in enum Direction#invert()!"); throw new IllegalStateException("Programming error: Not all enum values covered in enum Direction#invert()!");
} }
public Position translate(@NonNull final Position position) {
return this.translation.apply(position);
}
} }

View file

@ -126,7 +126,7 @@ public class Labyrinth {
if (directionToDigTo.isDefined()) { if (directionToDigTo.isDefined()) {
final Direction digTo = directionToDigTo.get(); final Direction digTo = directionToDigTo.get();
final Direction digFrom = digTo.invert(); final Direction digFrom = digTo.invert();
final Position neighborPosition = digTo.translate(currentPosition); final Position neighborPosition = currentPosition.move(digTo);
final Tile neighborTile = Labyrinth.this.getTileAt(neighborPosition); final Tile neighborTile = Labyrinth.this.getTileAt(neighborPosition);
if (currentTile.digTo(digTo) && neighborTile.digFrom(digFrom)) { if (currentTile.digTo(digTo) && neighborTile.digFrom(digFrom)) {
// all ok! // all ok!

View file

@ -1,5 +1,6 @@
package ch.fritteli.labyrinth.model; package ch.fritteli.labyrinth.model;
import lombok.NonNull;
import lombok.Value; import lombok.Value;
import lombok.With; import lombok.With;
@ -8,4 +9,19 @@ import lombok.With;
public class Position { public class Position {
int x; int x;
int y; int y;
public Position move(@NonNull final Direction direction) {
switch (direction) {
case BOTTOM:
return this.withY(this.y + 1);
case LEFT:
return this.withX(this.x - 1);
case RIGHT:
return this.withX(this.x + 1);
case TOP:
return this.withY(this.y - 1);
default:
throw new IllegalStateException("Programming error: Not all Direction enum values covered in Position#move(Direction)!");
}
}
} }

View file

@ -223,7 +223,7 @@ class Generator {
final Tile currentTile = this.labyrinth.getTileAt(currentPosition); final Tile currentTile = this.labyrinth.getTileAt(currentPosition);
for (final Direction direction : Direction.values()) { for (final Direction direction : Direction.values()) {
if (!currentTile.hasWallAt(direction)) { if (!currentTile.hasWallAt(direction)) {
final Position position = direction.translate(currentPosition); final Position position = currentPosition.move(direction);
final Tile tileAtPosition = this.labyrinth.getTileAtOrNull(position); final Tile tileAtPosition = this.labyrinth.getTileAtOrNull(position);
if (position.equals(previousPosition) || tileAtPosition == null) { if (position.equals(previousPosition) || tileAtPosition == null) {
continue; continue;