From 3958b3504d2610ed154f717ffbf914eb973c1135 Mon Sep 17 00:00:00 2001 From: Manuel Friedli Date: Wed, 30 Sep 2020 22:24:51 +0200 Subject: [PATCH] Refactor Direction --- .../java/ch/fritteli/labyrinth/Direction.java | 26 +++---------------- .../java/ch/fritteli/labyrinth/Labyrinth.java | 2 +- 2 files changed, 4 insertions(+), 24 deletions(-) diff --git a/src/main/java/ch/fritteli/labyrinth/Direction.java b/src/main/java/ch/fritteli/labyrinth/Direction.java index 217f51a..f9d3b76 100644 --- a/src/main/java/ch/fritteli/labyrinth/Direction.java +++ b/src/main/java/ch/fritteli/labyrinth/Direction.java @@ -1,11 +1,7 @@ package ch.fritteli.labyrinth; -import io.vavr.collection.Stream; -import io.vavr.control.Option; import lombok.NonNull; -import java.util.EnumSet; -import java.util.Random; import java.util.function.UnaryOperator; public enum Direction { @@ -13,7 +9,7 @@ public enum Direction { BOTTOM(position -> position.withY(position.getY() + 1)), LEFT(position -> position.withX(position.getX() - 1)), RIGHT(position -> position.withX(position.getX() + 1)); - private static final Random random = new Random(); + @NonNull private final UnaryOperator translation; @@ -21,23 +17,7 @@ public enum Direction { this.translation = translation; } - public static Direction getRandom() { - return values()[random.nextInt(4)]; - } - - public static Option getRandomExcluding(@NonNull final EnumSet directions) { - final EnumSet allowedDirections = EnumSet.complementOf(directions); - return Stream.ofAll(allowedDirections).shuffle().headOption(); - } - - public static Option getRandomExcluding(@NonNull final Direction... directions) { - return Stream.of(values()) - .removeAll(Stream.of(directions)) - .shuffle() - .headOption(); - } - - public Direction getOpposite() { + public Direction invert() { switch (this) { case TOP: return BOTTOM; @@ -48,7 +28,7 @@ public enum Direction { case BOTTOM: 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) { diff --git a/src/main/java/ch/fritteli/labyrinth/Labyrinth.java b/src/main/java/ch/fritteli/labyrinth/Labyrinth.java index 553087c..c8baa80 100644 --- a/src/main/java/ch/fritteli/labyrinth/Labyrinth.java +++ b/src/main/java/ch/fritteli/labyrinth/Labyrinth.java @@ -78,7 +78,7 @@ public class Labyrinth { final Option directionToDigTo = currentTile.getRandomAvailableDirection(); if (directionToDigTo.isDefined()) { final Direction digTo = directionToDigTo.get(); - final Direction digFrom = digTo.getOpposite(); + final Direction digFrom = digTo.invert(); final Position neighborPosition = digTo.translate(currentPosition); final Tile neighborTile = Labyrinth.this.getTileAt(neighborPosition); if (currentTile.digTo(digTo) && neighborTile.digFrom(digFrom)) {