This commit is contained in:
48
app/src/main/java/sudoku/solver/StupidSolver.java
Normal file
48
app/src/main/java/sudoku/solver/StupidSolver.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user