refactor : Sudoku
Some checks failed
Linux arm64 / Build (push) Failing after 32s

This commit is contained in:
Melvyn
2025-02-02 16:22:08 +01:00
parent dd3be67be0
commit 1419898955

View File

@@ -12,24 +12,33 @@ import sudoku.constraint.IConstraint;
*/
public class Sudoku {
// <editor-fold defaultstate="collapsed" desc="ATTRIBUTS">
/**
* Liste des Block contenus dans le Sudoku.
*/
private final List<Block> blocks;
/**
* Liste des Cells contenus dans le Sudoku.
*/
private List<Cell> cells = new ArrayList<>();
private final List<Cell> cells;
/**
* Liste des contraintes (TODO) du Sudoku.
*/
private final List<IConstraint> 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;
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="METHODES">
public Sudoku(List<Cell> cells, List<Block> blocks, List<IConstraint> 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.
* Vérifie si le Sudoku est résolue, soit complet et cohérent avec ses
* contraintes.
*
* @param x int, abscisse de la Cell voulue.
* @param y int, coordonnée de la Cell voulue.
* @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<IConstraint> 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<IConstraint> 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());
}
// <editor-fold defaultstate="collapsed" desc="METHODES">
}