Compare commits

..

No commits in common. "e3bc8d8452e29f7f06565596da5aea401a205490" and "aa27d5f138c7cab02ae3084aa995a2b54e6af84d" have entirely different histories.

34 changed files with 290 additions and 279 deletions

View file

@ -3,7 +3,7 @@ type: docker
name: default
steps:
- name: build
image: maven:3.9-eclipse-temurin-21
image: maven:3.8-openjdk-18-slim
commands:
- mvn clean install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
when:
@ -13,7 +13,7 @@ steps:
- refs/head/feature/**
- refs/tags/**
- name: test
image: maven:3.9-eclipse-temurin-21
image: maven:3.8-openjdk-18-slim
commands:
- mvn test -B
when:
@ -22,7 +22,7 @@ steps:
- master
- feature/*
- name: deploy
image: maven:3.9-eclipse-temurin-21
image: maven:3.8-openjdk-18-slim
environment:
REPO_TOKEN:
from_secret: repo-token

13
pom.xml
View file

@ -46,6 +46,7 @@
<tag>HEAD</tag>
</scm>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
@ -55,22 +56,16 @@
</distributionManagement>
<properties>
<assertj.version>3.25.3</assertj.version>
<jackson.version>2.17.1</jackson.version>
<jackson.version>2.15.0</jackson.version>
<jsonschema2pojo-maven-plugin.version>1.2.1</jsonschema2pojo-maven-plugin.version>
<logback.version>1.5.6</logback.version>
<lombok.version>1.18.32</lombok.version>
<maven.compiler.release>21</maven.compiler.release>
<maven-site-plugin.version>4.0.0-M8</maven-site-plugin.version>
<pdfbox.version>3.0.2</pdfbox.version>
<slf4j.version>2.0.12</slf4j.version>
<pdfbox.version>2.0.28</pdfbox.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
@ -112,7 +107,7 @@
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<version>3.24.2</version>
<scope>test</scope>
</dependency>
</dependencies>

View file

@ -9,18 +9,17 @@ 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 lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import java.nio.file.Path;
import java.nio.file.Paths;
import lombok.NonNull;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@UtilityClass
public class Main {
public static void main(@NotNull final String[] args) {
public static void main(@NonNull final String[] args) {
final int width = 20;
final int height = 30;
final Maze maze = new Maze(width, height/*, 0*/);
@ -59,7 +58,7 @@ public class Main {
log.info("PDF rendering to file:\n{}", pdfFileRenderer.render(maze));
}
private static String getBaseFilename(@NotNull final Maze maze) {
private static String getBaseFilename(@NonNull final Maze maze) {
return "maze-" + maze.getWidth() + "x" + maze.getHeight() + "-" + maze.getRandomSeed();
}
}

View file

