refactor MixedSolver
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package sudoku.solver;
|
package sudoku.solver;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
|
||||||
import java.util.concurrent.CancellationException;
|
import java.util.concurrent.CancellationException;
|
||||||
|
|
||||||
import sudoku.structure.Cell;
|
import sudoku.structure.Cell;
|
||||||
@@ -9,6 +8,19 @@ import sudoku.structure.MultiDoku;
|
|||||||
|
|
||||||
public class MixedSolver implements Solver {
|
public class MixedSolver implements Solver {
|
||||||
|
|
||||||
|
private Cell findCellToBacktrack(MultiDoku doku, int maxPossibilities) {
|
||||||
|
for (Cell cell : doku.getCells()) {
|
||||||
|
if (!cell.isMutable() || !cell.isEmpty())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
List<Integer> possibleSymbols = cell.getPossibleSymbols();
|
||||||
|
if (possibleSymbols.size() == maxPossibilities) {
|
||||||
|
return cell;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Résout le MultiDoku passé en paramètre, avec règles de déduction et
|
* Résout le MultiDoku passé en paramètre, avec règles de déduction et
|
||||||
* backtracking.
|
* backtracking.
|
||||||
@@ -18,47 +30,45 @@ public class MixedSolver implements Solver {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean solve(MultiDoku doku, List<SolverStep> steps) {
|
public boolean solve(MultiDoku doku, List<SolverStep> steps) {
|
||||||
Random rand = new Random();
|
|
||||||
|
|
||||||
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.isSolved()) {
|
while (!doku.isSolved()) {
|
||||||
return true;
|
boolean filledCell = false;
|
||||||
}
|
for (Cell cell : doku.getCells()) {
|
||||||
|
if (!cell.isMutable() || !cell.isEmpty())
|
||||||
Cell cellToFill = doku.getFirstEmptyCell();
|
continue;
|
||||||
if (cellToFill == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Integer> possibleSymbols = cellToFill.getPossibleSymbols();
|
|
||||||
|
|
||||||
|
List<Integer> possibleSymbols = cell.getPossibleSymbols();
|
||||||
if (possibleSymbols.size() == 1) {
|
if (possibleSymbols.size() == 1) {
|
||||||
cellToFill.setSymbolIndex(possibleSymbols.getFirst());
|
cell.setSymbolIndex(possibleSymbols.getFirst());
|
||||||
addStep(cellToFill, steps);
|
addStep(cell, steps);
|
||||||
if (this.solve(doku, steps)) {
|
filledCell = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// on ne peut plus remplir de cases, on tente de backtrack
|
||||||
|
if (!filledCell) {
|
||||||
|
int maxPossibilities = 2;
|
||||||
|
Cell backtrackCell = null;
|
||||||
|
while (backtrackCell == null) {
|
||||||
|
backtrackCell = findCellToBacktrack(doku, maxPossibilities);
|
||||||
|
maxPossibilities++;
|
||||||
|
}
|
||||||
|
// on fait du backtracking
|
||||||
|
List<Integer> possibilities = backtrackCell.getPossibleSymbols();
|
||||||
|
for (int symbol : possibilities) {
|
||||||
|
doku.getStateManager().pushState();
|
||||||
|
backtrackCell.setSymbolIndex(symbol);
|
||||||
|
if (solve(doku, steps))
|
||||||
|
return true;
|
||||||
|
doku.getStateManager().popState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
while (!possibleSymbols.isEmpty()) {
|
|
||||||
int nextPossibleSymbolIndex = rand.nextInt(possibleSymbols.size());
|
|
||||||
int nextSymbol = possibleSymbols.get(nextPossibleSymbolIndex);
|
|
||||||
|
|
||||||
cellToFill.setSymbolIndex(nextSymbol);
|
|
||||||
addStep(cellToFill, steps);
|
|
||||||
if (this.solve(doku, steps)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
cellToFill.setSymbolIndex(Cell.NOSYMBOL);
|
|
||||||
addStep(cellToFill, steps);
|
|
||||||
possibleSymbols.remove(nextPossibleSymbolIndex);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user