feat : création MultiDoku à résoudre
All checks were successful
Linux arm64 / Build (push) Successful in 1m7s

This commit is contained in:
Melvyn
2025-01-29 10:21:26 +01:00
parent cd45d1c22c
commit 7c06fe2e31
2 changed files with 48 additions and 6 deletions

View File

@@ -190,5 +190,13 @@ public class MultiDoku {
Cell cellToEmpty = cells.get(cells.indexOf(cell)); Cell cellToEmpty = cells.get(cells.indexOf(cell));
cellToEmpty.setSymbolIndex(Cell.NOSYMBOL); cellToEmpty.setSymbolIndex(Cell.NOSYMBOL);
} }
public int getNbCells() {
int result = 0;
for (Sudoku sudoku : this.subGrids) {
result += sudoku.getCells().size();
}
return result;
}
} }

View File

@@ -18,6 +18,13 @@ public class SudokuFactory {
* Générateur de nombre aléatoire. * Générateur de nombre aléatoire.
*/ */
private static final Random random = new Random(); private static final Random random = new Random();
/**
* Difficulté avec le ration des cases qui seront vides.
*/
private static final double VERY_EASY = 0.1;
private static final double EASY = 0.25;
private static final double MEDIUM = 0.5;
private static final double HARD = 0.75;
/** /**
* Liste des contraintes par défaut d'un Multi- ou Sudoku. * Liste des contraintes par défaut d'un Multi- ou Sudoku.
* Comprend les contraintes de blocs, de lignes, et de colonnes. * Comprend les contraintes de blocs, de lignes, et de colonnes.
@@ -106,18 +113,19 @@ public class SudokuFactory {
/** /**
* Créée un MultiDoku de difficulté difficulty à partir d'un MultiDoku fourni. * Créée un MultiDoku de difficulté difficulty à partir d'un MultiDoku fourni.
* @param doku MultiDoku, MultiDoku dont on doit vider des Cells. *
* @param difficulty int, nombre de cases à retirer. * @param doku MultiDoku, MultiDoku dont on doit vider des Cells.
* @param nbCellsToEmpty int, nombre de cases à retirer.
* @return boolean, valant true si un MultiDoku de difficulté donnée peut être créée, false sinon. * @return boolean, valant true si un MultiDoku de difficulté donnée peut être créée, false sinon.
* @throws Exception si la difficulté n'est pas compatible avec la taille du MultiDoku. * @throws Exception si la difficulté n'est pas compatible avec la taille du MultiDoku.
*/ */
public static boolean newDokuFromFilledOne (MultiDoku doku, int difficulty) throws Exception { public static boolean newDokuFromFilledOne (MultiDoku doku, int nbCellsToEmpty) throws Exception {
if (difficulty > doku.getCells().size()) { if (nbCellsToEmpty > doku.getCells().size()) {
throw new Exception(); throw new Exception();
} }
if (difficulty == 0) { if (nbCellsToEmpty == 0) {
return true; return true;
} }
@@ -130,7 +138,7 @@ public class SudokuFactory {
int oldSymbol = cellToEmpty.empty(); int oldSymbol = cellToEmpty.empty();
if (Solver.countSolution(doku) == 1) { if (Solver.countSolution(doku) == 1) {
if (newDokuFromFilledOne(doku, --difficulty)) { if (newDokuFromFilledOne(doku, --nbCellsToEmpty)) {
return true; return true;
} }
} }
@@ -228,4 +236,30 @@ public class SudokuFactory {
return new MultiDoku(Arrays.asList(sudoku1, sudoku2, sudoku3, sudoku4, sudoku5)); return new MultiDoku(Arrays.asList(sudoku1, sudoku2, sudoku3, sudoku4, sudoku5));
} }
public static MultiDoku createBasicRectangleDokuToSolve(int width, int height, double difficulty) throws Exception {
MultiDoku doku = createBasicEmptyRectangleSudoku(width, height);
Solver.solveRandom(doku, random);
int nbCellsToEmpty = (int)(difficulty*doku.getNbCells());
boolean successfull = newDokuFromFilledOne(doku, nbCellsToEmpty);
if (!successfull) {
throw new Exception("Canno't create this doku with this difficulty");
}
return doku;
}
public static MultiDoku createBasicSquareDokuToSolve(int size, double difficulty) throws Exception {
return createBasicRectangleDokuToSolve(size, size, difficulty);
}
public static MultiDoku createBasicXShapedMultiDokuToSolve(int size, double difficulty) throws Exception {
MultiDoku doku = createBasicXShapedMultidoku(size);
Solver.solveRandom(doku, random);
int nbCellsToEmpty = (int)(difficulty*doku.getNbCells());
boolean successful = newDokuFromFilledOne(doku, nbCellsToEmpty);
if (!successful) {
throw new Exception("Cannot create this Doku with this difficulty");
}
return doku;
}
} }