Add very simple text renderer.

This commit is contained in:
Manuel Friedli 2020-09-30 01:27:12 +02:00
parent f31821f100
commit 483e00d964
3 changed files with 116 additions and 103 deletions

View file

@ -7,7 +7,7 @@ import java.util.Deque;
import java.util.LinkedList; import java.util.LinkedList;
public class Labyrinth { public class Labyrinth {
private final Tile[][] field; final Tile[][] field;
private final int width; private final int width;
private final int height; private final int height;
@ -16,110 +16,10 @@ public class Labyrinth {
this.height = height; this.height = height;
this.field = new Tile[width][height]; this.field = new Tile[width][height];
this.initField(); this.initField();
System.out.println(this);
this.generate(); 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 Tile getTileAt(@NonNull final Position position) {
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) {
return this.field[position.getX()][position.getY()]; return this.field[position.getX()][position.getY()];
} }

View file

@ -7,6 +7,6 @@ public class Main {
int width = 5; int width = 5;
int height = 8; int height = 8;
final Labyrinth labyrinth = new Labyrinth(width, height); final Labyrinth labyrinth = new Labyrinth(width, height);
System.out.println(labyrinth); System.out.println(SimpleTextRenderer.render(labyrinth));
} }
} }

View file

@ -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();
}
}