fix : MultiDoku.getCells
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package sudoku.constraint;
|
||||
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
public class ColumnConstraint implements IConstraint {
|
||||
@@ -7,10 +8,12 @@ public class ColumnConstraint implements IConstraint {
|
||||
@Override
|
||||
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
|
||||
for (int i = 0; i < s.getSize(); i++) {
|
||||
if (s.getCell(x, i).getSymbolIndex() == newSymbolIndex)
|
||||
Cell cell = s.getCell(x, i);
|
||||
int symbol = cell.getSymbolIndex();
|
||||
if (symbol == newSymbolIndex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,12 +32,12 @@ public class Solver {
|
||||
throw new CancellationException("User wants to stop the solver");
|
||||
|
||||
Sudoku sudoku = doku.getSubGrid(0);
|
||||
logger.log(Level.INFO,
|
||||
logger.log(Level.FINE,
|
||||
'\n' + SudokuPrinter.toStringRectangleSudoku(sudoku,
|
||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getBlockWidth(),
|
||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getSize() / sudoku.getBlockWidth()));
|
||||
|
||||
if (doku.isValid()) {
|
||||
if (doku.isSolved()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ public class Solver {
|
||||
|
||||
/**
|
||||
* Compte le nombre de solutions possibles au MultiDoku passé en paramètres.
|
||||
* @param doku MultiDoku, MultiDoku dont on veut le nombre de solutions.
|
||||
* @param oldDoku MultiDoku, MultiDoku dont on veut le nombre de solutions.
|
||||
* @return int, nombre de solutions possibles.
|
||||
*/
|
||||
public static int countSolution(MultiDoku oldDoku) {
|
||||
@@ -73,21 +73,23 @@ public class Solver {
|
||||
|
||||
MultiDoku doku = oldDoku.clone();
|
||||
|
||||
if (doku.isValid()) {
|
||||
if (doku.isSolved()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
Cell cellToFill = doku.getFirstEmptyCell();
|
||||
if (cellToFill == null) {
|
||||
System.out.println("AAAAAAAAAAAAAA");
|
||||
return 0;
|
||||
}
|
||||
|
||||
List<Integer> possibleSymbols = doku.getPossibleSymbolsOfCell(cellToFill);
|
||||
|
||||
for (int symbol : possibleSymbols) {
|
||||
|
||||
cellToFill.setSymbolIndex(symbol);
|
||||
if (Solver.solve(doku)) {
|
||||
System.out.println("symbol : "+symbol);
|
||||
System.out.println("doku.isSolved() || Solver.solve(doku) ? "+ (doku.isSolved() || Solver.solve(doku)));
|
||||
if (doku.isSolved() || Solver.solve(doku)) {
|
||||
result++;
|
||||
}
|
||||
cellToFill.setSymbolIndex(Cell.NOSYMBOL);
|
||||
@@ -105,7 +107,7 @@ public class Solver {
|
||||
if (Thread.interrupted())
|
||||
throw new CancellationException("User wants to stop the solver");
|
||||
|
||||
if (doku.isValid()) {
|
||||
if (doku.isSolved()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -144,6 +146,13 @@ public class Solver {
|
||||
List<Cell> cellsToFill = doku.getEmptyCells();
|
||||
|
||||
while (!cellsToFill.isEmpty()) {
|
||||
|
||||
Sudoku sudoku = doku.getSubGrid(0);
|
||||
logger.log(Level.FINE,
|
||||
'\n' + SudokuPrinter.toStringRectangleSudoku(sudoku,
|
||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getBlockWidth(),
|
||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getSize() / sudoku.getBlockWidth()));
|
||||
|
||||
boolean blocked = true;
|
||||
for (Cell cellToFill : cellsToFill) {
|
||||
|
||||
@@ -163,6 +172,6 @@ public class Solver {
|
||||
}
|
||||
}
|
||||
|
||||
return doku.isValid();
|
||||
return doku.isSolved();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ public class StupidSolver {
|
||||
}
|
||||
|
||||
public static boolean solve(MultiDoku doku) {
|
||||
if (doku.isValid())
|
||||
if (doku.isSolved())
|
||||
return true;
|
||||
|
||||
for (Sudoku sudoku : doku.getSubGrids()) {
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -56,7 +56,7 @@ class SolverTest {
|
||||
SudokuPrinter.printRectangleSudoku(sudokuResult, 3, 3);
|
||||
|
||||
|
||||
assert(dokuResult.isValid());
|
||||
assert(dokuResult.isSolved());
|
||||
|
||||
Solver.solveRandom(dokuToTest, rand);
|
||||
|
||||
@@ -65,7 +65,7 @@ class SolverTest {
|
||||
SudokuPrinter.printRectangleSudoku(dokuToTest.getSubGrid(0), 3, 3);
|
||||
|
||||
|
||||
assert(dokuToTest.isValid());
|
||||
assert(dokuToTest.isSolved());
|
||||
|
||||
assert(dokuToTest.equals(dokuResult));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user