Add HTMLRenderer
This commit is contained in:
parent
e10621c08c
commit
0a655bd617
2 changed files with 68 additions and 2 deletions
65
src/main/java/ch/fritteli/labyrinth/HTMLRenderer.java
Normal file
65
src/main/java/ch/fritteli/labyrinth/HTMLRenderer.java
Normal file
|
@ -0,0 +1,65 @@
|
|||
package ch.fritteli.labyrinth;
|
||||
|
||||
import lombok.NonNull;
|
||||
|
||||
public class HTMLRenderer {
|
||||
private static final String PREAMBLE = "<!DOCTYPE html><html><head><title>Labyrinth</title><style>table{border-collapse:collapse;}td{border:0 solid black;height:1em;width:1em;}td.top{border-top-width:1px;}td.right{border-right-width:1px;}td.bottom{border-bottom-width:1px;}td.left{border-left-width:1px;}</style></head><body>";
|
||||
private static final String POSTAMBLE = "</body></html>";
|
||||
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("<table>");
|
||||
while (renderer.hasNext()) {
|
||||
sb.append(renderer.next());
|
||||
}
|
||||
sb.append("</table>");
|
||||
sb.append(POSTAMBLE);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private boolean hasNext() {
|
||||
return this.y < this.height;
|
||||
}
|
||||
|
||||
private String next() {
|
||||
StringBuilder sb = new StringBuilder("<tr>");
|
||||
for (int x = 0; x < this.width; x++) {
|
||||
final Tile currentTile = this.labyrinth.getTileAt(x, this.y);
|
||||
sb.append("<td class=\"");
|
||||
if (currentTile.hasWallAt(Direction.TOP)) {
|
||||
sb.append("top ");
|
||||
}
|
||||
if (currentTile.hasWallAt(Direction.RIGHT)) {
|
||||
sb.append("right ");
|
||||
}
|
||||
if (currentTile.hasWallAt(Direction.BOTTOM)) {
|
||||
sb.append("bottom ");
|
||||
}
|
||||
if (currentTile.hasWallAt(Direction.LEFT)) {
|
||||
sb.append("left ");
|
||||
}
|
||||
sb.append("\"> </td>");
|
||||
}
|
||||
sb.append("</tr>");
|
||||
this.y++;
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue