File Parser for levels #18

Merged
tonitch merged 29 commits from MapParser into master 2023-04-21 20:00:16 +02:00
4 changed files with 114 additions and 5 deletions
Showing only changes of commit 0188fd8420 - Show all commits

View File

@ -47,11 +47,16 @@ public class BinaryParser implements FileParser {
if (save_data){
for (Piece p : level_data.getPieces()) {
Vec2 _piece_pos = p.getPosition();
data[i++] = (byte) _piece_pos.x;
data[i++] = (byte) _piece_pos.y;
if(_piece_pos == null){
data[i++] = 'F';
data[i++] = 'L';
}else{
data[i++] = (byte) _piece_pos.x;
data[i++] = (byte) _piece_pos.y;
}
}
}
data[i++] = 'S'; data[i++] = 'M'; data[++i] = 'E';
data[i++] = 'S'; data[i++] = 'M'; data[i++] = 'E';
FileOutputStream save_file = new FileOutputStream(file);
save_file.write(data);
save_file.flush();
@ -146,7 +151,7 @@ public class BinaryParser implements FileParser {
byte[] ret = new byte[width * height / 8 + width * height % 8 == 0 ? 0 : 1 ];
for (int i = 0; i < ret.length; i++) {
byte b = 0;
for (int b_count = i * 8; b_count < i * 8 + 8; b_count++) {
for (int b_count = i * 8; b_count < (i * 8 + 8) % (i * 8 - ret.length); b_count++) {
b = (byte) ((b << 1) | (list[b_count] ? 1 : 0));
}
ret[i] = b;

View File

@ -56,4 +56,14 @@ public class Piece extends Shape{
matrix = temp_matrix;
}
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Piece pieceObj){
if( pieceObj.getPosition().equals(this.getPosition()) && pieceObj.getShape().equals(getShape())) {
return true;
}
}
return false;
}
}

View File

@ -7,7 +7,7 @@ import java.io.Serializable;
* Base class for everything that is a shape kind, like map and pieces
* it contain a matrix of boolean where the shape is defined by the true's
*/
public class Shape implements Serializable {
public class Shape implements Serializable, Cloneable{
protected boolean[][] matrix;
protected int height, width;

View File

@ -0,0 +1,94 @@
package school_project.Parsers;
import org.junit.jupiter.api.*;
import school_project.Map;
import school_project.Piece;
import java.io.File;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.*;
@TestMethodOrder(value = MethodOrderer.OrderAnnotation.class)
class FileParserFactoryTest {
static Map generateMapTest(){
boolean[][] map_shape = {
{true, true, true, true},
{true, false, true, true},
{true, true, false, true},
{true, true, false, false},
};
boolean[][] piece1_shape = {
{true, true},
{true, false},
{true, true},
{true, true},
};
boolean[][] piece2_shape = {
{true, true},
};
boolean[][] piece3_shape = {
{true, true},
{false, true},
};
Piece[] pieces = { new Piece(piece1_shape), new Piece(piece2_shape), new Piece(piece3_shape) };
Map map = new Map(map_shape);
map.addPiece(pieces);
return map;
}
@Test
void saveFileFromMap_Binary() throws IOException {
Map map = generateMapTest();
FileParserFactory.saveFileFromMap(new File("TestBinaryLevel.level"), map);
}
@Test
void saveFileFromMap_save_Binary() throws IOException {
Map map = generateMapTest();
FileParserFactory.saveFileFromMap(new File("TestBinarySave.slevel"), map);
}
@Test
void saveFileFromMap_Serialized() throws IOException {
Map map = generateMapTest();
FileParserFactory.saveFileFromMap(new File("TestSerializedLevel.serialized"), map);
}
@Test
void saveFileFromMap_save_Serialized() throws IOException{
Map map = generateMapTest();
FileParserFactory.saveFileFromMap(new File("TestSerializedSave.sserialized"), map);
}
@Test
void loadMapFromFile_Binary() throws IOException {
Map map = generateMapTest();
Map testMap = FileParserFactory.loadMapFromFile(new File("src/test/resources/saveFileExamples/TestBinaryLevel.level"));
assertArrayEquals(map.getCleanedMap().getShape(), testMap.getShape());
assertTrue(map.getCleanedMap().getPieces().equals(testMap.getPieces()));
}
@Test
void loadMapFromFile_saved_Binary() throws IOException {
Map map = generateMapTest();
Map testMap = FileParserFactory.loadMapFromFile(new File("src/test/resources/saveFileExamples/TestBinarySave.slevel"));
assertArrayEquals(map.getShape(), testMap.getShape());
assert(map.getPieces().equals(testMap.getPieces()));
}
@Test
void loadMapFromFile_Serialized() throws IOException {
Map map = generateMapTest();
Map testMap = FileParserFactory.loadMapFromFile(new File("src/test/resources/saveFileExamples/TestSerializedLevel.serialized"));
assertArrayEquals(map.getCleanedMap().getShape(), testMap.getShape());
assert(map.getCleanedMap().getPieces().equals(testMap.getPieces()));
}
@Test
void loadMapFromFile_saved_Serialized() throws IOException {
Map map = generateMapTest();
Map testMap = FileParserFactory.loadMapFromFile(new File("src/test/resources/saveFileExamples/TestSerializedSave.sserialized"));
assertArrayEquals(map.getShape(), testMap.getShape());
assertTrue(map.getPieces().equals(testMap.getPieces()));
}
}