Refactor Direction
This commit is contained in:
parent
0faf8f5f94
commit
3958b3504d
2 changed files with 4 additions and 24 deletions
|
@ -1,11 +1,7 @@
|
||||||
package ch.fritteli.labyrinth;
|
package ch.fritteli.labyrinth;
|
||||||
|
|
||||||
import io.vavr.collection.Stream;
|
|
||||||
import io.vavr.control.Option;
|
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
|
|
||||||
import java.util.EnumSet;
|
|
||||||
import java.util.Random;
|
|
||||||
import java.util.function.UnaryOperator;
|
import java.util.function.UnaryOperator;
|
||||||
|
|
||||||
public enum Direction {
|
public enum Direction {
|
||||||
|
@ -13,7 +9,7 @@ public enum Direction {
|
||||||
BOTTOM(position -> position.withY(position.getY() + 1)),
|
BOTTOM(position -> position.withY(position.getY() + 1)),
|
||||||
LEFT(position -> position.withX(position.getX() - 1)),
|
LEFT(position -> position.withX(position.getX() - 1)),
|
||||||
RIGHT(position -> position.withX(position.getX() + 1));
|
RIGHT(position -> position.withX(position.getX() + 1));
|
||||||
private static final Random random = new Random();
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private final UnaryOperator<Position> translation;
|
private final UnaryOperator<Position> translation;
|
||||||
|
|
||||||
|
@ -21,23 +17,7 @@ public enum Direction {
|
||||||
this.translation = translation;
|
this.translation = translation;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Direction getRandom() {
|
public Direction invert() {
|
||||||
return values()[random.nextInt(4)];
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Option<Direction> getRandomExcluding(@NonNull final EnumSet<Direction> directions) {
|
|
||||||
final EnumSet<Direction> allowedDirections = EnumSet.complementOf(directions);
|
|
||||||
return Stream.ofAll(allowedDirections).shuffle().headOption();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Option<Direction> getRandomExcluding(@NonNull final Direction... directions) {
|
|
||||||
return Stream.of(values())
|
|
||||||
.removeAll(Stream.of(directions))
|
|
||||||
.shuffle()
|
|
||||||
.headOption();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Direction getOpposite() {
|
|
||||||
switch (this) {
|
switch (this) {
|
||||||
case TOP:
|
case TOP:
|
||||||
return BOTTOM;
|
return BOTTOM;
|
||||||
|
@ -48,7 +28,7 @@ public enum Direction {
|
||||||
case BOTTOM:
|
case BOTTOM:
|
||||||
return TOP;
|
return TOP;
|
||||||
}
|
}
|
||||||
throw new IllegalStateException("Programming error: Not all enum values covered in enum Direction#getOpposite()!");
|
throw new IllegalStateException("Programming error: Not all enum values covered in enum Direction#invert()!");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Position translate(@NonNull final Position position) {
|
public Position translate(@NonNull final Position position) {
|
||||||
|
|
|
@ -78,7 +78,7 @@ public class Labyrinth {
|
||||||
final Option<Direction> directionToDigTo = currentTile.getRandomAvailableDirection();
|
final Option<Direction> directionToDigTo = currentTile.getRandomAvailableDirection();
|
||||||
if (directionToDigTo.isDefined()) {
|
if (directionToDigTo.isDefined()) {
|
||||||
final Direction digTo = directionToDigTo.get();
|
final Direction digTo = directionToDigTo.get();
|
||||||
final Direction digFrom = digTo.getOpposite();
|
final Direction digFrom = digTo.invert();
|
||||||
final Position neighborPosition = digTo.translate(currentPosition);
|
final Position neighborPosition = digTo.translate(currentPosition);
|
||||||
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)) {
|
||||||
|
|
Loading…
Reference in a new issue