fix : MultiDoku.getCells
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package sudoku.constraint;
|
package sudoku.constraint;
|
||||||
|
|
||||||
|
import sudoku.structure.Cell;
|
||||||
import sudoku.structure.Sudoku;
|
import sudoku.structure.Sudoku;
|
||||||
|
|
||||||
public class ColumnConstraint implements IConstraint {
|
public class ColumnConstraint implements IConstraint {
|
||||||
@@ -7,10 +8,12 @@ public class ColumnConstraint implements IConstraint {
|
|||||||
@Override
|
@Override
|
||||||
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
|
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
|
||||||
for (int i = 0; i < s.getSize(); i++) {
|
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 false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,12 +32,12 @@ public class Solver {
|
|||||||
throw new CancellationException("User wants to stop the solver");
|
throw new CancellationException("User wants to stop the solver");
|
||||||
|
|
||||||
Sudoku sudoku = doku.getSubGrid(0);
|
Sudoku sudoku = doku.getSubGrid(0);
|
||||||
logger.log(Level.INFO,
|
logger.log(Level.FINE,
|
||||||
'\n' + SudokuPrinter.toStringRectangleSudoku(sudoku,
|
'\n' + SudokuPrinter.toStringRectangleSudoku(sudoku,
|
||||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getBlockWidth(),
|
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getBlockWidth(),
|
||||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getSize() / sudoku.getBlockWidth()));
|
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getSize() / sudoku.getBlockWidth()));
|
||||||
|
|
||||||
if (doku.isValid()) {
|
if (doku.isSolved()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,7 +65,7 @@ public class Solver {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Compte le nombre de solutions possibles au MultiDoku passé en paramètres.
|
* 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.
|
* @return int, nombre de solutions possibles.
|
||||||
*/
|
*/
|
||||||
public static int countSolution(MultiDoku oldDoku) {
|
public static int countSolution(MultiDoku oldDoku) {
|
||||||
@@ -73,21 +73,23 @@ public class Solver {
|
|||||||
|
|
||||||
MultiDoku doku = oldDoku.clone();
|
MultiDoku doku = oldDoku.clone();
|
||||||
|
|
||||||
if (doku.isValid()) {
|
if (doku.isSolved()) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Cell cellToFill = doku.getFirstEmptyCell();
|
Cell cellToFill = doku.getFirstEmptyCell();
|
||||||
if (cellToFill == null) {
|
if (cellToFill == null) {
|
||||||
|
System.out.println("AAAAAAAAAAAAAA");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Integer> possibleSymbols = doku.getPossibleSymbolsOfCell(cellToFill);
|
List<Integer> possibleSymbols = doku.getPossibleSymbolsOfCell(cellToFill);
|
||||||
|
|
||||||
for (int symbol : possibleSymbols) {
|
for (int symbol : possibleSymbols) {
|
||||||
|
|
||||||
cellToFill.setSymbolIndex(symbol);
|
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++;
|
result++;
|
||||||
}
|
}
|
||||||
cellToFill.setSymbolIndex(Cell.NOSYMBOL);
|
cellToFill.setSymbolIndex(Cell.NOSYMBOL);
|
||||||
@@ -105,7 +107,7 @@ public class Solver {
|
|||||||
if (Thread.interrupted())
|
if (Thread.interrupted())
|
||||||
throw new CancellationException("User wants to stop the solver");
|
throw new CancellationException("User wants to stop the solver");
|
||||||
|
|
||||||
if (doku.isValid()) {
|
if (doku.isSolved()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,6 +146,13 @@ public class Solver {
|
|||||||
List<Cell> cellsToFill = doku.getEmptyCells();
|
List<Cell> cellsToFill = doku.getEmptyCells();
|
||||||
|
|
||||||
while (!cellsToFill.isEmpty()) {
|
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;
|
boolean blocked = true;
|
||||||
for (Cell cellToFill : cellsToFill) {
|
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) {
|
public static boolean solve(MultiDoku doku) {
|
||||||
if (doku.isValid())
|
if (doku.isSolved())
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
for (Sudoku sudoku : doku.getSubGrids()) {
|
for (Sudoku sudoku : doku.getSubGrids()) {
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package sudoku.structure;
|
package sudoku.structure;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import sudoku.io.SudokuSerializer;
|
import sudoku.io.SudokuSerializer;
|
||||||
|
|
||||||
@@ -49,11 +51,11 @@ public class MultiDoku {
|
|||||||
* @return List<Cell>
|
* @return List<Cell>
|
||||||
*/
|
*/
|
||||||
public List<Cell> getCells(){
|
public List<Cell> getCells(){
|
||||||
List<Cell> cells = new ArrayList<>();
|
Set<Cell> cellsSet = new HashSet<>();
|
||||||
for (Sudoku sudoku : subGrids){
|
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.
|
* Check si le MultiDoku est valide, en fonction de ses sous-Sudokus.
|
||||||
* @return boolean, true s'il est valide et false sinon.
|
* @return boolean, true s'il est valide et false sinon.
|
||||||
*/
|
*/
|
||||||
public boolean isValid() {
|
public boolean isSolved() {
|
||||||
boolean result = true;
|
boolean result = true;
|
||||||
for (Sudoku sudoku : this.subGrids) {
|
for (Sudoku sudoku : this.subGrids) {
|
||||||
result = sudoku.isValid() && result;
|
result = sudoku.isSolved() && result;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -191,14 +193,17 @@ public class MultiDoku {
|
|||||||
cellToEmpty.setSymbolIndex(Cell.NOSYMBOL);
|
cellToEmpty.setSymbolIndex(Cell.NOSYMBOL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renvoie le nombre de Cell contenue dans le MultiDoku.
|
||||||
|
* @return int, nombre de Cell dans le MultiDoku.
|
||||||
|
*/
|
||||||
public int getNbCells() {
|
public int getNbCells() {
|
||||||
int result = 0;
|
return getCells().size();
|
||||||
for (Sudoku sudoku : this.subGrids) {
|
|
||||||
result += sudoku.getCells().size();
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Change les Cells de ce MultiDoku avec des symboles, en Cells immuables.
|
||||||
|
*/
|
||||||
public void setFilledCellsImmutable() {
|
public void setFilledCellsImmutable() {
|
||||||
for (Cell filledCell : getFilledCells()) {
|
for (Cell filledCell : getFilledCells()) {
|
||||||
filledCell.setImmutable();
|
filledCell.setImmutable();
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ public class Sudoku {
|
|||||||
/**
|
/**
|
||||||
* Liste des Cells contenus dans le 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.
|
* Liste des contraintes (TODO) du Sudoku.
|
||||||
*/
|
*/
|
||||||
@@ -334,26 +334,42 @@ public class Sudoku {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Vérifie que le Sudoku est cohérent avec ses contraintes.
|
* Vérifie si le Sudoku est résolue, soit complet et cohérent avec ses contraintes.
|
||||||
* @return boolean, valant true si le Sudoku est cohérent avec ses contraintes, false sinon.
|
* @return boolean, valant true si le Sudoku est résolu, false sinon.
|
||||||
*/
|
*/
|
||||||
public boolean isValid() {
|
public boolean isSolved() {
|
||||||
for (Cell cell : this.cells) {
|
boolean isComplete = isComplete();
|
||||||
if (cell.isMutable()) {
|
boolean isValid = isValid();
|
||||||
if (cell.isEmpty()) {
|
return isComplete && isValid;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
for (IConstraint constraint : this.constraints) {
|
|
||||||
Coordinate coords;
|
|
||||||
try {
|
|
||||||
int symbolPlaced = cell.getSymbolIndex();
|
|
||||||
coords = this.getCoordinateCell(cell);
|
|
||||||
|
|
||||||
cell.setSymbolIndex(Cell.NOSYMBOL);
|
/**
|
||||||
List<Integer> possibleSymbols = constraint.getPossibleSymbols(this, coords.getX(),
|
* Vérifie que le Sudoku est complet, soit qu'il n'y ait aucune case vide.
|
||||||
coords.getY());
|
* @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 (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);
|
cell.setSymbolIndex(symbolPlaced);
|
||||||
if (possibleSymbols.size() != 1 || possibleSymbols.get(0) != symbolPlaced) {
|
if (!possibleSymbols.contains(symbolPlaced)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -362,10 +378,51 @@ public class Sudoku {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return true;
|
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
|
@Override
|
||||||
public boolean equals(Object object) {
|
public boolean equals(Object object) {
|
||||||
if (!(object instanceof Sudoku)) {
|
if (!(object instanceof Sudoku)) {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import sudoku.constraint.BlockConstraint;
|
|||||||
import sudoku.constraint.ColumnConstraint;
|
import sudoku.constraint.ColumnConstraint;
|
||||||
import sudoku.constraint.IConstraint;
|
import sudoku.constraint.IConstraint;
|
||||||
import sudoku.constraint.LineConstraint;
|
import sudoku.constraint.LineConstraint;
|
||||||
|
import sudoku.io.SudokuPrinter;
|
||||||
import sudoku.solver.Solver;
|
import sudoku.solver.Solver;
|
||||||
|
|
||||||
public class SudokuFactory {
|
public class SudokuFactory {
|
||||||
@@ -121,7 +122,8 @@ public class SudokuFactory {
|
|||||||
*/
|
*/
|
||||||
public static boolean newDokuFromFilledOne (MultiDoku doku, int nbCellsToEmpty) throws Exception {
|
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();
|
throw new Exception();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,7 +139,9 @@ public class SudokuFactory {
|
|||||||
|
|
||||||
int oldSymbol = cellToEmpty.empty();
|
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)) {
|
if (newDokuFromFilledOne(doku, --nbCellsToEmpty)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -239,9 +243,13 @@ public class SudokuFactory {
|
|||||||
|
|
||||||
public static void fillDoku(MultiDoku doku, Difficulty difficulty) throws Exception {
|
public static void fillDoku(MultiDoku doku, Difficulty difficulty) throws Exception {
|
||||||
Solver.solveRandom(doku, random);
|
Solver.solveRandom(doku, random);
|
||||||
|
//SudokuPrinter.printRectangleSudoku(doku.getSubGrid(0), 3, 3);
|
||||||
int nbCellsToEmpty = (int)(difficulty.getFactor()*doku.getNbCells());
|
int nbCellsToEmpty = (int)(difficulty.getFactor()*doku.getNbCells());
|
||||||
boolean successfull = newDokuFromFilledOne(doku, nbCellsToEmpty);
|
//System.out.println(nbCellsToEmpty);
|
||||||
if (!successfull) {
|
boolean successful = newDokuFromFilledOne(doku, nbCellsToEmpty);
|
||||||
|
|
||||||
|
|
||||||
|
if (!successful) {
|
||||||
throw new Exception("Canno't create this doku with this difficulty");
|
throw new Exception("Canno't create this doku with this difficulty");
|
||||||
}
|
}
|
||||||
doku.setFilledCellsImmutable();
|
doku.setFilledCellsImmutable();
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ class SolverTest {
|
|||||||
SudokuPrinter.printRectangleSudoku(sudokuResult, 3, 3);
|
SudokuPrinter.printRectangleSudoku(sudokuResult, 3, 3);
|
||||||
|
|
||||||
|
|
||||||
assert(dokuResult.isValid());
|
assert(dokuResult.isSolved());
|
||||||
|
|
||||||
Solver.solveRandom(dokuToTest, rand);
|
Solver.solveRandom(dokuToTest, rand);
|
||||||
|
|
||||||
@@ -65,7 +65,7 @@ class SolverTest {
|
|||||||
SudokuPrinter.printRectangleSudoku(dokuToTest.getSubGrid(0), 3, 3);
|
SudokuPrinter.printRectangleSudoku(dokuToTest.getSubGrid(0), 3, 3);
|
||||||
|
|
||||||
|
|
||||||
assert(dokuToTest.isValid());
|
assert(dokuToTest.isSolved());
|
||||||
|
|
||||||
assert(dokuToTest.equals(dokuResult));
|
assert(dokuToTest.equals(dokuResult));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user