Nicer rendering of the solution in the TextRenderer.
This commit is contained in:
		
							parent
							
								
									e52e482b9a
								
							
						
					
					
						commit
						ae270b7eb9
					
				
					 2 changed files with 114 additions and 36 deletions
				
			
		|  | @ -54,15 +54,17 @@ public class TextRenderer implements Renderer<String> { | ||||||
| 
 | 
 | ||||||
|         private String next() { |         private String next() { | ||||||
|             final Tile currentTile = this.labyrinth.getTileAt(this.x, this.y); |             final Tile currentTile = this.labyrinth.getTileAt(this.x, this.y); | ||||||
|             final Tile leftTile = this.getTileOrNull(this.x - 1, this.y); |  | ||||||
|             final Tile topTile = this.getTileOrNull(this.x, this.y - 1); |             final Tile topTile = this.getTileOrNull(this.x, this.y - 1); | ||||||
|  |             final Tile rightTile = this.getTileOrNull(this.x + 1, this.y); | ||||||
|  |             final Tile bottomTile = this.getTileOrNull(this.x, this.y + 1); | ||||||
|  |             final Tile leftTile = this.getTileOrNull(this.x - 1, this.y); | ||||||
|             final String s; |             final String s; | ||||||
|             switch (this.line) { |             switch (this.line) { | ||||||
|                 case 0: |                 case 0: | ||||||
|                     s = this.renderTopLine(currentTile, leftTile, topTile); |                     s = this.renderTopLine(currentTile, leftTile, topTile); | ||||||
|                     break; |                     break; | ||||||
|                 case 1: |                 case 1: | ||||||
|                     s = this.renderCenterLine(currentTile); |                     s = this.renderCenterLine(currentTile, topTile, rightTile, bottomTile, leftTile); | ||||||
|                     break; |                     break; | ||||||
|                 case 2: |                 case 2: | ||||||
|                     s = this.renderBottomLine(currentTile, leftTile); |                     s = this.renderBottomLine(currentTile, leftTile); | ||||||
|  | @ -117,20 +119,22 @@ public class TextRenderer implements Renderer<String> { | ||||||
|                 charDef1.right(); |                 charDef1.right(); | ||||||
|                 charDef2.horizontal(); |                 charDef2.horizontal(); | ||||||
|                 charDef3.left(); |                 charDef3.left(); | ||||||
|  |             } else { | ||||||
|  |                 if (this.isSolution(currentTile) && (this.isSolution(topTile) || topTile == null)) { | ||||||
|  |                     charDef2.solution().vertical(); | ||||||
|  |                 } | ||||||
|             } |             } | ||||||
|             if (currentTile.hasWallAt(Direction.RIGHT)) { |             if (currentTile.hasWallAt(Direction.RIGHT)) { | ||||||
|                 charDef3.down(); |                 charDef3.down(); | ||||||
|             } |             } | ||||||
|             if (leftTile != null && leftTile.hasWallAt(Direction.TOP)) { |             if (this.hasWallAt(leftTile, Direction.TOP)) { | ||||||
|                 charDef1.left(); |                 charDef1.left(); | ||||||
|             } |             } | ||||||
|             if (topTile != null) { |             if (this.hasWallAt(topTile, Direction.LEFT)) { | ||||||
|                 if (topTile.hasWallAt(Direction.LEFT)) { |                 charDef1.up(); | ||||||
|                     charDef1.up(); |             } | ||||||
|                 } |             if (this.hasWallAt(topTile, Direction.RIGHT)) { | ||||||
|                 if (topTile.hasWallAt(Direction.RIGHT)) { |                 charDef3.up(); | ||||||
|                     charDef3.up(); |  | ||||||
|                 } |  | ||||||
|             } |             } | ||||||
|             result = charDef1.toString() + charDef2; |             result = charDef1.toString() + charDef2; | ||||||
|             if (this.x == this.labyrinth.getWidth() - 1) { |             if (this.x == this.labyrinth.getWidth() - 1) { | ||||||
|  | @ -139,21 +143,49 @@ public class TextRenderer implements Renderer<String> { | ||||||
|             return result; |             return result; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         private String renderCenterLine(@NonNull final Tile currentTile) { |         private String renderCenterLine(@NonNull final Tile currentTile, | ||||||
|  |                                         @Nullable final Tile topTile, | ||||||
|  |                                         @Nullable final Tile rightTile, | ||||||
|  |                                         @Nullable final Tile bottomTile, | ||||||
|  |                                         @Nullable final Tile leftTile) { | ||||||
|             final CharDefinition charDef1 = new CharDefinition(); |             final CharDefinition charDef1 = new CharDefinition(); | ||||||
|             final CharDefinition charDef2 = new CharDefinition(); |             final CharDefinition charDef2 = new CharDefinition(); | ||||||
|  |             final CharDefinition charDef3 = new CharDefinition(); | ||||||
|             String result; |             String result; | ||||||
|             if (currentTile.hasWallAt(Direction.LEFT)) { |             if (currentTile.hasWallAt(Direction.LEFT)) { | ||||||
|                 charDef1.vertical(); |                 charDef1.vertical(); | ||||||
|  |             } else { | ||||||
|  |                 if (this.isSolution(currentTile) && this.isSolution(leftTile)) { | ||||||
|  |                     charDef1.solution().horizontal(); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             if (this.isSolution(currentTile)) { | ||||||
|  |                 charDef2.solution(); | ||||||
|  |                 if (!currentTile.hasWallAt(Direction.LEFT) && (this.isSolution(leftTile) || leftTile == null)) { | ||||||
|  |                     charDef2.left(); | ||||||
|  |                 } | ||||||
|  |                 if (!currentTile.hasWallAt(Direction.TOP) && (this.isSolution(topTile) || topTile == null)) { | ||||||
|  |                     charDef2.up(); | ||||||
|  |                 } | ||||||
|  |                 if (!currentTile.hasWallAt(Direction.RIGHT) && (this.isSolution(rightTile) || rightTile == null)) { | ||||||
|  |                     charDef2.right(); | ||||||
|  |                 } | ||||||
|  |                 if (!currentTile.hasWallAt(Direction.BOTTOM) && (this.isSolution(bottomTile) || bottomTile == null)) { | ||||||
|  |                     charDef2.down(); | ||||||
|  |                 } | ||||||
|             } |             } | ||||||
|             if (currentTile.hasWallAt(Direction.RIGHT)) { |             if (currentTile.hasWallAt(Direction.RIGHT)) { | ||||||
|                 charDef2.vertical(); |                 charDef3.vertical(); | ||||||
|  |             } else { | ||||||
|  |                 if (this.isSolution(currentTile) && this.isSolution(rightTile)) { | ||||||
|  |                     charDef3.solution().horizontal(); | ||||||
|  |                 } | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             result = charDef1 + (currentTile.isSolution() && this.renderSolution ? "x" : " "); |             result = charDef1.toString() + charDef2; | ||||||
| 
 | 
 | ||||||
|             if (this.x == this.labyrinth.getWidth() - 1) { |             if (this.x == this.labyrinth.getWidth() - 1) { | ||||||
|                 result += charDef2 + "\n"; |                 result += charDef3 + "\n"; | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             return result; |             return result; | ||||||
|  | @ -172,11 +204,15 @@ public class TextRenderer implements Renderer<String> { | ||||||
|                 charDef1.right(); |                 charDef1.right(); | ||||||
|                 charDef2.horizontal(); |                 charDef2.horizontal(); | ||||||
|                 charDef3.left(); |                 charDef3.left(); | ||||||
|  |             } else { | ||||||
|  |                 if (this.isSolution(currentTile)) { | ||||||
|  |                     charDef2.solution().vertical(); | ||||||
|  |                 } | ||||||
|             } |             } | ||||||
|             if (currentTile.hasWallAt(Direction.RIGHT)) { |             if (currentTile.hasWallAt(Direction.RIGHT)) { | ||||||
|                 charDef3.up(); |                 charDef3.up(); | ||||||
|             } |             } | ||||||
|             if (leftTile != null && leftTile.hasWallAt(Direction.BOTTOM)) { |             if (this.hasWallAt(leftTile, Direction.BOTTOM)) { | ||||||
|                 charDef1.left(); |                 charDef1.left(); | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|  | @ -188,6 +224,14 @@ public class TextRenderer implements Renderer<String> { | ||||||
|             return result; |             return result; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |         private boolean hasWallAt(@Nullable final Tile tile, @NonNull final Direction direction) { | ||||||
|  |             return tile != null && tile.hasWallAt(direction); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         private boolean isSolution(@Nullable final Tile tile) { | ||||||
|  |             return this.renderSolution && tile != null && tile.isSolution(); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|         @FieldDefaults(level = AccessLevel.PRIVATE) |         @FieldDefaults(level = AccessLevel.PRIVATE) | ||||||
|         @NoArgsConstructor |         @NoArgsConstructor | ||||||
|         @AllArgsConstructor |         @AllArgsConstructor | ||||||
|  | @ -222,10 +266,24 @@ public class TextRenderer implements Renderer<String> { | ||||||
|             static final String HORIZONTAL_UP = "\u2534"; |             static final String HORIZONTAL_UP = "\u2534"; | ||||||
|             // ┼ |             // ┼ | ||||||
|             static final String CROSS = "\u253c"; |             static final String CROSS = "\u253c"; | ||||||
|  |             // ╭ | ||||||
|  |             static final String SOLUTION_DOWN_RIGHT = "\u256d"; | ||||||
|  |             // ╮ | ||||||
|  |             static final String SOLUTION_DOWN_LEFT = "\u256e"; | ||||||
|  |             // ╯ | ||||||
|  |             static final String SOLUTION_UP_LEFT = "\u256f"; | ||||||
|  |             // ╰ | ||||||
|  |             static final String SOLUTION_UP_RIGHT = "\u2570"; | ||||||
|             boolean up = false; |             boolean up = false; | ||||||
|             boolean down = false; |             boolean down = false; | ||||||
|             boolean left = false; |             boolean left = false; | ||||||
|             boolean right = false; |             boolean right = false; | ||||||
|  |             boolean solution = false; | ||||||
|  | 
 | ||||||
|  |             CharDefinition solution() { | ||||||
|  |                 this.solution = true; | ||||||
|  |                 return this; | ||||||
|  |             } | ||||||
| 
 | 
 | ||||||
|             CharDefinition up() { |             CharDefinition up() { | ||||||
|                 this.up = true; |                 this.up = true; | ||||||
|  | @ -276,11 +334,11 @@ public class TextRenderer implements Renderer<String> { | ||||||
|                             if (this.right) { |                             if (this.right) { | ||||||
|                                 return HORIZONTAL_UP; |                                 return HORIZONTAL_UP; | ||||||
|                             } else { |                             } else { | ||||||
|                                 return UP_LEFT; |                                 return this.solution ? SOLUTION_UP_LEFT : UP_LEFT; | ||||||
|                             } |                             } | ||||||
|                         } else { |                         } else { | ||||||
|                             if (this.right) { |                             if (this.right) { | ||||||
|                                 return UP_RIGHT; |                                 return this.solution ? SOLUTION_UP_RIGHT : UP_RIGHT; | ||||||
|                             } else { |                             } else { | ||||||
|                                 return UP; |                                 return UP; | ||||||
|                             } |                             } | ||||||
|  | @ -292,11 +350,11 @@ public class TextRenderer implements Renderer<String> { | ||||||
|                             if (this.right) { |                             if (this.right) { | ||||||
|                                 return HORIZONTAL_DOWN; |                                 return HORIZONTAL_DOWN; | ||||||
|                             } else { |                             } else { | ||||||
|                                 return DOWN_LEFT; |                                 return this.solution ? SOLUTION_DOWN_LEFT : DOWN_LEFT; | ||||||
|                             } |                             } | ||||||
|                         } else { |                         } else { | ||||||
|                             if (this.right) { |                             if (this.right) { | ||||||
|                                 return DOWN_RIGHT; |                                 return this.solution ? SOLUTION_DOWN_RIGHT : DOWN_RIGHT; | ||||||
|                             } else { |                             } else { | ||||||
|                                 return DOWN; |                                 return DOWN; | ||||||
|                             } |                             } | ||||||
|  |  | ||||||
|  | @ -10,23 +10,43 @@ class TextRendererTest { | ||||||
|     @Nested |     @Nested | ||||||
|     class CharDefinitionTest { |     class CharDefinitionTest { | ||||||
|         @Test |         @Test | ||||||
|         void testRendering() { |         void testRenderingWall() { | ||||||
|             assertEquals(" ", new CharDefinition(false, false, false, false).toString()); |             assertEquals(" ", new CharDefinition(false, false, false, false, false).toString()); | ||||||
|             assertEquals("╶", new CharDefinition(false, false, false, true).toString()); |             assertEquals("╶", new CharDefinition(false, false, false, true, false).toString()); | ||||||
|             assertEquals("╴", new CharDefinition(false, false, true, false).toString()); |             assertEquals("╴", new CharDefinition(false, false, true, false, false).toString()); | ||||||
|             assertEquals("─", new CharDefinition(false, false, true, true).toString()); |             assertEquals("─", new CharDefinition(false, false, true, true, false).toString()); | ||||||
|             assertEquals("╷", new CharDefinition(false, true, false, false).toString()); |             assertEquals("╷", new CharDefinition(false, true, false, false, false).toString()); | ||||||
|             assertEquals("┌", new CharDefinition(false, true, false, true).toString()); |             assertEquals("┌", new CharDefinition(false, true, false, true, false).toString()); | ||||||
|             assertEquals("┐", new CharDefinition(false, true, true, false).toString()); |             assertEquals("┐", new CharDefinition(false, true, true, false, false).toString()); | ||||||
|             assertEquals("┬", new CharDefinition(false, true, true, true).toString()); |             assertEquals("┬", new CharDefinition(false, true, true, true, false).toString()); | ||||||
|             assertEquals("╵", new CharDefinition(true, false, false, false).toString()); |             assertEquals("╵", new CharDefinition(true, false, false, false, false).toString()); | ||||||
|             assertEquals("└", new CharDefinition(true, false, false, true).toString()); |             assertEquals("└", new CharDefinition(true, false, false, true, false).toString()); | ||||||
|             assertEquals("┘", new CharDefinition(true, false, true, false).toString()); |             assertEquals("┘", new CharDefinition(true, false, true, false, false).toString()); | ||||||
|             assertEquals("┴", new CharDefinition(true, false, true, true).toString()); |             assertEquals("┴", new CharDefinition(true, false, true, true, false).toString()); | ||||||
|             assertEquals("│", new CharDefinition(true, true, false, false).toString()); |             assertEquals("│", new CharDefinition(true, true, false, false, false).toString()); | ||||||
|             assertEquals("├", new CharDefinition(true, true, false, true).toString()); |             assertEquals("├", new CharDefinition(true, true, false, true, false).toString()); | ||||||
|             assertEquals("┤", new CharDefinition(true, true, true, false).toString()); |             assertEquals("┤", new CharDefinition(true, true, true, false, false).toString()); | ||||||
|             assertEquals("┼", new CharDefinition(true, true, true, true).toString()); |             assertEquals("┼", new CharDefinition(true, true, true, true, false).toString()); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         @Test | ||||||
|  |         void testRenderingSolution() { | ||||||
|  |             assertEquals(" ", new CharDefinition(false, false, false, false, true).toString()); | ||||||
|  |             assertEquals("╶", new CharDefinition(false, false, false, true, true).toString()); | ||||||
|  |             assertEquals("╴", new CharDefinition(false, false, true, false, true).toString()); | ||||||
|  |             assertEquals("─", new CharDefinition(false, false, true, true, true).toString()); | ||||||
|  |             assertEquals("╷", new CharDefinition(false, true, false, false, true).toString()); | ||||||
|  |             assertEquals("╭", new CharDefinition(false, true, false, true, true).toString()); | ||||||
|  |             assertEquals("╮", new CharDefinition(false, true, true, false, true).toString()); | ||||||
|  |             assertEquals("┬", new CharDefinition(false, true, true, true, true).toString()); | ||||||
|  |             assertEquals("╵", new CharDefinition(true, false, false, false, true).toString()); | ||||||
|  |             assertEquals("╰", new CharDefinition(true, false, false, true, true).toString()); | ||||||
|  |             assertEquals("╯", new CharDefinition(true, false, true, false, true).toString()); | ||||||
|  |             assertEquals("┴", new CharDefinition(true, false, true, true, true).toString()); | ||||||
|  |             assertEquals("│", new CharDefinition(true, true, false, false, true).toString()); | ||||||
|  |             assertEquals("├", new CharDefinition(true, true, false, true, true).toString()); | ||||||
|  |             assertEquals("┤", new CharDefinition(true, true, true, false, true).toString()); | ||||||
|  |             assertEquals("┼", new CharDefinition(true, true, true, true, true).toString()); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue