Compare commits
5 commits
master
...
feature/wi
Author | SHA1 | Date | |
---|---|---|---|
9599be21b3 | |||
3bf438ffb9 | |||
5a642b354b | |||
9579066cb5 | |||
cdfa1f4476 |
26 changed files with 1066 additions and 310 deletions
|
@ -0,0 +1,20 @@
|
|||
package ch.fritteli.maze.generator.algorithm;
|
||||
|
||||
import ch.fritteli.maze.generator.model.Maze;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public abstract class AbstractMazeGeneratorAlgorithm implements MazeGeneratorAlgorithm {
|
||||
@NotNull
|
||||
protected final Maze maze;
|
||||
@NotNull
|
||||
protected final Random random;
|
||||
|
||||
|
||||
protected AbstractMazeGeneratorAlgorithm(@NotNull final Maze maze, @NotNull final String algorithmName) {
|
||||
this.maze = maze;
|
||||
this.random = new Random(maze.getRandomSeed());
|
||||
this.maze.setAlgorithm(algorithmName);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package ch.fritteli.maze.generator.algorithm;
|
||||
|
||||
public interface MazeGeneratorAlgorithm {
|
||||
void run();
|
||||
}
|
|
@ -10,20 +10,13 @@ import org.jetbrains.annotations.Nullable;
|
|||
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Random;
|
||||
|
||||
public class RandomDepthFirst {
|
||||
|
||||
@NotNull
|
||||
private final Maze maze;
|
||||
@NotNull
|
||||
private final Random random;
|
||||
public class RandomDepthFirst extends AbstractMazeGeneratorAlgorithm {
|
||||
@NotNull
|
||||
private final Deque<Position> positions = new LinkedList<>();
|
||||
|
||||
public RandomDepthFirst(@NotNull final Maze maze) {
|
||||
this.maze = maze;
|
||||
this.random = new Random(maze.getRandomSeed());
|
||||
super(maze, "Random Depth First");
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
|
312
src/main/java/ch/fritteli/maze/generator/algorithm/Wilson.java
Normal file
312
src/main/java/ch/fritteli/maze/generator/algorithm/Wilson.java
Normal file
|
@ -0,0 +1,312 @@
|
|||
package ch.fritteli.maze.generator.algorithm;
|
||||
|
||||
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.Tuple2;
|
||||
import io.vavr.collection.Iterator;
|
||||
import io.vavr.collection.Seq;
|
||||
import io.vavr.collection.Stream;
|
||||
import io.vavr.collection.Vector;
|
||||
import io.vavr.control.Option;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* An implementation of <a href="https://en.wikipedia.org/wiki/Maze_generation_algorithm#Wilson's_algorithm">Wilson's Algorithm</a>.
|
||||
*/
|
||||
public class Wilson extends AbstractMazeGeneratorAlgorithm {
|
||||
public Wilson(@NotNull final Maze maze) {
|
||||
super(maze, "Wilson");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
final MyMaze myMaze = new MyMaze(this.maze.getWidth(), this.maze.getHeight());
|
||||
// 1. Initialization: pick random location, add to maze
|
||||
final Position position = myMaze.getRandomAvailablePosition(this.random).get();
|
||||
myMaze.setPartOfMaze(position);
|
||||
|
||||
// 2. while locations-not-in-maze exist, repeat:
|
||||
|
||||
final List<MyMaze.Path> pths = new ArrayList<>();
|
||||
Position startPosition;
|
||||
while ((startPosition = myMaze.getRandomAvailablePosition(this.random).getOrNull()) != null) {
|
||||
final MyMaze.Path path = myMaze.createPath(startPosition);
|
||||
while (true) {
|
||||
final Position nextPosition = path.nextRandomPosition(this.random);
|
||||
if (path.contains(nextPosition)) {
|
||||
path.removeLoopUpTo(nextPosition);
|
||||
} else {
|
||||
path.append(nextPosition);
|
||||
if (myMaze.isPartOfMaze(nextPosition)) {
|
||||
myMaze.setPartOfMaze(path);
|
||||
pths.add(path);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
applyToMaze(pths, maze);
|
||||
solve(maze);
|
||||
// 1. pick random location not in maze
|
||||
// 2. perform random walk until you reach the maze (*)
|
||||
// 3. add path to maze
|
||||
// (*): random walk:
|
||||
// 1. advance in random direction
|
||||
// 2. if loop with current path is formed, remove loop, continue from last location before loop
|
||||
}
|
||||
|
||||
private void applyToMaze(@NotNull final List<MyMaze.Path> pths, @NotNull final Maze maze) {
|
||||
pths.forEach(path -> {
|
||||
final Iterator<Direction> moves = Stream.ofAll(path.path)
|
||||
.sliding(2)
|
||||
.flatMap(poss -> poss.head().getDirectionTo(poss.get(1)));
|
||||
Position position = path.path.getFirst();
|
||||
while (moves.hasNext()) {
|
||||
Tile tile = maze.getTileAt(position).get();
|
||||
final Direction move = moves.next();
|
||||
tile.digTo(move);
|
||||
position = position.move(move);
|
||||
maze.getTileAt(position).forEach(t -> t.digTo(move.invert()));
|
||||
}
|
||||
});
|
||||
Direction direction = determineDirectionForDigging(maze.getStart());
|
||||
Tile t = maze.getStartTile();
|
||||
this.digTo(t, direction);
|
||||
direction = determineDirectionForDigging(maze.getEnd());
|
||||
t = maze.getEndTile();
|
||||
this.digTo(t, direction);
|
||||
// seal all walls, mark all as visited
|
||||
for (int x = 0; x < maze.getWidth(); x++) {
|
||||
for (int y = 0; y < maze.getHeight(); y++) {
|
||||
maze.getTileAt(x, y).forEach(tile -> {
|
||||
Stream.of(Direction.values())
|
||||
.forEach(d -> {
|
||||
if (tile.hasWallAt(d)) {
|
||||
tile.preventDiggingToOrFrom(d);
|
||||
} else {
|
||||
tile.digFrom(d);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Direction determineDirectionForDigging(@NotNull final Position position) {
|
||||
if (position.y() == 0) {
|
||||
return Direction.TOP;
|
||||
}
|
||||
if (position.x() == 0) {
|
||||
return Direction.LEFT;
|
||||
}
|
||||
if (position.y() == this.maze.getHeight() - 1) {
|
||||
return Direction.BOTTOM;
|
||||
}
|
||||
if (position.x() == this.maze.getWidth() - 1) {
|
||||
return Direction.RIGHT;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void digTo(@NotNull final Tile tile, @NotNull final Direction direction) {
|
||||
tile.enableDiggingToOrFrom(direction);
|
||||
tile.digTo(direction);
|
||||
}
|
||||
|
||||
private void solve(@NotNull final Maze maze) {
|
||||
EnumSet<Direction>[][] remainingDirs = new EnumSet[maze.getWidth()][maze.getHeight()];
|
||||
for (int x = 0; x < remainingDirs.length; x++) {
|
||||
for (int y = 0; y < remainingDirs[x].length; y++) {
|
||||
remainingDirs[x][y] = EnumSet.allOf(Direction.class);
|
||||
}
|
||||
}
|
||||
Position p = maze.getStart();
|
||||
final Direction direction = this.determineDirectionForDigging(p);
|
||||
remainingDirs[p.x()][p.y()].remove(direction);
|
||||
LinkedList<Position> solution = new LinkedList<>();
|
||||
solution.add(p);
|
||||
while (!p.equals(maze.getEnd())) {
|
||||
final Tile tile = maze.getTileAt(p).get();
|
||||
EnumSet<Direction> dirs = remainingDirs[p.x()][p.y()];
|
||||
dirs.removeIf(tile::hasWallAt);
|
||||
if (dirs.isEmpty()) {
|
||||
solution.pop();
|
||||
p = solution.peek();
|
||||
} else {
|
||||
final Direction nextDir = dirs.iterator().next();
|
||||
final Position nextPos = p.move(nextDir);
|
||||
solution.push(nextPos);
|
||||
remainingDirs[p.x()][p.y()].remove(nextDir);
|
||||
remainingDirs[nextPos.x()][nextPos.y()].remove(nextDir.invert());
|
||||
p = nextPos;
|
||||
}
|
||||
}
|
||||
solution.forEach(s -> maze.getTileAt(s).forEach(Tile::setSolution));
|
||||
}
|
||||
|
||||
private static class MyMaze {
|
||||
private final int width;
|
||||
private final int height;
|
||||
private final boolean[][] partOfMaze;
|
||||
private final boolean[] completeColumns;
|
||||
|
||||
MyMaze(final int width, final int height) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.partOfMaze = new boolean[this.width][this.height];
|
||||
for (int x = 0; x < this.width; x++) {
|
||||
this.partOfMaze[x] = new boolean[this.height];
|
||||
}
|
||||
this.completeColumns = new boolean[this.width];
|
||||
}
|
||||
|
||||
boolean isPartOfMaze(final int x, final int y) {
|
||||
return this.partOfMaze[x][y];
|
||||
}
|
||||
|
||||
boolean isPartOfMaze(@NotNull final Position position) {
|
||||
return this.isPartOfMaze(position.x(), position.y());
|
||||
}
|
||||
|
||||
void setPartOfMaze(final int x, final int y) {
|
||||
this.partOfMaze[x][y] = true;
|
||||
this.checkCompleteColumn(x);
|
||||
}
|
||||
|
||||
void setPartOfMaze(@NotNull final Position position) {
|
||||
this.setPartOfMaze(position.x(), position.y());
|
||||
}
|
||||
|
||||
void setPartOfMaze(@NotNull final Path path) {
|
||||
path.path.forEach(this::setPartOfMaze);
|
||||
}
|
||||
|
||||
void checkCompleteColumn(final int x) {
|
||||
if (this.completeColumns[x]) {
|
||||
return;
|
||||
}
|
||||
for (int y = 0; y < this.height; y++) {
|
||||
if (!this.isPartOfMaze(x, y)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.completeColumns[x] = true;
|
||||
}
|
||||
|
||||
Option<Position> getRandomAvailablePosition(@NotNull final Random random) {
|
||||
final Seq<Integer> allowedColumns = Vector.ofAll(this.completeColumns)
|
||||
.zipWithIndex()
|
||||
.reject(Tuple2::_1)
|
||||
.map(Tuple2::_2);
|
||||
if (allowedColumns.isEmpty()) {
|
||||
return Option.none();
|
||||
}
|
||||
final int x = allowedColumns.get(random.nextInt(allowedColumns.size()));
|
||||
final boolean[] column = partOfMaze[x];
|
||||
|
||||
final Seq<Integer> allowedRows = Vector.ofAll(column)
|
||||
.zipWithIndex()
|
||||
.reject(Tuple2::_1)
|
||||
.map(Tuple2::_2);
|
||||
if (allowedRows.isEmpty()) {
|
||||
return Option.none();
|
||||
}
|
||||
final int y = allowedRows.get(random.nextInt(allowedRows.size()));
|
||||
return Option.some(new Position(x, y));
|
||||
}
|
||||
|
||||
public Path createPath(@NotNull final Position position) {
|
||||
return new Path(position);
|
||||
}
|
||||
|
||||
private class Path {
|
||||
@NotNull
|
||||
private final List<Position> path = new LinkedList<>();
|
||||
|
||||
Path(@NotNull final Position position) {
|
||||
this.path.add(position);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
Position nextRandomPosition(@NotNull final Random random) {
|
||||
final Direction direction = this.getRandomDirectionFromLastPosition(random);
|
||||
return this.path.getLast().move(direction);
|
||||
}
|
||||
|
||||
boolean contains(@NotNull final Position position) {
|
||||
return this.path.contains(position);
|
||||
}
|
||||
|
||||
void removeLoopUpTo(@NotNull final Position position) {
|
||||
while (!this.path.removeLast().equals(position)) {
|
||||
}
|
||||
this.path.add(position);
|
||||
}
|
||||
|
||||
public void append(@NotNull final Position nextPosition) {
|
||||
this.path.add(nextPosition);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Direction getRandomDirectionFromLastPosition(@NotNull final Random random) {
|
||||
final EnumSet<Direction> validDirections = this.getValidDirectionsFromLastPosition();
|
||||
if (validDirections.isEmpty()) {
|
||||
throw new IllegalStateException("WE MUST NOT GET HERE! analyze why it happened!!!");
|
||||
}
|
||||
if (validDirections.size() == 1) {
|
||||
return validDirections.iterator().next();
|
||||
}
|
||||
final Direction[] directionArray = validDirections.toArray(Direction[]::new);
|
||||
final int index = random.nextInt(directionArray.length);
|
||||
return directionArray[index];
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private EnumSet<Direction> getValidDirectionsFromLastPosition() {
|
||||
final Position fromPosition = this.path.getLast();
|
||||
final EnumSet<Direction> validDirections = EnumSet.allOf(Direction.class);
|
||||
if (this.path.size() > 1) {
|
||||
final Position prevP = this.path.get(this.path.size() - 2);
|
||||
fromPosition.getDirectionTo(prevP)
|
||||
.forEach(validDirections::remove);
|
||||
}
|
||||
boolean canLeft = fromPosition.x() > 0;
|
||||
boolean canRight = fromPosition.x() < width - 1;
|
||||
boolean canUp = fromPosition.y() > 0;
|
||||
boolean canDown = fromPosition.y() < height - 1;
|
||||
if (!canLeft) {
|
||||
validDirections.remove(Direction.LEFT);
|
||||
}
|
||||
if (!canRight) {
|
||||
validDirections.remove(Direction.RIGHT);
|
||||
}
|
||||
if (!canUp) {
|
||||
validDirections.remove(Direction.TOP);
|
||||
}
|
||||
if (!canDown) {
|
||||
validDirections.remove(Direction.BOTTOM);
|
||||
}
|
||||
return validDirections;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return Stream.ofAll(this.path)
|
||||
.map(position -> "(%s,%s)".formatted(position.x(), position.y()))
|
||||
.mkString("Path[", "->", "]");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,11 +1,14 @@
|
|||
package ch.fritteli.maze.generator.model;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public enum Direction {
|
||||
TOP,
|
||||
RIGHT,
|
||||
BOTTOM,
|
||||
LEFT;
|
||||
|
||||
@NotNull
|
||||
public Direction invert() {
|
||||
return switch (this) {
|
||||
case TOP -> BOTTOM;
|
||||
|
|
|
@ -3,6 +3,7 @@ package ch.fritteli.maze.generator.model;
|
|||
import io.vavr.control.Option;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
@ -21,6 +22,9 @@ public class Maze {
|
|||
private final Position start;
|
||||
@Getter
|
||||
private final Position end;
|
||||
@Getter
|
||||
@Setter
|
||||
private String algorithm;
|
||||
|
||||
public Maze(final int width, final int height) {
|
||||
this(width, height, System.nanoTime());
|
||||
|
@ -34,7 +38,11 @@ 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,
|
||||
@NotNull final Position start,
|
||||
@NotNull final Position end) {
|
||||
if (width <= 1 || height <= 1) {
|
||||
throw new IllegalArgumentException("width and height must be >1");
|
||||
}
|
||||
|
@ -71,7 +79,12 @@ 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(@NotNull final Tile[][] field,
|
||||
final int width,
|
||||
final int height,
|
||||
@NotNull final Position start,
|
||||
@NotNull final Position end,
|
||||
final long randomSeed) {
|
||||
this.field = field;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
@ -80,6 +93,25 @@ public class Maze {
|
|||
this.end = end;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,
|
||||
@NotNull final String algorithm) {
|
||||
this.field = field;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.randomSeed = randomSeed;
|
||||
this.algorithm = algorithm;
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Option<Tile> getTileAt(@NotNull final Position position) {
|
||||
return this.getTileAt(position.x(), position.y());
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package ch.fritteli.maze.generator.model;
|
||||
|
||||
import io.vavr.control.Option;
|
||||
import lombok.With;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@With
|
||||
public record Position(int x, int y) {
|
||||
@NotNull
|
||||
public Position move(@NotNull final Direction direction) {
|
||||
return switch (direction) {
|
||||
case BOTTOM -> this.withY(this.y + 1);
|
||||
|
@ -13,4 +15,20 @@ public record Position(int x, int y) {
|
|||
case TOP -> this.withY(this.y - 1);
|
||||
};
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Option<Direction> getDirectionTo(@NotNull final Position position) {
|
||||
final int xDiff = position.x - this.x;
|
||||
final int yDiff = position.y - this.y;
|
||||
return switch (xDiff) {
|
||||
case -1 -> Option.when(yDiff == 0, Direction.LEFT);
|
||||
case 0 -> switch (yDiff) {
|
||||
case -1 -> Option.some(Direction.TOP);
|
||||
case 1 -> Option.some(Direction.BOTTOM);
|
||||
default -> Option.none();
|
||||
};
|
||||
case 1 -> Option.when(yDiff == 0, Direction.RIGHT);
|
||||
default -> Option.none();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,108 +6,109 @@ import org.jetbrains.annotations.NotNull;
|
|||
|
||||
public class HTMLRenderer implements Renderer<String> {
|
||||
|
||||
private static final String POSTAMBLE = "<script>"
|
||||
+ "let userPath = [];"
|
||||
+ "const DIR_UNDEF = -1;"
|
||||
+ "const DIR_SAME = 0;"
|
||||
+ "const DIR_UP = 1;"
|
||||
+ "const DIR_RIGHT = 2;"
|
||||
+ "const DIR_DOWN = 3;"
|
||||
+ "const DIR_LEFT = 4;"
|
||||
+ "function getCoords(cell) {"
|
||||
+ " return {"
|
||||
+ " x: cell.cellIndex,"
|
||||
+ " y: cell.parentElement.rowIndex"
|
||||
+ " };"
|
||||
+ "}"
|
||||
+ "function distance(prev, next) {"
|
||||
+ " return Math.abs(prev.x - next.x) + Math.abs(prev.y - next.y);"
|
||||
+ "}"
|
||||
+ "function direction(prev, next) {"
|
||||
+ " const dist = distance(prev, next);"
|
||||
+ " if (dist === 0) {"
|
||||
+ " return DIR_SAME;"
|
||||
+ " }"
|
||||
+ " if (dist !== 1) {"
|
||||
+ " return DIR_UNDEF;"
|
||||
+ " }"
|
||||
+ " if (next.x === prev.x) {"
|
||||
+ " if (next.y === prev.y + 1) {"
|
||||
+ " return DIR_DOWN;"
|
||||
+ " }"
|
||||
+ " return DIR_UP;"
|
||||
+ " }"
|
||||
+ " if (next.x === prev.x + 1) {"
|
||||
+ " return DIR_RIGHT;"
|
||||
+ " }"
|
||||
+ " return DIR_LEFT;"
|
||||
+ "}"
|
||||
+ "(function () {"
|
||||
+ " const labyrinthTable = document.getElementById(\"labyrinth\");"
|
||||
+ " const labyrinthCells = labyrinthTable.getElementsByTagName(\"td\");"
|
||||
+ " const start = {x: 0, y: 0};"
|
||||
+ " const end = {"
|
||||
+ " x: labyrinthTable.getElementsByTagName(\"tr\")[0].getElementsByTagName(\"td\").length - 1,"
|
||||
+ " y: labyrinthTable.getElementsByTagName(\"tr\").length - 1"
|
||||
+ " };"
|
||||
+ " for (let i = 0; i < labyrinthCells.length; i++) {"
|
||||
+ " let cell = labyrinthCells.item(i);"
|
||||
+ " cell.onclick = (event) => {"
|
||||
+ " let target = event.target;"
|
||||
+ " const coords = getCoords(target);"
|
||||
+ " if (coords.x === end.x && coords.y === end.y) {"
|
||||
+ " alert(\"HOORAY! You did it! Congratulations!\")"
|
||||
+ " }"
|
||||
+ " if (userPath.length === 0) {"
|
||||
+ " if (coords.x === start.x && coords.y === start.y) {"
|
||||
+ " userPath.push(coords);"
|
||||
+ " target.classList.toggle(\"user\");"
|
||||
+ " }"
|
||||
+ " } else {"
|
||||
+ " const dir = direction(userPath[userPath.length - 1], coords);"
|
||||
+ " switch (dir) {"
|
||||
+ " case DIR_UNDEF:"
|
||||
+ " return;"
|
||||
+ " case DIR_SAME:"
|
||||
+ " userPath.pop();"
|
||||
+ " target.classList.toggle(\"user\");"
|
||||
+ " return;"
|
||||
+ " default:"
|
||||
+ " if (userPath.find(value => value.x === coords.x && value.y === coords.y)) {"
|
||||
+ " return;"
|
||||
+ " } else {"
|
||||
+ " switch (dir) {"
|
||||
+ " case DIR_UP:"
|
||||
+ " if (target.classList.contains(\"bottom\")) {"
|
||||
+ " return;"
|
||||
+ " }"
|
||||
+ " break;"
|
||||
+ " case DIR_RIGHT:"
|
||||
+ " if (target.classList.contains(\"left\")) {"
|
||||
+ " return;"
|
||||
+ " }"
|
||||
+ " break;"
|
||||
+ " case DIR_DOWN:"
|
||||
+ " if (target.classList.contains(\"top\")) {"
|
||||
+ " return;"
|
||||
+ " }"
|
||||
+ " break;"
|
||||
+ " case DIR_LEFT:"
|
||||
+ " if (target.classList.contains(\"right\")) {"
|
||||
+ " return;"
|
||||
+ " }"
|
||||
+ " break;"
|
||||
+ " }"
|
||||
+ " userPath.push(coords);"
|
||||
+ " target.classList.toggle(\"user\");"
|
||||
+ " return;"
|
||||
+ " }"
|
||||
+ " }"
|
||||
+ " }"
|
||||
+ " };"
|
||||
+ " }"
|
||||
+ "})();"
|
||||
+ "</script></body></html>";
|
||||
private static final String POSTAMBLE = """
|
||||
<script>
|
||||
let userPath = [];
|
||||
const DIR_UNDEF = -1;
|
||||
const DIR_SAME = 0;
|
||||
const DIR_UP = 1;
|
||||
const DIR_RIGHT = 2;
|
||||
const DIR_DOWN = 3;
|
||||
const DIR_LEFT = 4;
|
||||
function getCoords(cell) {
|
||||
return {
|
||||
x: cell.cellIndex,
|
||||
y: cell.parentElement.rowIndex
|
||||
};
|
||||
}
|
||||
function distance(prev, next) {
|
||||
return Math.abs(prev.x - next.x) + Math.abs(prev.y - next.y);
|
||||
}
|
||||
function direction(prev, next) {
|
||||
const dist = distance(prev, next);
|
||||
if (dist === 0) {
|
||||
return DIR_SAME;
|
||||
}
|
||||
if (dist !== 1) {
|
||||
return DIR_UNDEF;
|
||||
}
|
||||
if (next.x === prev.x) {
|
||||
if (next.y === prev.y + 1) {
|
||||
return DIR_DOWN;
|
||||
}
|
||||
return DIR_UP;
|
||||
}
|
||||
if (next.x === prev.x + 1) {
|
||||
return DIR_RIGHT;
|
||||
}
|
||||
return DIR_LEFT;
|
||||
}
|
||||
(function () {
|
||||
const labyrinthTable = document.getElementById("labyrinth");
|
||||
const labyrinthCells = labyrinthTable.getElementsByTagName("td");
|
||||
const start = {x: 0, y: 0};
|
||||
const end = {
|
||||
x: labyrinthTable.getElementsByTagName("tr")[0].getElementsByTagName("td").length - 1,
|
||||
y: labyrinthTable.getElementsByTagName("tr").length - 1
|
||||
};
|
||||
for (let i = 0; i < labyrinthCells.length; i++) {
|
||||
let cell = labyrinthCells.item(i);
|
||||
cell.onclick = (event) => {
|
||||
let target = event.target;
|
||||
const coords = getCoords(target);
|
||||
if (coords.x === end.x && coords.y === end.y) {
|
||||
alert("HOORAY! You did it! Congratulations!")
|
||||
}
|
||||
if (userPath.length === 0) {
|
||||
if (coords.x === start.x && coords.y === start.y) {
|
||||
userPath.push(coords);
|
||||
target.classList.toggle("user");
|
||||
}
|
||||
} else {
|
||||
const dir = direction(userPath[userPath.length - 1], coords);
|
||||
switch (dir) {
|
||||
case DIR_UNDEF:
|
||||
return;
|
||||
case DIR_SAME:
|
||||
userPath.pop();
|
||||
target.classList.toggle("user");
|
||||
return;
|
||||
default:
|
||||
if (userPath.find(value => value.x === coords.x && value.y === coords.y)) {
|
||||
return;
|
||||
} else {
|
||||
switch (dir) {
|
||||
case DIR_UP:
|
||||
if (target.classList.contains("bottom")) {
|
||||
return;
|
||||
}
|
||||
break;
|
||||
case DIR_RIGHT:
|
||||
if (target.classList.contains("left")) {
|
||||
return;
|
||||
}
|
||||
break;
|
||||
case DIR_DOWN:
|
||||
if (target.classList.contains("top")) {
|
||||
return;
|
||||
}
|
||||
break;
|
||||
case DIR_LEFT:
|
||||
if (target.classList.contains("right")) {
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
userPath.push(coords);
|
||||
target.classList.toggle("user");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
})();
|
||||
</script></body></html>""";
|
||||
|
||||
private HTMLRenderer() {
|
||||
}
|
||||
|
@ -135,33 +136,42 @@ public class HTMLRenderer implements Renderer<String> {
|
|||
}
|
||||
|
||||
private String getPreamble(@NotNull final Maze maze) {
|
||||
return "<!DOCTYPE html><html lang=\"en\">" +
|
||||
"<head>" +
|
||||
"<title>Maze " + maze.getWidth() + "x" + maze.getHeight() + ", ID " + maze.getRandomSeed() + "</title>" +
|
||||
"<meta charset=\"utf-8\">" +
|
||||
"<style>" +
|
||||
"table{border-collapse:collapse;}" +
|
||||
"td{border:0 solid black;height:1em;width:1em;cursor:pointer;}" +
|
||||
"td.top{border-top-width:1px;}" +
|
||||
"td.right{border-right-width:1px;}" +
|
||||
"td.bottom{border-bottom-width:1px;}" +
|
||||
"td.left{border-left-width:1px;}" +
|
||||
"td.user{background:hotpink;}" +
|
||||
"</style>" +
|
||||
"<script>" +
|
||||
"let solution = false;" +
|
||||
"function toggleSolution() {" +
|
||||
"let stylesheet = document.styleSheets[0];" +
|
||||
"if(solution){" +
|
||||
"stylesheet.deleteRule(0);" +
|
||||
"}else{" +
|
||||
"stylesheet.insertRule(\"td.solution{background-color:lightgray;}\", 0);" +
|
||||
"}" +
|
||||
"solution = !solution;" +
|
||||
"}" +
|
||||
"</script>" +
|
||||
"</head>" +
|
||||
"<body>" +
|
||||
"<input id=\"solutionbox\" type=\"checkbox\" onclick=\"toggleSolution()\"/><label for=\"solutionbox\">show solution</label>";
|
||||
return """
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Maze %dx%d, ID %d, Algorithm %s</title>
|
||||
<meta charset="utf-8">
|
||||
<style>
|
||||
table{border-collapse:collapse;}
|
||||
td{border:0 solid black;height:1em;width:1em;cursor:pointer;}
|
||||
td.top{border-top-width:1px;}
|
||||
td.right{border-right-width:1px;}
|
||||
td.bottom{border-bottom-width:1px;}
|
||||
td.left{border-left-width:1px;}
|
||||
td.user{background:hotpink;}
|
||||
</style>
|
||||
<script>
|
||||
let solution = false;
|
||||
function toggleSolution() {
|
||||
let stylesheet = document.styleSheets[0];
|
||||
if (solution) {
|
||||
stylesheet.deleteRule(0);
|
||||
} else {
|
||||
stylesheet.insertRule("td.solution{background-color:lightgray;}", 0);
|
||||
}
|
||||
solution = !solution;
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<input id="solutionbox" type="checkbox" onclick="toggleSolution()"/>
|
||||
<label for="solutionbox">show solution</label>"""
|
||||
.formatted(
|
||||
maze.getWidth(),
|
||||
maze.getHeight(),
|
||||
maze.getRandomSeed(),
|
||||
maze.getAlgorithm()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ class Generator {
|
|||
JsonMaze generate() {
|
||||
final JsonMaze result = new JsonMaze();
|
||||
result.setId(String.valueOf(this.maze.getRandomSeed()));
|
||||
result.setAlgorithm(this.maze.getAlgorithm());
|
||||
result.setWidth(this.maze.getWidth());
|
||||
result.setHeight(this.maze.getHeight());
|
||||
final List<List<JsonCell>> rows = new ArrayList<>();
|
||||
|
|
|
@ -34,7 +34,7 @@ class Generator {
|
|||
|
||||
final PDDocument pdDocument = new PDDocument();
|
||||
final PDDocumentInformation info = new PDDocumentInformation();
|
||||
info.setTitle("Maze %sx%s, ID %s".formatted(this.maze.getWidth(), this.maze.getHeight(), this.maze.getRandomSeed()));
|
||||
info.setTitle("Maze %sx%s, ID %s (%s)".formatted(this.maze.getWidth(), this.maze.getHeight(), this.maze.getRandomSeed(), this.maze.getAlgorithm()));
|
||||
pdDocument.setDocumentInformation(info);
|
||||
final PDPage puzzlePage = new PDPage(new PDRectangle(pageWidth, pageHeight));
|
||||
final PDPage solutionPage = new PDPage(new PDRectangle(pageWidth, pageHeight));
|
||||
|
@ -233,7 +233,7 @@ class Generator {
|
|||
if (position.equals(previousPosition)) {
|
||||
continue;
|
||||
}
|
||||
if (tileAtPosition.map(Tile::isSolution).getOrElse(false)) {
|
||||
if (tileAtPosition.exists(Tile::isSolution)) {
|
||||
return position;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import ch.fritteli.maze.generator.model.Maze;
|
|||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class AbstractMazeInputStream extends ByteArrayInputStream {
|
||||
|
||||
|
@ -14,7 +15,7 @@ public abstract class AbstractMazeInputStream extends ByteArrayInputStream {
|
|||
public abstract void checkHeader();
|
||||
|
||||
@NotNull
|
||||
public abstract Maze readMazeData();
|
||||
public abstract Maze readMazeData() throws IOException;
|
||||
|
||||
public byte readByte() {
|
||||
final int read = this.read();
|
||||
|
@ -25,6 +26,10 @@ public abstract class AbstractMazeInputStream extends ByteArrayInputStream {
|
|||
return (byte) read;
|
||||
}
|
||||
|
||||
public int readByteAsInt() {
|
||||
return 0xff & this.readByte();
|
||||
}
|
||||
|
||||
public int readInt() {
|
||||
int result = 0;
|
||||
result |= (0xff & this.readByte()) << 24;
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
package ch.fritteli.maze.generator.serialization;
|
||||
|
||||
import ch.fritteli.maze.generator.model.Direction;
|
||||
import ch.fritteli.maze.generator.model.Tile;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.EnumSet;
|
||||
|
||||
|
||||
/**
|
||||
* Binary format description of a {@link Tile}.<br>
|
||||
* A tile is stored in one byte:
|
||||
* <ul>
|
||||
* <li>bits 0..2: always 0</li>
|
||||
* <li>bit 3: 1=solution, 0=not solution</li>
|
||||
* <li>bits 4..7: encode walls</li>
|
||||
* </ul>
|
||||
* The values for bits 4..7 are as follows:
|
||||
* <pre>
|
||||
* decimal hex bin border
|
||||
* 0 0 0000 no border
|
||||
* 1 1 0001 top
|
||||
* 2 2 0010 right
|
||||
* 3 3 0011 top+right
|
||||
* 4 4 0100 bottom
|
||||
* 5 5 0101 top+bottom
|
||||
* 6 6 0110 right+bottom
|
||||
* 7 7 0111 top+right+bottom
|
||||
* 8 8 1000 left
|
||||
* 9 9 1001 top+left
|
||||
* 10 a 1010 right+left
|
||||
* 11 b 1011 top+right+left
|
||||
* 12 c 1100 bottom+left
|
||||
* 13 d 1101 top+bottom+left
|
||||
* 14 e 1110 right+bottom+left
|
||||
* 15 f 1111 top+right+bottom+left
|
||||
* </pre>
|
||||
*/
|
||||
@UtilityClass
|
||||
public class CommonTileHandler {
|
||||
private final byte TOP_BIT = 0b0000_0001;
|
||||
private final byte RIGHT_BIT = 0b0000_0010;
|
||||
private final byte BOTTOM_BIT = 0b0000_0100;
|
||||
private final byte LEFT_BIT = 0b0000_1000;
|
||||
private final byte SOLUTION_BIT = 0b0001_0000;
|
||||
|
||||
public byte getBitmaskForTile(@NotNull final Tile tile) {
|
||||
byte bitmask = 0;
|
||||
if (tile.hasWallAt(Direction.TOP)) {
|
||||
bitmask |= TOP_BIT;
|
||||
}
|
||||
if (tile.hasWallAt(Direction.RIGHT)) {
|
||||
bitmask |= RIGHT_BIT;
|
||||
}
|
||||
if (tile.hasWallAt(Direction.BOTTOM)) {
|
||||
bitmask |= BOTTOM_BIT;
|
||||
}
|
||||
if (tile.hasWallAt((Direction.LEFT))) {
|
||||
bitmask |= LEFT_BIT;
|
||||
}
|
||||
if (tile.isSolution()) {
|
||||
bitmask |= SOLUTION_BIT;
|
||||
}
|
||||
return bitmask;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Tile getTileForBitmask(final byte bitmask) {
|
||||
final EnumSet<Direction> walls = EnumSet.noneOf(Direction.class);
|
||||
if ((bitmask & TOP_BIT) == TOP_BIT) {
|
||||
walls.add(Direction.TOP);
|
||||
}
|
||||
if ((bitmask & RIGHT_BIT) == RIGHT_BIT) {
|
||||
walls.add(Direction.RIGHT);
|
||||
}
|
||||
if ((bitmask & BOTTOM_BIT) == BOTTOM_BIT) {
|
||||
walls.add(Direction.BOTTOM);
|
||||
}
|
||||
if ((bitmask & LEFT_BIT) == LEFT_BIT) {
|
||||
walls.add(Direction.LEFT);
|
||||
}
|
||||
final boolean solution = (bitmask & SOLUTION_BIT) == SOLUTION_BIT;
|
||||
return createTile(walls, solution);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Tile createTile(@NotNull 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 |
|
||||
InvocationTargetException e) {
|
||||
throw new RuntimeException("Can not deserialize Tile from maze data.", e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,6 +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.AbstractMazeInputStream;
|
||||
import ch.fritteli.maze.generator.serialization.CommonTileHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class MazeInputStreamV1 extends AbstractMazeInputStream {
|
||||
|
@ -13,6 +14,9 @@ public class MazeInputStreamV1 extends AbstractMazeInputStream {
|
|||
|
||||
@Override
|
||||
public void checkHeader() {
|
||||
// 00 0x1a magic
|
||||
// 01 0xb1 magic
|
||||
// 02 0x01 version
|
||||
final byte magic1 = this.readByte();
|
||||
if (magic1 != SerializerDeserializerV1.MAGIC_BYTE_1) {
|
||||
throw new IllegalArgumentException("Invalid maze data.");
|
||||
|
@ -30,6 +34,10 @@ public class MazeInputStreamV1 extends AbstractMazeInputStream {
|
|||
@NotNull
|
||||
@Override
|
||||
public Maze readMazeData() {
|
||||
// 03..10 random seed number (long)
|
||||
// 11..14 width (int)
|
||||
// 15..18 height (int)
|
||||
// 19.. tiles
|
||||
final long randomSeed = this.readLong();
|
||||
final int width = this.readInt();
|
||||
final int height = this.readInt();
|
||||
|
@ -42,7 +50,7 @@ public class MazeInputStreamV1 extends AbstractMazeInputStream {
|
|||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
final byte bitmask = this.readByte();
|
||||
tiles[x][y] = SerializerDeserializerV1.getTileForBitmask(bitmask);
|
||||
tiles[x][y] = CommonTileHandler.getTileForBitmask(bitmask);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,12 +3,16 @@ 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 ch.fritteli.maze.generator.serialization.CommonTileHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class MazeOutputStreamV1 extends AbstractMazeOutputStream {
|
||||
|
||||
@Override
|
||||
public void writeHeader() {
|
||||
// 00 0x1a magic
|
||||
// 01 0xb1 magic
|
||||
// 02 0x02 version
|
||||
this.writeByte(SerializerDeserializerV1.MAGIC_BYTE_1);
|
||||
this.writeByte(SerializerDeserializerV1.MAGIC_BYTE_2);
|
||||
this.writeByte(SerializerDeserializerV1.VERSION_BYTE);
|
||||
|
@ -16,6 +20,10 @@ public class MazeOutputStreamV1 extends AbstractMazeOutputStream {
|
|||
|
||||
@Override
|
||||
public void writeMazeData(@NotNull final Maze maze) {
|
||||
// 03..10 random seed number (long)
|
||||
// 11..14 width (int)
|
||||
// 15..18 height (int)
|
||||
// 19.. tiles
|
||||
final long randomSeed = maze.getRandomSeed();
|
||||
final int width = maze.getWidth();
|
||||
final int height = maze.getHeight();
|
||||
|
@ -27,7 +35,7 @@ public class MazeOutputStreamV1 extends AbstractMazeOutputStream {
|
|||
for (int x = 0; x < width; x++) {
|
||||
// We .get() it, because we want to crash hard if it is not available.
|
||||
final Tile tile = maze.getTileAt(x, y).get();
|
||||
final byte bitmask = SerializerDeserializerV1.getBitmaskForTile(tile);
|
||||
final byte bitmask = CommonTileHandler.getBitmaskForTile(tile);
|
||||
this.writeByte(bitmask);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
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.experimental.UtilityClass;
|
||||
|
@ -8,38 +7,17 @@ import org.jetbrains.annotations.NotNull;
|
|||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.EnumSet;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* decimal hex bin border
|
||||
* 0 0 0000 no border
|
||||
* 1 1 0001 top
|
||||
* 2 2 0010 right
|
||||
* 3 3 0011 top+right
|
||||
* 4 4 0100 bottom
|
||||
* 5 5 0101 top+bottom
|
||||
* 6 6 0110 right+bottom
|
||||
* 7 7 0111 top+right+bottom
|
||||
* 8 8 1000 left
|
||||
* 9 9 1001 top+left
|
||||
* 10 a 1010 right+left
|
||||
* 11 b 1011 top+right+left
|
||||
* 12 c 1100 bottom+left
|
||||
* 13 d 1101 top+bottom+left
|
||||
* 14 e 1110 right+bottom+left
|
||||
* 15 f 1111 top+right+bottom+left
|
||||
* </pre>
|
||||
* ==> bits 0..2: always 0; bit 3: 1=solution, 0=not solution; bits 4..7: encode walls
|
||||
* ==> first bytes are:
|
||||
* Header bytes are:
|
||||
* <pre>
|
||||
* byte hex meaning
|
||||
* 00 0x1a magic
|
||||
* 01 0xb1 magic
|
||||
* 02 0x01 version (0x00 -> dev, 0x01 -> stable)
|
||||
* 03..06 width (int)
|
||||
* 07..10 height (int)
|
||||
* 11..18 random seed number (long)
|
||||
* 03..10 random seed number (long)
|
||||
* 11..14 width (int)
|
||||
* 15..18 height (int)
|
||||
* 19.. tiles
|
||||
* </pre>
|
||||
* Extraneous space (poss. last nibble) is ignored.
|
||||
|
@ -50,12 +28,6 @@ public class SerializerDeserializerV1 {
|
|||
final byte MAGIC_BYTE_2 = (byte) 0xb1;
|
||||
final byte VERSION_BYTE = 0x01;
|
||||
|
||||
private final byte TOP_BIT = 0b0000_0001;
|
||||
private final byte RIGHT_BIT = 0b0000_0010;
|
||||
private final byte BOTTOM_BIT = 0b0000_0100;
|
||||
private final byte LEFT_BIT = 0b0000_1000;
|
||||
private final byte SOLUTION_BIT = 0b0001_0000;
|
||||
|
||||
/**
|
||||
* Serializes the {@code maze} into a byte array.
|
||||
*
|
||||
|
@ -94,55 +66,4 @@ public class SerializerDeserializerV1 {
|
|||
throw new RuntimeException("Can not deserialize Maze from maze data.", e);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Tile createTile(@NotNull 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 |
|
||||
InvocationTargetException e) {
|
||||
throw new RuntimeException("Can not deserialize Tile from maze data.", e);
|
||||
}
|
||||
}
|
||||
|
||||
byte getBitmaskForTile(@NotNull final Tile tile) {
|
||||
byte bitmask = 0;
|
||||
if (tile.hasWallAt(Direction.TOP)) {
|
||||
bitmask |= TOP_BIT;
|
||||
}
|
||||
if (tile.hasWallAt(Direction.RIGHT)) {
|
||||
bitmask |= RIGHT_BIT;
|
||||
}
|
||||
if (tile.hasWallAt(Direction.BOTTOM)) {
|
||||
bitmask |= BOTTOM_BIT;
|
||||
}
|
||||
if (tile.hasWallAt((Direction.LEFT))) {
|
||||
bitmask |= LEFT_BIT;
|
||||
}
|
||||
if (tile.isSolution()) {
|
||||
bitmask |= SOLUTION_BIT;
|
||||
}
|
||||
return bitmask;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
Tile getTileForBitmask(final byte bitmask) {
|
||||
final EnumSet<Direction> walls = EnumSet.noneOf(Direction.class);
|
||||
if ((bitmask & TOP_BIT) == TOP_BIT) {
|
||||
walls.add(Direction.TOP);
|
||||
}
|
||||
if ((bitmask & RIGHT_BIT) == RIGHT_BIT) {
|
||||
walls.add(Direction.RIGHT);
|
||||
}
|
||||
if ((bitmask & BOTTOM_BIT) == BOTTOM_BIT) {
|
||||
walls.add(Direction.BOTTOM);
|
||||
}
|
||||
if ((bitmask & LEFT_BIT) == LEFT_BIT) {
|
||||
walls.add(Direction.LEFT);
|
||||
}
|
||||
final boolean solution = (bitmask & SOLUTION_BIT) == SOLUTION_BIT;
|
||||
return createTile(walls, solution);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +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.AbstractMazeInputStream;
|
||||
import ch.fritteli.maze.generator.serialization.CommonTileHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class MazeInputStreamV2 extends AbstractMazeInputStream {
|
||||
|
@ -58,7 +59,7 @@ public class MazeInputStreamV2 extends AbstractMazeInputStream {
|
|||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
final byte bitmask = this.readByte();
|
||||
tiles[x][y] = SerializerDeserializerV2.getTileForBitmask(bitmask);
|
||||
tiles[x][y] = CommonTileHandler.getTileForBitmask(bitmask);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +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 ch.fritteli.maze.generator.serialization.CommonTileHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class MazeOutputStreamV2 extends AbstractMazeOutputStream {
|
||||
|
@ -45,7 +46,7 @@ public class MazeOutputStreamV2 extends AbstractMazeOutputStream {
|
|||
for (int x = 0; x < width; x++) {
|
||||
// We .get() it, because we want to crash hard if it is not available.
|
||||
final Tile tile = maze.getTileAt(x, y).get();
|
||||
final byte bitmask = SerializerDeserializerV2.getBitmaskForTile(tile);
|
||||
final byte bitmask = CommonTileHandler.getBitmaskForTile(tile);
|
||||
this.writeByte(bitmask);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
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;
|
||||
|
@ -9,29 +8,9 @@ import org.jetbrains.annotations.NotNull;
|
|||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.EnumSet;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* decimal hex bin border
|
||||
* 0 0 0000 no border
|
||||
* 1 1 0001 top
|
||||
* 2 2 0010 right
|
||||
* 3 3 0011 top+right
|
||||
* 4 4 0100 bottom
|
||||
* 5 5 0101 top+bottom
|
||||
* 6 6 0110 right+bottom
|
||||
* 7 7 0111 top+right+bottom
|
||||
* 8 8 1000 left
|
||||
* 9 9 1001 top+left
|
||||
* 10 a 1010 right+left
|
||||
* 11 b 1011 top+right+left
|
||||
* 12 c 1100 bottom+left
|
||||
* 13 d 1101 top+bottom+left
|
||||
* 14 e 1110 right+bottom+left
|
||||
* 15 f 1111 top+right+bottom+left
|
||||
* </pre>
|
||||
* ==> bits 0..2: always 0; bit 3: 1=solution, 0=not solution; bits 4..7: encode walls ==> first bytes are:
|
||||
* Header bytes are:
|
||||
* <pre>
|
||||
* byte hex meaning
|
||||
* 00 0x1a magic
|
||||
|
@ -55,12 +34,6 @@ public class SerializerDeserializerV2 {
|
|||
final byte MAGIC_BYTE_2 = (byte) 0xb1;
|
||||
final byte VERSION_BYTE = 0x02;
|
||||
|
||||
private final byte TOP_BIT = 0b0000_0001;
|
||||
private final byte RIGHT_BIT = 0b0000_0010;
|
||||
private final byte BOTTOM_BIT = 0b0000_0100;
|
||||
private final byte LEFT_BIT = 0b0000_1000;
|
||||
private final byte SOLUTION_BIT = 0b0001_0000;
|
||||
|
||||
/**
|
||||
* Serializes the {@code maze} into a byte array.
|
||||
*
|
||||
|
@ -99,55 +72,4 @@ public class SerializerDeserializerV2 {
|
|||
throw new RuntimeException("Can not deserialize Maze from maze data.", e);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Tile createTile(@NotNull 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 |
|
||||
InvocationTargetException e) {
|
||||
throw new RuntimeException("Can not deserialize Tile from maze data.", e);
|
||||
}
|
||||
}
|
||||
|
||||
byte getBitmaskForTile(@NotNull final Tile tile) {
|
||||
byte bitmask = 0;
|
||||
if (tile.hasWallAt(Direction.TOP)) {
|
||||
bitmask |= TOP_BIT;
|
||||
}
|
||||
if (tile.hasWallAt(Direction.RIGHT)) {
|
||||
bitmask |= RIGHT_BIT;
|
||||
}
|
||||
if (tile.hasWallAt(Direction.BOTTOM)) {
|
||||
bitmask |= BOTTOM_BIT;
|
||||
}
|
||||
if (tile.hasWallAt((Direction.LEFT))) {
|
||||
bitmask |= LEFT_BIT;
|
||||
}
|
||||
if (tile.isSolution()) {
|
||||
bitmask |= SOLUTION_BIT;
|
||||
}
|
||||
return bitmask;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
Tile getTileForBitmask(final byte bitmask) {
|
||||
final EnumSet<Direction> walls = EnumSet.noneOf(Direction.class);
|
||||
if ((bitmask & TOP_BIT) == TOP_BIT) {
|
||||
walls.add(Direction.TOP);
|
||||
}
|
||||
if ((bitmask & RIGHT_BIT) == RIGHT_BIT) {
|
||||
walls.add(Direction.RIGHT);
|
||||
}
|
||||
if ((bitmask & BOTTOM_BIT) == BOTTOM_BIT) {
|
||||
walls.add(Direction.BOTTOM);
|
||||
}
|
||||
if ((bitmask & LEFT_BIT) == LEFT_BIT) {
|
||||
walls.add(Direction.LEFT);
|
||||
}
|
||||
final boolean solution = (bitmask & SOLUTION_BIT) == SOLUTION_BIT;
|
||||
return createTile(walls, solution);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
package ch.fritteli.maze.generator.serialization.v3;
|
||||
|
||||
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 ch.fritteli.maze.generator.serialization.CommonTileHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class MazeInputStreamV3 extends AbstractMazeInputStream {
|
||||
|
||||
public MazeInputStreamV3(@NotNull final byte[] buf) {
|
||||
super(buf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkHeader() {
|
||||
// 00 0x1a magic
|
||||
// 01 0xb1 magic
|
||||
// 02 0x03 version
|
||||
final byte magic1 = this.readByte();
|
||||
if (magic1 != SerializerDeserializerV3.MAGIC_BYTE_1) {
|
||||
throw new IllegalArgumentException("Invalid maze data.");
|
||||
}
|
||||
final byte magic2 = this.readByte();
|
||||
if (magic2 != SerializerDeserializerV3.MAGIC_BYTE_2) {
|
||||
throw new IllegalArgumentException("Invalid maze data.");
|
||||
}
|
||||
final int version = this.readByte();
|
||||
if (version != SerializerDeserializerV3.VERSION_BYTE) {
|
||||
throw new IllegalArgumentException("Unknown maze data version: " + version);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Maze readMazeData() throws IOException {
|
||||
// 03..06 width (int)
|
||||
// 07..10 height (int)
|
||||
// 11..14 start-x (int)
|
||||
// 15..18 start-y (int)
|
||||
// 19..22 end-x (int)
|
||||
// 23..26 end-y (int)
|
||||
// 27..34 random seed number (long)
|
||||
// 35 length of the algorithm's name (unsigned byte)
|
||||
// 36..+len name (bytes of String)
|
||||
// +len+1.. tiles
|
||||
final int width = this.readInt();
|
||||
final int height = this.readInt();
|
||||
final int startX = this.readInt();
|
||||
final int startY = this.readInt();
|
||||
final int endX = this.readInt();
|
||||
final int endY = this.readInt();
|
||||
final long randomSeed = this.readLong();
|
||||
final int algorithmLength = this.readByteAsInt();
|
||||
|
||||
final String algorithm = new String(this.readNBytes(algorithmLength), StandardCharsets.UTF_8);
|
||||
|
||||
final Tile[][] tiles = new Tile[width][height];
|
||||
for (int x = 0; x < width; x++) {
|
||||
tiles[x] = new Tile[height];
|
||||
}
|
||||
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
final byte bitmask = this.readByte();
|
||||
tiles[x][y] = CommonTileHandler.getTileForBitmask(bitmask);
|
||||
}
|
||||
}
|
||||
|
||||
final Position start = new Position(startX, startY);
|
||||
final Position end = new Position(endX, endY);
|
||||
return SerializerDeserializerV3.createMaze(tiles, width, height, start, end, randomSeed, algorithm);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
package ch.fritteli.maze.generator.serialization.v3;
|
||||
|
||||
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 ch.fritteli.maze.generator.serialization.CommonTileHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class MazeOutputStreamV3 extends AbstractMazeOutputStream {
|
||||
|
||||
@Override
|
||||
public void writeHeader() {
|
||||
// 00 0x1a magic
|
||||
// 01 0xb1 magic
|
||||
// 02 0x03 version
|
||||
this.writeByte(SerializerDeserializerV3.MAGIC_BYTE_1);
|
||||
this.writeByte(SerializerDeserializerV3.MAGIC_BYTE_2);
|
||||
this.writeByte(SerializerDeserializerV3.VERSION_BYTE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeMazeData(@NotNull final Maze maze) {
|
||||
// 03..06 width (int)
|
||||
// 07..10 height (int)
|
||||
// 11..14 start-x (int)
|
||||
// 15..18 start-y (int)
|
||||
// 19..22 end-x (int)
|
||||
// 23..26 end-y (int)
|
||||
// 27..34 random seed number (long)
|
||||
// 35 length of the algorithm's name (unsigned byte)
|
||||
// 36..+len name (bytes of String)
|
||||
// +len+1.. tiles
|
||||
final long randomSeed = maze.getRandomSeed();
|
||||
final AlgorithmWrapper algorithm = this.getAlgorithmWrapper(maze.getAlgorithm());
|
||||
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.x());
|
||||
this.writeInt(start.y());
|
||||
this.writeInt(end.x());
|
||||
this.writeInt(end.y());
|
||||
this.writeLong(randomSeed);
|
||||
this.writeByte(algorithm.length());
|
||||
this.writeBytes(algorithm.name());
|
||||
|
||||
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 = maze.getTileAt(x, y).get();
|
||||
final byte bitmask = CommonTileHandler.getBitmaskForTile(tile);
|
||||
this.writeByte(bitmask);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private AlgorithmWrapper getAlgorithmWrapper(@NotNull final String algorithm) {
|
||||
final byte[] bytes = algorithm.getBytes(StandardCharsets.UTF_8);
|
||||
if (bytes.length < 256) {
|
||||
// Phew, that's the easy case!
|
||||
return new AlgorithmWrapper(bytes, (byte) bytes.length);
|
||||
}
|
||||
|
||||
// Let's use a very primitive, brute-force approach
|
||||
int strLen = Math.min(255, algorithm.length());
|
||||
int len;
|
||||
byte[] name;
|
||||
do {
|
||||
name = algorithm.substring(0, strLen).getBytes(StandardCharsets.UTF_8);
|
||||
len = name.length;
|
||||
strLen--;
|
||||
} while (len > 255);
|
||||
|
||||
return new AlgorithmWrapper(name, (byte) len);
|
||||
}
|
||||
|
||||
private record AlgorithmWrapper(@NotNull byte[] name, byte length) {
|
||||
}
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
package ch.fritteli.maze.generator.serialization.v3;
|
||||
|
||||
import ch.fritteli.maze.generator.model.Maze;
|
||||
import ch.fritteli.maze.generator.model.Position;
|
||||
import ch.fritteli.maze.generator.model.Tile;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
/**
|
||||
* Header bytes are:
|
||||
* <pre>
|
||||
* byte hex meaning
|
||||
* 00 0x1a magic
|
||||
* 01 0xb1 magic
|
||||
* 02 0x03 version (0x00 -> dev, 0x01, 0x02 -> deprecated, 0x03 -> stable)
|
||||
* 03..06 width (int)
|
||||
* 07..10 height (int)
|
||||
* 11..14 start-x (int)
|
||||
* 15..18 start-y (int)
|
||||
* 19..22 end-x (int)
|
||||
* 23..26 end-y (int)
|
||||
* 27..34 random seed number (long)
|
||||
* 35 length of the algorithm's name (number of bytes of the Java String) (unsigned byte)
|
||||
* 36..(36+len) algorithm's name (bytes of the Java String) (byte...)
|
||||
* 36+len+1.. tiles
|
||||
* </pre>
|
||||
* Extraneous space (poss. last nibble) is ignored.
|
||||
*/
|
||||
@UtilityClass
|
||||
public class SerializerDeserializerV3 {
|
||||
|
||||
final byte MAGIC_BYTE_1 = 0x1a;
|
||||
final byte MAGIC_BYTE_2 = (byte) 0xb1;
|
||||
final byte VERSION_BYTE = 0x03;
|
||||
|
||||
/**
|
||||
* Serializes the {@code maze} into a byte array.
|
||||
*
|
||||
* @param maze The {@link Maze} to be serialized.
|
||||
* @return The resulting byte array.
|
||||
*/
|
||||
@NotNull
|
||||
public byte[] serialize(@NotNull final Maze maze) {
|
||||
final MazeOutputStreamV3 stream = new MazeOutputStreamV3();
|
||||
stream.writeHeader();
|
||||
stream.writeMazeData(maze);
|
||||
return stream.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the byte array into an instance of {@link Maze}.
|
||||
*
|
||||
* @param bytes The byte array to be deserialized.
|
||||
* @return An instance of {@link Maze}.
|
||||
*/
|
||||
@NotNull
|
||||
public Maze deserialize(@NotNull final byte[] bytes) throws IOException {
|
||||
final MazeInputStreamV3 stream = new MazeInputStreamV3(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,
|
||||
@NotNull final String algorithm) {
|
||||
try {
|
||||
final Constructor<Maze> constructor = Maze.class.getDeclaredConstructor(
|
||||
Tile[][].class,
|
||||
Integer.TYPE,
|
||||
Integer.TYPE,
|
||||
Position.class,
|
||||
Position.class,
|
||||
Long.TYPE,
|
||||
String.class
|
||||
);
|
||||
constructor.setAccessible(true);
|
||||
return constructor.newInstance(field, width, height, start, end, randomSeed, algorithm);
|
||||
} catch (@NotNull final NoSuchMethodException | IllegalAccessException | InstantiationException |
|
||||
InvocationTargetException e) {
|
||||
throw new RuntimeException("Can not deserialize Maze from maze data.", e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@
|
|||
"additionalProperties": false,
|
||||
"required": [
|
||||
"id",
|
||||
"algorithm",
|
||||
"width",
|
||||
"height",
|
||||
"start",
|
||||
|
@ -17,6 +18,10 @@
|
|||
"type": "string",
|
||||
"description": "64 bit precision signed integer value. Transmitted as string, because ECMAScript (browsers) don't normally handle 64 bit integers well, as the ECMAScript 'number' type is a 64 bit signed double value, leaving only 53 bits for the integer part, thus losing precision."
|
||||
},
|
||||
"algorithm": {
|
||||
"type": "string",
|
||||
"description": "The name of the algorithm used to generate the maze."
|
||||
},
|
||||
"width": {
|
||||
"type": "integer",
|
||||
"minimum": 1
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
package ch.fritteli.maze.generator.algorithm;
|
||||
|
||||
import ch.fritteli.maze.generator.model.Maze;
|
||||
import ch.fritteli.maze.generator.renderer.text.TextRenderer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class WilsonTest {
|
||||
@Test
|
||||
void foo() {
|
||||
// arrange
|
||||
final Maze maze = new Maze(10, 10, 0);
|
||||
final Wilson wilson = new Wilson(maze);
|
||||
|
||||
// act
|
||||
wilson.run();
|
||||
|
||||
// assert
|
||||
final String textRepresentation = TextRenderer.newInstance().render(maze);
|
||||
assertThat(textRepresentation).isEqualTo("""
|
||||
╷ ╶─────┬─────┬───┬─┐
|
||||
│ │ │ │ │
|
||||
│ ╷ ╶─┬─┴─┬─╴ ╵ ╷ │ │
|
||||
│ │ │ │ │ │ │
|
||||
│ └─┐ │ ╶─┼─┬─┬─┘ │ │
|
||||
│ │ │ │ │ │ │ │
|
||||
│ ╷ │ └─╴ │ ╵ ╵ ╶─┘ │
|
||||
│ │ │ │ │
|
||||
├─┴─┘ ╷ ┌─┴───┐ ╷ ╶─┤
|
||||
│ │ │ │ │ │
|
||||
│ ╷ ┌─┘ └───╴ │ └─┬─┤
|
||||
│ │ │ │ │ │
|
||||
├─┴─┘ ┌───╴ ╷ └─┐ ╵ │
|
||||
│ │ │ │ │
|
||||
│ ╶─┬─┴─╴ ┌─┘ ╶─┘ ╶─┤
|
||||
│ │ │ │
|
||||
├─╴ ├─╴ ┌─┘ ╶─┐ ╶───┤
|
||||
│ │ │ │ │
|
||||
├───┴─╴ └─┐ ╶─┴───┐ │
|
||||
│ │ │ │
|
||||
└─────────┴───────┘ ╵""");
|
||||
}
|
||||
}
|
|
@ -13,6 +13,8 @@ class SerializerDeserializerV1Test {
|
|||
new RandomDepthFirst(expected).run();
|
||||
final byte[] bytes = SerializerDeserializerV1.serialize(expected);
|
||||
final Maze result = SerializerDeserializerV1.deserialize(bytes);
|
||||
assertThat(result.getAlgorithm()).isNull();
|
||||
expected.setAlgorithm(null);
|
||||
assertThat(result).isEqualTo(expected);
|
||||
}
|
||||
|
||||
|
@ -22,6 +24,8 @@ class SerializerDeserializerV1Test {
|
|||
new RandomDepthFirst(expected).run();
|
||||
final byte[] bytes = SerializerDeserializerV1.serialize(expected);
|
||||
final Maze result = SerializerDeserializerV1.deserialize(bytes);
|
||||
assertThat(result.getAlgorithm()).isNull();
|
||||
expected.setAlgorithm(null);
|
||||
assertThat(result).isEqualTo(expected);
|
||||
}
|
||||
|
||||
|
@ -31,6 +35,8 @@ class SerializerDeserializerV1Test {
|
|||
new RandomDepthFirst(expected).run();
|
||||
final byte[] bytes = SerializerDeserializerV1.serialize(expected);
|
||||
final Maze result = SerializerDeserializerV1.deserialize(bytes);
|
||||
assertThat(result.getAlgorithm()).isNull();
|
||||
expected.setAlgorithm(null);
|
||||
assertThat(result).isEqualTo(expected);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
package ch.fritteli.maze.generator.serialization.v2;
|
||||
|
||||
import ch.fritteli.maze.generator.algorithm.RandomDepthFirst;
|
||||
import ch.fritteli.maze.generator.algorithm.Wilson;
|
||||
import ch.fritteli.maze.generator.model.Maze;
|
||||
import ch.fritteli.maze.generator.model.Position;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class SerializerDeserializerV2Test {
|
||||
@Test
|
||||
void testSerializeDeserializeTiny() throws IOException {
|
||||
final Maze expected = new Maze(2, 2, 255, new Position(0, 0), new Position(1, 1));
|
||||
new RandomDepthFirst(expected).run();
|
||||
final byte[] bytes = SerializerDeserializerV2.serialize(expected);
|
||||
final Maze result = SerializerDeserializerV2.deserialize(bytes);
|
||||
assertThat(result.getAlgorithm()).isNull();
|
||||
expected.setAlgorithm(null);
|
||||
assertThat(result).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSerializeDeserializeMedium() throws IOException {
|
||||
final Maze expected = new Maze(20, 20, -271828182846L);
|
||||
new Wilson(expected).run();
|
||||
final byte[] bytes = SerializerDeserializerV2.serialize(expected);
|
||||
final Maze result = SerializerDeserializerV2.deserialize(bytes);
|
||||
assertThat(result.getAlgorithm()).isNull();
|
||||
expected.setAlgorithm(null);
|
||||
assertThat(result).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSerializeDeserializeLarge() throws IOException {
|
||||
final Maze expected = new Maze(200, 320, 3141592653589793238L);
|
||||
new Wilson(expected).run();
|
||||
final byte[] bytes = SerializerDeserializerV2.serialize(expected);
|
||||
final Maze result = SerializerDeserializerV2.deserialize(bytes);
|
||||
assertThat(result.getAlgorithm()).isNull();
|
||||
expected.setAlgorithm(null);
|
||||
assertThat(result).isEqualTo(expected);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package ch.fritteli.maze.generator.serialization.v3;
|
||||
|
||||
import ch.fritteli.maze.generator.algorithm.RandomDepthFirst;
|
||||
import ch.fritteli.maze.generator.algorithm.Wilson;
|
||||
import ch.fritteli.maze.generator.model.Maze;
|
||||
import ch.fritteli.maze.generator.model.Position;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class SerializerDeserializerV3Test {
|
||||
@Test
|
||||
void testSerializeDeserializeTiny() throws IOException {
|
||||
final Maze expected = new Maze(2, 2, 255, new Position(0, 0), new Position(1, 1));
|
||||
new RandomDepthFirst(expected).run();
|
||||
final byte[] bytes = SerializerDeserializerV3.serialize(expected);
|
||||
final Maze result = SerializerDeserializerV3.deserialize(bytes);
|
||||
assertThat(result).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSerializeDeserializeMedium() throws IOException {
|
||||
final Maze expected = new Maze(20, 20, -271828182846L);
|
||||
new Wilson(expected).run();
|
||||
final byte[] bytes = SerializerDeserializerV3.serialize(expected);
|
||||
final Maze result = SerializerDeserializerV3.deserialize(bytes);
|
||||
assertThat(result).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSerializeDeserializeLarge() throws IOException {
|
||||
final Maze expected = new Maze(200, 320, 3141592653589793238L);
|
||||
new Wilson(expected).run();
|
||||
final byte[] bytes = SerializerDeserializerV3.serialize(expected);
|
||||
final Maze result = SerializerDeserializerV3.deserialize(bytes);
|
||||
assertThat(result).isEqualTo(expected);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue