Various clean ups.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Manuel Friedli 2023-04-04 02:06:24 +02:00
parent e1781dbdc4
commit 2275651cc1
Signed by: manuel
GPG key ID: 41D08ABA75634DA1
3 changed files with 16 additions and 29 deletions

View file

@ -33,15 +33,13 @@ class Generator {
final PDDocument pdDocument = new PDDocument(); final PDDocument pdDocument = new PDDocument();
final PDDocumentInformation info = new PDDocumentInformation(); final PDDocumentInformation info = new PDDocumentInformation();
info.setTitle("Labyrinth " + this.labyrinth.getWidth() + "x" + this.labyrinth.getHeight() + ", ID " + info.setTitle("Labyrinth %sx%s, ID %s".formatted(this.labyrinth.getWidth(), this.labyrinth.getHeight(), this.labyrinth.getRandomSeed()));
this.labyrinth.getRandomSeed());
pdDocument.setDocumentInformation(info); pdDocument.setDocumentInformation(info);
final PDPage puzzlePage = new PDPage(new PDRectangle(pageWidth, pageHeight)); final PDPage puzzlePage = new PDPage(new PDRectangle(pageWidth, pageHeight));
final PDPage solutionPage = new PDPage(new PDRectangle(pageWidth, pageHeight)); final PDPage solutionPage = new PDPage(new PDRectangle(pageWidth, pageHeight));
pdDocument.addPage(puzzlePage); pdDocument.addPage(puzzlePage);
pdDocument.addPage(solutionPage); pdDocument.addPage(solutionPage);
try (final PDPageContentStream puzzlePageContentStream = new PDPageContentStream(pdDocument, puzzlePage); 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(puzzlePageContentStream);
setUpPageContentStream(solutionPageContentStream); setUpPageContentStream(solutionPageContentStream);
this.drawHorizontalLines(puzzlePageContentStream, solutionPageContentStream); this.drawHorizontalLines(puzzlePageContentStream, solutionPageContentStream);
@ -69,8 +67,7 @@ class Generator {
} }
private void drawHorizontalLines(@NonNull 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 // PDF has the origin in the lower left corner. We want it in the upper left corner, hence some magic is required.
// required.
Coordinate coordinate = new Coordinate(0f, 0f); Coordinate coordinate = new Coordinate(0f, 0f);
// Draw the TOP borders of all tiles. // Draw the TOP borders of all tiles.
for (int y = 0; y < this.labyrinth.getHeight(); y++) { for (int y = 0; y < this.labyrinth.getHeight(); y++) {
@ -138,8 +135,7 @@ class Generator {
} }
private void drawVerticalLines(@NonNull 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 // PDF has the origin in the lower left corner. We want it in the upper left corner, hence some magic is required.
// required.
Coordinate coordinate = new Coordinate(0f, 0f); Coordinate coordinate = new Coordinate(0f, 0f);
// Draw the LEFT borders of all tiles. // Draw the LEFT borders of all tiles.
for (int x = 0; x < this.labyrinth.getWidth(); x++) { for (int x = 0; x < this.labyrinth.getWidth(); x++) {
@ -209,8 +205,7 @@ class Generator {
private void drawSolution(@NonNull final PDPageContentStream pageContentStream) throws IOException { private void drawSolution(@NonNull final PDPageContentStream pageContentStream) throws IOException {
// Draw the solution in red // Draw the solution in red
pageContentStream.setStrokingColor(Color.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 // PDF has the origin in the lower left corner. We want it in the upper left corner, hence some magic is required.
// required.
final Position end = this.labyrinth.getEnd(); final Position end = this.labyrinth.getEnd();
Position currentPosition = this.labyrinth.getStart(); Position currentPosition = this.labyrinth.getStart();
Position previousPosition = null; Position previousPosition = null;
@ -227,8 +222,7 @@ class Generator {
} }
@NonNull @NonNull
private Position findNextSolutionPosition(@Nullable final Position previousPosition, private Position findNextSolutionPosition(@Nullable final Position previousPosition, @NonNull final Position currentPosition) {
@NonNull final Position currentPosition) {
final Tile currentTile = this.labyrinth.getTileAt(currentPosition).get(); final Tile currentTile = this.labyrinth.getTileAt(currentPosition).get();
for (final Direction direction : Direction.values()) { for (final Direction direction : Direction.values()) {
if (!currentTile.hasWallAt(direction)) { if (!currentTile.hasWallAt(direction)) {
@ -287,8 +281,7 @@ class Generator {
} }
private float calcY(final int y) { private float calcY(final int y) {
return (Generator.this.labyrinth.getHeight() - y) * PDFRenderer.SCALE - PDFRenderer.SCALE / 2 + return (Generator.this.labyrinth.getHeight() - y) * PDFRenderer.SCALE - PDFRenderer.SCALE / 2 + PDFRenderer.MARGIN;
PDFRenderer.MARGIN;
} }
} }
} }

View file

@ -6,10 +6,6 @@ import lombok.NonNull;
import java.io.ByteArrayInputStream; 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 class LabyrinthInputStream extends ByteArrayInputStream {
public LabyrinthInputStream(@NonNull final byte[] buf) { public LabyrinthInputStream(@NonNull final byte[] buf) {
super(buf); super(buf);
@ -33,15 +29,15 @@ public class LabyrinthInputStream extends ByteArrayInputStream {
public void checkHeader() { public void checkHeader() {
final byte magic1 = this.readByte(); final byte magic1 = this.readByte();
if (magic1 != MAGIC_BYTE_1) { if (magic1 != SerializerDeserializer.MAGIC_BYTE_1) {
throw new IllegalArgumentException("Invalid labyrinth data."); throw new IllegalArgumentException("Invalid labyrinth data.");
} }
final byte magic2 = this.readByte(); final byte magic2 = this.readByte();
if (magic2 != MAGIC_BYTE_2) { if (magic2 != SerializerDeserializer.MAGIC_BYTE_2) {
throw new IllegalArgumentException("Invalid labyrinth data."); throw new IllegalArgumentException("Invalid labyrinth data.");
} }
final int version = this.readByte(); final int version = this.readByte();
if (version != VERSION_BYTE) { if (version != SerializerDeserializer.VERSION_BYTE) {
throw new IllegalArgumentException("Unknown Labyrinth data version: " + version); throw new IllegalArgumentException("Unknown Labyrinth data version: " + version);
} }
} }

View file

@ -46,9 +46,9 @@ import java.util.EnumSet;
*/ */
@UtilityClass @UtilityClass
public class SerializerDeserializer { public class SerializerDeserializer {
static final byte MAGIC_BYTE_1 = 0x1a; final byte MAGIC_BYTE_1 = 0x1a;
static final byte MAGIC_BYTE_2 = (byte) 0xb1; final byte MAGIC_BYTE_2 = (byte) 0xb1;
static final byte VERSION_BYTE = 0x01; final byte VERSION_BYTE = 0x01;
private final byte TOP_BIT = 0b0000_0001; private final byte TOP_BIT = 0b0000_0001;
private final byte RIGHT_BIT = 0b0000_0010; 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); final Constructor<Labyrinth> constructor = Labyrinth.class.getDeclaredConstructor(Tile[][].class, Integer.TYPE, Integer.TYPE, Long.TYPE);
constructor.setAccessible(true); constructor.setAccessible(true);
return constructor.newInstance(field, width, height, randomSeed); return constructor.newInstance(field, width, height, randomSeed);
} catch (final NoSuchMethodException | IllegalAccessException | InstantiationException | } catch (@NonNull final NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
InvocationTargetException e) {
throw new RuntimeException("Can not deserialize Labyrinth from labyrinth data.", e); throw new RuntimeException("Can not deserialize Labyrinth from labyrinth data.", e);
} }
} }
@ -98,11 +97,10 @@ public class SerializerDeserializer {
@NonNull @NonNull
private Tile createTile(@NonNull final EnumSet<Direction> walls, boolean solution) { private Tile createTile(@NonNull final EnumSet<Direction> walls, boolean solution) {
try { 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); constructor.setAccessible(true);
return constructor.newInstance(walls, solution); return constructor.newInstance(walls, solution);
} catch (final NoSuchMethodException | InstantiationException | IllegalAccessException | } catch (@NonNull final NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
InvocationTargetException e) {
throw new RuntimeException("Can not deserialize Tile from labyrinth data.", e); throw new RuntimeException("Can not deserialize Tile from labyrinth data.", e);
} }
} }