valid tests save and getFromSave sudoku
All checks were successful
Linux arm64 / Build (push) Successful in 42s

This commit is contained in:
Janet-Doe
2025-01-29 10:55:35 +01:00
parent b8553428ee
commit aa86e9b956
3 changed files with 284 additions and 10027 deletions

View File

@@ -1,6 +1,9 @@
package sudoku.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.List;
@@ -101,26 +104,68 @@ public class SudokuSerializer {
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);
File f = new File("save", "save.json");
InputStream is = null;
int i = 1;
int i = 0;
while (f.exists()) {
String newName = "save-" + i + ".json";
String newName = "save-" + ++i + ".json";
f = new File("save", newName);
i++;
}
try (FileWriter file = new FileWriter(f)) {
file.write(jsonRoot.toString(3));
//file.flush();
} catch (IOException e) {
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) {