diff --git a/app/src/main/java/sudoku/structure/MultiDoku.java b/app/src/main/java/sudoku/structure/MultiDoku.java index 98fe623..995896f 100644 --- a/app/src/main/java/sudoku/structure/MultiDoku.java +++ b/app/src/main/java/sudoku/structure/MultiDoku.java @@ -1,85 +1,41 @@ package sudoku.structure; +import sudoku.io.SudokuSerializer; + import java.util.ArrayList; import java.util.HashSet; import java.util.List; -import java.util.Random; import java.util.Set; -import sudoku.io.SudokuSerializer; - /** * @class MultiDoku * @brief Représente une grille de Multidoku. - * Une grille de sudoku est un multidoku avec un seul sous-sudoku + * Une grille de sudoku est un multidoku avec un seul sous-sudoku. */ public class MultiDoku { + // + /** * Liste des sous-Sudoku contenue dans le multidoku. */ private final List subGrids; + /** + * Pile, qui contient des états du MultiDoku, + * utile pour la résolution. + */ private final StateManager stateManager; + // + + // + public MultiDoku(List subGrids) { this.subGrids = subGrids; this.stateManager = new StateManager(this); } - /** - * Renvoie le nombre de sudoku contenu dans ce MultiDoku. - * - * @return int - */ - public int getNbSubGrids() { - return subGrids.size(); - } - - /** - * Renvoie la ie sudoku contenue dans ce MultiDoku. - * - * @param i int, indice du sudoku à renvoyer. - * @return Sudoku, ie Sudoku - */ - public Sudoku getSubGrid(int i) { - return subGrids.get(i); - } - - /** - * Renvoie la liste des Cells contenue dans ce MultiDoku, - * soit les Cells contenues de chaques sous-Sudoku. - * - * @return List - */ - public List getCells() { - Set cellsSet = new HashSet<>(); - for (Sudoku sudoku : subGrids) { - cellsSet.addAll(sudoku.getCells()); - } - return new ArrayList<>(cellsSet); - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("Multidoku {"); - for (Sudoku sudoku : subGrids) { - sb.append("\n\t").append(sudoku.toString()); - } - sb.append("\n}"); - return sb.toString(); - } - - /** - * Renvoie les sous-Sudoku - * - * @return List - */ - public List getSubGrids() { - return this.subGrids; - } - /** * Check si le MultiDoku est résolu, c'est à dire complet et cohérent avec ses contraintes. * @@ -93,21 +49,6 @@ public class MultiDoku { return true; } - /** - * Renvoie la 1re Cell vide des sous-Sudoku. - * - * @return Cell, une Cell vide, ou null s'il n'y en a pas. - */ - public Cell getFirstEmptyCell() { - for (Sudoku sudoku : this.subGrids) { - Cell cellTmp = sudoku.getFirstEmptyCell(); - if (cellTmp != null) { - return cellTmp; - } - } - return null; - } - /** * Renvoie la liste des Cells préalablement remplies du MultiDoku. * @@ -139,14 +80,51 @@ public class MultiDoku { } /** - * Vide une Cell donnée. - * - * @param cell Cell, à vider. + * Renvoie la 1re Cell vide des sous-Sudoku. + * + * @return Cell, une Cell vide, ou null s'il n'y en a pas. */ - public void empty(Cell cell) { - List cells = getCells(); - Cell cellToEmpty = cells.get(cells.indexOf(cell)); - cellToEmpty.setSymbolIndex(Cell.NOSYMBOL); + public Cell getFirstEmptyCell() { + for (Sudoku sudoku : this.subGrids) { + Cell cellTmp = sudoku.getFirstEmptyCell(); + if (cellTmp != null) { + return cellTmp; + } + } + return null; + } + + /** + * Renvoie le nombre de sudoku contenu dans ce MultiDoku. + * + * @return int + */ + public int getNbSubGrids() { + return subGrids.size(); + } + + /** + * Renvoie la ie sudoku contenue dans ce MultiDoku. + * + * @param i int, indice du sudoku à renvoyer. + * @return Sudoku, ie Sudoku + */ + public Sudoku getSubGrid(int i) { + return subGrids.get(i); + } + + /** + * Renvoie la liste des Cells contenue dans ce MultiDoku, + * soit les Cells contenues de chaques sous-Sudoku. + * + * @return List + */ + public List getCells() { + Set cellsSet = new HashSet<>(); + for (Sudoku sudoku : subGrids) { + cellsSet.addAll(sudoku.getCells()); + } + return new ArrayList<>(cellsSet); } /** @@ -167,22 +145,9 @@ public class MultiDoku { } } - public StateManager getStateManager() { - return stateManager; - } - /** - * Renvoie une Cell vide choisie aléatoirement. - * - * @param rand Random, pour le choix aléatoire. - * @return Cell, une Cell vide. + * Vide les Cells modifiable. */ - public Cell getRandomEmptyCell(Random rand) { - List emptyCells = getEmptyCells(); - int randomIndex = rand.nextInt(emptyCells.size()); - return emptyCells.get(randomIndex); - } - public void clearMutableCells() { for (Sudoku s : getSubGrids()) { for (Cell cell : s.getCells()) { @@ -192,11 +157,23 @@ public class MultiDoku { } } + /** + * Renvoie les sous-Sudoku + * + * @return List + */ + public List getSubGrids() { + return this.subGrids; + } + + public StateManager getStateManager() { + return stateManager; + } + @Override public boolean equals(Object other) { - if (!(other instanceof MultiDoku)) + if (!(other instanceof MultiDoku otherDoku)) return false; - MultiDoku otherDoku = (MultiDoku) other; if (this.getNbSubGrids() != otherDoku.getNbSubGrids()) return false; for (int i = 0; i < this.getNbSubGrids(); i++) { @@ -212,8 +189,21 @@ public class MultiDoku { return true; } + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("Multidoku {"); + for (Sudoku sudoku : subGrids) { + sb.append("\n\t").append(sudoku.toString()); + } + sb.append("\n}"); + return sb.toString(); + } + public MultiDoku clone() { - // TODO: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah + // TODO: C'est pas dingue de le faire comme ça... return SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(this)); } + + // }