valid tests save and getFromSave sudoku
All checks were successful
Linux arm64 / Build (push) Successful in 42s
All checks were successful
Linux arm64 / Build (push) Successful in 42s
This commit is contained in:
10224
app/save/save.json
10224
app/save/save.json
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,9 @@
|
|||||||
package sudoku.io;
|
package sudoku.io;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -101,26 +104,68 @@ public class SudokuSerializer {
|
|||||||
return jsonRoot;
|
return jsonRoot;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void saveMultiDoku(final MultiDoku doku) {
|
/**
|
||||||
|
* Save a serialized MultiDoku in a JSON file.
|
||||||
|
* @param doku MultiDoku, MultiDoku to save.
|
||||||
|
* @return int, number of the save.
|
||||||
|
*/
|
||||||
|
public static int saveMultiDoku(final MultiDoku doku) {
|
||||||
|
|
||||||
JSONObject jsonRoot = serializeSudoku(doku);
|
JSONObject jsonRoot = serializeSudoku(doku);
|
||||||
|
|
||||||
File f = new File("save", "save.json");
|
File f = new File("save", "save.json");
|
||||||
InputStream is = null;
|
int i = 0;
|
||||||
int i = 1;
|
|
||||||
|
|
||||||
while (f.exists()) {
|
while (f.exists()) {
|
||||||
String newName = "save-" + i + ".json";
|
String newName = "save-" + ++i + ".json";
|
||||||
f = new File("save", newName);
|
f = new File("save", newName);
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try (FileWriter file = new FileWriter(f)) {
|
try (FileWriter file = new FileWriter(f)) {
|
||||||
file.write(jsonRoot.toString(3));
|
file.write(jsonRoot.toString(3));
|
||||||
//file.flush();
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.fillInStackTrace();
|
e.fillInStackTrace();
|
||||||
}
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a MultiDoku from a pre-existing json save file.
|
||||||
|
* @param numberSave int, number of the save file to open.
|
||||||
|
* @return MultiDoku, MultoDoku contained in the file.
|
||||||
|
* @throws Exception when the given save file does not exist.
|
||||||
|
*/
|
||||||
|
public static MultiDoku getSavedMultiDoku(int numberSave) throws Exception {
|
||||||
|
String fileName;
|
||||||
|
if (numberSave < 1) {
|
||||||
|
fileName = "save.json";
|
||||||
|
} else {
|
||||||
|
fileName = "save-" + numberSave + ".json";
|
||||||
|
}
|
||||||
|
|
||||||
|
File f = new File("save", fileName);
|
||||||
|
String fileContent;
|
||||||
|
|
||||||
|
if (!f.exists()) {
|
||||||
|
throw new Exception("This save does not exist.");
|
||||||
|
} else {
|
||||||
|
fileContent = new String(Files.readAllBytes(Paths.get("save/" + fileName)));
|
||||||
|
/*
|
||||||
|
try {
|
||||||
|
FileReader file = new FileReader(f);
|
||||||
|
char[] rawFileContent = {};
|
||||||
|
int length = 1000;
|
||||||
|
while (file.read(rawFileContent, 0, length) != -1) {
|
||||||
|
rawFileContent = new char[]{};
|
||||||
|
length = length * 10;
|
||||||
|
}
|
||||||
|
fileContent = new String(rawFileContent);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new Exception("Error reading file.");
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
return deserializeSudoku(fileContent);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MultiDoku deserializeSudoku(final String json) {
|
public static MultiDoku deserializeSudoku(final String json) {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
|
|
||||||
import sudoku.io.SudokuPrinter;
|
import sudoku.io.SudokuPrinter;
|
||||||
import sudoku.io.SudokuSerializer;
|
import sudoku.io.SudokuSerializer;
|
||||||
|
import sudoku.structure.MultiDoku;
|
||||||
import sudoku.structure.SudokuFactory;
|
import sudoku.structure.SudokuFactory;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
@@ -17,20 +18,41 @@ public class SudokuSerializerTest {
|
|||||||
void testSerializeWithSize(int blockWidth, int blockHeight) {
|
void testSerializeWithSize(int blockWidth, int blockHeight) {
|
||||||
var sudoku = SudokuFactory.createBasicEmptyRectangleSudoku(blockWidth, blockHeight);
|
var sudoku = SudokuFactory.createBasicEmptyRectangleSudoku(blockWidth, blockHeight);
|
||||||
JSONObject data = SudokuSerializer.serializeSudoku(sudoku);
|
JSONObject data = SudokuSerializer.serializeSudoku(sudoku);
|
||||||
SudokuSerializer.saveMultiDoku(sudoku);
|
MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
|
||||||
//MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
|
assert(data.toString().equals(SudokuSerializer.serializeSudoku(multiDoku).toString()));
|
||||||
//assertEquals(data, SudokuSerializer.serializeSudoku(multiDoku));
|
}
|
||||||
|
|
||||||
|
void testSaveWithSize(int blockWidth, int blockHeight) {
|
||||||
|
MultiDoku doku = SudokuFactory.createBasicEmptyRectangleSudoku(blockWidth, blockHeight);
|
||||||
|
int saveNumber = SudokuSerializer.saveMultiDoku(doku);
|
||||||
|
MultiDoku otherDoku = null;
|
||||||
|
try {
|
||||||
|
otherDoku = SudokuSerializer.getSavedMultiDoku(saveNumber);
|
||||||
|
assert (otherDoku != null);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
assert false;
|
||||||
|
}
|
||||||
|
assert(doku.equals(otherDoku));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testSerialize() {
|
void testSerialize() {
|
||||||
int testCount = 5;
|
|
||||||
Random r = new Random();
|
Random r = new Random();
|
||||||
|
int testCount = 5;
|
||||||
for (int i = 0; i < testCount; i++) {
|
for (int i = 0; i < testCount; i++) {
|
||||||
int blockWidth = r.nextInt(20) + 1;
|
int blockWidth = r.nextInt(20) + 1;
|
||||||
int blockHeight = r.nextInt(20) + 1;
|
int blockHeight = r.nextInt(20) + 1;
|
||||||
testSerializeWithSize(blockWidth, blockHeight);
|
testSerializeWithSize(blockWidth, blockHeight);
|
||||||
}
|
}
|
||||||
|
for (int i = 0; i < testCount; i++) {
|
||||||
|
int blockWidth = r.nextInt(20) + 1;
|
||||||
|
int blockHeight = r.nextInt(20) + 1;
|
||||||
|
testSaveWithSize(blockWidth, blockHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user