canellable stupid solver
All checks were successful
Linux arm64 / Build (push) Successful in 24m8s

This commit is contained in:
2025-01-24 21:41:12 +01:00
parent 00941edb19
commit 4b4ed76eb8
4 changed files with 142 additions and 10 deletions

View File

@@ -0,0 +1,48 @@
package sudoku.solver;
import java.util.concurrent.CancellationException;
import sudoku.structure.MultiDoku;
import sudoku.structure.Sudoku;
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);
var coords = sudoku.toCoords(index);
for (int symbol = 0; symbol < sudoku.getSize(); symbol++) {
if (sudoku.tryPlaceCellSymbol(coords[0], coords[1], 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.clearCell(coords[0], coords[1]);
return false;
}
public static boolean solve(MultiDoku doku) {
if (doku.isValid())
return true;
for (Sudoku sudoku : doku.getSubGrids()) {
if (!solve(sudoku, 0))
return false;
}
return true;
}
}