From 0a655bd6173d426d9d3114ec3ccf47051e88fc69 Mon Sep 17 00:00:00 2001 From: Manuel Friedli Date: Thu, 1 Oct 2020 00:48:54 +0200 Subject: [PATCH] Add HTMLRenderer --- .../ch/fritteli/labyrinth/HTMLRenderer.java | 65 +++++++++++++++++++ src/main/java/ch/fritteli/labyrinth/Main.java | 5 +- 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 src/main/java/ch/fritteli/labyrinth/HTMLRenderer.java diff --git a/src/main/java/ch/fritteli/labyrinth/HTMLRenderer.java b/src/main/java/ch/fritteli/labyrinth/HTMLRenderer.java new file mode 100644 index 0000000..e8d626a --- /dev/null +++ b/src/main/java/ch/fritteli/labyrinth/HTMLRenderer.java @@ -0,0 +1,65 @@ +package ch.fritteli.labyrinth; + +import lombok.NonNull; + +public class HTMLRenderer { + private static final String PREAMBLE = "Labyrinth"; + private static final String POSTAMBLE = ""; + private final Labyrinth labyrinth; + private final int width; + private final int height; + // row counter + private int y = 0; + + private HTMLRenderer(@NonNull final Labyrinth labyrinth, final int width, final int height) { + this.labyrinth = labyrinth; + this.width = width; + this.height = height; + } + + public static String render(@NonNull final Labyrinth labyrinth) { + final int width = labyrinth.getWidth(); + final int height = labyrinth.getHeight(); + if (width == 0 || height == 0) { + return PREAMBLE + POSTAMBLE; + } + final HTMLRenderer renderer = new HTMLRenderer(labyrinth, width, height); + + final StringBuilder sb = new StringBuilder(PREAMBLE); + sb.append(""); + while (renderer.hasNext()) { + sb.append(renderer.next()); + } + sb.append("
"); + sb.append(POSTAMBLE); + return sb.toString(); + } + + private boolean hasNext() { + return this.y < this.height; + } + + private String next() { + StringBuilder sb = new StringBuilder(""); + for (int x = 0; x < this.width; x++) { + final Tile currentTile = this.labyrinth.getTileAt(x, this.y); + sb.append(" "); + } + sb.append(""); + this.y++; + return sb.toString(); + } +} diff --git a/src/main/java/ch/fritteli/labyrinth/Main.java b/src/main/java/ch/fritteli/labyrinth/Main.java index d668ea1..29a8942 100644 --- a/src/main/java/ch/fritteli/labyrinth/Main.java +++ b/src/main/java/ch/fritteli/labyrinth/Main.java @@ -4,9 +4,10 @@ import lombok.NonNull; public class Main { public static void main(@NonNull final String[] args) { - int width = 110; - int height = 15; + int width = 100; + int height = 50; final Labyrinth labyrinth = new Labyrinth(width, height); System.out.println(TextRenderer.render(labyrinth)); + System.out.println(HTMLRenderer.render(labyrinth)); } }