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; | ||||
| 
 | ||||
| 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<Position> translation; | ||||
| 
 | ||||
|  | @ -21,23 +17,7 @@ public enum Direction { | |||
|         this.translation = translation; | ||||
|     } | ||||
| 
 | ||||
|     public static Direction getRandom() { | ||||
|         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() { | ||||
|     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) { | ||||
|  |  | |||
|  | @ -78,7 +78,7 @@ public class Labyrinth { | |||
|                 final Option<Direction> 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)) { | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue