51 lines
1.1 KiB
Java
51 lines
1.1 KiB
Java
package sudoku.solver;
|
|
|
|
import java.util.concurrent.CancellationException;
|
|
|
|
import sudoku.structure.MultiDoku;
|
|
import sudoku.structure.Sudoku;
|
|
|
|
/**
|
|
* Class de test non utilisé
|
|
*/
|
|
public class StupidSolver {
|
|
|
|
private static boolean solve(Sudoku sudoku, int index) {
|
|
// 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);
|
|
|
|
for (int symbol = 0; symbol < sudoku.getSize(); symbol++) {
|
|
if (sudoku.getCell(index).trySetValue(symbol)) {
|
|
// on tente de placer sur la case suivante
|
|
if (solve(sudoku, index + 1)) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
// on a tout essayé et rien n'a fonctionné
|
|
sudoku.getCell(index).empty();
|
|
return false;
|
|
}
|
|
|
|
public static boolean solve(MultiDoku doku) {
|
|
if (doku.isSolved())
|
|
return true;
|
|
|
|
for (Sudoku sudoku : doku.getSubGrids()) {
|
|
if (!solve(sudoku, 0))
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
}
|