55 lines
1.3 KiB
Java
55 lines
1.3 KiB
Java
package sudoku.solver;
|
|
|
|
import java.util.List;
|
|
import java.util.concurrent.CancellationException;
|
|
|
|
import sudoku.structure.MultiDoku;
|
|
import sudoku.structure.Sudoku;
|
|
|
|
/**
|
|
* Class de test non utilisé
|
|
*/
|
|
public class StupidSolver implements Solver{
|
|
|
|
private boolean solve(Sudoku sudoku, int index, List<SolverStep> steps) {
|
|
// mécanisme d'abandon
|
|
if (Thread.interrupted())
|
|
throw new CancellationException("User wants to stop the solver");
|
|
|
|
// si tout est bon avant, on a gagné
|
|
if (!sudoku.isValidCoords(index))
|
|
return true;
|
|
|
|
// si la case n'est pas modifiable, on passe à la suivante
|
|
if (!sudoku.getCell(index).isMutable())
|
|
return solve(sudoku, index + 1, steps);
|
|
|
|
for (int symbol = 0; symbol < sudoku.getSize(); symbol++) {
|
|
if (sudoku.getCell(index).trySetValue(symbol)) {
|
|
addStep(sudoku.getCell(index), steps);
|
|
// on tente de placer sur la case suivante
|
|
if (solve(sudoku, index + 1, steps)) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
// on a tout essayé et rien n'a fonctionné
|
|
sudoku.getCell(index).empty();
|
|
addStep(sudoku.getCell(index), steps);
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean solve(MultiDoku doku, List<SolverStep> steps) {
|
|
if (doku.isSolved())
|
|
return true;
|
|
|
|
for (Sudoku sudoku : doku.getSubGrids()) {
|
|
if (!solve(sudoku, 0, steps))
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
}
|