This commit is contained in:
parent
e1781dbdc4
commit
2275651cc1
3 changed files with 16 additions and 29 deletions
|
@ -33,15 +33,13 @@ class Generator {
|
|||
|
||||
final PDDocument pdDocument = new PDDocument();
|
||||
final PDDocumentInformation info = new PDDocumentInformation();
|
||||
info.setTitle("Labyrinth " + this.labyrinth.getWidth() + "x" + this.labyrinth.getHeight() + ", ID " +
|
||||
this.labyrinth.getRandomSeed());
|
||||
info.setTitle("Labyrinth %sx%s, ID %s".formatted(this.labyrinth.getWidth(), this.labyrinth.getHeight(), this.labyrinth.getRandomSeed()));
|
||||
pdDocument.setDocumentInformation(info);
|
||||
final PDPage puzzlePage = new PDPage(new PDRectangle(pageWidth, pageHeight));
|
||||
final PDPage solutionPage = new PDPage(new PDRectangle(pageWidth, pageHeight));
|
||||
pdDocument.addPage(puzzlePage);
|
||||
pdDocument.addPage(solutionPage);
|
||||
try (final PDPageContentStream puzzlePageContentStream = new PDPageContentStream(pdDocument, puzzlePage);
|
||||
final PDPageContentStream solutionPageContentStream = new PDPageContentStream(pdDocument, solutionPage)) {
|
||||
try (final PDPageContentStream puzzlePageContentStream = new PDPageContentStream(pdDocument, puzzlePage); final PDPageContentStream solutionPageContentStream = new PDPageContentStream(pdDocument, solutionPage)) {
|
||||
setUpPageContentStream(puzzlePageContentStream);
|
||||
setUpPageContentStream(solutionPageContentStream);
|
||||
this.drawHorizontalLines(puzzlePageContentStream, solutionPageContentStream);
|
||||
|
@ -69,8 +67,7 @@ class Generator {
|
|||
}
|
||||
|
||||
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.
|
||||
// PDF has the origin in the lower left corner. We want it in the upper left corner, hence some magic is required.
|
||||
Coordinate coordinate = new Coordinate(0f, 0f);
|
||||
// Draw the TOP borders of all tiles.
|
||||
for (int y = 0; y < this.labyrinth.getHeight(); y++) {
|
||||
|
@ -138,8 +135,7 @@ class Generator {
|
|||
}
|
||||
|
||||
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.
|
||||
// PDF has the origin in the lower left corner. We want it in the upper left corner, hence some magic is required.
|
||||
Coordinate coordinate = new Coordinate(0f, 0f);
|
||||
// Draw the LEFT borders of all tiles.
|
||||
for (int x = 0; x < this.labyrinth.getWidth(); x++) {
|
||||
|
@ -209,8 +205,7 @@ class Generator {
|
|||
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.
|
||||
// PDF has the origin in the lower left corner. We want it in the upper left corner, hence some magic is required.
|
||||
final Position end = this.labyrinth.getEnd();
|
||||
Position currentPosition = this.labyrinth.getStart();
|
||||
Position previousPosition = null;
|
||||
|
@ -227,8 +222,7 @@ class Generator {
|
|||
}
|
||||
|
||||
@NonNull
|
||||
private Position findNextSolutionPosition(@Nullable final Position previousPosition,
|
||||
@NonNull final Position currentPosition) {
|
||||
private Position findNextSolutionPosition(@Nullable final Position previousPosition, @NonNull final Position currentPosition) {
|
||||
final Tile currentTile = this.labyrinth.getTileAt(currentPosition).get();
|
||||
for (final Direction direction : Direction.values()) {
|
||||
if (!currentTile.hasWallAt(direction)) {
|
||||
|
@ -287,8 +281,7 @@ class Generator {
|
|||
}
|
||||
|
||||
private float calcY(final int y) {
|
||||
return (Generator.this.labyrinth.getHeight() - y) * PDFRenderer.SCALE - PDFRenderer.SCALE / 2 +
|
||||
PDFRenderer.MARGIN;
|
||||
return (Generator.this.labyrinth.getHeight() - y) * PDFRenderer.SCALE - PDFRenderer.SCALE / 2 + PDFRenderer.MARGIN;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,10 +6,6 @@ import lombok.NonNull;
|
|||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
import static ch.fritteli.labyrinth.generator.serialization.SerializerDeserializer.MAGIC_BYTE_1;
|
||||
import static ch.fritteli.labyrinth.generator.serialization.SerializerDeserializer.MAGIC_BYTE_2;
|
||||
import static ch.fritteli.labyrinth.generator.serialization.SerializerDeserializer.VERSION_BYTE;
|
||||
|
||||
public class LabyrinthInputStream extends ByteArrayInputStream {
|
||||
public LabyrinthInputStream(@NonNull final byte[] buf) {
|
||||
super(buf);
|
||||
|
@ -33,15 +29,15 @@ public class LabyrinthInputStream extends ByteArrayInputStream {
|
|||
|
||||
public void checkHeader() {
|
||||
final byte magic1 = this.readByte();
|
||||
if (magic1 != MAGIC_BYTE_1) {
|
||||
if (magic1 != SerializerDeserializer.MAGIC_BYTE_1) {
|
||||
throw new IllegalArgumentException("Invalid labyrinth data.");
|
||||
}
|
||||
final byte magic2 = this.readByte();
|
||||
if (magic2 != MAGIC_BYTE_2) {
|
||||
if (magic2 != SerializerDeserializer.MAGIC_BYTE_2) {
|
||||
throw new IllegalArgumentException("Invalid labyrinth data.");
|
||||
}
|
||||
final int version = this.readByte();
|
||||
if (version != VERSION_BYTE) {
|
||||
if (version != SerializerDeserializer.VERSION_BYTE) {
|
||||
throw new IllegalArgumentException("Unknown Labyrinth data version: " + version);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,9 +46,9 @@ import java.util.EnumSet;
|
|||
*/
|
||||
@UtilityClass
|
||||
public class SerializerDeserializer {
|
||||
static final byte MAGIC_BYTE_1 = 0x1a;
|
||||
static final byte MAGIC_BYTE_2 = (byte) 0xb1;
|
||||
static final byte VERSION_BYTE = 0x01;
|
||||
final byte MAGIC_BYTE_1 = 0x1a;
|
||||
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;
|
||||
|
@ -89,8 +89,7 @@ public class SerializerDeserializer {
|
|||
final Constructor<Labyrinth> constructor = Labyrinth.class.getDeclaredConstructor(Tile[][].class, Integer.TYPE, Integer.TYPE, Long.TYPE);
|
||||
constructor.setAccessible(true);
|
||||
return constructor.newInstance(field, width, height, randomSeed);
|
||||
} catch (final NoSuchMethodException | IllegalAccessException | InstantiationException |
|
||||
InvocationTargetException e) {
|
||||
} catch (@NonNull final NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
|
||||
throw new RuntimeException("Can not deserialize Labyrinth from labyrinth data.", e);
|
||||
}
|
||||
}
|
||||
|
@ -98,11 +97,10 @@ public class SerializerDeserializer {
|
|||
@NonNull
|
||||
private Tile createTile(@NonNull final EnumSet<Direction> walls, boolean solution) {
|
||||
try {
|
||||
@NonNull final Constructor<Tile> constructor = Tile.class.getDeclaredConstructor(EnumSet.class, Boolean.TYPE);
|
||||
final Constructor<Tile> constructor = Tile.class.getDeclaredConstructor(EnumSet.class, Boolean.TYPE);
|
||||
constructor.setAccessible(true);
|
||||
return constructor.newInstance(walls, solution);
|
||||
} catch (final NoSuchMethodException | InstantiationException | IllegalAccessException |
|
||||
InvocationTargetException e) {
|
||||
} catch (@NonNull final NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException("Can not deserialize Tile from labyrinth data.", e);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue