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