Rename all "labyrinth" to "maze" and configure the Sonatype maven repo.

This commit is contained in:
Manuel Friedli 2023-05-16 02:15:02 +02:00
parent cb1c016cc7
commit fcb87e13a5
Signed by: manuel
GPG key ID: 41D08ABA75634DA1
42 changed files with 510 additions and 483 deletions

View file

@ -1,2 +1 @@
# labyrinth-generator
# maze-generator

8
maze-generator.iml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="AdditionalModuleElements">
<content url="file://$MODULE_DIR$" dumb="true">
<sourceFolder url="file://$MODULE_DIR$/target/generated-sources/jsonschema2pojo" isTestSource="false" generated="true" />
</content>
</component>
</module>

177
pom.xml
View file

@ -1,31 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ch.fritteli</groupId>
<artifactId>fritteli-build-parent</artifactId>
<version>2.0.4</version>
<version>5.0.0</version>
</parent>
<groupId>ch.fritteli.labyrinth</groupId>
<artifactId>labyrinth-generator</artifactId>
<groupId>ch.fritteli.a-maze-r</groupId>
<artifactId>maze-generator</artifactId>
<version>0.0.9-SNAPSHOT</version>
<description>The Labyrinth generator, a library for generating Labyrinths in various output formats.</description>
<url>https://manuel.friedli.info/labyrinth.html</url>
<properties>
<jackson.version>2.14.2</jackson.version>
<java.source.version>17</java.source.version>
<java.target.version>17</java.target.version>
<jetbrains-annotations.version>24.0.1</jetbrains-annotations.version>
<junit-jupiter.version>5.9.2</junit-jupiter.version>
<logback.version>1.4.6</logback.version>
<lombok.version>1.18.26</lombok.version>
<pdfbox.version>2.0.27</pdfbox.version>
<slf4j.version>2.0.7</slf4j.version>
<vavr.version>0.10.4</vavr.version>
</properties>
<description>A-Maze-R, The Maze Generator. It is a library for generating mazes in various output formats.
</description>
<url>https://manuel.friedli.info/maze.html</url>
<inceptionYear>2022</inceptionYear>
<licenses>
<license>
<name>GNU Affero General Public License</name>
<url>https://www.gnu.org/licenses/agpl-3.0.html</url>
</license>
</licenses>
<developers>
<developer>
@ -42,6 +40,29 @@
</developer>
</developers>
<scm>
<connection>scm:git:https://gittr.ch/java/maze-generator.git</connection>
<developerConnection>scm:git:ssh://git@gittr.ch/java/maze-generator.git</developerConnection>
<url>https://gittr.ch/java/maze-generator</url>
<tag>HEAD</tag>
</scm>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<name>Sonatype Maven Repository - Snapshots</name>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<properties>
<jackson.version>2.15.0</jackson.version>
<jsonschema2pojo-maven-plugin.version>1.2.1</jsonschema2pojo-maven-plugin.version>
<maven-site-plugin.version>4.0.0-M8</maven-site-plugin.version>
<pdfbox.version>2.0.28</pdfbox.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
@ -91,12 +112,13 @@
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>1.2.1</version>
<version>${jsonschema2pojo-maven-plugin.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
@ -105,34 +127,12 @@
</goals>
<configuration>
<sourcePaths>
<sourcePath>src/main/resources/labyrinth.schema.json</sourcePath>
<sourcePath>src/main/resources/maze.schema.json</sourcePath>
</sourcePaths>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>ch.fritteli.labyrinth.generator.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
@ -149,52 +149,55 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>4.0.0-M6</version>
<version>${maven-site-plugin.version}</version>
</plugin>
</plugins>
</build>
<scm>
<connection>scm:git:https://gittr.ch/java/labyrinth-generator.git</connection>
<developerConnection>scm:git:ssh://git@gittr.ch/java/labyrinth-generator.git</developerConnection>
<url>https://gittr.ch/java/labyrinth-generator</url>
<tag>HEAD</tag>
</scm>
<distributionManagement>
<repository>
<id>repo.gittr.ch</id>
<name>gittr.ch</name>
<url>https://repo.gittr.ch/releases/</url>
</repository>
<snapshotRepository>
<id>repo.gittr.ch</id>
<name>gittr.ch</name>
<url>https://repo.gittr.ch/snapshots/</url>
</snapshotRepository>
</distributionManagement>
<repositories>
<repository>
<id>repo.gittr.ch.releases</id>
<url>https://repo.gittr.ch/releases/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
<repository>
<id>repo.gittr.ch.snapshots</id>
<url>https://repo.gittr.ch/snapshots/</url>
<releases>
<enabled>false</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View file

@ -1,9 +0,0 @@
package ch.fritteli.labyrinth.generator.renderer;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import lombok.NonNull;
public interface Renderer<T> {
@NonNull
T render(@NonNull final Labyrinth labyrinth);
}

View file

@ -1,13 +1,14 @@
package ch.fritteli.labyrinth.generator;
package ch.fritteli.maze.generator;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.renderer.html.HTMLRenderer;
import ch.fritteli.labyrinth.generator.renderer.htmlfile.HTMLFileRenderer;
import ch.fritteli.labyrinth.generator.renderer.json.JsonRenderer;
import ch.fritteli.labyrinth.generator.renderer.jsonfile.JsonFileRenderer;
import ch.fritteli.labyrinth.generator.renderer.pdffile.PDFFileRenderer;
import ch.fritteli.labyrinth.generator.renderer.text.TextRenderer;
import ch.fritteli.labyrinth.generator.renderer.textfile.TextFileRenderer;
import ch.fritteli.maze.generator.algorithm.RandomDepthFirst;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.renderer.html.HTMLRenderer;
import ch.fritteli.maze.generator.renderer.htmlfile.HTMLFileRenderer;
import ch.fritteli.maze.generator.renderer.json.JsonRenderer;
import ch.fritteli.maze.generator.renderer.jsonfile.JsonFileRenderer;
import ch.fritteli.maze.generator.renderer.pdffile.PDFFileRenderer;
import ch.fritteli.maze.generator.renderer.text.TextRenderer;
import ch.fritteli.maze.generator.renderer.textfile.TextFileRenderer;
import java.nio.file.Path;
import java.nio.file.Paths;
import lombok.NonNull;
@ -21,15 +22,15 @@ public class Main {
public static void main(@NonNull final String[] args) {
final int width = 20;
final int height = 30;
final Labyrinth labyrinth = new Labyrinth(width, height/*, 0*/);
new Generator(labyrinth).run();
final Maze maze = new Maze(width, height/*, 0*/);
new RandomDepthFirst(maze).run();
final TextRenderer textRenderer = TextRenderer.newInstance();
final HTMLRenderer htmlRenderer = HTMLRenderer.newInstance();
final JsonRenderer jsonRenderer = JsonRenderer.newInstance();
final Path userHome = Paths.get(System.getProperty("user.home"));
final String baseFilename = getBaseFilename(labyrinth);
final String baseFilename = getBaseFilename(maze);
final TextFileRenderer textFileRenderer = TextFileRenderer.newInstance()
.setTargetLabyrinthFile(userHome.resolve(baseFilename + ".txt"))
.setTargetMazeFile(userHome.resolve(baseFilename + ".txt"))
.setTargetSolutionFile(userHome.resolve(baseFilename + "-solution.txt"));
final HTMLFileRenderer htmlFileRenderer = HTMLFileRenderer.newInstance()
.setTargetFile(userHome.resolve(baseFilename + ".html"));
@ -38,26 +39,26 @@ public class Main {
final PDFFileRenderer pdfFileRenderer = PDFFileRenderer.newInstance()
.setTargetFile(userHome.resolve(baseFilename + ".pdf"));
log.info("Labyrinth-ID: {}", labyrinth.getRandomSeed());
// Render Labyrinth to stdout
log.info("Text rendering:\n{}", textRenderer.render(labyrinth));
// Render Labyrinth solution to stdout
log.info("Text rendering with solution:\n{}", textRenderer.setRenderSolution(true).render(labyrinth));
log.info("Maze-ID: {}", maze.getRandomSeed());
// Render Maze to stdout
log.info("Text rendering:\n{}", textRenderer.render(maze));
// Render Maze solution to stdout
log.info("Text rendering with solution:\n{}", textRenderer.setRenderSolution(true).render(maze));
// Render HTML to stdout
log.info("HTML rendering:\n{}", htmlRenderer.render(labyrinth));
log.info("HTML rendering:\n{}", htmlRenderer.render(maze));
// Render JSON to stdout
log.info("JSON rendering:\n{}", jsonRenderer.render(labyrinth));
// Render Labyrinth and solution to (separate) files
log.info("Text rendering to file:\n{}", textFileRenderer.render(labyrinth));
log.info("JSON rendering:\n{}", jsonRenderer.render(maze));
// Render Maze and solution to (separate) files
log.info("Text rendering to file:\n{}", textFileRenderer.render(maze));
// Render HTML to file
log.info("HTML rendering to file:\n{}", htmlFileRenderer.render(labyrinth));
log.info("HTML rendering to file:\n{}", htmlFileRenderer.render(maze));
// Render JSON to file
log.info("JSON rendering to file:\n{}", jsonFileRenderer.render(labyrinth));
log.info("JSON rendering to file:\n{}", jsonFileRenderer.render(maze));
// Render PDF to file
log.info("PDF rendering to file:\n{}", pdfFileRenderer.render(labyrinth));
log.info("PDF rendering to file:\n{}", pdfFileRenderer.render(maze));
}
private static String getBaseFilename(@NonNull final Labyrinth labyrinth) {
return "labyrinth-" + labyrinth.getWidth() + "x" + labyrinth.getHeight() + "-" + labyrinth.getRandomSeed();
private static String getBaseFilename(@NonNull final Maze maze) {
return "maze-" + maze.getWidth() + "x" + maze.getHeight() + "-" + maze.getRandomSeed();
}
}

View file

@ -1,27 +1,27 @@
package ch.fritteli.labyrinth.generator;
package ch.fritteli.maze.generator.algorithm;
import ch.fritteli.labyrinth.generator.model.Direction;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.model.Position;
import ch.fritteli.labyrinth.generator.model.Tile;
import ch.fritteli.maze.generator.model.Direction;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.model.Position;
import ch.fritteli.maze.generator.model.Tile;
import io.vavr.control.Option;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Random;
import lombok.NonNull;
public class Generator {
public class RandomDepthFirst {
@NonNull
private final Labyrinth labyrinth;
private final Maze maze;
@NonNull
private final Random random;
@NonNull
private final Deque<Position> positions = new LinkedList<>();
public Generator(@NonNull final Labyrinth labyrinth) {
this.labyrinth = labyrinth;
this.random = new Random(labyrinth.getRandomSeed());
public RandomDepthFirst(@NonNull final Maze maze) {
this.maze = maze;
this.random = new Random(maze.getRandomSeed());
}
public void run() {
@ -31,8 +31,8 @@ public class Generator {
}
private void preDig() {
final Position end = this.labyrinth.getEnd();
final Tile endTile = this.labyrinth.getEndTile();
final Position end = this.maze.getEnd();
final Tile endTile = this.maze.getEndTile();
if (end.getY() == 0) {
endTile.enableDiggingToOrFrom(Direction.TOP);
@ -40,10 +40,10 @@ public class Generator {
} else if (end.getX() == 0) {
endTile.enableDiggingToOrFrom(Direction.LEFT);
endTile.digFrom(Direction.LEFT);
} else if (end.getY() == this.labyrinth.getHeight() - 1) {
} else if (end.getY() == this.maze.getHeight() - 1) {
endTile.enableDiggingToOrFrom(Direction.BOTTOM);
endTile.digFrom(Direction.BOTTOM);
} else if (end.getX() == this.labyrinth.getWidth() - 1) {
} else if (end.getX() == this.maze.getWidth() - 1) {
endTile.enableDiggingToOrFrom(Direction.RIGHT);
endTile.digFrom(Direction.RIGHT);
}
@ -54,17 +54,17 @@ public class Generator {
private void dig() {
while (!this.positions.isEmpty()) {
final Position currentPosition = this.positions.peek();
final Tile currentTile = this.labyrinth.getTileAt(currentPosition).get();
final Tile currentTile = this.maze.getTileAt(currentPosition).get();
final Option<Direction> directionToDigTo = currentTile.getRandomAvailableDirection(this.random);
if (directionToDigTo.isDefined()) {
final Direction digTo = directionToDigTo.get();
final Direction digFrom = digTo.invert();
final Position neighborPosition = currentPosition.move(digTo);
final Tile neighborTile = this.labyrinth.getTileAt(neighborPosition).get();
final Tile neighborTile = this.maze.getTileAt(neighborPosition).get();
if (currentTile.digTo(digTo) && neighborTile.digFrom(digFrom)) {
// all ok!
this.positions.push(neighborPosition);
if (neighborPosition.equals(this.labyrinth.getStart())) {
if (neighborPosition.equals(this.maze.getStart())) {
this.markSolution();
}
} else {
@ -79,12 +79,12 @@ public class Generator {
}
private void markSolution() {
this.positions.forEach(position -> this.labyrinth.getTileAt(position).get().setSolution());
this.positions.forEach(position -> this.maze.getTileAt(position).get().setSolution());
}
private void postDig() {
final Position start = this.labyrinth.getStart();
final Tile startTile = this.labyrinth.getStartTile();
final Position start = this.maze.getStart();
final Tile startTile = this.maze.getStartTile();
if (start.getY() == 0) {
startTile.enableDiggingToOrFrom(Direction.TOP);
@ -92,10 +92,10 @@ public class Generator {
} else if (start.getX() == 0) {
startTile.enableDiggingToOrFrom(Direction.LEFT);
startTile.digTo(Direction.LEFT);
} else if (start.getY() == this.labyrinth.getHeight() - 1) {
} else if (start.getY() == this.maze.getHeight() - 1) {
startTile.enableDiggingToOrFrom(Direction.BOTTOM);
startTile.digTo(Direction.BOTTOM);
} else if (start.getX() == this.labyrinth.getWidth() - 1) {
} else if (start.getX() == this.maze.getWidth() - 1) {
startTile.enableDiggingToOrFrom(Direction.RIGHT);
startTile.digTo(Direction.RIGHT);
}

View file

@ -1,4 +1,4 @@
package ch.fritteli.labyrinth.generator.model;
package ch.fritteli.maze.generator.model;
public enum Direction {
TOP,

View file

@ -1,4 +1,4 @@
package ch.fritteli.labyrinth.generator.model;
package ch.fritteli.maze.generator.model;
import io.vavr.control.Option;
import lombok.EqualsAndHashCode;
@ -8,7 +8,7 @@ import lombok.ToString;
@EqualsAndHashCode
@ToString
public class Labyrinth {
public class Maze {
private final Tile[][] field;
@Getter
@ -22,19 +22,19 @@ public class Labyrinth {
@Getter
private final Position end;
public Labyrinth(final int width, final int height) {
public Maze(final int width, final int height) {
this(width, height, System.nanoTime());
}
public Labyrinth(final int width, final int height, @NonNull final Position start, @NonNull final Position end) {
public Maze(final int width, final int height, @NonNull final Position start, @NonNull final Position end) {
this(width, height, System.nanoTime(), start, end);
}
public Labyrinth(final int width, final int height, final long randomSeed) {
public Maze(final int width, final int height, final long randomSeed) {
this(width, height, randomSeed, new Position(0, 0), new Position(width - 1, height - 1));
}
public Labyrinth(final int width, final int height, final long randomSeed, @NonNull final Position start, @NonNull final Position end) {
public Maze(final int width, final int height, final long randomSeed, @NonNull final Position start, @NonNull final Position end) {
if (width <= 1 || height <= 1) {
throw new IllegalArgumentException("width and height must be >1");
}
@ -42,10 +42,10 @@ public class Labyrinth {
throw new IllegalArgumentException("'start' must not be equal to 'end'");
}
if (start.getX() != 0 && start.getX() != width - 1 && start.getY() != 0 && start.getY() != height - 1) {
throw new IllegalArgumentException("'start' must be at the edge of the labyrinth");
throw new IllegalArgumentException("'start' must be at the edge of the maze");
}
if (end.getX() != 0 && end.getX() != width - 1 && end.getY() != 0 && end.getY() != height - 1) {
throw new IllegalArgumentException("'end' must be at the edge of the labyrinth");
throw new IllegalArgumentException("'end' must be at the edge of the maze");
}
this.width = width;
this.height = height;
@ -59,7 +59,7 @@ public class Labyrinth {
/**
* INTERNAL API. Exists only for deserialization. Not to be called from user code.
*/
private Labyrinth(@NonNull final Tile[][] field, final int width, final int height, final long randomSeed) {
private Maze(@NonNull final Tile[][] field, final int width, final int height, final long randomSeed) {
this.field = field;
this.width = width;
this.height = height;
@ -71,7 +71,7 @@ public class Labyrinth {
/**
* INTERNAL API. Exists only for deserialization. Not to be called from user code.
*/
private Labyrinth(@NonNull final Tile[][] field, final int width, final int height, @NonNull final Position start, @NonNull final Position end, final long randomSeed) {
private Maze(@NonNull final Tile[][] field, final int width, final int height, @NonNull final Position start, @NonNull final Position end, final long randomSeed) {
this.field = field;
this.width = width;
this.height = height;

View file

@ -1,4 +1,4 @@
package ch.fritteli.labyrinth.generator.model;
package ch.fritteli.maze.generator.model;
import lombok.NonNull;
import lombok.Value;

View file

@ -1,4 +1,4 @@
package ch.fritteli.labyrinth.generator.model;
package ch.fritteli.maze.generator.model;
import io.vavr.collection.Stream;
import io.vavr.control.Option;

View file

@ -1,4 +1,4 @@
package ch.fritteli.labyrinth.generator.model;
package ch.fritteli.maze.generator.model;
import io.vavr.collection.Stream;
import lombok.EqualsAndHashCode;

View file

@ -0,0 +1,9 @@
package ch.fritteli.maze.generator.renderer;
import ch.fritteli.maze.generator.model.Maze;
import lombok.NonNull;
public interface Renderer<T> {
@NonNull
T render(@NonNull final Maze maze);
}

View file

@ -1,8 +1,8 @@
package ch.fritteli.labyrinth.generator.renderer.html;
package ch.fritteli.maze.generator.renderer.html;
import ch.fritteli.labyrinth.generator.model.Direction;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.model.Tile;
import ch.fritteli.maze.generator.model.Direction;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.model.Tile;
import io.vavr.collection.HashSet;
import io.vavr.collection.Set;
import lombok.AccessLevel;
@ -11,17 +11,17 @@ import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
class Generator {
private final Labyrinth labyrinth;
private final Maze maze;
private int y = 0;
boolean hasNext() {
return this.y < this.labyrinth.getHeight();
return this.y < this.maze.getHeight();
}
String next() {
StringBuilder sb = new StringBuilder("<tr>");
for (int x = 0; x < this.labyrinth.getWidth(); x++) {
final Tile currentTile = this.labyrinth.getTileAt(x, this.y).get();
for (int x = 0; x < this.maze.getWidth(); x++) {
final Tile currentTile = this.maze.getTileAt(x, this.y).get();
sb.append("<td class=\"");
sb.append(this.getClasses(currentTile).mkString(" "));
sb.append("\">&nbsp;</td>");

View file

@ -1,7 +1,7 @@
package ch.fritteli.labyrinth.generator.renderer.html;
package ch.fritteli.maze.generator.renderer.html;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.renderer.Renderer;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.renderer.Renderer;
import lombok.NonNull;
public class HTMLRenderer implements Renderer<String> {
@ -119,12 +119,12 @@ public class HTMLRenderer implements Renderer<String> {
@NonNull
@Override
public String render(@NonNull final Labyrinth labyrinth) {
if (labyrinth.getWidth() == 0 || labyrinth.getHeight() == 0) {
return this.getPreamble(labyrinth) + POSTAMBLE;
public String render(@NonNull final Maze maze) {
if (maze.getWidth() == 0 || maze.getHeight() == 0) {
return this.getPreamble(maze) + POSTAMBLE;
}
final Generator generator = new Generator(labyrinth);
final StringBuilder sb = new StringBuilder(this.getPreamble(labyrinth));
final Generator generator = new Generator(maze);
final StringBuilder sb = new StringBuilder(this.getPreamble(maze));
sb.append("<table id=\"labyrinth\">");
while (generator.hasNext()) {
sb.append(generator.next());
@ -134,10 +134,10 @@ public class HTMLRenderer implements Renderer<String> {
return sb.toString();
}
private String getPreamble(@NonNull final Labyrinth labyrinth) {
private String getPreamble(@NonNull final Maze maze) {
return "<!DOCTYPE html><html lang=\"en\">" +
"<head>" +
"<title>Labyrinth " + labyrinth.getWidth() + "x" + labyrinth.getHeight() + ", ID " + labyrinth.getRandomSeed() + "</title>" +
"<title>Maze " + maze.getWidth() + "x" + maze.getHeight() + ", ID " + maze.getRandomSeed() + "</title>" +
"<meta charset=\"utf-8\">" +
"<style>" +
"table{border-collapse:collapse;}" +

View file

@ -1,8 +1,8 @@
package ch.fritteli.labyrinth.generator.renderer.htmlfile;
package ch.fritteli.maze.generator.renderer.htmlfile;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.renderer.Renderer;
import ch.fritteli.labyrinth.generator.renderer.html.HTMLRenderer;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.renderer.Renderer;
import ch.fritteli.maze.generator.renderer.html.HTMLRenderer;
import io.vavr.control.Option;
import io.vavr.control.Try;
import lombok.NonNull;
@ -24,7 +24,7 @@ public class HTMLFileRenderer implements Renderer<Path> {
private HTMLFileRenderer() {
this.targetFile = Try
.of(() -> Files.createTempFile("labyrinth_", ".html"))
.of(() -> Files.createTempFile("maze_", ".html"))
.onFailure(ex -> log.error("Unable to set default target file.", ex))
.toOption();
}
@ -48,7 +48,7 @@ public class HTMLFileRenderer implements Renderer<Path> {
@NonNull
@Override
public Path render(@NonNull final Labyrinth labyrinth) {
public Path render(@NonNull final Maze maze) {
if (!this.isTargetFileDefinedAndWritable()) {
try {
Files.createFile(this.targetFile.get());
@ -56,7 +56,7 @@ public class HTMLFileRenderer implements Renderer<Path> {
throw new IllegalArgumentException("Cannot write to target file.", e);
}
}
final String html = HTML_RENDERER.render(labyrinth);
final String html = HTML_RENDERER.render(maze);
final Path targetFile = this.targetFile.get();
try {
Files.writeString(targetFile, html, StandardCharsets.UTF_8);

View file

@ -1,37 +1,38 @@
package ch.fritteli.labyrinth.generator.renderer.json;
package ch.fritteli.maze.generator.renderer.json;
import ch.fritteli.labyrinth.generator.json.JsonCell;
import ch.fritteli.labyrinth.generator.json.JsonCoordinates;
import ch.fritteli.labyrinth.generator.json.JsonLabyrinth;
import ch.fritteli.labyrinth.generator.model.Direction;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.model.Tile;
import java.util.ArrayList;
import java.util.List;
import ch.fritteli.maze.generator.json.JsonCell;
import ch.fritteli.maze.generator.json.JsonCoordinates;
import ch.fritteli.maze.generator.json.JsonMaze;
import ch.fritteli.maze.generator.model.Direction;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.model.Tile;
import lombok.AccessLevel;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.util.ArrayList;
import java.util.List;
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
class Generator {
@NonNull
private final Labyrinth labyrinth;
private final Maze maze;
@NonNull
JsonLabyrinth generate() {
final JsonLabyrinth result = new JsonLabyrinth();
result.setId(String.valueOf(this.labyrinth.getRandomSeed()));
result.setWidth(this.labyrinth.getWidth());
result.setHeight(this.labyrinth.getHeight());
JsonMaze generate() {
final JsonMaze result = new JsonMaze();
result.setId(String.valueOf(this.maze.getRandomSeed()));
result.setWidth(this.maze.getWidth());
result.setHeight(this.maze.getHeight());
final List<List<JsonCell>> rows = new ArrayList<>();
for (int y = 0; y < this.labyrinth.getHeight(); y++) {
for (int y = 0; y < this.maze.getHeight(); y++) {
final ArrayList<JsonCell> row = new ArrayList<>();
for (int x = 0; x < this.labyrinth.getWidth(); x++) {
for (int x = 0; x < this.maze.getWidth(); x++) {
// x and y are not effectively final and can therefore not be accessed from within the lambda. Hence, create the string beforehand.
final String exceptionString = "Failed to obtain tile at %dx%d, although labyrinth has dimensoins %dx%d"
.formatted(x, y, this.labyrinth.getWidth(), this.labyrinth.getHeight());
final Tile tile = this.labyrinth.getTileAt(x, y)
final String exceptionString = "Failed to obtain tile at %dx%d, although maze has dimensoins %dx%d"
.formatted(x, y, this.maze.getWidth(), this.maze.getHeight());
final Tile tile = this.maze.getTileAt(x, y)
.getOrElseThrow(() -> new IllegalStateException(exceptionString));
final JsonCell cell = new JsonCell();
cell.setTop(tile.hasWallAt(Direction.TOP));
@ -45,12 +46,12 @@ class Generator {
}
result.setGrid(rows);
final JsonCoordinates start = new JsonCoordinates();
start.setX(this.labyrinth.getStart().getX());
start.setY(this.labyrinth.getStart().getY());
start.setX(this.maze.getStart().getX());
start.setY(this.maze.getStart().getY());
result.setStart(start);
final JsonCoordinates end = new JsonCoordinates();
end.setX(this.labyrinth.getEnd().getX());
end.setY(this.labyrinth.getEnd().getY());
end.setX(this.maze.getEnd().getX());
end.setY(this.maze.getEnd().getY());
result.setEnd(end);
return result;
}

View file

@ -1,15 +1,16 @@
package ch.fritteli.labyrinth.generator.renderer.json;
package ch.fritteli.maze.generator.renderer.json;
import ch.fritteli.labyrinth.generator.json.JsonCell;
import ch.fritteli.labyrinth.generator.json.JsonLabyrinth;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.renderer.Renderer;
import ch.fritteli.maze.generator.json.JsonCell;
import ch.fritteli.maze.generator.json.JsonMaze;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.renderer.Renderer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.NonNull;
import java.util.ArrayList;
import java.util.List;
import lombok.NonNull;
public class JsonRenderer implements Renderer<String> {
@ -27,7 +28,7 @@ public class JsonRenderer implements Renderer<String> {
}
@NonNull
private static JsonLabyrinth createSingleCellLabyrinth() {
private static JsonMaze createSingleCellMaze() {
// This is the only cell.
final JsonCell cell = new JsonCell();
cell.setRight(true);
@ -37,17 +38,17 @@ public class JsonRenderer implements Renderer<String> {
final List<List<JsonCell>> rows = new ArrayList<>();
rows.add(new ArrayList<>());
rows.get(0).add(cell);
// Wrap it all in an instance of JsonLabyrinth.
final JsonLabyrinth jsonLabyrinth = new JsonLabyrinth();
jsonLabyrinth.setId("0");
jsonLabyrinth.setGrid(rows);
return jsonLabyrinth;
// Wrap it all in an instance of JsonMaze.
final JsonMaze jsonMaze = new JsonMaze();
jsonMaze.setId("0");
jsonMaze.setGrid(rows);
return jsonMaze;
}
@NonNull
private String toString(@NonNull final JsonLabyrinth jsonLabyrinth) {
private String toString(@NonNull final JsonMaze jsonMaze) {
try {
return this.objectMapper.writeValueAsString(jsonLabyrinth);
return this.objectMapper.writeValueAsString(jsonMaze);
} catch (final JsonProcessingException e) {
throw new RuntimeException(e);
}
@ -55,14 +56,14 @@ public class JsonRenderer implements Renderer<String> {
@NonNull
@Override
public String render(@NonNull final Labyrinth labyrinth) {
final JsonLabyrinth jsonLabyrinth;
if (labyrinth.getWidth() == 0 || labyrinth.getHeight() == 0) {
jsonLabyrinth = createSingleCellLabyrinth();
public String render(@NonNull final Maze maze) {
final JsonMaze jsonMaze;
if (maze.getWidth() == 0 || maze.getHeight() == 0) {
jsonMaze = createSingleCellMaze();
} else {
final Generator generator = new Generator(labyrinth);
jsonLabyrinth = generator.generate();
final Generator generator = new Generator(maze);
jsonMaze = generator.generate();
}
return toString(jsonLabyrinth);
return toString(jsonMaze);
}
}

View file

@ -1,8 +1,8 @@
package ch.fritteli.labyrinth.generator.renderer.jsonfile;
package ch.fritteli.maze.generator.renderer.jsonfile;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.renderer.Renderer;
import ch.fritteli.labyrinth.generator.renderer.json.JsonRenderer;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.renderer.Renderer;
import ch.fritteli.maze.generator.renderer.json.JsonRenderer;
import io.vavr.control.Option;
import io.vavr.control.Try;
import java.io.File;
@ -24,7 +24,7 @@ public class JsonFileRenderer implements Renderer<Path> {
private JsonFileRenderer() {
this.targetFile = Try
.of(() -> Files.createTempFile("labyrinth_", ".json"))
.of(() -> Files.createTempFile("maze_", ".json"))
.onFailure(ex -> log.error("Unable to set default target file.", ex))
.toOption();
}
@ -48,7 +48,7 @@ public class JsonFileRenderer implements Renderer<Path> {
@NonNull
@Override
public Path render(@NonNull final Labyrinth labyrinth) {
public Path render(@NonNull final Maze maze) {
if (!this.isTargetFileDefinedAndWritable()) {
try {
Files.createFile(this.targetFile.get());
@ -56,7 +56,7 @@ public class JsonFileRenderer implements Renderer<Path> {
throw new IllegalArgumentException("Cannot write to target file.", e);
}
}
final String json = JSON_RENDERER.render(labyrinth);
final String json = JSON_RENDERER.render(maze);
final Path outputFile = this.targetFile.get();
try {
Files.writeString(outputFile, json, StandardCharsets.UTF_8);

View file

@ -1,9 +1,9 @@
package ch.fritteli.labyrinth.generator.renderer.pdf;
package ch.fritteli.maze.generator.renderer.pdf;
import ch.fritteli.labyrinth.generator.model.Direction;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.model.Position;
import ch.fritteli.labyrinth.generator.model.Tile;
import ch.fritteli.maze.generator.model.Direction;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.model.Position;
import ch.fritteli.maze.generator.model.Tile;
import io.vavr.control.Option;
import java.awt.BasicStroke;
import java.awt.Color;
@ -25,16 +25,16 @@ import org.jetbrains.annotations.Nullable;
class Generator {
@NonNull
private final Labyrinth labyrinth;
private final Maze maze;
@NonNull
public ByteArrayOutputStream generate() {
final float pageWidth = this.labyrinth.getWidth() * PDFRenderer.SCALE + 2 * PDFRenderer.MARGIN;
final float pageHeight = this.labyrinth.getHeight() * PDFRenderer.SCALE + 2 * PDFRenderer.MARGIN;
final float pageWidth = this.maze.getWidth() * PDFRenderer.SCALE + 2 * PDFRenderer.MARGIN;
final float pageHeight = this.maze.getHeight() * PDFRenderer.SCALE + 2 * PDFRenderer.MARGIN;
final PDDocument pdDocument = new PDDocument();
final PDDocumentInformation info = new PDDocumentInformation();
info.setTitle("Labyrinth %sx%s, ID %s".formatted(this.labyrinth.getWidth(), this.labyrinth.getHeight(), this.labyrinth.getRandomSeed()));
info.setTitle("Maze %sx%s, ID %s".formatted(this.maze.getWidth(), this.maze.getHeight(), this.maze.getRandomSeed()));
pdDocument.setDocumentInformation(info);
final PDPage puzzlePage = new PDPage(new PDRectangle(pageWidth, pageHeight));
final PDPage solutionPage = new PDPage(new PDRectangle(pageWidth, pageHeight));
@ -72,11 +72,11 @@ class Generator {
// PDF has the origin in the lower left corner. We want it in the upper left corner, hence some magic is required.
Coordinate coordinate = new Coordinate(0f, 0f);
// Draw the TOP borders of all tiles.
for (int y = 0; y < this.labyrinth.getHeight(); y++) {
for (int y = 0; y < this.maze.getHeight(); y++) {
boolean isPainting = false;
coordinate = coordinate.withY(y);
for (int x = 0; x < this.labyrinth.getWidth(); x++) {
final Tile currentTile = this.labyrinth.getTileAt(x, y).get();
for (int x = 0; x < this.maze.getWidth(); x++) {
final Tile currentTile = this.maze.getTileAt(x, y).get();
coordinate = coordinate.withX(x);
if (currentTile.hasWallAt(Direction.TOP)) {
if (!isPainting) {
@ -96,7 +96,7 @@ class Generator {
}
}
if (isPainting) {
coordinate = coordinate.withX(this.labyrinth.getWidth());
coordinate = coordinate.withX(this.maze.getWidth());
for (final PDPageContentStream contentStream : contentStreams) {
contentStream.lineTo(coordinate.getX(), coordinate.getY());
contentStream.stroke();
@ -105,10 +105,10 @@ class Generator {
}
// Draw the BOTTOM border of the last row of tiles.
boolean isPainting = false;
int y = this.labyrinth.getHeight();
coordinate = coordinate.withY(this.labyrinth.getHeight());
for (int x = 0; x < this.labyrinth.getWidth(); x++) {
final Tile currentTile = this.labyrinth.getTileAt(x, y - 1).get();
int y = this.maze.getHeight();
coordinate = coordinate.withY(this.maze.getHeight());
for (int x = 0; x < this.maze.getWidth(); x++) {
final Tile currentTile = this.maze.getTileAt(x, y - 1).get();
coordinate = coordinate.withX(x);
if (currentTile.hasWallAt(Direction.BOTTOM)) {
if (!isPainting) {
@ -128,7 +128,7 @@ class Generator {
}
}
if (isPainting) {
coordinate = coordinate.withX(this.labyrinth.getWidth());
coordinate = coordinate.withX(this.maze.getWidth());
for (final PDPageContentStream contentStream : contentStreams) {
contentStream.lineTo(coordinate.getX(), coordinate.getY());
contentStream.stroke();
@ -140,11 +140,11 @@ class Generator {
// PDF has the origin in the lower left corner. We want it in the upper left corner, hence some magic is required.
Coordinate coordinate = new Coordinate(0f, 0f);
// Draw the LEFT borders of all tiles.
for (int x = 0; x < this.labyrinth.getWidth(); x++) {
for (int x = 0; x < this.maze.getWidth(); x++) {
boolean isPainting = false;
coordinate = coordinate.withX(x);
for (int y = 0; y < this.labyrinth.getHeight(); y++) {
final Tile currentTile = this.labyrinth.getTileAt(x, y).get();
for (int y = 0; y < this.maze.getHeight(); y++) {
final Tile currentTile = this.maze.getTileAt(x, y).get();
coordinate = coordinate.withY(y);
if (currentTile.hasWallAt(Direction.LEFT)) {
if (!isPainting) {
@ -164,7 +164,7 @@ class Generator {
}
}
if (isPainting) {
coordinate = coordinate.withY(this.labyrinth.getHeight());
coordinate = coordinate.withY(this.maze.getHeight());
for (final PDPageContentStream contentStream : contentStreams) {
contentStream.lineTo(coordinate.getX(), coordinate.getY());
contentStream.stroke();
@ -173,10 +173,10 @@ class Generator {
}
// Draw the RIGHT border of the last column of tiles.
boolean isPainting = false;
int x = this.labyrinth.getWidth();
coordinate = coordinate.withX(this.labyrinth.getWidth());
for (int y = 0; y < this.labyrinth.getHeight(); y++) {
final Tile currentTile = this.labyrinth.getTileAt(x - 1, y).get();
int x = this.maze.getWidth();
coordinate = coordinate.withX(this.maze.getWidth());
for (int y = 0; y < this.maze.getHeight(); y++) {
final Tile currentTile = this.maze.getTileAt(x - 1, y).get();
coordinate = coordinate.withY(y);
if (currentTile.hasWallAt(Direction.RIGHT)) {
if (!isPainting) {
@ -196,7 +196,7 @@ class Generator {
}
}
if (isPainting) {
coordinate = coordinate.withY(this.labyrinth.getHeight());
coordinate = coordinate.withY(this.maze.getHeight());
for (final PDPageContentStream contentStream : contentStreams) {
contentStream.lineTo(coordinate.getX(), coordinate.getY());
contentStream.stroke();
@ -208,8 +208,8 @@ class Generator {
// Draw the solution in red
pageContentStream.setStrokingColor(Color.RED);
// PDF has the origin in the lower left corner. We want it in the upper left corner, hence some magic is required.
final Position end = this.labyrinth.getEnd();
Position currentPosition = this.labyrinth.getStart();
final Position end = this.maze.getEnd();
Position currentPosition = this.maze.getStart();
Position previousPosition = null;
SolutionCoordinate coordinate = new SolutionCoordinate(currentPosition.getX(), currentPosition.getY());
pageContentStream.moveTo(coordinate.getX(), coordinate.getY());
@ -225,11 +225,11 @@ class Generator {
@NonNull
private Position findNextSolutionPosition(@Nullable final Position previousPosition, @NonNull final Position currentPosition) {
final Tile currentTile = this.labyrinth.getTileAt(currentPosition).get();
final Tile currentTile = this.maze.getTileAt(currentPosition).get();
for (final Direction direction : Direction.values()) {
if (!currentTile.hasWallAt(direction)) {
final Position position = currentPosition.move(direction);
final Option<Tile> tileAtPosition = this.labyrinth.getTileAt(position);
final Option<Tile> tileAtPosition = this.maze.getTileAt(position);
if (position.equals(previousPosition)) {
continue;
}
@ -265,7 +265,7 @@ class Generator {
}
private float calcY(final int y) {
return (Generator.this.labyrinth.getHeight() - y) * PDFRenderer.SCALE + PDFRenderer.MARGIN;
return (Generator.this.maze.getHeight() - y) * PDFRenderer.SCALE + PDFRenderer.MARGIN;
}
}
@ -285,7 +285,7 @@ class Generator {
}
private float calcY(final int y) {
return (Generator.this.labyrinth.getHeight() - y) * PDFRenderer.SCALE - PDFRenderer.SCALE / 2 + PDFRenderer.MARGIN;
return (Generator.this.maze.getHeight() - y) * PDFRenderer.SCALE - PDFRenderer.SCALE / 2 + PDFRenderer.MARGIN;
}
}
}

View file

@ -1,8 +1,9 @@
package ch.fritteli.labyrinth.generator.renderer.pdf;
package ch.fritteli.maze.generator.renderer.pdf;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.renderer.Renderer;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.renderer.Renderer;
import java.io.ByteArrayOutputStream;
import lombok.NonNull;
public class PDFRenderer implements Renderer<ByteArrayOutputStream> {
@ -19,8 +20,8 @@ public class PDFRenderer implements Renderer<ByteArrayOutputStream> {
@NonNull
@Override
public ByteArrayOutputStream render(@NonNull final Labyrinth labyrinth) {
final Generator generator = new Generator(labyrinth);
public ByteArrayOutputStream render(@NonNull final Maze maze) {
final Generator generator = new Generator(maze);
return generator.generate();
}
}

View file

@ -1,8 +1,8 @@
package ch.fritteli.labyrinth.generator.renderer.pdffile;
package ch.fritteli.maze.generator.renderer.pdffile;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.renderer.Renderer;
import ch.fritteli.labyrinth.generator.renderer.pdf.PDFRenderer;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.renderer.Renderer;
import ch.fritteli.maze.generator.renderer.pdf.PDFRenderer;
import io.vavr.control.Option;
import io.vavr.control.Try;
import java.io.ByteArrayOutputStream;
@ -24,7 +24,7 @@ public class PDFFileRenderer implements Renderer<Path> {
private PDFFileRenderer() {
this.targetFile = Try
.of(() -> Files.createTempFile("labyrinth_", ".pdf"))
.of(() -> Files.createTempFile("maze_", ".pdf"))
.onFailure(ex -> log.error("Unable to set default target file.", ex))
.toOption();
}
@ -48,7 +48,7 @@ public class PDFFileRenderer implements Renderer<Path> {
@NonNull
@Override
public Path render(@NonNull final Labyrinth labyrinth) {
public Path render(@NonNull final Maze maze) {
if (!this.isTargetFileDefinedAndWritable()) {
try {
Files.createFile(this.targetFile.get());
@ -56,7 +56,7 @@ public class PDFFileRenderer implements Renderer<Path> {
throw new IllegalArgumentException("Cannot write to target file.", e);
}
}
final ByteArrayOutputStream bytes = PDF_RENDERER.render(labyrinth);
final ByteArrayOutputStream bytes = PDF_RENDERER.render(maze);
final Path outputFile = this.targetFile.get();
try {
bytes.writeTo(Files.newOutputStream(outputFile));

View file

@ -1,4 +1,4 @@
package ch.fritteli.labyrinth.generator.renderer.text;
package ch.fritteli.maze.generator.renderer.text;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;

View file

@ -1,8 +1,8 @@
package ch.fritteli.labyrinth.generator.renderer.text;
package ch.fritteli.maze.generator.renderer.text;
import ch.fritteli.labyrinth.generator.model.Direction;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.model.Tile;
import ch.fritteli.maze.generator.model.Direction;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.model.Tile;
import io.vavr.control.Option;
import lombok.AccessLevel;
import lombok.NonNull;
@ -11,7 +11,7 @@ import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
class Generator {
@NonNull
private final Labyrinth labyrinth;
private final Maze maze;
private final boolean renderSolution;
private int x = 0;
private int y = 0;
@ -25,15 +25,15 @@ class Generator {
private int line = 0;
boolean hasNext() {
return this.y < this.labyrinth.getHeight();
return this.y < this.maze.getHeight();
}
String next() {
final Tile currentTile = this.labyrinth.getTileAt(this.x, this.y).get();
final Option<Tile> topTile = this.labyrinth.getTileAt(this.x, this.y - 1);
final Option<Tile> rightTile = this.labyrinth.getTileAt(this.x + 1, this.y);
final Option<Tile> bottomTile = this.labyrinth.getTileAt(this.x, this.y + 1);
final Option<Tile> leftTile = this.labyrinth.getTileAt(this.x - 1, this.y);
final Tile currentTile = this.maze.getTileAt(this.x, this.y).get();
final Option<Tile> topTile = this.maze.getTileAt(this.x, this.y - 1);
final Option<Tile> rightTile = this.maze.getTileAt(this.x + 1, this.y);
final Option<Tile> bottomTile = this.maze.getTileAt(this.x, this.y + 1);
final Option<Tile> leftTile = this.maze.getTileAt(this.x - 1, this.y);
final String s;
switch (this.line) {
case 0:
@ -56,13 +56,13 @@ class Generator {
private void prepareNextStep() {
// do some magic ...
this.x++;
if (this.x == this.labyrinth.getWidth()) {
if (this.x == this.maze.getWidth()) {
// Reached the end of the row?
// On to the next line then!
this.x = 0;
this.line++;
}
if (this.line == 2 && this.y < this.labyrinth.getHeight() - 1) {
if (this.line == 2 && this.y < this.maze.getHeight() - 1) {
// Finished rendering the center line, and more rows available?
// On to the next row then!
this.line = 0;
@ -106,7 +106,7 @@ class Generator {
charDef3.up();
}
result = charDef1.toString() + charDef2;
if (this.x == this.labyrinth.getWidth() - 1) {
if (this.x == this.maze.getWidth() - 1) {
result += charDef3 + "\n";
}
return result;
@ -153,7 +153,7 @@ class Generator {
result = charDef1.toString() + charDef2;
if (this.x == this.labyrinth.getWidth() - 1) {
if (this.x == this.maze.getWidth() - 1) {
result += charDef3 + "\n";
}
@ -187,7 +187,7 @@ class Generator {
result = charDef1.toString() + charDef2;
if (this.x == this.labyrinth.getWidth() - 1) {
if (this.x == this.maze.getWidth() - 1) {
result += charDef3;
}
return result;

View file

@ -1,7 +1,7 @@
package ch.fritteli.labyrinth.generator.renderer.text;
package ch.fritteli.maze.generator.renderer.text;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.renderer.Renderer;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.renderer.Renderer;
import lombok.NonNull;
public class TextRenderer implements Renderer<String> {
@ -24,11 +24,11 @@ public class TextRenderer implements Renderer<String> {
@NonNull
@Override
public String render(@NonNull final Labyrinth labyrinth) {
if (labyrinth.getWidth() == 0 || labyrinth.getHeight() == 0) {
public String render(@NonNull final Maze maze) {
if (maze.getWidth() == 0 || maze.getHeight() == 0) {
return "";
}
final Generator generator = new Generator(labyrinth, this.renderSolution);
final Generator generator = new Generator(maze, this.renderSolution);
final StringBuilder sb = new StringBuilder();
while (generator.hasNext()) {
sb.append(generator.next());

View file

@ -1,8 +1,8 @@
package ch.fritteli.labyrinth.generator.renderer.textfile;
package ch.fritteli.maze.generator.renderer.textfile;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.renderer.Renderer;
import ch.fritteli.labyrinth.generator.renderer.text.TextRenderer;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.renderer.Renderer;
import ch.fritteli.maze.generator.renderer.text.TextRenderer;
import io.vavr.collection.List;
import io.vavr.control.Option;
import io.vavr.control.Try;
@ -21,19 +21,19 @@ public class TextFileRenderer implements Renderer<List<Path>> {
@NonNull
private static final TextRenderer TEXT_RENDERER = TextRenderer.newInstance();
@NonNull
private Option<Path> targetLabyrinthFile;
private Option<Path> targetMazeFile;
@NonNull
private Option<Path> targetSolutionFile;
private TextFileRenderer() {
this.targetLabyrinthFile = Try
.of(() -> Files.createTempFile("labyrinth_", ".txt"))
this.targetMazeFile = Try
.of(() -> Files.createTempFile("maze_", ".txt"))
.onFailure(ex -> log.error("Unable to set default target file", ex))
.toOption();
this.targetSolutionFile = this.targetLabyrinthFile.toTry()
this.targetSolutionFile = this.targetMazeFile.toTry()
.map(Path::getParent)
.flatMap(parent -> this.targetLabyrinthFile.toTry()
.flatMap(parent -> this.targetMazeFile.toTry()
.map(Path::getFileName)
.map(Path::toString)
.map(a -> a.replace(".txt", "-solution.txt"))
@ -47,8 +47,8 @@ public class TextFileRenderer implements Renderer<List<Path>> {
return new TextFileRenderer();
}
public boolean isTargetLabyrinthFileDefinedAndWritable() {
return this.targetLabyrinthFile
public boolean isTargetMazeFileDefinedAndWritable() {
return this.targetMazeFile
.map(Path::toFile)
.exists(File::canWrite);
}
@ -60,8 +60,8 @@ public class TextFileRenderer implements Renderer<List<Path>> {
}
@NonNull
public TextFileRenderer setTargetLabyrinthFile(@NonNull final Path targetLabyrinthFile) {
this.targetLabyrinthFile = Option.of(targetLabyrinthFile);
public TextFileRenderer setTargetMazeFile(@NonNull final Path targetMazeFile) {
this.targetMazeFile = Option.of(targetMazeFile);
return this;
}
@ -73,13 +73,13 @@ public class TextFileRenderer implements Renderer<List<Path>> {
@NonNull
@Override
public List<Path> render(@NonNull final Labyrinth labyrinth) {
if (!this.isTargetLabyrinthFileDefinedAndWritable()) {
public List<Path> render(@NonNull final Maze maze) {
if (!this.isTargetMazeFileDefinedAndWritable()) {
try {
Files.createFile(this.targetLabyrinthFile.get());
Files.createFile(this.targetMazeFile.get());
} catch (IOException | NoSuchElementException e) {
log.error("Cannot write to target labyrinth file.", e);
throw new IllegalArgumentException("Cannot write to target labyrinth file.", e);
log.error("Cannot write to target maze file.", e);
throw new IllegalArgumentException("Cannot write to target maze file.", e);
}
}
if (!this.isTargetSolutionFileDefinedAndWritable()) {
@ -92,21 +92,21 @@ public class TextFileRenderer implements Renderer<List<Path>> {
final String text;
final String solution;
synchronized (TEXT_RENDERER) {
text = TEXT_RENDERER.setRenderSolution(false).render(labyrinth).strip();
solution = TEXT_RENDERER.setRenderSolution(true).render(labyrinth).strip();
text = TEXT_RENDERER.setRenderSolution(false).render(maze).strip();
solution = TEXT_RENDERER.setRenderSolution(true).render(maze).strip();
}
final Path targetLabyrinthFile = this.targetLabyrinthFile.get();
final Path targetMazeFile = this.targetMazeFile.get();
final Path targetSolutionFile = this.targetSolutionFile.get();
try {
Files.write(targetLabyrinthFile, text.getBytes(StandardCharsets.UTF_8));
Files.write(targetMazeFile, text.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
log.error("Failed writing to file " + targetLabyrinthFile.normalize(), e);
log.error("Failed writing to file " + targetMazeFile.normalize(), e);
}
try {
Files.write(targetSolutionFile, solution.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
log.error("Failed writing to file " + targetSolutionFile.normalize());
}
return List.of(targetLabyrinthFile, targetSolutionFile);
return List.of(targetMazeFile, targetSolutionFile);
}
}

View file

@ -1,19 +1,19 @@
package ch.fritteli.labyrinth.generator.serialization;
package ch.fritteli.maze.generator.serialization;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.maze.generator.model.Maze;
import java.io.ByteArrayInputStream;
import lombok.NonNull;
public abstract class AbstractLabyrinthInputStream extends ByteArrayInputStream {
public abstract class AbstractMazeInputStream extends ByteArrayInputStream {
public AbstractLabyrinthInputStream(@NonNull final byte[] buf) {
public AbstractMazeInputStream(@NonNull final byte[] buf) {
super(buf);
}
public abstract void checkHeader();
@NonNull
public abstract Labyrinth readLabyrinthData();
public abstract Maze readMazeData();
public byte readByte() {
final int read = this.read();

View file

@ -1,14 +1,14 @@
package ch.fritteli.labyrinth.generator.serialization;
package ch.fritteli.maze.generator.serialization;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.maze.generator.model.Maze;
import java.io.ByteArrayOutputStream;
import lombok.NonNull;
public abstract class AbstractLabyrinthOutputStream extends ByteArrayOutputStream {
public abstract class AbstractMazeOutputStream extends ByteArrayOutputStream {
public abstract void writeHeader();
public abstract void writeLabyrinthData(@NonNull final Labyrinth labyrinth);
public abstract void writeMazeData(@NonNull final Maze maze);
public void writeByte(final byte value) {
this.write(value);

View file

@ -1,13 +1,13 @@
package ch.fritteli.labyrinth.generator.serialization.v1;
package ch.fritteli.maze.generator.serialization.v1;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.model.Tile;
import ch.fritteli.labyrinth.generator.serialization.AbstractLabyrinthInputStream;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.model.Tile;
import ch.fritteli.maze.generator.serialization.AbstractMazeInputStream;
import lombok.NonNull;
public class LabyrinthInputStreamV1 extends AbstractLabyrinthInputStream {
public class MazeInputStreamV1 extends AbstractMazeInputStream {
public LabyrinthInputStreamV1(@NonNull final byte[] buf) {
public MazeInputStreamV1(@NonNull final byte[] buf) {
super(buf);
}
@ -15,21 +15,21 @@ public class LabyrinthInputStreamV1 extends AbstractLabyrinthInputStream {
public void checkHeader() {
final byte magic1 = this.readByte();
if (magic1 != SerializerDeserializerV1.MAGIC_BYTE_1) {
throw new IllegalArgumentException("Invalid labyrinth data.");
throw new IllegalArgumentException("Invalid maze data.");
}
final byte magic2 = this.readByte();
if (magic2 != SerializerDeserializerV1.MAGIC_BYTE_2) {
throw new IllegalArgumentException("Invalid labyrinth data.");
throw new IllegalArgumentException("Invalid maze data.");
}
final int version = this.readByte();
if (version != SerializerDeserializerV1.VERSION_BYTE) {
throw new IllegalArgumentException("Unknown Labyrinth data version: " + version);
throw new IllegalArgumentException("Unknown maze data version: " + version);
}
}
@NonNull
@Override
public Labyrinth readLabyrinthData() {
public Maze readMazeData() {
final long randomSeed = this.readLong();
final int width = this.readInt();
final int height = this.readInt();
@ -46,6 +46,6 @@ public class LabyrinthInputStreamV1 extends AbstractLabyrinthInputStream {
}
}
return SerializerDeserializerV1.createLabyrinth(tiles, width, height, randomSeed);
return SerializerDeserializerV1.createMaze(tiles, width, height, randomSeed);
}
}

View file

@ -1,11 +1,11 @@
package ch.fritteli.labyrinth.generator.serialization.v1;
package ch.fritteli.maze.generator.serialization.v1;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.model.Tile;
import ch.fritteli.labyrinth.generator.serialization.AbstractLabyrinthOutputStream;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.model.Tile;
import ch.fritteli.maze.generator.serialization.AbstractMazeOutputStream;
import lombok.NonNull;
public class LabyrinthOutputStreamV1 extends AbstractLabyrinthOutputStream {
public class MazeOutputStreamV1 extends AbstractMazeOutputStream {
@Override
public void writeHeader() {
@ -15,10 +15,10 @@ public class LabyrinthOutputStreamV1 extends AbstractLabyrinthOutputStream {
}
@Override
public void writeLabyrinthData(@NonNull final Labyrinth labyrinth) {
final long randomSeed = labyrinth.getRandomSeed();
final int width = labyrinth.getWidth();
final int height = labyrinth.getHeight();
public void writeMazeData(@NonNull final Maze maze) {
final long randomSeed = maze.getRandomSeed();
final int width = maze.getWidth();
final int height = maze.getHeight();
this.writeLong(randomSeed);
this.writeInt(width);
this.writeInt(height);
@ -26,7 +26,7 @@ public class LabyrinthOutputStreamV1 extends AbstractLabyrinthOutputStream {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// We .get() it, because we want to crash hard if it is not available.
final Tile tile = labyrinth.getTileAt(x, y).get();
final Tile tile = maze.getTileAt(x, y).get();
final byte bitmask = SerializerDeserializerV1.getBitmaskForTile(tile);
this.writeByte(bitmask);
}

View file

@ -1,8 +1,8 @@
package ch.fritteli.labyrinth.generator.serialization.v1;
package ch.fritteli.maze.generator.serialization.v1;
import ch.fritteli.labyrinth.generator.model.Direction;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.model.Tile;
import ch.fritteli.maze.generator.model.Direction;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.model.Tile;
import lombok.NonNull;
import lombok.experimental.UtilityClass;
@ -57,40 +57,41 @@ public class SerializerDeserializerV1 {
private final byte SOLUTION_BIT = 0b0001_0000;
/**
* Serializes the {@code labyrinth} into a byte array.
* Serializes the {@code maze} into a byte array.
*
* @param labyrinth The labyrinth to be serialized.
* @param maze The {@link Maze} to be serialized.
* @return The resulting byte array.
*/
@NonNull
public byte[] serialize(@NonNull final Labyrinth labyrinth) {
final LabyrinthOutputStreamV1 stream = new LabyrinthOutputStreamV1();
public byte[] serialize(@NonNull final Maze maze) {
final MazeOutputStreamV1 stream = new MazeOutputStreamV1();
stream.writeHeader();
stream.writeLabyrinthData(labyrinth);
stream.writeMazeData(maze);
return stream.toByteArray();
}
/**
* Deserializes the byte array into an instance of {@link Labyrinth}.
* Deserializes the byte array into an instance of {@link Maze}.
*
* @param bytes The byte array to be deserialized.
* @return An instance of {@link Labyrinth}.
* @return An instance of {@link Maze}.
*/
@NonNull
public Labyrinth deserialize(@NonNull final byte[] bytes) {
final LabyrinthInputStreamV1 stream = new LabyrinthInputStreamV1(bytes);
public Maze deserialize(@NonNull final byte[] bytes) {
final MazeInputStreamV1 stream = new MazeInputStreamV1(bytes);
stream.checkHeader();
return stream.readLabyrinthData();
return stream.readMazeData();
}
@NonNull
Labyrinth createLabyrinth(@NonNull final Tile[][] field, final int width, final int height, final long randomSeed) {
Maze createMaze(@NonNull final Tile[][] field, final int width, final int height, final long randomSeed) {
try {
final Constructor<Labyrinth> constructor = Labyrinth.class.getDeclaredConstructor(Tile[][].class, Integer.TYPE, Integer.TYPE, Long.TYPE);
final Constructor<Maze> constructor = Maze.class.getDeclaredConstructor(Tile[][].class, Integer.TYPE, Integer.TYPE, Long.TYPE);
constructor.setAccessible(true);
return constructor.newInstance(field, width, height, randomSeed);
} catch (@NonNull final NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
throw new RuntimeException("Can not deserialize Labyrinth from labyrinth data.", e);
} catch (@NonNull final NoSuchMethodException | IllegalAccessException | InstantiationException |
InvocationTargetException e) {
throw new RuntimeException("Can not deserialize Maze from maze data.", e);
}
}
@ -100,8 +101,9 @@ public class SerializerDeserializerV1 {
final Constructor<Tile> constructor = Tile.class.getDeclaredConstructor(EnumSet.class, Boolean.TYPE);
constructor.setAccessible(true);
return constructor.newInstance(walls, solution);
} catch (@NonNull final NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("Can not deserialize Tile from labyrinth data.", e);
} catch (@NonNull final NoSuchMethodException | InstantiationException | IllegalAccessException |
InvocationTargetException e) {
throw new RuntimeException("Can not deserialize Tile from maze data.", e);
}
}

View file

@ -1,14 +1,14 @@
package ch.fritteli.labyrinth.generator.serialization.v2;
package ch.fritteli.maze.generator.serialization.v2;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.model.Position;
import ch.fritteli.labyrinth.generator.model.Tile;
import ch.fritteli.labyrinth.generator.serialization.AbstractLabyrinthInputStream;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.model.Position;
import ch.fritteli.maze.generator.model.Tile;
import ch.fritteli.maze.generator.serialization.AbstractMazeInputStream;
import lombok.NonNull;
public class LabyrinthInputStreamV2 extends AbstractLabyrinthInputStream {
public class MazeInputStreamV2 extends AbstractMazeInputStream {
public LabyrinthInputStreamV2(@NonNull final byte[] buf) {
public MazeInputStreamV2(@NonNull final byte[] buf) {
super(buf);
}
@ -19,21 +19,21 @@ public class LabyrinthInputStreamV2 extends AbstractLabyrinthInputStream {
// 02 0x02 version
final byte magic1 = this.readByte();
if (magic1 != SerializerDeserializerV2.MAGIC_BYTE_1) {
throw new IllegalArgumentException("Invalid labyrinth data.");
throw new IllegalArgumentException("Invalid maze data.");
}
final byte magic2 = this.readByte();
if (magic2 != SerializerDeserializerV2.MAGIC_BYTE_2) {
throw new IllegalArgumentException("Invalid labyrinth data.");
throw new IllegalArgumentException("Invalid maze data.");
}
final int version = this.readByte();
if (version != SerializerDeserializerV2.VERSION_BYTE) {
throw new IllegalArgumentException("Unknown Labyrinth data version: " + version);
throw new IllegalArgumentException("Unknown maze data version: " + version);
}
}
@NonNull
@Override
public Labyrinth readLabyrinthData() {
public Maze readMazeData() {
// 03..06 width (int)
// 07..10 height (int)
// 11..14 start-x (int)
@ -64,6 +64,6 @@ public class LabyrinthInputStreamV2 extends AbstractLabyrinthInputStream {
final Position start = new Position(startX, startY);
final Position end = new Position(endX, endY);
return SerializerDeserializerV2.createLabyrinth(tiles, width, height, start, end, randomSeed);
return SerializerDeserializerV2.createMaze(tiles, width, height, start, end, randomSeed);
}
}

View file

@ -1,12 +1,12 @@
package ch.fritteli.labyrinth.generator.serialization.v2;
package ch.fritteli.maze.generator.serialization.v2;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.model.Position;
import ch.fritteli.labyrinth.generator.model.Tile;
import ch.fritteli.labyrinth.generator.serialization.AbstractLabyrinthOutputStream;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.model.Position;
import ch.fritteli.maze.generator.model.Tile;
import ch.fritteli.maze.generator.serialization.AbstractMazeOutputStream;
import lombok.NonNull;
public class LabyrinthOutputStreamV2 extends AbstractLabyrinthOutputStream {
public class MazeOutputStreamV2 extends AbstractMazeOutputStream {
@Override
public void writeHeader() {
@ -19,7 +19,7 @@ public class LabyrinthOutputStreamV2 extends AbstractLabyrinthOutputStream {
}
@Override
public void writeLabyrinthData(@NonNull final Labyrinth labyrinth) {
public void writeMazeData(@NonNull final Maze maze) {
// 03..06 width (int)
// 07..10 height (int)
// 11..14 start-x (int)
@ -28,11 +28,11 @@ public class LabyrinthOutputStreamV2 extends AbstractLabyrinthOutputStream {
// 23..26 end-y (int)
// 27..34 random seed number (long)
// 35.. tiles
final long randomSeed = labyrinth.getRandomSeed();
final int width = labyrinth.getWidth();
final int height = labyrinth.getHeight();
final Position start = labyrinth.getStart();
final Position end = labyrinth.getEnd();
final long randomSeed = maze.getRandomSeed();
final int width = maze.getWidth();
final int height = maze.getHeight();
final Position start = maze.getStart();
final Position end = maze.getEnd();
this.writeInt(width);
this.writeInt(height);
this.writeInt(start.getX());
@ -44,7 +44,7 @@ public class LabyrinthOutputStreamV2 extends AbstractLabyrinthOutputStream {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// We .get() it, because we want to crash hard if it is not available.
final Tile tile = labyrinth.getTileAt(x, y).get();
final Tile tile = maze.getTileAt(x, y).get();
final byte bitmask = SerializerDeserializerV2.getBitmaskForTile(tile);
this.writeByte(bitmask);
}

View file

@ -1,14 +1,15 @@
package ch.fritteli.labyrinth.generator.serialization.v2;
package ch.fritteli.maze.generator.serialization.v2;
import ch.fritteli.maze.generator.model.Direction;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.model.Position;
import ch.fritteli.maze.generator.model.Tile;
import lombok.NonNull;
import lombok.experimental.UtilityClass;
import ch.fritteli.labyrinth.generator.model.Direction;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import ch.fritteli.labyrinth.generator.model.Position;
import ch.fritteli.labyrinth.generator.model.Tile;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.EnumSet;
import lombok.NonNull;
import lombok.experimental.UtilityClass;
/**
* <pre>
@ -61,40 +62,41 @@ public class SerializerDeserializerV2 {
private final byte SOLUTION_BIT = 0b0001_0000;
/**
* Serializes the {@code labyrinth} into a byte array.
* Serializes the {@code maze} into a byte array.
*
* @param labyrinth The labyrinth to be serialized.
* @param maze The {@link Maze} to be serialized.
* @return The resulting byte array.
*/
@NonNull
public byte[] serialize(@NonNull final Labyrinth labyrinth) {
final LabyrinthOutputStreamV2 stream = new LabyrinthOutputStreamV2();
public byte[] serialize(@NonNull final Maze maze) {
final MazeOutputStreamV2 stream = new MazeOutputStreamV2();
stream.writeHeader();
stream.writeLabyrinthData(labyrinth);
stream.writeMazeData(maze);
return stream.toByteArray();
}
/**
* Deserializes the byte array into an instance of {@link Labyrinth}.
* Deserializes the byte array into an instance of {@link Maze}.
*
* @param bytes The byte array to be deserialized.
* @return An instance of {@link Labyrinth}.
* @return An instance of {@link Maze}.
*/
@NonNull
public Labyrinth deserialize(@NonNull final byte[] bytes) {
final LabyrinthInputStreamV2 stream = new LabyrinthInputStreamV2(bytes);
public Maze deserialize(@NonNull final byte[] bytes) {
final MazeInputStreamV2 stream = new MazeInputStreamV2(bytes);
stream.checkHeader();
return stream.readLabyrinthData();
return stream.readMazeData();
}
@NonNull
Labyrinth createLabyrinth(@NonNull final Tile[][] field, final int width, final int height, @NonNull final Position start, @NonNull final Position end, final long randomSeed) {
Maze createMaze(@NonNull final Tile[][] field, final int width, final int height, @NonNull final Position start, @NonNull final Position end, final long randomSeed) {
try {
final Constructor<Labyrinth> constructor = Labyrinth.class.getDeclaredConstructor(Tile[][].class, Integer.TYPE, Integer.TYPE, Position.class, Position.class, Long.TYPE);
final Constructor<Maze> constructor = Maze.class.getDeclaredConstructor(Tile[][].class, Integer.TYPE, Integer.TYPE, Position.class, Position.class, Long.TYPE);
constructor.setAccessible(true);
return constructor.newInstance(field, width, height, start, end, randomSeed);
} catch (@NonNull final NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
throw new RuntimeException("Can not deserialize Labyrinth from labyrinth data.", e);
} catch (@NonNull final NoSuchMethodException | IllegalAccessException | InstantiationException |
InvocationTargetException e) {
throw new RuntimeException("Can not deserialize Maze from maze data.", e);
}
}
@ -104,8 +106,9 @@ public class SerializerDeserializerV2 {
final Constructor<Tile> constructor = Tile.class.getDeclaredConstructor(EnumSet.class, Boolean.TYPE);
constructor.setAccessible(true);
return constructor.newInstance(walls, solution);
} catch (@NonNull final NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("Can not deserialize Tile from labyrinth data.", e);
} catch (@NonNull final NoSuchMethodException | InstantiationException | IllegalAccessException |
InvocationTargetException e) {
throw new RuntimeException("Can not deserialize Tile from maze data.", e);
}
}

View file

@ -1,7 +1,7 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://manuel.friedli.info/labyrinth-2/labyrinth.schema.json",
"javaType": "ch.fritteli.labyrinth.generator.json.JsonLabyrinth",
"$id": "https://manuel.friedli.info/maze/maze.schema.json",
"javaType": "ch.fritteli.maze.generator.json.JsonMaze",
"type": "object",
"additionalProperties": false,
"required": [
@ -52,7 +52,7 @@
},
"cell": {
"type": "object",
"javaType": "ch.fritteli.labyrinth.generator.json.JsonCell",
"javaType": "ch.fritteli.maze.generator.json.JsonCell",
"additionalProperties": false,
"required": [],
"properties": {
@ -75,7 +75,7 @@
},
"coordinates": {
"type": "object",
"javaType": "ch.fritteli.labyrinth.generator.json.JsonCoordinates",
"javaType": "ch.fritteli.maze.generator.json.JsonCoordinates",
"additionalProperties": false,
"required": [
"x",

View file

@ -1,36 +0,0 @@
package ch.fritteli.labyrinth.generator.serialization.v1;
import ch.fritteli.labyrinth.generator.Generator;
import ch.fritteli.labyrinth.generator.model.Labyrinth;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class SerializerDeserializerV1Test {
@Test
void testSerializeDeserializeTiny() {
final Labyrinth expected = new Labyrinth(2, 2, 255);
new Generator(expected).run();
final byte[] bytes = SerializerDeserializerV1.serialize(expected);
final Labyrinth result = SerializerDeserializerV1.deserialize(bytes);
assertThat(result).isEqualTo(expected);
}
@Test
void testSerializeDeserializeMedium() {
final Labyrinth expected = new Labyrinth(20, 20, -271828182846L);
new Generator(expected).run();
final byte[] bytes = SerializerDeserializerV1.serialize(expected);
final Labyrinth result = SerializerDeserializerV1.deserialize(bytes);
assertThat(result).isEqualTo(expected);
}
@Test
void testSerializeDeserializeLarge() {
final Labyrinth expected = new Labyrinth(200, 320, 3141592653589793238L);
new Generator(expected).run();
final byte[] bytes = SerializerDeserializerV1.serialize(expected);
final Labyrinth result = SerializerDeserializerV1.deserialize(bytes);
assertThat(result).isEqualTo(expected);
}
}

View file

@ -1,5 +1,6 @@
package ch.fritteli.labyrinth.generator.model;
package ch.fritteli.maze.generator.model;
import ch.fritteli.maze.generator.model.Direction;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;

View file

@ -1,29 +1,29 @@
package ch.fritteli.labyrinth.generator.model;
package ch.fritteli.maze.generator.model;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
class LabyrinthTest {
class MazeTest {
@Test
void testConstruct() {
// act / assert on simple cases
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> new Labyrinth(0, 0))
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> new Maze(0, 0))
.withMessage("width and height must be >1");
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> new Labyrinth(0, 0, 0))
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> new Maze(0, 0, 0))
.withMessage("width and height must be >1");
// now for the real work:
// arrange
final Labyrinth sut = new Labyrinth(2, 3, 5);
final Maze sut = new Maze(2, 3, 5);
// assert
assertThat(sut)
.returns(2, Labyrinth::getWidth)
.returns(3, Labyrinth::getHeight)
.returns(5L, Labyrinth::getRandomSeed)
.returns(new Position(0, 0), Labyrinth::getStart)
.returns(new Position(1, 2), Labyrinth::getEnd);
.returns(2, Maze::getWidth)
.returns(3, Maze::getHeight)
.returns(5L, Maze::getRandomSeed)
.returns(new Position(0, 0), Maze::getStart)
.returns(new Position(1, 2), Maze::getEnd);
}
}

View file

@ -1,5 +1,7 @@
package ch.fritteli.labyrinth.generator.model;
package ch.fritteli.maze.generator.model;
import ch.fritteli.maze.generator.model.Direction;
import ch.fritteli.maze.generator.model.Position;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;

View file

@ -1,5 +1,7 @@
package ch.fritteli.labyrinth.generator.model;
package ch.fritteli.maze.generator.model;
import ch.fritteli.maze.generator.model.Direction;
import ch.fritteli.maze.generator.model.Tile;
import io.vavr.control.Option;
import org.junit.jupiter.api.Test;

View file

@ -1,5 +1,7 @@
package ch.fritteli.labyrinth.generator.model;
package ch.fritteli.maze.generator.model;
import ch.fritteli.maze.generator.model.Direction;
import ch.fritteli.maze.generator.model.Walls;
import io.vavr.collection.Stream;
import org.junit.jupiter.api.Test;

View file

@ -1,5 +1,6 @@
package ch.fritteli.labyrinth.generator.renderer.text;
package ch.fritteli.maze.generator.renderer.text;
import ch.fritteli.maze.generator.renderer.text.CharDefinition;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;

View file

@ -0,0 +1,36 @@
package ch.fritteli.maze.generator.serialization.v1;
import ch.fritteli.maze.generator.algorithm.RandomDepthFirst;
import ch.fritteli.maze.generator.model.Maze;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class SerializerDeserializerV1Test {
@Test
void testSerializeDeserializeTiny() {
final Maze expected = new Maze(2, 2, 255);
new RandomDepthFirst(expected).run();
final byte[] bytes = SerializerDeserializerV1.serialize(expected);
final Maze result = SerializerDeserializerV1.deserialize(bytes);
assertThat(result).isEqualTo(expected);
}
@Test
void testSerializeDeserializeMedium() {
final Maze expected = new Maze(20, 20, -271828182846L);
new RandomDepthFirst(expected).run();
final byte[] bytes = SerializerDeserializerV1.serialize(expected);
final Maze result = SerializerDeserializerV1.deserialize(bytes);
assertThat(result).isEqualTo(expected);
}
@Test
void testSerializeDeserializeLarge() {
final Maze expected = new Maze(200, 320, 3141592653589793238L);
new RandomDepthFirst(expected).run();
final byte[] bytes = SerializerDeserializerV1.serialize(expected);
final Maze result = SerializerDeserializerV1.deserialize(bytes);
assertThat(result).isEqualTo(expected);
}
}