Merge branch 'constraint'
All checks were successful
Linux arm64 / Build (push) Successful in 4m2s

This commit is contained in:
2025-02-01 22:29:03 +01:00
8 changed files with 119 additions and 59 deletions

View File

@@ -45,7 +45,7 @@ public class ConsoleInterface {
System.out.println("Your sudoku will have blocks of a " + width + " x " + height + " format.");
int numberOfSymbols = width * height;
List<String> listSymbols = pickSymbols(numberOfSymbols);
List<Constraint> listConstraints = getListConstraints();
List<IConstraint> listConstraints = getListConstraints();
System.out.println("Now that we have the size of our sudoku, " +
"would you rather have a single grid ('one', default), " +
"or a a multidoku composed of 5 subgrids ('multi') ?");
@@ -171,12 +171,12 @@ public class ConsoleInterface {
}
}
private List<Constraint> getListConstraints() {
List<Constraint> listConstraints = SudokuFactory.DEFAULT_CONSTRAINTS;
private List<IConstraint> getListConstraints() {
List<IConstraint> listConstraints = SudokuFactory.DEFAULT_CONSTRAINTS;
System.out.println(
"The sudoku have constraints of blocks, lines and columns. Would you like to add the diagonal constraints ? (y/n, default 'no')");
if (reader.next().equalsIgnoreCase("y")) {
listConstraints.add(Constraint.Diagonal);
listConstraints.add(Constraint.Diagonal.getConstraint());
}
return listConstraints;
}

View File

@@ -12,6 +12,7 @@ import org.json.JSONArray;
import org.json.JSONObject;
import sudoku.constraint.Constraint;
import sudoku.constraint.IConstraint;
import sudoku.structure.Block;
import sudoku.structure.Cell;
import sudoku.structure.MultiDoku;
@@ -44,7 +45,7 @@ public class SudokuSerializer {
}
int blockID = blockIds.indexOf(block);
assert(blockID >= 0);
assert (blockID >= 0);
int symbolIndex = cell.getSymbolIndex();
JSONObject cellJsonObject = new JSONObject();
@@ -99,8 +100,17 @@ public class SudokuSerializer {
// serialize constraints
for (Constraint cons : sudoku.getConstraints()) {
constraintsJsonArray.put(cons.ordinal());
for (IConstraint cons : sudoku.getConstraints()) {
boolean constraintSerialized = false;
for (Constraint enumCons : Constraint.values()) {
if (cons.getClass().isAssignableFrom(enumCons.getConstraint().getClass())) {
constraintSerialized = true;
constraintsJsonArray.put(enumCons.ordinal());
}
}
if (!constraintSerialized) {
System.out.println("La contrainte " + cons.getClass() + " n'a pas pu être sérialisé !");
}
}
jsonSudoku.put("constraints", constraintsJsonArray);
@@ -125,6 +135,7 @@ public class SudokuSerializer {
*/
public static String saveMultiDoku(final MultiDoku doku) {
JSONObject jsonRoot = serializeSudoku(doku);
File f = new File("save", "save.json");
int i = 0;
while (f.exists()) {
@@ -143,14 +154,12 @@ public class SudokuSerializer {
File f;
if (saveToOverwrite == 0) {
f = new File("save", "save.json");
}
else {
} else {
f = new File("save", "save-" + saveToOverwrite + ".json");
}
if (!f.exists()) {
return saveMultiDoku(doku);
}
else {
} else {
try (FileWriter file = new FileWriter(f)) {
file.write(serializeSudoku(doku).toString(3));
} catch (IOException e) {
@@ -234,7 +243,7 @@ public class SudokuSerializer {
List<Cell> sudokuCells = new ArrayList<>();
List<Block> sudokuBlocks = new ArrayList<>();
List<Constraint> sudokuConstraints = new ArrayList<>();
List<IConstraint> sudokuConstraints = new ArrayList<>();
for (int j = 0; j < sudokuCellsJsonArray.length(); j++) {
int cellID = sudokuCellsJsonArray.getInt(j);
@@ -248,7 +257,7 @@ public class SudokuSerializer {
for (int j = 0; j < sudokuConstraintsJsonArray.length(); j++) {
int constraintID = sudokuConstraintsJsonArray.getInt(j);
sudokuConstraints.add(Constraint.values()[constraintID]);
sudokuConstraints.add(Constraint.values()[constraintID].getConstraint());
}
Sudoku s = new Sudoku(sudokuCells, sudokuBlocks, sudokuConstraints);