@ -5,22 +5,21 @@ 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 org.jetbrains.annotations.NotNull;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Random;
import lombok.NonNull;
public class RandomDepthFirst {
@NotNull
@NonNull
private final Maze maze;
@NotNull
@NonNull
private final Random random;
@NotNull
@NonNull
private final Deque<Position> positions = new LinkedList<>();
public RandomDepthFirst(@NotNull final Maze maze) {
public RandomDepthFirst(@NonNull final Maze maze) {
this.maze = maze;
this.random = new Random(maze.getRandomSeed());
}
@ -35,16 +34,16 @@ public class RandomDepthFirst {
final Position end = this.maze.getEnd();
final Tile endTile = this.maze.getEndTile();
if (end.y() == 0) {
if (end.getY() == 0) {
endTile.enableDiggingToOrFrom(Direction.TOP);
endTile.digFrom(Direction.TOP);
} else if (end.x() == 0) {
} else if (end.getX() == 0) {
endTile.enableDiggingToOrFrom(Direction.LEFT);
endTile.digFrom(Direction.LEFT);
} else if (end.y() == this.maze.getHeight() - 1) {
} else if (end.getY() == this.maze.getHeight() - 1) {
endTile.enableDiggingToOrFrom(Direction.BOTTOM);
endTile.digFrom(Direction.BOTTOM);
} else if (end.x() == this.maze.getWidth() - 1) {
} else if (end.getX() == this.maze.getWidth() - 1) {
endTile.enableDiggingToOrFrom(Direction.RIGHT);
endTile.digFrom(Direction.RIGHT);
}
@ -87,16 +86,16 @@ public class RandomDepthFirst {
final Position start = this.maze.getStart();
final Tile startTile = this.maze.getStartTile();
if (start.y() == 0) {
if (start.getY() == 0) {
startTile.enableDiggingToOrFrom(Direction.TOP);
startTile.digTo(Direction.TOP);
} else if (start.x() == 0) {
} else if (start.getX() == 0) {
startTile.enableDiggingToOrFrom(Direction.LEFT);
startTile.digTo(Direction.LEFT);
} else if (start.y() == this.maze.getHeight() - 1) {
} else if (start.getY() == this.maze.getHeight() - 1) {
startTile.enableDiggingToOrFrom(Direction.BOTTOM);
startTile.digTo(Direction.BOTTOM);
} else if (start.x() == this.maze.getWidth() - 1) {
} else if (start.getX() == this.maze.getWidth() - 1) {
startTile.enableDiggingToOrFrom(Direction.RIGHT);
startTile.digTo(Direction.RIGHT);
}

View file

@ -7,11 +7,17 @@ public enum Direction {
LEFT;
public Direction invert() {
return switch (this) {
case TOP -> BOTTOM;
case RIGHT -> LEFT;
case BOTTOM -> TOP;
case LEFT -> RIGHT;
};
switch (this) {
case TOP:
return BOTTOM;
case RIGHT:
return LEFT;
case BOTTOM:
return TOP;
case LEFT:
return RIGHT;
default:
throw new IllegalStateException("Programming error: Not all enum values covered in enum Direction#invert()!");
}
}
}

View file

@ -3,8 +3,8 @@ package ch.fritteli.maze.generator.model;
import io.vavr.control.Option;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;
import org.jetbrains.annotations.NotNull;
@EqualsAndHashCode
@ToString
@ -26,7 +26,7 @@ public class Maze {
this(width, height, System.nanoTime());
}
public Maze(final int width, final int height, @NotNull final Position start, @NotNull 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);
}
@ -34,17 +34,17 @@ public class Maze {
this(width, height, randomSeed, new Position(0, 0), new Position(width - 1, height - 1));
}
public Maze(final int width, final int height, final long randomSeed, @NotNull final Position start, @NotNull 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");
}
if (start.equals(end)) {
throw new IllegalArgumentException("'start' must not be equal to 'end'");
}
if (start.x() != 0 && start.x() != width - 1 && start.y() != 0 && start.y() != height - 1) {
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 maze");
}
if (end.x() != 0 && end.x() != width - 1 && end.y() != 0 && end.y() != height - 1) {
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 maze");
}
this.width = width;
@ -59,7 +59,7 @@ public class Maze {
/**
* INTERNAL API. Exists only for deserialization. Not to be called from user code.
*/
private Maze(@NotNull 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 Maze {
/**
* INTERNAL API. Exists only for deserialization. Not to be called from user code.
*/
private Maze(@NotNull final Tile[][] field, final int width, final int height, @NotNull final Position start, @NotNull 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;
@ -80,12 +80,12 @@ public class Maze {
this.end = end;
}
@NotNull
public Option<Tile> getTileAt(@NotNull final Position position) {
return this.getTileAt(position.x(), position.y());
@NonNull
public Option<Tile> getTileAt(@NonNull final Position position) {
return this.getTileAt(position.getX(), position.getY());
}
@NotNull
@NonNull
public Option<Tile> getTileAt(final int x, final int y) {
if (x < 0 || y < 0 || x >= this.width || y >= this.height) {
return Option.none();
@ -93,12 +93,12 @@ public class Maze {
return Option.of(this.field[x][y]);
}
@NotNull
@NonNull
public Tile getStartTile() {
return this.getTileAt(this.start).get();
}
@NotNull
@NonNull
public Tile getEndTile() {
return this.getTileAt(this.end).get();
}
@ -114,7 +114,7 @@ public class Maze {
}
}
private void hardenWalls(@NotNull final Tile tile, final int x, final int y) {
private void hardenWalls(@NonNull final Tile tile, final int x, final int y) {
if (x == 0) {
tile.preventDiggingToOrFrom(Direction.LEFT);
}

View file

@ -1,16 +1,27 @@
package ch.fritteli.maze.generator.model;
import lombok.NonNull;
import lombok.Value;
import lombok.With;
import org.jetbrains.annotations.NotNull;
@Value
@With
public record Position(int x, int y) {
public Position move(@NotNull final Direction direction) {
return switch (direction) {
case BOTTOM -> this.withY(this.y + 1);
case LEFT -> this.withX(this.x - 1);
case RIGHT -> this.withX(this.x + 1);
case TOP -> this.withY(this.y - 1);
};
public class Position {
int x;
int y;
public Position move(@NonNull final Direction direction) {
switch (direction) {
case BOTTOM:
return this.withY(this.y + 1);
case LEFT:
return this.withX(this.x - 1);
case RIGHT:
return this.withX(this.x + 1);
case TOP:
return this.withY(this.y - 1);
default:
throw new IllegalStateException("Programming error: Not all Direction enum values covered in Position#move(Direction)!");
}
}
}

View file

@ -5,9 +5,9 @@ import io.vavr.control.Option;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;
import lombok.experimental.FieldDefaults;
import org.jetbrains.annotations.NotNull;
import java.util.EnumSet;
import java.util.Random;
@ -32,7 +32,7 @@ public class Tile {
* @param walls
* @param solution
*/
private Tile(@NotNull final EnumSet<Direction> walls, final boolean solution) {
private Tile(@NonNull final EnumSet<Direction> walls, final boolean solution) {
for (final Direction direction : walls) {
this.walls.set(direction);
this.walls.harden(direction);
@ -41,15 +41,15 @@ public class Tile {
this.solution = solution;
}
public void preventDiggingToOrFrom(@NotNull final Direction direction) {
public void preventDiggingToOrFrom(@NonNull final Direction direction) {
this.walls.harden(direction);
}
public void enableDiggingToOrFrom(@NotNull final Direction direction) {
public void enableDiggingToOrFrom(@NonNull final Direction direction) {
this.walls.unharden(direction);
}
public boolean digFrom(@NotNull final Direction direction) {
public boolean digFrom(@NonNull final Direction direction) {
if (this.visited) {
return false;
}
@ -57,15 +57,15 @@ public class Tile {
return this.walls.clear(direction);
}
public boolean digTo(@NotNull final Direction direction) {
public boolean digTo(@NonNull final Direction direction) {
return this.walls.clear(direction);
}
public void undigTo(@NotNull final Direction direction) {
public void undigTo(@NonNull final Direction direction) {
this.walls.set(direction);
}
public Option<Direction> getRandomAvailableDirection(@NotNull final Random random) {
public Option<Direction> getRandomAvailableDirection(@NonNull final Random random) {
final Stream<Direction> availableDirections = this.walls.getUnhardenedSet();
if (availableDirections.isEmpty()) {
return Option.none();
@ -74,7 +74,7 @@ public class Tile {
return Option.of(availableDirections.get(index));
}
public boolean hasWallAt(@NotNull final Direction direction) {
public boolean hasWallAt(@NonNull final Direction direction) {
return this.walls.isSet(direction);
}

View file

@ -2,8 +2,8 @@ package ch.fritteli.maze.generator.model;
import io.vavr.collection.Stream;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import lombok.ToString;
import org.jetbrains.annotations.NotNull;
import java.util.EnumSet;
import java.util.HashSet;
@ -17,7 +17,7 @@ public class Walls {
private final SortedSet<Direction> directions = new TreeSet<>();
private final Set<Direction> hardened = new HashSet<>();
public void set(@NotNull final Direction direction) {
public void set(@NonNull final Direction direction) {
this.directions.add(direction);
}
@ -25,14 +25,14 @@ public class Walls {
this.directions.addAll(EnumSet.allOf(Direction.class));
}
public boolean clear(@NotNull final Direction direction) {
public boolean clear(@NonNull final Direction direction) {
if (this.hardened.contains(direction)) {
return false;
}
return this.directions.remove(direction);
}
public boolean isSet(@NotNull final Direction direction) {
public boolean isSet(@NonNull final Direction direction) {
return this.directions.contains(direction);
}
@ -41,14 +41,14 @@ public class Walls {
.removeAll(this.hardened);
}
public void harden(@NotNull final Direction direction) {
public void harden(@NonNull final Direction direction) {
if (!this.directions.contains(direction)) {
throw new IllegalStateException("Trying to harden cleared Direction: " + direction);
}
this.hardened.add(direction);
}
public void unharden(@NotNull final Direction direction) {
public void unharden(@NonNull final Direction direction) {
this.hardened.remove(direction);
}
}

View file

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

View file

@ -6,8 +6,8 @@ import ch.fritteli.maze.generator.model.Tile;
import io.vavr.collection.HashSet;
import io.vavr.collection.Set;
import lombok.AccessLevel;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
class Generator {
@ -31,7 +31,7 @@ class Generator {
return sb.toString();
}
private Set<String> getClasses(@NotNull final Tile tile) {
private Set<String> getClasses(@NonNull final Tile tile) {
Set<String> result = HashSet.empty();
if (tile.hasWallAt(Direction.TOP)) {
result = result.add("top");

View file

@ -2,7 +2,7 @@ package ch.fritteli.maze.generator.renderer.html;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.renderer.Renderer;
import org.jetbrains.annotations.NotNull;
import lombok.NonNull;
public class HTMLRenderer implements Renderer<String> {
@ -112,14 +112,14 @@ public class HTMLRenderer implements Renderer<String> {
private HTMLRenderer() {
}
@NotNull
@NonNull
public static HTMLRenderer newInstance() {
return new HTMLRenderer();
}
@NotNull
@NonNull
@Override
public String render(@NotNull final Maze maze) {
public String render(@NonNull final Maze maze) {
if (maze.getWidth() == 0 || maze.getHeight() == 0) {
return this.getPreamble(maze) + POSTAMBLE;
}
@ -134,7 +134,7 @@ public class HTMLRenderer implements Renderer<String> {
return sb.toString();
}
private String getPreamble(@NotNull final Maze maze) {
private String getPreamble(@NonNull final Maze maze) {
return "<!DOCTYPE html><html lang=\"en\">" +
"<head>" +
"<title>Maze " + maze.getWidth() + "x" + maze.getHeight() + ", ID " + maze.getRandomSeed() + "</title>" +

View file

@ -5,8 +5,8 @@ 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;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
@ -17,9 +17,9 @@ import java.util.NoSuchElementException;
@Slf4j
public class HTMLFileRenderer implements Renderer<Path> {
@NotNull
@NonNull
private static final HTMLRenderer HTML_RENDERER = HTMLRenderer.newInstance();
@NotNull
@NonNull
private Option<Path> targetFile;
private HTMLFileRenderer() {
@ -29,7 +29,7 @@ public class HTMLFileRenderer implements Renderer<Path> {
.toOption();
}
@NotNull
@NonNull
public static HTMLFileRenderer newInstance() {
return new HTMLFileRenderer();
}
@ -40,15 +40,15 @@ public class HTMLFileRenderer implements Renderer<Path> {
.exists(File::canWrite);
}
@NotNull
public HTMLFileRenderer setTargetFile(@NotNull final Path targetFile) {
@NonNull
public HTMLFileRenderer setTargetFile(@NonNull final Path targetFile) {
this.targetFile = Option.of(targetFile);
return this;
}
@NotNull
@NonNull
@Override
public Path render(@NotNull final Maze maze) {
public Path render(@NonNull final Maze maze) {
if (!this.isTargetFileDefinedAndWritable()) {
try {
Files.createFile(this.targetFile.get());

View file

@ -7,8 +7,8 @@ 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 org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
@ -16,10 +16,10 @@ import java.util.List;
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
class Generator {
@NotNull
@NonNull
private final Maze maze;
@NotNull
@NonNull
JsonMaze generate() {
final JsonMaze result = new JsonMaze();
result.setId(String.valueOf(this.maze.getRandomSeed()));
@ -30,7 +30,7 @@ class Generator {
final ArrayList<JsonCell> row = new ArrayList<>();
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 maze has dimensions %dx%d"
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));
@ -46,12 +46,12 @@ class Generator {
}
result.setGrid(rows);
final JsonCoordinates start = new JsonCoordinates();
start.setX(this.maze.getStart().x());
start.setY(this.maze.getStart().y());
start.setX(this.maze.getStart().getX());
start.setY(this.maze.getStart().getY());
result.setStart(start);
final JsonCoordinates end = new JsonCoordinates();
end.setX(this.maze.getEnd().x());
end.setY(this.maze.getEnd().y());
end.setX(this.maze.getEnd().getX());
end.setY(this.maze.getEnd().getY());
result.setEnd(end);
return result;
}

View file

@ -7,14 +7,14 @@ 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 org.jetbrains.annotations.NotNull;
import lombok.NonNull;
import java.util.ArrayList;
import java.util.List;
public class JsonRenderer implements Renderer<String> {
@NotNull
@NonNull
private final ObjectMapper objectMapper;
private JsonRenderer() {
@ -22,12 +22,12 @@ public class JsonRenderer implements Renderer<String> {
.enable(SerializationFeature.INDENT_OUTPUT);
}
@NotNull
@NonNull
public static JsonRenderer newInstance() {
return new JsonRenderer();
}
@NotNull
@NonNull
private static JsonMaze createSingleCellMaze() {
// This is the only cell.
final JsonCell cell = new JsonCell();
@ -37,7 +37,7 @@ public class JsonRenderer implements Renderer<String> {
// Wrap that in a nested list.
final List<List<JsonCell>> rows = new ArrayList<>();
rows.add(new ArrayList<>());
rows.getFirst().add(cell);
rows.get(0).add(cell);
// Wrap it all in an instance of JsonMaze.
final JsonMaze jsonMaze = new JsonMaze();
jsonMaze.setId("0");
@ -45,8 +45,8 @@ public class JsonRenderer implements Renderer<String> {
return jsonMaze;
}
@NotNull
private String toString(@NotNull final JsonMaze jsonMaze) {
@NonNull
private String toString(@NonNull final JsonMaze jsonMaze) {
try {
return this.objectMapper.writeValueAsString(jsonMaze);
} catch (final JsonProcessingException e) {
@ -54,9 +54,9 @@ public class JsonRenderer implements Renderer<String> {
}
}
@NotNull
@NonNull
@Override
public String render(@NotNull final Maze maze) {
public String render(@NonNull final Maze maze) {
final JsonMaze jsonMaze;
if (maze.getWidth() == 0 || maze.getHeight() == 0) {
jsonMaze = createSingleCellMaze();

View file

@ -5,22 +5,21 @@ 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 lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.NoSuchElementException;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class JsonFileRenderer implements Renderer<Path> {
@NotNull
@NonNull
private static final JsonRenderer JSON_RENDERER = JsonRenderer.newInstance();
@NotNull
@NonNull
private Option<Path> targetFile;
private JsonFileRenderer() {
@ -30,7 +29,7 @@ public class JsonFileRenderer implements Renderer<Path> {
.toOption();
}
@NotNull
@NonNull
public static JsonFileRenderer newInstance() {
return new JsonFileRenderer();
}
@ -41,15 +40,15 @@ public class JsonFileRenderer implements Renderer<Path> {
.exists(File::canWrite);
}
@NotNull
public JsonFileRenderer setTargetFile(@NotNull final Path targetFile) {
@NonNull
public JsonFileRenderer setTargetFile(@NonNull final Path targetFile) {
this.targetFile = Option.of(targetFile);
return this;
}
@NotNull
@NonNull
@Override
public Path render(@NotNull final Maze maze) {
public Path render(@NonNull final Maze maze) {
if (!this.isTargetFileDefinedAndWritable()) {
try {
Files.createFile(this.targetFile.get());

View file

@ -5,6 +5,11 @@ 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;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
@ -13,21 +18,16 @@ import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@RequiredArgsConstructor
@Slf4j
class Generator {
@NotNull
@NonNull
private final Maze maze;
@NotNull
@NonNull
public ByteArrayOutputStream generate() {
final float pageWidth = this.maze.getWidth() * PDFRenderer.SCALE + 2 * PDFRenderer.MARGIN;
final float pageHeight = this.maze.getHeight() * PDFRenderer.SCALE + 2 * PDFRenderer.MARGIN;
@ -41,7 +41,7 @@ class Generator {
pdDocument.addPage(puzzlePage);
pdDocument.addPage(solutionPage);
try (final PDPageContentStream puzzlePageContentStream = new PDPageContentStream(pdDocument, puzzlePage);
final PDPageContentStream solutionPageContentStream = new PDPageContentStream(pdDocument, solutionPage)) {
final PDPageContentStream solutionPageContentStream = new PDPageContentStream(pdDocument, solutionPage)) {
setUpPageContentStream(puzzlePageContentStream);
setUpPageContentStream(solutionPageContentStream);
this.drawHorizontalLines(puzzlePageContentStream, solutionPageContentStream);
@ -60,7 +60,7 @@ class Generator {
return output;
}
private void setUpPageContentStream(@NotNull final PDPageContentStream pageContentStream) throws IOException {
private void setUpPageContentStream(@NonNull final PDPageContentStream pageContentStream) throws IOException {
pageContentStream.setLineCapStyle(BasicStroke.CAP_ROUND);
pageContentStream.setLineJoinStyle(BasicStroke.JOIN_ROUND);
pageContentStream.setLineWidth(1.0f);
@ -68,7 +68,7 @@ class Generator {
pageContentStream.setNonStrokingColor(Color.BLACK);
}
private void drawHorizontalLines(@NotNull final PDPageContentStream... contentStreams) throws IOException {
private void drawHorizontalLines(@NonNull final PDPageContentStream... contentStreams) throws IOException {
// 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.
@ -136,7 +136,7 @@ class Generator {
}
}
private void drawVerticalLines(@NotNull final PDPageContentStream... contentStreams) throws IOException {
private void drawVerticalLines(@NonNull final PDPageContentStream... contentStreams) throws IOException {
// 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.
@ -204,27 +204,27 @@ class Generator {
}
}
private void drawSolution(@NotNull final PDPageContentStream pageContentStream) throws IOException {
private void drawSolution(@NonNull final PDPageContentStream pageContentStream) throws IOException {
// 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.maze.getEnd();
Position currentPosition = this.maze.getStart();
Position previousPosition = null;
SolutionCoordinate coordinate = new SolutionCoordinate(currentPosition.x(), currentPosition.y());
SolutionCoordinate coordinate = new SolutionCoordinate(currentPosition.getX(), currentPosition.getY());
pageContentStream.moveTo(coordinate.getX(), coordinate.getY());
do {
Position newCurrent = this.findNextSolutionPosition(previousPosition, currentPosition);
previousPosition = currentPosition;
currentPosition = newCurrent;
coordinate = new SolutionCoordinate(currentPosition.x(), currentPosition.y());
coordinate = new SolutionCoordinate(currentPosition.getX(), currentPosition.getY());
pageContentStream.lineTo(coordinate.getX(), coordinate.getY());
} while (!currentPosition.equals(end));
pageContentStream.stroke();
}
@NotNull
private Position findNextSolutionPosition(@Nullable final Position previousPosition, @NotNull final Position currentPosition) {
@NonNull
private Position findNextSolutionPosition(@Nullable final Position previousPosition, @NonNull final Position currentPosition) {
final Tile currentTile = this.maze.getTileAt(currentPosition).get();
for (final Direction direction : Direction.values()) {
if (!currentTile.hasWallAt(direction)) {

View file

@ -2,10 +2,10 @@ package ch.fritteli.maze.generator.renderer.pdf;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.renderer.Renderer;
import org.jetbrains.annotations.NotNull;
import java.io.ByteArrayOutputStream;
import lombok.NonNull;
public class PDFRenderer implements Renderer<ByteArrayOutputStream> {
static final float MARGIN = 10;
static final float SCALE = 10;
@ -13,14 +13,14 @@ public class PDFRenderer implements Renderer<ByteArrayOutputStream> {
private PDFRenderer() {
}
@NotNull
@NonNull
public static PDFRenderer newInstance() {
return new PDFRenderer();
}
@NotNull
@NonNull
@Override
public ByteArrayOutputStream render(@NotNull final Maze maze) {
public ByteArrayOutputStream render(@NonNull final Maze maze) {
final Generator generator = new Generator(maze);
return generator.generate();
}

View file

@ -5,22 +5,21 @@ 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 lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.NoSuchElementException;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class PDFFileRenderer implements Renderer<Path> {
@NotNull
@NonNull
private static final PDFRenderer PDF_RENDERER = PDFRenderer.newInstance();
@NotNull
@NonNull
private Option<Path> targetFile;
private PDFFileRenderer() {
@ -30,7 +29,7 @@ public class PDFFileRenderer implements Renderer<Path> {
.toOption();
}
@NotNull
@NonNull
public static PDFFileRenderer newInstance() {
return new PDFFileRenderer();
}
@ -41,15 +40,15 @@ public class PDFFileRenderer implements Renderer<Path> {
.exists(File::canWrite);
}
@NotNull
public PDFFileRenderer setTargetFile(@NotNull final Path targetFile) {
@NonNull
public PDFFileRenderer setTargetFile(@NonNull final Path targetFile) {
this.targetFile = Option.of(targetFile);
return this;
}
@NotNull
@NonNull
@Override
public Path render(@NotNull final Maze maze) {
public Path render(@NonNull final Maze maze) {
if (!this.isTargetFileDefinedAndWritable()) {
try {
Files.createFile(this.targetFile.get());

View file

@ -5,12 +5,12 @@ 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;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
class Generator {
@NotNull
@NonNull
private final Maze maze;
private final boolean renderSolution;
private int x = 0;
@ -34,12 +34,21 @@ class Generator {
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 -> this.renderTopLine(currentTile, leftTile, topTile);
case 1 -> this.renderCenterLine(currentTile, topTile, rightTile, bottomTile, leftTile);
case 2 -> this.renderBottomLine(currentTile, leftTile);
default -> "";
};
final String s;
switch (this.line) {
case 0:
s = this.renderTopLine(currentTile, leftTile, topTile);
break;
case 1:
s = this.renderCenterLine(currentTile, topTile, rightTile, bottomTile, leftTile);
break;
case 2:
s = this.renderBottomLine(currentTile, leftTile);
break;
default:
s = "";
break;
}
this.prepareNextStep();
return s;
}
@ -67,7 +76,7 @@ class Generator {
}
}
private String renderTopLine(@NotNull final Tile currentTile, @NotNull final Option<Tile> leftTile, @NotNull final Option<Tile> topTile) {
private String renderTopLine(@NonNull final Tile currentTile, @NonNull final Option<Tile> leftTile, @NonNull final Option<Tile> topTile) {
final CharDefinition charDef1 = new CharDefinition();
final CharDefinition charDef2 = new CharDefinition();
final CharDefinition charDef3 = new CharDefinition();
@ -103,11 +112,11 @@ class Generator {
return result;
}
private String renderCenterLine(@NotNull final Tile currentTile,
@NotNull final Option<Tile> topTile,
@NotNull final Option<Tile> rightTile,
@NotNull final Option<Tile> bottomTile,
@NotNull final Option<Tile> leftTile) {
private String renderCenterLine(@NonNull final Tile currentTile,
@NonNull final Option<Tile> topTile,
@NonNull final Option<Tile> rightTile,
@NonNull final Option<Tile> bottomTile,
@NonNull final Option<Tile> leftTile) {
final CharDefinition charDef1 = new CharDefinition();
final CharDefinition charDef2 = new CharDefinition();
final CharDefinition charDef3 = new CharDefinition();
@ -151,7 +160,7 @@ class Generator {
return result;
}
private String renderBottomLine(@NotNull final Tile currentTile, @NotNull final Option<Tile> leftTile) {
private String renderBottomLine(@NonNull final Tile currentTile, @NonNull final Option<Tile> leftTile) {
String result;
final CharDefinition charDef1 = new CharDefinition();
final CharDefinition charDef2 = new CharDefinition();
@ -184,15 +193,15 @@ class Generator {
return result;
}
private boolean hasWallAt(@NotNull final Option<Tile> tile, @NotNull final Direction direction) {
private boolean hasWallAt(@NonNull final Option<Tile> tile, @NonNull final Direction direction) {
return tile.map(t -> t.hasWallAt(direction)).getOrElse(false);
}
private boolean isSolution(@NotNull final Tile tile) {
return this.renderSolution && tile.isSolution();
private boolean isSolution(@NonNull final Tile tile) {
return this.renderSolution && tile != null && tile.isSolution();
}
private boolean isSolution(@NotNull final Option<Tile> tile) {
private boolean isSolution(@NonNull final Option<Tile> tile) {
return this.renderSolution && tile.map(Tile::isSolution).getOrElse(false);
}
}

View file

@ -2,7 +2,7 @@ package ch.fritteli.maze.generator.renderer.text;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.renderer.Renderer;
import org.jetbrains.annotations.NotNull;
import lombok.NonNull;
public class TextRenderer implements Renderer<String> {
private boolean renderSolution;
@ -11,20 +11,20 @@ public class TextRenderer implements Renderer<String> {
this.renderSolution = false;
}
@NotNull
@NonNull
public static TextRenderer newInstance() {
return new TextRenderer();
}
@NotNull
@NonNull
public TextRenderer setRenderSolution(final boolean renderSolution) {
this.renderSolution = renderSolution;
return this;
}
@NotNull
@NonNull
@Override
public String render(@NotNull final Maze maze) {
public String render(@NonNull final Maze maze) {
if (maze.getWidth() == 0 || maze.getHeight() == 0) {
return "";
}

View file

@ -6,22 +6,23 @@ import ch.fritteli.maze.generator.renderer.text.TextRenderer;
import io.vavr.collection.List;
import io.vavr.control.Option;
import io.vavr.control.Try;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.NoSuchElementException;
@Slf4j
public class TextFileRenderer implements Renderer<List<Path>> {
@NotNull
@NonNull
private static final TextRenderer TEXT_RENDERER = TextRenderer.newInstance();
@NotNull
@NonNull
private Option<Path> targetMazeFile;
@NotNull
@NonNull
private Option<Path> targetSolutionFile;
private TextFileRenderer() {
@ -41,7 +42,7 @@ public class TextFileRenderer implements Renderer<List<Path>> {
.toOption();
}
@NotNull
@NonNull
public static TextFileRenderer newInstance() {
return new TextFileRenderer();
}
@ -58,21 +59,21 @@ public class TextFileRenderer implements Renderer<List<Path>> {
.exists(File::canWrite);
}
@NotNull
public TextFileRenderer setTargetMazeFile(@NotNull final Path targetMazeFile) {
@NonNull
public TextFileRenderer setTargetMazeFile(@NonNull final Path targetMazeFile) {
this.targetMazeFile = Option.of(targetMazeFile);
return this;
}
@NotNull
public TextFileRenderer setTargetSolutionFile(@NotNull final Path targetSolutionFile) {
@NonNull
public TextFileRenderer setTargetSolutionFile(@NonNull final Path targetSolutionFile) {
this.targetSolutionFile = Option.of(targetSolutionFile);
return this;
}
@NotNull
@NonNull
@Override
public List<Path> render(@NotNull final Maze maze) {
public List<Path> render(@NonNull final Maze maze) {
if (!this.isTargetMazeFileDefinedAndWritable()) {
try {
Files.createFile(this.targetMazeFile.get());
@ -97,12 +98,12 @@ public class TextFileRenderer implements Renderer<List<Path>> {
final Path targetMazeFile = this.targetMazeFile.get();
final Path targetSolutionFile = this.targetSolutionFile.get();
try {
Files.writeString(targetMazeFile, text);
Files.write(targetMazeFile, text.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
log.error("Failed writing to file " + targetMazeFile.normalize(), e);
}
try {
Files.writeString(targetSolutionFile, solution);
Files.write(targetSolutionFile, solution.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
log.error("Failed writing to file " + targetSolutionFile.normalize());
}

View file

@ -1,19 +1,18 @@
package ch.fritteli.maze.generator.serialization;
import ch.fritteli.maze.generator.model.Maze;
import org.jetbrains.annotations.NotNull;
import java.io.ByteArrayInputStream;
import lombok.NonNull;
public abstract class AbstractMazeInputStream extends ByteArrayInputStream {
public AbstractMazeInputStream(@NotNull final byte[] buf) {
public AbstractMazeInputStream(@NonNull final byte[] buf) {
super(buf);
}
public abstract void checkHeader();
@NotNull
@NonNull
public abstract Maze readMazeData();
public byte readByte() {

View file

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

View file

@ -3,11 +3,11 @@ package ch.fritteli.maze.generator.serialization.v1;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.model.Tile;
import ch.fritteli.maze.generator.serialization.AbstractMazeInputStream;
import org.jetbrains.annotations.NotNull;
import lombok.NonNull;
public class MazeInputStreamV1 extends AbstractMazeInputStream {
public MazeInputStreamV1(@NotNull final byte[] buf) {
public MazeInputStreamV1(@NonNull final byte[] buf) {
super(buf);
}
@ -27,7 +27,7 @@ public class MazeInputStreamV1 extends AbstractMazeInputStream {
}
}
@NotNull
@NonNull
@Override
public Maze readMazeData() {
final long randomSeed = this.readLong();

View file

@ -3,7 +3,7 @@ package ch.fritteli.maze.generator.serialization.v1;
import ch.fritteli.maze.generator.model.Maze;
import ch.fritteli.maze.generator.model.Tile;
import ch.fritteli.maze.generator.serialization.AbstractMazeOutputStream;
import org.jetbrains.annotations.NotNull;
import lombok.NonNull;
public class MazeOutputStreamV1 extends AbstractMazeOutputStream {
@ -15,7 +15,7 @@ public class MazeOutputStreamV1 extends AbstractMazeOutputStream {
}
@Override
public void writeMazeData(@NotNull final Maze maze) {
public void writeMazeData(@NonNull final Maze maze) {
final long randomSeed = maze.getRandomSeed();
final int width = maze.getWidth();
final int height = maze.getHeight();

View file

@ -3,8 +3,8 @@ package ch.fritteli.maze.generator.serialization.v1;
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;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
@ -62,8 +62,8 @@ public class SerializerDeserializerV1 {
* @param maze The {@link Maze} to be serialized.
* @return The resulting byte array.
*/
@NotNull
public byte[] serialize(@NotNull final Maze maze) {
@NonNull
public byte[] serialize(@NonNull final Maze maze) {
final MazeOutputStreamV1 stream = new MazeOutputStreamV1();
stream.writeHeader();
stream.writeMazeData(maze);
@ -76,38 +76,38 @@ public class SerializerDeserializerV1 {
* @param bytes The byte array to be deserialized.
* @return An instance of {@link Maze}.
*/
@NotNull
public Maze deserialize(@NotNull final byte[] bytes) {
@NonNull
public Maze deserialize(@NonNull final byte[] bytes) {
final MazeInputStreamV1 stream = new MazeInputStreamV1(bytes);
stream.checkHeader();
return stream.readMazeData();
}
@NotNull
Maze createMaze(@NotNull final Tile[][] field, final int width, final int height, final long randomSeed) {
@NonNull
Maze createMaze(@NonNull final Tile[][] field, final int width, final int height, final long randomSeed) {
try {
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 (@NotNull final NoSuchMethodException | IllegalAccessException | InstantiationException |
} catch (@NonNull final NoSuchMethodException | IllegalAccessException | InstantiationException |
InvocationTargetException e) {
throw new RuntimeException("Can not deserialize Maze from maze data.", e);
}
}
@NotNull
private Tile createTile(@NotNull final EnumSet<Direction> walls, boolean solution) {
@NonNull
private Tile createTile(@NonNull final EnumSet<Direction> walls, boolean solution) {
try {
final Constructor<Tile> constructor = Tile.class.getDeclaredConstructor(EnumSet.class, Boolean.TYPE);
constructor.setAccessible(true);
return constructor.newInstance(walls, solution);
} catch (@NotNull final NoSuchMethodException | InstantiationException | IllegalAccessException |
} catch (@NonNull final NoSuchMethodException | InstantiationException | IllegalAccessException |
InvocationTargetException e) {
throw new RuntimeException("Can not deserialize Tile from maze data.", e);
}
}
byte getBitmaskForTile(@NotNull final Tile tile) {
byte getBitmaskForTile(@NonNull final Tile tile) {
byte bitmask = 0;
if (tile.hasWallAt(Direction.TOP)) {
bitmask |= TOP_BIT;
@ -127,7 +127,7 @@ public class SerializerDeserializerV1 {
return bitmask;
}
@NotNull
@NonNull
Tile getTileForBitmask(final byte bitmask) {
final EnumSet<Direction> walls = EnumSet.noneOf(Direction.class);
if ((bitmask & TOP_BIT) == TOP_BIT) {

View file

@ -4,11 +4,11 @@ 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 org.jetbrains.annotations.NotNull;
import lombok.NonNull;
public class MazeInputStreamV2 extends AbstractMazeInputStream {
public MazeInputStreamV2(@NotNull final byte[] buf) {
public MazeInputStreamV2(@NonNull final byte[] buf) {
super(buf);
}
@ -31,7 +31,7 @@ public class MazeInputStreamV2 extends AbstractMazeInputStream {
}
}
@NotNull
@NonNull
@Override
public Maze readMazeData() {
// 03..06 width (int)

View file

@ -4,7 +4,7 @@ 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 org.jetbrains.annotations.NotNull;
import lombok.NonNull;
public class MazeOutputStreamV2 extends AbstractMazeOutputStream {
@ -19,7 +19,7 @@ public class MazeOutputStreamV2 extends AbstractMazeOutputStream {
}
@Override
public void writeMazeData(@NotNull final Maze maze) {
public void writeMazeData(@NonNull final Maze maze) {
// 03..06 width (int)
// 07..10 height (int)
// 11..14 start-x (int)
@ -35,10 +35,10 @@ public class MazeOutputStreamV2 extends AbstractMazeOutputStream {
final Position end = maze.getEnd();
this.writeInt(width);
this.writeInt(height);
this.writeInt(start.x());
this.writeInt(start.y());
this.writeInt(end.x());
this.writeInt(end.y());
this.writeInt(start.getX());
this.writeInt(start.getY());
this.writeInt(end.getX());
this.writeInt(end.getY());
this.writeLong(randomSeed);
for (int y = 0; y < height; y++) {

View file

@ -4,8 +4,8 @@ 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 org.jetbrains.annotations.NotNull;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
@ -67,8 +67,8 @@ public class SerializerDeserializerV2 {
* @param maze The {@link Maze} to be serialized.
* @return The resulting byte array.
*/
@NotNull
public byte[] serialize(@NotNull final Maze maze) {
@NonNull
public byte[] serialize(@NonNull final Maze maze) {
final MazeOutputStreamV2 stream = new MazeOutputStreamV2();
stream.writeHeader();
stream.writeMazeData(maze);
@ -81,38 +81,38 @@ public class SerializerDeserializerV2 {
* @param bytes The byte array to be deserialized.
* @return An instance of {@link Maze}.
*/
@NotNull
public Maze deserialize(@NotNull final byte[] bytes) {
@NonNull
public Maze deserialize(@NonNull final byte[] bytes) {
final MazeInputStreamV2 stream = new MazeInputStreamV2(bytes);
stream.checkHeader();
return stream.readMazeData();
}
@NotNull
Maze createMaze(@NotNull final Tile[][] field, final int width, final int height, @NotNull final Position start, @NotNull final Position end, final long randomSeed) {
@NonNull
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<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 (@NotNull final NoSuchMethodException | IllegalAccessException | InstantiationException |
} catch (@NonNull final NoSuchMethodException | IllegalAccessException | InstantiationException |
InvocationTargetException e) {
throw new RuntimeException("Can not deserialize Maze from maze data.", e);
}
}
@NotNull
private Tile createTile(@NotNull final EnumSet<Direction> walls, boolean solution) {
@NonNull
private Tile createTile(@NonNull final EnumSet<Direction> walls, boolean solution) {
try {
final Constructor<Tile> constructor = Tile.class.getDeclaredConstructor(EnumSet.class, Boolean.TYPE);
constructor.setAccessible(true);
return constructor.newInstance(walls, solution);
} catch (@NotNull final NoSuchMethodException | InstantiationException | IllegalAccessException |
} catch (@NonNull final NoSuchMethodException | InstantiationException | IllegalAccessException |
InvocationTargetException e) {
throw new RuntimeException("Can not deserialize Tile from maze data.", e);
}
}
byte getBitmaskForTile(@NotNull final Tile tile) {
byte getBitmaskForTile(@NonNull final Tile tile) {
byte bitmask = 0;
if (tile.hasWallAt(Direction.TOP)) {
bitmask |= TOP_BIT;
@ -132,7 +132,7 @@ public class SerializerDeserializerV2 {
return bitmask;
}
@NotNull
@NonNull
Tile getTileForBitmask(final byte bitmask) {
final EnumSet<Direction> walls = EnumSet.noneOf(Direction.class);
if ((bitmask & TOP_BIT) == TOP_BIT) {

View file

@ -1,12 +1,13 @@
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;
class DirectionTest {
@Test
void shouldInvertTheDirection() {
void invert() {
assertThat(Direction.TOP.invert()).isEqualTo(Direction.BOTTOM);
assertThat(Direction.RIGHT.invert()).isEqualTo(Direction.LEFT);
assertThat(Direction.BOTTOM.invert()).isEqualTo(Direction.TOP);

View file

@ -7,24 +7,14 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
class MazeTest {
@Test
void shouldNotAccept0AsWidthOrHeight() {
// act / assert
void testConstruct() {
// act / assert on simple cases
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> new Maze(0, 0))
.withMessage("width and height must be >1");
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> new Maze(0, 0, 0))
.withMessage("width and height must be >1");
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> new Maze(0, 5))
.withMessage("width and height must be >1");
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> new Maze(0, 5, 0))
.withMessage("width and height must be >1");
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> new Maze(5, 0))
.withMessage("width and height must be >1");
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> new Maze(5, 0, 0))
.withMessage("width and height must be >1");
}
@Test
void testConstruct() {
// now for the real work:
// arrange
final Maze sut = new Maze(2, 3, 5);
@ -33,9 +23,7 @@ class MazeTest {
.returns(2, Maze::getWidth)
.returns(3, Maze::getHeight)
.returns(5L, Maze::getRandomSeed)
.satisfies(
maze -> assertThat(maze.getStart()).isEqualTo(new Position(0, 0)),
maze -> assertThat(maze.getEnd()).isEqualTo(new Position(1, 2))
);
.returns(new Position(0, 0), Maze::getStart)
.returns(new Position(1, 2), Maze::getEnd);
}
}

View file

@ -1,12 +1,14 @@
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;
class PositionTest {
@Test
void shouldMoveToCorrectNeighborPosition() {
void move() {
// arrange
final Position sut = new Position(0, 0);

View file

@ -1,5 +1,7 @@
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;
@ -9,20 +11,21 @@ import static org.assertj.core.api.Assertions.assertThat;
class TileTest {
@Test
void shouldConstructATileWithDefaultValues() {
void testConstruct() {
// arrange / act
final Tile sut = new Tile();
//assert
assertThat(sut.hasWallAt(Direction.TOP)).isTrue();
assertThat(sut.hasWallAt(Direction.RIGHT)).isTrue();
assertThat(sut.hasWallAt(Direction.BOTTOM)).isTrue();
assertThat(sut.hasWallAt(Direction.LEFT)).isTrue();
assertThat(sut.isSolution()).isFalse();
assertThat(sut)
.returns(true, v -> v.hasWallAt(Direction.TOP))
.returns(true, v -> v.hasWallAt(Direction.RIGHT))
.returns(true, v -> v.hasWallAt(Direction.BOTTOM))
.returns(true, v -> v.hasWallAt(Direction.LEFT))
.returns(false, Tile::isSolution);
}
@Test
void tileCanBeDugIntoOnlyOnce() {
void testDigFrom() {
// arrange
final Tile sut = new Tile();
@ -42,27 +45,29 @@ class TileTest {
}
@Test
void canDigToDirectionOnlyOnce() {
void testDigTo() {
// arrange
final Tile sut = new Tile();
// act / assert
assertThat(sut.digTo(Direction.TOP)).isTrue();
assertThat(sut.digTo(Direction.RIGHT)).isTrue();
assertThat(sut.digTo(Direction.BOTTOM)).isTrue();
assertThat(sut.digTo(Direction.LEFT)).isTrue();
// digging a second time does not succeed
assertThat(sut.digTo(Direction.LEFT)).isFalse();
assertThat(sut)
.returns(true, v -> v.digTo(Direction.TOP))
.returns(true, v -> v.digTo(Direction.RIGHT))
.returns(true, v -> v.digTo(Direction.BOTTOM))
.returns(true, v -> v.digTo(Direction.LEFT))
// digging a second time does not succeed
.returns(false, v -> v.digTo(Direction.LEFT));
// assert
assertThat(sut.hasWallAt(Direction.TOP)).isFalse();
assertThat(sut.hasWallAt(Direction.RIGHT)).isFalse();
assertThat(sut.hasWallAt(Direction.BOTTOM)).isFalse();
assertThat(sut.hasWallAt(Direction.LEFT)).isFalse();
assertThat(sut)
.returns(false, v -> v.hasWallAt(Direction.TOP))
.returns(false, v -> v.hasWallAt(Direction.RIGHT))
.returns(false, v -> v.hasWallAt(Direction.BOTTOM))
.returns(false, v -> v.hasWallAt(Direction.LEFT));
}
@Test
void canNotDigToOrFromDirectionWhenPrevented() {
void testPreventDiggingToOrFrom() {
// arrange
final Tile sut = new Tile();
@ -70,11 +75,10 @@ class TileTest {
sut.preventDiggingToOrFrom(Direction.LEFT);
// assert
assertThat(sut.digTo(Direction.LEFT)).isFalse();
assertThat(sut.digFrom(Direction.LEFT)).isFalse();
assertThat(sut.hasWallAt(Direction.LEFT)).isTrue();
// Digging in another direction is still permitted
assertThat(sut.digTo(Direction.RIGHT)).isTrue();
assertThat(sut)
.returns(false, v -> v.digTo(Direction.LEFT))
.returns(false, v -> v.digFrom(Direction.LEFT))
.returns(true, v -> v.hasWallAt(Direction.LEFT));
}
@Test