Introduce the Renderer interface.
This commit is contained in:
parent
c6b8b0e3bd
commit
487ed4604d
4 changed files with 61 additions and 37 deletions
|
@ -4,10 +4,11 @@ import io.vavr.collection.HashSet;
|
|||
import io.vavr.collection.Set;
|
||||
import lombok.NonNull;
|
||||
|
||||
public class HTMLRenderer {
|
||||
public class HTMLRenderer implements Renderer<String> {
|
||||
private static final String PREAMBLE = "<!DOCTYPE html><html lang=\"en\">" +
|
||||
"<head>" +
|
||||
"<title>Labyrinth</title>" +
|
||||
"<meta charset=\"utf-8\">" +
|
||||
"<style>" +
|
||||
"table{border-collapse:collapse;}" +
|
||||
"td{border:0 solid black;height:1em;width:1em;}" +
|
||||
|
@ -32,30 +33,32 @@ public class HTMLRenderer {
|
|||
"<body>" +
|
||||
"<input type=\"checkbox\" onclick=\"toggleSolution()\">show solution</input>";
|
||||
private static final String POSTAMBLE = "</body></html>";
|
||||
private final Labyrinth labyrinth;
|
||||
private final int width;
|
||||
private final int height;
|
||||
private Labyrinth labyrinth;
|
||||
private int width;
|
||||
private 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;
|
||||
private HTMLRenderer() {
|
||||
}
|
||||
|
||||
public static String render(@NonNull final Labyrinth labyrinth) {
|
||||
final int width = labyrinth.getWidth();
|
||||
final int height = labyrinth.getHeight();
|
||||
if (width == 0 || height == 0) {
|
||||
@NonNull
|
||||
public static HTMLRenderer newInstance() {
|
||||
return new HTMLRenderer();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public String render(@NonNull final Labyrinth labyrinth) {
|
||||
this.labyrinth = labyrinth;
|
||||
this.width = labyrinth.getWidth();
|
||||
this.height = labyrinth.getHeight();
|
||||
if (this.width == 0 || this.height == 0) {
|
||||
return PREAMBLE + POSTAMBLE;
|
||||
}
|
||||
final HTMLRenderer renderer = new HTMLRenderer(labyrinth, width, height);
|
||||
|
||||
final StringBuilder sb = new StringBuilder(PREAMBLE);
|
||||
sb.append("<table>");
|
||||
while (renderer.hasNext()) {
|
||||
sb.append(renderer.next());
|
||||
while (this.hasNext()) {
|
||||
sb.append(this.next());
|
||||
}
|
||||
sb.append("</table>");
|
||||
sb.append(POSTAMBLE);
|
||||
|
|
|
@ -4,10 +4,13 @@ import lombok.NonNull;
|
|||
|
||||
public class Main {
|
||||
public static void main(@NonNull final String[] args) {
|
||||
int width = 100;
|
||||
int height = 50;
|
||||
int width = 10;
|
||||
int height = 10;
|
||||
final Labyrinth labyrinth = new Labyrinth(width, height);
|
||||
System.out.println(TextRenderer.render(labyrinth));
|
||||
System.out.println(HTMLRenderer.render(labyrinth));
|
||||
final TextRenderer textRenderer = TextRenderer.newInstance();
|
||||
System.out.println(textRenderer.render(labyrinth));
|
||||
System.out.println(textRenderer.setRenderingSolution(true).render(labyrinth));
|
||||
final HTMLRenderer htmlRenderer = HTMLRenderer.newInstance();
|
||||
System.out.println(htmlRenderer.render(labyrinth));
|
||||
}
|
||||
}
|
||||
|
|
8
src/main/java/ch/fritteli/labyrinth/Renderer.java
Normal file
8
src/main/java/ch/fritteli/labyrinth/Renderer.java
Normal file
|
@ -0,0 +1,8 @@
|
|||
package ch.fritteli.labyrinth;
|
||||
|
||||
import lombok.NonNull;
|
||||
|
||||
public interface Renderer<T> {
|
||||
@NonNull
|
||||
T render(@NonNull final Labyrinth labyrinth);
|
||||
}
|
|
@ -7,10 +7,11 @@ import lombok.NonNull;
|
|||
import lombok.experimental.FieldDefaults;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class TextRenderer {
|
||||
private final Labyrinth labyrinth;
|
||||
private final int width;
|
||||
private final int height;
|
||||
public class TextRenderer implements Renderer<String> {
|
||||
private Labyrinth labyrinth;
|
||||
private int width;
|
||||
private int height;
|
||||
private boolean renderSolution;
|
||||
// column counter
|
||||
private int x = 0;
|
||||
// row counter
|
||||
|
@ -18,23 +19,32 @@ public class TextRenderer {
|
|||
// line counter (top-, center- or bottom line of a row)
|
||||
private int line = 0;
|
||||
|
||||
private TextRenderer(@NonNull final Labyrinth labyrinth, final int width, final int height) {
|
||||
this.labyrinth = labyrinth;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
private TextRenderer() {
|
||||
this.renderSolution = false;
|
||||
}
|
||||
|
||||
public static String render(@NonNull final Labyrinth labyrinth) {
|
||||
final int width = labyrinth.getWidth();
|
||||
final int height = labyrinth.getHeight();
|
||||
if (width == 0 || height == 0) {
|
||||
@NonNull
|
||||
public static TextRenderer newInstance() {
|
||||
return new TextRenderer();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public TextRenderer setRenderingSolution(final boolean renderSolution) {
|
||||
this.renderSolution = renderSolution;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public String render(@NonNull final Labyrinth labyrinth) {
|
||||
this.labyrinth = labyrinth;
|
||||
this.width = labyrinth.getWidth();
|
||||
this.height = labyrinth.getHeight();
|
||||
if (this.width == 0 || this.height == 0) {
|
||||
return "";
|
||||
}
|
||||
final TextRenderer renderer = new TextRenderer(labyrinth, width, height);
|
||||
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
while (renderer.hasNext()) {
|
||||
sb.append(renderer.next());
|
||||
while (this.hasNext()) {
|
||||
sb.append(this.next());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
@ -141,7 +151,7 @@ public class TextRenderer {
|
|||
charDef2.vertical();
|
||||
}
|
||||
|
||||
result = charDef1 + (currentTile.isSolution() ? "x" : " ");
|
||||
result = charDef1 + (currentTile.isSolution() && this.renderSolution ? "x" : " ");
|
||||
|
||||
if (this.x == this.width - 1) {
|
||||
result += charDef2 + "\n";
|
||||
|
|
Loading…
Reference in a new issue