fix : MultiDoku.getCells

This commit is contained in:
Melvyn
2025-01-29 18:42:58 +01:00
parent 9213a10c17
commit cd4d01e1e6
7 changed files with 132 additions and 50 deletions

View File

@@ -18,7 +18,7 @@ public class Sudoku {
/**
* Liste des Cells contenus dans le Sudoku.
*/
private final List<Cell> cells;
private List<Cell> cells = new ArrayList<>();
/**
* Liste des contraintes (TODO) du Sudoku.
*/
@@ -334,38 +334,95 @@ public class Sudoku {
}
/**
* Vérifie que le Sudoku est cohérent avec ses contraintes.
* @return boolean, valant true si le Sudoku est cohérent avec ses contraintes, false sinon.
* 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 isValid() {
for (Cell cell : this.cells) {
if (cell.isMutable()) {
if (cell.isEmpty()) {
return false;
}
for (IConstraint constraint : this.constraints) {
Coordinate coords;
try {
int symbolPlaced = cell.getSymbolIndex();
coords = this.getCoordinateCell(cell);
public boolean isSolved() {
boolean isComplete = isComplete();
boolean isValid = isValid();
return isComplete && isValid;
}
cell.setSymbolIndex(Cell.NOSYMBOL);
List<Integer> possibleSymbols = constraint.getPossibleSymbols(this, coords.getX(),
coords.getY());
cell.setSymbolIndex(symbolPlaced);
if (possibleSymbols.size() != 1 || possibleSymbols.get(0) != symbolPlaced) {
return false;
}
/**
* 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;
}
} catch (Exception e) {
throw new RuntimeException(e);
/**
* 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 (Cell cell : this.getFilledCells()) {
for (IConstraint constraint : this.constraints) {
try {
Coordinate coords = this.getCoordinateCell(cell);
int symbolPlaced = cell.empty();
List<Integer> possibleSymbols = constraint.getPossibleSymbols(
this,
coords.getX(),
coords.getY()
);
cell.setSymbolIndex(symbolPlaced);
if (!possibleSymbols.contains(symbolPlaced)) {
return false;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
return true;
}
/**
* Renvoie la liste des Cells remplies.
* @return List<Cell>
*/
private List<Cell> getFilledCells() {
List<Cell> result = new ArrayList<>();
for (Cell cell : getCells()) {
if (!cell.isEmpty()) {
result.add(cell);
}
}
return result;
}
/**
* Renvoie la liste des Cells modifiables.
* @return List<Cell>
*/
private List<Cell> getEmptyCells() {
List<Cell> result = new ArrayList<>();
for (Cell cell : getCells()) {
if (cell.isMutable()) {
result.add(cell);
}
}
return result;
}
/**
* Renvoie la liste des Cells immuables.
* @return List<Cell>
*/
private List<Cell> getImmutableCells() {
List<Cell> result = new ArrayList<>();
for (Cell cell : getCells()) {
if (!cell.isMutable()) {
result.add(cell);
}
}
return result;
}
@Override
public boolean equals(Object object) {
if (!(object instanceof Sudoku)) {