From 1419898955d038782510de933d58c8d82e52011b Mon Sep 17 00:00:00 2001 From: Melvyn Date: Sun, 2 Feb 2025 16:22:08 +0100 Subject: [PATCH] refactor : Sudoku --- .../main/java/sudoku/structure/Sudoku.java | 195 ++++++++---------- 1 file changed, 90 insertions(+), 105 deletions(-) diff --git a/app/src/main/java/sudoku/structure/Sudoku.java b/app/src/main/java/sudoku/structure/Sudoku.java index f8411e4..281a8a7 100644 --- a/app/src/main/java/sudoku/structure/Sudoku.java +++ b/app/src/main/java/sudoku/structure/Sudoku.java @@ -12,24 +12,33 @@ import sudoku.constraint.IConstraint; */ public class Sudoku { + // + /** * Liste des Block contenus dans le Sudoku. */ private final List blocks; + /** * Liste des Cells contenus dans le Sudoku. */ - private List cells = new ArrayList<>(); + private final List cells; + /** * Liste des contraintes (TODO) du Sudoku. */ private final List constraints; + /** - * Largeur des Blocks s'ils sont rectangulaires, valant 0 si ce n'est pas le - * cas. + * Largeur des Blocks s'ils sont rectangulaires, + * valant 0 si ce n'est pas le cas. */ private int blockWidth; + // + + // + public Sudoku(List cells, List blocks, List constraints) { this.cells = cells; this.blocks = blocks; @@ -97,31 +106,79 @@ public class Sudoku { } /** - * Vide la Cell dotn les coordonnées sont renseignées de son symbole. - * - * @param x int, abscisse de la Cell voulue. - * @param y int, coordonnée de la Cell voulue. + * Vérifie si le Sudoku est résolue, soit complet et cohérent avec ses + * contraintes. + * + * @return boolean, valant true si le Sudoku est résolu, false sinon. */ - - public void clearCell(int x, int y) { - assert (isValidCoords(x, y)); - Cell cell = getCell(x, y); - cell.setSymbolIndex(Cell.NOSYMBOL); + public boolean isSolved() { + boolean isComplete = isComplete(); + boolean isValid = isValid(); + return isComplete && isValid; } /** - * Vide toutes les Cell du Sudoku. + * Vérifie que le Sudoku est complet, soit qu'il n'y ait aucune case vide. + * + * @return boolean, true si le Sudoku est complet, false sinon. */ - public void clear() { - for (int i = 0; i < getSize() * getSize(); i++) { + private boolean isComplete() { + return getFirstEmptyCell() == null; + } + + /** + * Vérifie si le Sudoku est valide, soit qu'il est cohérent avec ses + * contraintes. + * + * @return bollean, true si le Sudoku est valide, false sinon + */ + private boolean isValid() { + for (int i = 0; i < cells.size(); i++) { Cell cell = getCell(i); - if (cell.isMutable()) - cell.setSymbolIndex(Cell.NOSYMBOL); + if (cell.isEmpty()) + continue; + + Coordinate coordinate = toCoords(i); + + int symbolPlaced = cell.empty(); + if (!canBePlaced(coordinate.getX(), coordinate.getY(), symbolPlaced)) { + cell.setSymbolIndex(symbolPlaced); + return false; + } + cell.setSymbolIndex(symbolPlaced); + } + return true; + } + + /** + * Renvoie la Cell aux coordonées données. + * + * @param x int, abscisse. + * @param y int, ordonnée. + * @return Cell, si une Cell existe aux coordonnées données, null sinon. + */ + public Cell getCell(int x, int y) { + int index = toIndex(x, y); + assert (isValidCoords(x, y)); + try { + return this.cells.get(index); + } catch (IndexOutOfBoundsException e) { + return null; } } - public int getBlockWidth() { - return blockWidth; + /** + * Renvoie la 1re Cell vide du Sudoku. + * + * @return Cell, une Cell vide, ou null s'il n'y en a pas. + */ + public Cell getFirstEmptyCell() { + for (Cell cell : this.cells) { + if (cell.isEmpty()) { + return cell; + } + } + return null; } /** @@ -189,24 +246,14 @@ public class Sudoku { return true; } - public Cell getCell(int x, int y) { - int index = toIndex(x, y); - assert (isValidCoords(x, y)); - try { - return this.cells.get(index); - } catch (IndexOutOfBoundsException e) { - return null; - } + public boolean hasConstraint(Constraint constraint) { + return this.constraints.contains(constraint.getConstraint()); } public Cell getCell(int i) { return this.cells.get(i); } - public List getConstraints() { - return constraints; - } - public int getSize() { return this.blocks.size(); } @@ -219,16 +266,19 @@ public class Sudoku { return this.blocks; } - /** - * Vérifie si une Cell appartient au Sudoku. - * - * @param cell Cell, cellule dont on veut vérifier l'appartenance au Sudoku. - * @return boolean, vaut true si la Cell appartient au Sudoku. - */ - public boolean contains(Cell cell) { - return this.cells.contains(cell); + public List getConstraints() { + return constraints; } + public int getBlockWidth() { + return blockWidth; + } + + public void setBlockWidth(int blockWidth) { + this.blockWidth = blockWidth; + } + + @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("Sudoku {"); @@ -243,71 +293,6 @@ public class Sudoku { return sb.toString(); } - /** - * Renvoie la 1re Cell vide du Sudoku. - * - * @return Cell, une Cell vide, ou null s'il n'y en a pas. - */ - public Cell getFirstEmptyCell() { - for (Cell cell : this.cells) { - if (cell.isEmpty()) { - return cell; - } - } - return null; - } - - /** - * Vérifie si le Sudoku est résolue, soit complet et cohérent avec ses - * contraintes. - * - * @return boolean, valant true si le Sudoku est résolu, false sinon. - */ - public boolean isSolved() { - boolean isComplete = isComplete(); - boolean isValid = isValid(); - return isComplete && isValid; - } - - /** - * Vérifie que le Sudoku est complet, soit qu'il n'y ait aucune case vide. - * - * @return boolean, true si le Sudoku est complet, false sinon. - */ - private boolean isComplete() { - return getFirstEmptyCell() == null; - } - - /** - * Vérifie si le Sudoku est valide, soit qu'il est cohérent avec ses - * contraintes. - * - * @return bollean, true si le Sudoku est valide, false sinon - */ - private boolean isValid() { - for (int i = 0; i < cells.size(); i++) { - Cell cell = getCell(i); - if (cell.isEmpty()) - continue; - - Coordinate coordinate = toCoords(i); - - int symbolPlaced = cell.empty(); - if (!canBePlaced(coordinate.getX(), coordinate.getY(), symbolPlaced)) { - cell.setSymbolIndex(symbolPlaced); - return false; - } - cell.setSymbolIndex(symbolPlaced); - } - return true; - } - - public void setBlockWidth(int blockWidth) { - this.blockWidth = blockWidth; - } - - public boolean hasConstraint(Constraint constraint) { - return this.constraints.contains(constraint.getConstraint()); - } + // }