Minor refactoring.
This commit is contained in:
parent
88d86ed186
commit
5959200bed
4 changed files with 22 additions and 21 deletions
|
@ -1,21 +1,10 @@
|
|||
package ch.fritteli.labyrinth.model;
|
||||
|
||||
import lombok.NonNull;
|
||||
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
public enum Direction {
|
||||
TOP(position -> position.withY(position.getY() - 1)),
|
||||
BOTTOM(position -> position.withY(position.getY() + 1)),
|
||||
LEFT(position -> position.withX(position.getX() - 1)),
|
||||
RIGHT(position -> position.withX(position.getX() + 1));
|
||||
|
||||
@NonNull
|
||||
private final UnaryOperator<Position> translation;
|
||||
|
||||
Direction(@NonNull final UnaryOperator<Position> translation) {
|
||||
this.translation = translation;
|
||||
}
|
||||
TOP,
|
||||
BOTTOM,
|
||||
LEFT,
|
||||
RIGHT;
|
||||
|
||||
public Direction invert() {
|
||||
switch (this) {
|
||||
|
@ -30,8 +19,4 @@ public enum Direction {
|
|||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -126,7 +126,7 @@ public class Labyrinth {
|
|||
if (directionToDigTo.isDefined()) {
|
||||
final Direction digTo = directionToDigTo.get();
|
||||
final Direction digFrom = digTo.invert();
|
||||
final Position neighborPosition = digTo.translate(currentPosition);
|
||||
final Position neighborPosition = currentPosition.move(digTo);
|
||||
final Tile neighborTile = Labyrinth.this.getTileAt(neighborPosition);
|
||||
if (currentTile.digTo(digTo) && neighborTile.digFrom(digFrom)) {
|
||||
// all ok!
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package ch.fritteli.labyrinth.model;
|
||||
|
||||
import lombok.NonNull;
|
||||
import lombok.Value;
|
||||
import lombok.With;
|
||||
|
||||
|
@ -8,4 +9,19 @@ import lombok.With;
|
|||
public class Position {
|
||||
int x;
|
||||
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)!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -223,7 +223,7 @@ class Generator {
|
|||
final Tile currentTile = this.labyrinth.getTileAt(currentPosition);
|
||||
for (final Direction direction : Direction.values()) {
|
||||
if (!currentTile.hasWallAt(direction)) {
|
||||
final Position position = direction.translate(currentPosition);
|
||||
final Position position = currentPosition.move(direction);
|
||||
final Tile tileAtPosition = this.labyrinth.getTileAtOrNull(position);
|
||||
if (position.equals(previousPosition) || tileAtPosition == null) {
|
||||
continue;
|
||||
|
|
Loading…
Reference in a new issue