diff --git a/app/src/main/java/sudoku/structure/MultiDoku.java b/app/src/main/java/sudoku/structure/MultiDoku.java index e000664..ecee5fd 100644 --- a/app/src/main/java/sudoku/structure/MultiDoku.java +++ b/app/src/main/java/sudoku/structure/MultiDoku.java @@ -190,5 +190,13 @@ public class MultiDoku { Cell cellToEmpty = cells.get(cells.indexOf(cell)); cellToEmpty.setSymbolIndex(Cell.NOSYMBOL); } + + public int getNbCells() { + int result = 0; + for (Sudoku sudoku : this.subGrids) { + result += sudoku.getCells().size(); + } + return result; + } } diff --git a/app/src/main/java/sudoku/structure/SudokuFactory.java b/app/src/main/java/sudoku/structure/SudokuFactory.java index cfa0ea7..f0cdb0f 100644 --- a/app/src/main/java/sudoku/structure/SudokuFactory.java +++ b/app/src/main/java/sudoku/structure/SudokuFactory.java @@ -18,6 +18,13 @@ public class SudokuFactory { * Générateur de nombre aléatoire. */ 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. * 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. - * @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. * @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(); } - if (difficulty == 0) { + if (nbCellsToEmpty == 0) { return true; } @@ -130,7 +138,7 @@ public class SudokuFactory { int oldSymbol = cellToEmpty.empty(); if (Solver.countSolution(doku) == 1) { - if (newDokuFromFilledOne(doku, --difficulty)) { + if (newDokuFromFilledOne(doku, --nbCellsToEmpty)) { return true; } } @@ -228,4 +236,30 @@ public class SudokuFactory { 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; + } }