Very simple way to specify the algorithm to use to generate mazes.
	
		
			
	
		
	
	
		
	
		
			Some checks failed
		
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is failing
				
			
		
		
	
	
				
					
				
			
		
			Some checks failed
		
		
	
	continuous-integration/drone/push Build is failing
				
			This commit is contained in:
		
							parent
							
								
									a613a92adb
								
							
						
					
					
						commit
						c4d707d64a
					
				
					 5 changed files with 62 additions and 4 deletions
				
			
		
							
								
								
									
										45
									
								
								src/main/java/ch/fritteli/maze/server/Algorithm.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								src/main/java/ch/fritteli/maze/server/Algorithm.java
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,45 @@ | |||
| package ch.fritteli.maze.server; | ||||
| 
 | ||||
| import ch.fritteli.maze.generator.algorithm.MazeGeneratorAlgorithm; | ||||
| import ch.fritteli.maze.generator.algorithm.RandomDepthFirst; | ||||
| import ch.fritteli.maze.generator.algorithm.Wilson; | ||||
| import ch.fritteli.maze.generator.model.Maze; | ||||
| import io.vavr.collection.List; | ||||
| import io.vavr.collection.Stream; | ||||
| import io.vavr.control.Option; | ||||
| import lombok.Getter; | ||||
| import org.jetbrains.annotations.NotNull; | ||||
| import org.jetbrains.annotations.Nullable; | ||||
| 
 | ||||
| import java.util.function.Function; | ||||
| 
 | ||||
| public enum Algorithm { | ||||
|     RANDOM_DEPTH_FIRST(RandomDepthFirst::new, "random", "random-depth-first"), | ||||
|     WILSON(Wilson::new, "wilson"); | ||||
| 
 | ||||
|     @NotNull | ||||
|     private final Function<Maze, MazeGeneratorAlgorithm> creator; | ||||
| 
 | ||||
|     @Getter | ||||
|     @NotNull | ||||
|     private final List<String> names; | ||||
| 
 | ||||
|     Algorithm(@NotNull final Function<Maze, MazeGeneratorAlgorithm> creator, | ||||
|               @NotNull final String... names) { | ||||
|         this.creator = creator; | ||||
|         this.names = List.of(names); | ||||
|     } | ||||
| 
 | ||||
|     @NotNull | ||||
|     public static Option<Algorithm> ofString(@Nullable final String name) { | ||||
|         return Option.of(name) | ||||
|                 .map(String::toLowerCase) | ||||
|                 .flatMap(nameLC -> Stream.of(values()) | ||||
|                         .find(algorithm -> algorithm.getNames().contains(nameLC))); | ||||
|     } | ||||
| 
 | ||||
|     @NotNull | ||||
|     public MazeGeneratorAlgorithm createAlgorithm(@NotNull final Maze maze) { | ||||
|         return this.creator.apply(maze); | ||||
|     } | ||||
| } | ||||
|  | @ -3,6 +3,7 @@ package ch.fritteli.maze.server.handler; | |||
| import ch.fritteli.maze.generator.algorithm.RandomDepthFirst; | ||||
| import ch.fritteli.maze.generator.model.Maze; | ||||
| import ch.fritteli.maze.generator.model.Position; | ||||
| import ch.fritteli.maze.server.Algorithm; | ||||
| import ch.fritteli.maze.server.InvalidRequestParameterException; | ||||
| import ch.fritteli.maze.server.OutputType; | ||||
| import io.vavr.collection.Stream; | ||||
|  | @ -33,6 +34,7 @@ class ParametersToMazeExtractor { | |||
|         final Option<Long> id = getParameterValue(RequestParameter.ID); | ||||
|         final Option<Position> start = getParameterValue(RequestParameter.START); | ||||
|         final Option<Position> end = getParameterValue(RequestParameter.END); | ||||
|         final Option<Algorithm> algorithm = getParameterValue(RequestParameter.ALGORITHM); | ||||
| 
 | ||||
|         if (output.isEmpty()) { | ||||
|             return Try.failure(new InvalidRequestParameterException("Path parameter %s is required and must be one of: %s".formatted( | ||||
|  | @ -77,7 +79,10 @@ class ParametersToMazeExtractor { | |||
|             } else { | ||||
|                 maze = new Maze(desiredWidth, desiredHeight, id.getOrElse(() -> new Random().nextLong())); | ||||
|             } | ||||
|             new RandomDepthFirst(maze).run(); | ||||
| 
 | ||||
|             algorithm.getOrElse(Algorithm.RANDOM_DEPTH_FIRST) | ||||
|                     .createAlgorithm(maze) | ||||
|                     .run(); | ||||
|             return new GeneratedMaze(maze, output.get(), RandomDepthFirst.class.getSimpleName()); | ||||
|         }); | ||||
|     } | ||||
|  |  | |||
|  | @ -1,6 +1,7 @@ | |||
| package ch.fritteli.maze.server.handler; | ||||
| 
 | ||||
| import ch.fritteli.maze.generator.model.Position; | ||||
| import ch.fritteli.maze.server.Algorithm; | ||||
| import ch.fritteli.maze.server.OutputType; | ||||
| import io.vavr.Tuple2; | ||||
| import io.vavr.collection.HashMap; | ||||
|  | @ -44,7 +45,9 @@ enum RequestParameter { | |||
|                 return new Position(x, y); | ||||
|             }) | ||||
|             .toOption() | ||||
|             .onEmpty(() -> log.debug("Unparseable value for parameter 'end': '{}'", p)), "e", "end"); | ||||
|             .onEmpty(() -> log.debug("Unparseable value for parameter 'end': '{}'", p)), "e", "end"), | ||||
|     ALGORITHM(p -> Algorithm.ofString(p) | ||||
|             .onEmpty(() -> log.debug("Unparseable value for parameter 'algorithm': '{}'", p)), "a", "algorithm"); | ||||
|     @NotNull | ||||
|     private final Function<String, Option<?>> extractor; | ||||
|     @Getter | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue