fix: sudoku save
All checks were successful
Linux arm64 / Build (push) Successful in 23m59s

This commit is contained in:
2025-02-01 16:11:38 +00:00
parent 140d37fbd9
commit 52ca8b208c
3 changed files with 39 additions and 6 deletions

View File

@@ -33,13 +33,21 @@ public class Main {
SudokuSerializer.saveMultiDoku(md);
}
public static void overwriteTest(int n){
MultiDoku md = SudokuFactory.createBasicXShapedMultidoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
new RandomSolver().solve(md);
SudokuPrinter.printMultiDoku(RenderableMultidoku.fromMultidoku(md), Symbols.Numbers, 3, 3);
SudokuSerializer.saveMultiDoku(md, n);
}
public static void main(String[] args) {
ConsoleInterface console = new ConsoleInterface();
/*
voidTest();
filledTest();
filledTest();
*/
overwriteTest(0);
overwriteTest(17)
*/
console.welcome();
}
}

View File

@@ -69,6 +69,8 @@ public class ConsoleInterface {
}
System.out.println("Here's your sudoku !");
showMultidoku(doku, listSymbols, width, height);
System.out.println("You can now save it!");
saveMultiDoku(doku);
}
private MultiDoku saveChoice() {
@@ -175,4 +177,10 @@ public class ConsoleInterface {
SudokuPrinter.printMultiDoku(doku, listSymbols, width, height);
}
private void saveMultiDoku(MultiDoku doku){
System.out.println("Number of the file to overwrite ('-1' or unused save file number to create a new save) :");
int n = reader.nextInt();
String path = SudokuSerializer.saveMultiDoku(doku, n);
System.out.println("The path to your save is:" + path);
}
}

View File

@@ -124,17 +124,13 @@ public class SudokuSerializer {
* @return String, the path of the save.
*/
public static String saveMultiDoku(final MultiDoku doku) {
JSONObject jsonRoot = serializeSudoku(doku);
File f = new File("save", "save.json");
int i = 0;
while (f.exists()) {
String newName = "save-" + ++i + ".json";
f = new File("save", newName);
}
try (FileWriter file = new FileWriter(f)) {
file.write(jsonRoot.toString(3));
} catch (IOException e) {
@@ -143,6 +139,27 @@ public class SudokuSerializer {
return f.getAbsolutePath();
}
public static String saveMultiDoku(final MultiDoku doku, final int saveToOverwrite) {
File f;
if (saveToOverwrite == 0) {
f = new File("save", "save.json");
}
else {
f = new File("save", "save-" + saveToOverwrite + ".json");
}
if (!f.exists()) {
return saveMultiDoku(doku);
}
else {
try (FileWriter file = new FileWriter(f)) {
file.write(serializeSudoku(doku).toString(3));
} catch (IOException e) {
e.printStackTrace();
}
return f.getAbsolutePath();
}
}
/**
* Get a MultiDoku from a pre-existing json save file.
*