This commit is contained in:
@@ -4,16 +4,54 @@ import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Cell;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.CancellationException;
|
||||
|
||||
public class Solver {
|
||||
|
||||
/**
|
||||
* Résout le multidoku passé en paramètre si c'est possible.
|
||||
* En testant toutes les possibilités avec un algorithme de backtracking.
|
||||
*
|
||||
* @param doku Multidoku, à résoudre
|
||||
* @param rand random pour tester aléatoirement les symboles
|
||||
* @return boolean, true s'il est résolut ou false s'il ne l'est pas.
|
||||
*/
|
||||
public static boolean solveRandom(MultiDoku doku, Random rand) {
|
||||
if (Thread.interrupted())
|
||||
throw new CancellationException("User wants to stop the solver");
|
||||
|
||||
if (doku.isValid()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Cell cellToFill = doku.getFirstEmptyCell();
|
||||
if (cellToFill == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
List<Integer> possibleSymbols = doku.getPossibleSymbolsOfCell(cellToFill);
|
||||
|
||||
while (!possibleSymbols.isEmpty()){
|
||||
int nextPossibleSymbolIndex = rand.nextInt(possibleSymbols.size());
|
||||
int nextSymbol = possibleSymbols.get(nextPossibleSymbolIndex);
|
||||
|
||||
cellToFill.setSymbolIndex(nextSymbol);
|
||||
if (Solver.solveRandom(doku, rand)) {
|
||||
return true;
|
||||
} else {
|
||||
cellToFill.setSymbolIndex(Cell.NOSYMBOL);
|
||||
possibleSymbols.remove(nextPossibleSymbolIndex);
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean solve(MultiDoku doku) {
|
||||
if (Thread.interrupted())
|
||||
throw new CancellationException("User wants to stop the solver");
|
||||
|
||||
if (doku.isValid()) {
|
||||
return true;
|
||||
}
|
||||
@@ -28,46 +66,16 @@ public class Solver {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int symbol : possibleSymbols) {
|
||||
for (int symbol : possibleSymbols){
|
||||
|
||||
cellToFill.setSymbolIndex(symbol);
|
||||
if (Solver.solve(doku)) {
|
||||
return true;
|
||||
} else {
|
||||
cellToFill.setSymbolIndex(Cell.NOSYMBOL);
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
Ancien algo abandonné pour le moment
|
||||
|
||||
private void rollBack() {
|
||||
stack.pop();
|
||||
}
|
||||
|
||||
|
||||
public MultiDoku solve(MultiDoku doku, List<IConstraint> constraints) throws Exception {
|
||||
List<MutableCell> allMutableCells = doku.getMutableCells();
|
||||
List<MutableCell> remainingCellsToCheck = new ArrayList<>(allMutableCells);
|
||||
Random rand = new Random();
|
||||
while (!remainingCellsToCheck.isEmpty()) {
|
||||
int indexCurrentCell = rand.nextInt(remainingCellsToCheck.size());
|
||||
MutableCell currentCell = remainingCellsToCheck.get(indexCurrentCell);
|
||||
|
||||
int symbol = currentCell.getPossibleSymbols().get(0);
|
||||
currentCell.setSymbolIndex(symbol);
|
||||
// stack.push(new MutableCell(currentCell));
|
||||
try {
|
||||
doku.updateSymbolsPossibilities();
|
||||
} catch (Exception e) {
|
||||
this.rollBack();
|
||||
System.out.println(this.stack);
|
||||
}
|
||||
remainingCellsToCheck.remove(indexCurrentCell);
|
||||
}
|
||||
return doku;
|
||||
}
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user