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,12 +33,20 @@ public class Main {
SudokuSerializer.saveMultiDoku(md); 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) { public static void main(String[] args) {
ConsoleInterface console = new ConsoleInterface(); ConsoleInterface console = new ConsoleInterface();
/* /*
voidTest(); voidTest();
filledTest(); filledTest();
filledTest(); overwriteTest(0);
overwriteTest(17)
*/ */
console.welcome(); console.welcome();
} }

View File

@@ -69,6 +69,8 @@ public class ConsoleInterface {
} }
System.out.println("Here's your sudoku !"); System.out.println("Here's your sudoku !");
showMultidoku(doku, listSymbols, width, height); showMultidoku(doku, listSymbols, width, height);
System.out.println("You can now save it!");
saveMultiDoku(doku);
} }
private MultiDoku saveChoice() { private MultiDoku saveChoice() {
@@ -175,4 +177,10 @@ public class ConsoleInterface {
SudokuPrinter.printMultiDoku(doku, listSymbols, width, height); 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. * @return String, the path of the save.
*/ */
public static String saveMultiDoku(final MultiDoku doku) { public static String 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");
int i = 0; int i = 0;
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);
} }
try (FileWriter file = new FileWriter(f)) { try (FileWriter file = new FileWriter(f)) {
file.write(jsonRoot.toString(3)); file.write(jsonRoot.toString(3));
} catch (IOException e) { } catch (IOException e) {
@@ -143,6 +139,27 @@ public class SudokuSerializer {
return f.getAbsolutePath(); 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. * Get a MultiDoku from a pre-existing json save file.
* *