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

@@ -1,7 +1,9 @@
package sudoku.structure;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import sudoku.io.SudokuSerializer;
@@ -49,11 +51,11 @@ public class MultiDoku {
* @return List<Cell>
*/
public List<Cell> getCells(){
List<Cell> cells = new ArrayList<>();
Set<Cell> cellsSet = new HashSet<>();
for (Sudoku sudoku : subGrids){
cells.addAll(sudoku.getCells());
cellsSet.addAll(sudoku.getCells());
}
return cells;
return new ArrayList<>(cellsSet);
}
/**
@@ -111,10 +113,10 @@ public class MultiDoku {
* Check si le MultiDoku est valide, en fonction de ses sous-Sudokus.
* @return boolean, true s'il est valide et false sinon.
*/
public boolean isValid() {
public boolean isSolved() {
boolean result = true;
for (Sudoku sudoku : this.subGrids) {
result = sudoku.isValid() && result;
result = sudoku.isSolved() && result;
}
return result;
}
@@ -191,14 +193,17 @@ public class MultiDoku {
cellToEmpty.setSymbolIndex(Cell.NOSYMBOL);
}
/**
* Renvoie le nombre de Cell contenue dans le MultiDoku.
* @return int, nombre de Cell dans le MultiDoku.
*/
public int getNbCells() {
int result = 0;
for (Sudoku sudoku : this.subGrids) {
result += sudoku.getCells().size();
}
return result;
return getCells().size();
}
/**
* Change les Cells de ce MultiDoku avec des symboles, en Cells immuables.
*/
public void setFilledCellsImmutable() {
for (Cell filledCell : getFilledCells()) {
filledCell.setImmutable();

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)) {

View File

@@ -10,6 +10,7 @@ import sudoku.constraint.BlockConstraint;
import sudoku.constraint.ColumnConstraint;
import sudoku.constraint.IConstraint;
import sudoku.constraint.LineConstraint;
import sudoku.io.SudokuPrinter;
import sudoku.solver.Solver;
public class SudokuFactory {
@@ -121,7 +122,8 @@ public class SudokuFactory {
*/
public static boolean newDokuFromFilledOne (MultiDoku doku, int nbCellsToEmpty) throws Exception {
if (nbCellsToEmpty > doku.getCells().size()) {
System.out.println("nbCellsToEmpty : "+nbCellsToEmpty);
if (nbCellsToEmpty >= doku.getCells().size()) {
throw new Exception();
}
@@ -137,7 +139,9 @@ public class SudokuFactory {
int oldSymbol = cellToEmpty.empty();
if (Solver.countSolution(doku) == 1) {
int nbDokuSultions = Solver.countSolution(doku);
System.out.println("oldSymbol : "+oldSymbol);
if (nbDokuSultions == 1) {
if (newDokuFromFilledOne(doku, --nbCellsToEmpty)) {
return true;
}
@@ -239,9 +243,13 @@ public class SudokuFactory {
public static void fillDoku(MultiDoku doku, Difficulty difficulty) throws Exception {
Solver.solveRandom(doku, random);
//SudokuPrinter.printRectangleSudoku(doku.getSubGrid(0), 3, 3);
int nbCellsToEmpty = (int)(difficulty.getFactor()*doku.getNbCells());
boolean successfull = newDokuFromFilledOne(doku, nbCellsToEmpty);
if (!successfull) {
//System.out.println(nbCellsToEmpty);
boolean successful = newDokuFromFilledOne(doku, nbCellsToEmpty);
if (!successful) {
throw new Exception("Canno't create this doku with this difficulty");
}
doku.setFilledCellsImmutable();