feature/serialize #1

Merged
manuel merged 7 commits from feature/serialize into master 2022-02-02 01:15:21 +01:00
4 changed files with 77 additions and 41 deletions
Showing only changes of commit c8619f80e8 - Show all commits

View file

@ -16,8 +16,8 @@
<orderEntry type="library" name="Maven: org.jetbrains:annotations:19.0.0" level="project" />
<orderEntry type="library" name="Maven: io.vavr:vavr:0.10.2" level="project" />
<orderEntry type="library" name="Maven: io.vavr:vavr-match:0.10.2" level="project" />
<orderEntry type="library" name="Maven: org.apache.pdfbox:pdfbox:2.0.20" level="project" />
<orderEntry type="library" name="Maven: org.apache.pdfbox:fontbox:2.0.20" level="project" />
<orderEntry type="library" name="Maven: org.apache.pdfbox:pdfbox:2.0.25" level="project" />
<orderEntry type="library" name="Maven: org.apache.pdfbox:fontbox:2.0.25" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.6.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" />

24
pom.xml
View file

@ -8,9 +8,15 @@
<artifactId>fritteli-build-parent</artifactId>
<version>2.0.4</version>
</parent>
<groupId>ch.fritteli.labyrinth</groupId>
<artifactId>labyrinth-generator</artifactId>
<version>0.0.2-SNAPSHOT</version>
<properties>
<pdfbox.version>2.0.25</pdfbox.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
@ -27,7 +33,7 @@
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.20</version>
<version>${pdfbox.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
@ -81,12 +87,24 @@
</distributionManagement>
<repositories>
<repository>
<id>repo.gittr.ch</id>
<url>https://repo.gittr.ch/</url>
<id>repo.gittr.ch.releases</id>
<url>https://repo.gittr.ch/releases/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
<repository>
<id>repo.gittr.ch.snapshots</id>
<url>https://repo.gittr.ch/snapshots/</url>
<releases>
<enabled>false</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>

View file

@ -15,6 +15,13 @@ class Generator {
private final boolean renderSolution;
private int x = 0;
private int y = 0;
// Each row has three lines:
// - The top border
// - The central pathway
// - The bottom border
// The bottom border of one row is identical to the top border of the following row. We use this variable here in
// order to be able to render the bottom border of the last row, which obviously does not have a following row with
// a top border.
private int line = 0;
boolean hasNext() {

View file

@ -12,23 +12,23 @@ import java.util.EnumSet;
/**
* <pre>
* decimal hex border
* 0 0 no border
* 1 1 top
* 2 2 right
* 3 3 top+right
* 4 4 bottom
* 5 5 top+bottom
* 6 6 right+bottom
* 7 7 top+right+bottom
* 8 8 left
* 9 9 top+left
* 10 a right+left
* 11 b top+right+left
* 12 c bottom+left
* 13 d top+bottom+left
* 14 e right+bottom+left
* 15 f top+right+bottom+left
* 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>
* ==&gt; bits 0..2: always 0; bit 3: 1=solution, 0=not solution; bits 4..7: encode walls
* ==&gt; first bytes are:
@ -64,24 +64,9 @@ public class SerializerDeserializer {
*/
@NonNull
public byte[] serialize(@NonNull final Labyrinth labyrinth) {
@NonNull final LabyrinthOutputStream stream = new LabyrinthOutputStream();
final int width = labyrinth.getWidth();
final int height = labyrinth.getHeight();
final long randomSeed = labyrinth.getRandomSeed();
stream.writeByte(MAGIC_BYTE_1);
stream.writeByte(MAGIC_BYTE_2);
stream.writeByte(VERSION_BYTE);
stream.writeLong(randomSeed);
stream.writeInt(width);
stream.writeInt(height);
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.
@NonNull final Tile tile = labyrinth.getTileAt(x, y).get();
final byte bitmask = getBitmaskForTile(tile);
stream.writeByte(bitmask);
}
}
final LabyrinthOutputStream stream = new LabyrinthOutputStream();
writeHeader(stream);
writeLabyrinthData(stream, labyrinth);
return stream.toByteArray();
}
@ -98,6 +83,13 @@ public class SerializerDeserializer {
return readLabyrinthData(stream);
}
private static void writeHeader(@NonNull final LabyrinthOutputStream stream) {
stream.writeByte(MAGIC_BYTE_1);
stream.writeByte(MAGIC_BYTE_2);
stream.writeByte(VERSION_BYTE);
}
private static void checkHeader(@NonNull final LabyrinthInputStream stream) {
final byte magic1 = stream.readByte();
if (magic1 != MAGIC_BYTE_1) {
@ -113,13 +105,32 @@ public class SerializerDeserializer {
}
}
private static void writeLabyrinthData(@NonNull final LabyrinthOutputStream stream, @NonNull final Labyrinth labyrinth) {
final long randomSeed = labyrinth.getRandomSeed();
final int width = labyrinth.getWidth();
final int height = labyrinth.getHeight();
stream.writeLong(randomSeed);
stream.writeInt(width);
stream.writeInt(height);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// We .get() it, because we want to crash hard if it is not available.
final Tile tile = labyrinth.getTileAt(x, y).get();
final byte bitmask = getBitmaskForTile(tile);
stream.writeByte(bitmask);
}
}
}
@NonNull
private static Labyrinth readLabyrinthData(@NonNull final LabyrinthInputStream stream) {
final long randomSeed = stream.readLong();
final int width = stream.readInt();
final int height = stream.readInt();
@NonNull final Tile[][] tiles = new Tile[width][height];
final Tile[][] tiles = new Tile[width][height];
for (int x = 0; x < width; x++) {
tiles[x] = new Tile[height];
}