From 483e00d9640185137ff0df0635d6b40b683e9c02 Mon Sep 17 00:00:00 2001 From: Manuel Friedli Date: Wed, 30 Sep 2020 01:27:12 +0200 Subject: [PATCH] Add very simple text renderer. --- src/main/java/labyrinth/Labyrinth.java | 104 +--------------- src/main/java/labyrinth/Main.java | 2 +- .../java/labyrinth/SimpleTextRenderer.java | 113 ++++++++++++++++++ 3 files changed, 116 insertions(+), 103 deletions(-) create mode 100644 src/main/java/labyrinth/SimpleTextRenderer.java diff --git a/src/main/java/labyrinth/Labyrinth.java b/src/main/java/labyrinth/Labyrinth.java index bb23734..c2599e5 100644 --- a/src/main/java/labyrinth/Labyrinth.java +++ b/src/main/java/labyrinth/Labyrinth.java @@ -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()]; } diff --git a/src/main/java/labyrinth/Main.java b/src/main/java/labyrinth/Main.java index c478990..26f0971 100644 --- a/src/main/java/labyrinth/Main.java +++ b/src/main/java/labyrinth/Main.java @@ -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)); } } diff --git a/src/main/java/labyrinth/SimpleTextRenderer.java b/src/main/java/labyrinth/SimpleTextRenderer.java new file mode 100644 index 0000000..e80bc69 --- /dev/null +++ b/src/main/java/labyrinth/SimpleTextRenderer.java @@ -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(); + } +}