Add very simple text renderer.
This commit is contained in:
		
							parent
							
								
									f31821f100
								
							
						
					
					
						commit
						483e00d964
					
				
					 3 changed files with 116 additions and 103 deletions
				
			
		|  | @ -7,7 +7,7 @@ import java.util.Deque; | |||
| import java.util.LinkedList; | ||||
| 
 | ||||
| public class Labyrinth { | ||||
|     private final Tile[][] field; | ||||
|     final Tile[][] field; | ||||
|     private final int width; | ||||
|     private final int height; | ||||
| 
 | ||||
|  | @ -16,110 +16,10 @@ public class Labyrinth { | |||
|         this.height = height; | ||||
|         this.field = new Tile[width][height]; | ||||
|         this.initField(); | ||||
|         System.out.println(this); | ||||
|         this.generate(); | ||||
|     } | ||||
|     private static final char TOP_LEFT = '\u250c'; | ||||
|     private static final char TOP_RIGHT = '\u2510'; | ||||
|     private static final char MIDDLE_LEFT = '\u251c'; | ||||
|     private static final char MIDDLE_RIGHT = '\u2524'; | ||||
|     private static final char BOTTOM_LEFT = '\u2514'; | ||||
|     private static final char BOTTOM_RIGHT = '\u2518'; | ||||
|     private static final char HORIZONTAL = '\u2500'; | ||||
|     private static final char VERTICAL = '\u2502'; | ||||
| 
 | ||||
|     @Override | ||||
|     public String toString() { | ||||
|         StringBuilder sb = new StringBuilder(); | ||||
|         for (int y = 0; y < this.height; y++) { | ||||
|             // TOP WALL | ||||
|             for (int x = 0; x < this.width; x++) { | ||||
|                 final Tile tile = this.getTileAt(new Position(x, y)); | ||||
|                 final boolean top = tile.getWalls().isSet(Direction.TOP); | ||||
|                 final boolean topHard = tile.getWalls().getHardened().contains(Direction.TOP); | ||||
|                 sb.append(TOP_LEFT); | ||||
|                 if (topHard) { | ||||
|                     if (top) { | ||||
|                         sb.append(HORIZONTAL); | ||||
|                     } else { | ||||
|                         sb.append("?"); | ||||
|                     } | ||||
|                 } else { | ||||
|                     if (top) { | ||||
|                         sb.append("-"); | ||||
|                     } else { | ||||
|                         sb.append(" "); | ||||
|                     } | ||||
|                 } | ||||
|                 sb.append(TOP_RIGHT); | ||||
|             } | ||||
|             sb.append("\n"); | ||||
|             // LEFT WALL, CENTER, RIGHT WALL | ||||
|             for (int x = 0; x < this.width; x++) { | ||||
|                 final Tile tile = this.getTileAt(new Position(x, y)); | ||||
|                 // left | ||||
|                 final boolean left = tile.getWalls().isSet(Direction.LEFT); | ||||
|                 final boolean leftHard = tile.getWalls().getHardened().contains(Direction.LEFT); | ||||
|                 if (leftHard) { | ||||
|                     if (left) { | ||||
|                         sb.append(VERTICAL); | ||||
|                     } else { | ||||
|                         sb.append("?"); | ||||
|                     } | ||||
|                 } else { | ||||
|                     if (left) { | ||||
|                         sb.append(":"); | ||||
|                     } else { | ||||
|                         sb.append(" "); | ||||
|                     } | ||||
|                 } | ||||
|                 // center | ||||
|                 sb.append(" "); | ||||
|                 // right | ||||
|                 final boolean right = tile.getWalls().isSet(Direction.RIGHT); | ||||
|                 final boolean rightHard = tile.getWalls().getHardened().contains(Direction.RIGHT); | ||||
|                 if (rightHard) { | ||||
|                     if (right) { | ||||
|                         sb.append(VERTICAL); | ||||
|                     } else { | ||||
|                         sb.append("?"); | ||||
|                     } | ||||
|                 } else { | ||||
|                     if (right) { | ||||
|                         sb.append(":"); | ||||
|                     } else { | ||||
|                         sb.append(" "); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|             sb.append("\n"); | ||||
|             // BOTTOM WALL | ||||
|             for (int x = 0; x < this.width; x++) { | ||||
|                 final Tile tile = this.getTileAt(new Position(x, y)); | ||||
|                 final boolean bottom = tile.getWalls().isSet(Direction.BOTTOM); | ||||
|                 final boolean bottomHard = tile.getWalls().getHardened().contains(Direction.BOTTOM); | ||||
|                 sb.append(BOTTOM_LEFT); | ||||
|                 if (bottomHard) { | ||||
|                     if (bottom) { | ||||
|                         sb.append(HORIZONTAL); | ||||
|                     } else { | ||||
|                         sb.append("?"); | ||||
|                     } | ||||
|                 } else { | ||||
|                     if (bottom) { | ||||
|                         sb.append("-"); | ||||
|                     } else { | ||||
|                         sb.append(" "); | ||||
|                     } | ||||
|                 } | ||||
|                 sb.append(BOTTOM_RIGHT); | ||||
|             } | ||||
|             sb.append("\n"); | ||||
|         } | ||||
|         return sb.toString(); | ||||
|     } | ||||
| 
 | ||||
|     private Tile getTileAt(@NonNull final Position position) { | ||||
|     Tile getTileAt(@NonNull final Position position) { | ||||
|         return this.field[position.getX()][position.getY()]; | ||||
|     } | ||||
| 
 | ||||
|  |  | |||
|  | @ -7,6 +7,6 @@ public class Main { | |||
|         int width = 5; | ||||
|         int height = 8; | ||||
|         final Labyrinth labyrinth = new Labyrinth(width, height); | ||||
|         System.out.println(labyrinth); | ||||
|         System.out.println(SimpleTextRenderer.render(labyrinth)); | ||||
|     } | ||||
| } | ||||
|  |  | |||
							
								
								
									
										113
									
								
								src/main/java/labyrinth/SimpleTextRenderer.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										113
									
								
								src/main/java/labyrinth/SimpleTextRenderer.java
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,113 @@ | |||
| package labyrinth; | ||||
| 
 | ||||
| import lombok.NonNull; | ||||
| import lombok.experimental.UtilityClass; | ||||
| 
 | ||||
| @UtilityClass | ||||
| public class SimpleTextRenderer { | ||||
|     private static final char TOP_LEFT = '\u250c'; | ||||
|     private static final char TOP_RIGHT = '\u2510'; | ||||
|     private static final char MIDDLE_LEFT = '\u251c'; | ||||
|     private static final char MIDDLE_RIGHT = '\u2524'; | ||||
|     private static final char BOTTOM_LEFT = '\u2514'; | ||||
|     private static final char BOTTOM_RIGHT = '\u2518'; | ||||
|     private static final char HORIZONTAL = '\u2500'; | ||||
|     private static final char VERTICAL = '\u2502'; | ||||
| 
 | ||||
|     public String render(@NonNull final Labyrinth labyrinth) { | ||||
|         final int width; | ||||
|         final int height; | ||||
|         width = labyrinth.field.length; | ||||
|         if (width == 0) { | ||||
|             return ""; | ||||
|         } | ||||
|         height = labyrinth.field[0].length; | ||||
|         StringBuilder sb = new StringBuilder(); | ||||
|         for (int y = 0; y < height; y++) { | ||||
|             // TOP WALL | ||||
|             for (int x = 0; x < width; x++) { | ||||
|                 final Tile tile = labyrinth.getTileAt(new Position(x, y)); | ||||
|                 final boolean top = tile.getWalls().isSet(Direction.TOP); | ||||
|                 final boolean topHard = tile.getWalls().getHardened().contains(Direction.TOP); | ||||
|                 sb.append(TOP_LEFT); | ||||
|                 if (topHard) { | ||||
|                     if (top) { | ||||
|                         sb.append(HORIZONTAL); | ||||
|                     } else { | ||||
|                         sb.append("?"); | ||||
|                     } | ||||
|                 } else { | ||||
|                     if (top) { | ||||
|                         sb.append("-"); | ||||
|                     } else { | ||||
|                         sb.append(" "); | ||||
|                     } | ||||
|                 } | ||||
|                 sb.append(TOP_RIGHT); | ||||
|             } | ||||
|             sb.append("\n"); | ||||
|             // LEFT WALL, CENTER, RIGHT WALL | ||||
|             for (int x = 0; x < width; x++) { | ||||
|                 final Tile tile = labyrinth.getTileAt(new Position(x, y)); | ||||
|                 // left | ||||
|                 final boolean left = tile.getWalls().isSet(Direction.LEFT); | ||||
|                 final boolean leftHard = tile.getWalls().getHardened().contains(Direction.LEFT); | ||||
|                 if (leftHard) { | ||||
|                     if (left) { | ||||
|                         sb.append(VERTICAL); | ||||
|                     } else { | ||||
|                         sb.append("?"); | ||||
|                     } | ||||
|                 } else { | ||||
|                     if (left) { | ||||
|                         sb.append(":"); | ||||
|                     } else { | ||||
|                         sb.append(" "); | ||||
|                     } | ||||
|                 } | ||||
|                 // center | ||||
|                 sb.append(" "); | ||||
|                 // right | ||||
|                 final boolean right = tile.getWalls().isSet(Direction.RIGHT); | ||||
|                 final boolean rightHard = tile.getWalls().getHardened().contains(Direction.RIGHT); | ||||
|                 if (rightHard) { | ||||
|                     if (right) { | ||||
|                         sb.append(VERTICAL); | ||||
|                     } else { | ||||
|                         sb.append("?"); | ||||
|                     } | ||||
|                 } else { | ||||
|                     if (right) { | ||||
|                         sb.append(":"); | ||||
|                     } else { | ||||
|                         sb.append(" "); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|             sb.append("\n"); | ||||
|             // BOTTOM WALL | ||||
|             for (int x = 0; x < width; x++) { | ||||
|                 final Tile tile = labyrinth.getTileAt(new Position(x, y)); | ||||
|                 final boolean bottom = tile.getWalls().isSet(Direction.BOTTOM); | ||||
|                 final boolean bottomHard = tile.getWalls().getHardened().contains(Direction.BOTTOM); | ||||
|                 sb.append(BOTTOM_LEFT); | ||||
|                 if (bottomHard) { | ||||
|                     if (bottom) { | ||||
|                         sb.append(HORIZONTAL); | ||||
|                     } else { | ||||
|                         sb.append("?"); | ||||
|                     } | ||||
|                 } else { | ||||
|                     if (bottom) { | ||||
|                         sb.append("-"); | ||||
|                     } else { | ||||
|                         sb.append(" "); | ||||
|                     } | ||||
|                 } | ||||
|                 sb.append(BOTTOM_RIGHT); | ||||
|             } | ||||
|             sb.append("\n"); | ||||
|         } | ||||
|         return sb.toString(); | ||||
|     } | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue