refactor stupidsolver
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package sudoku.solver;
|
package sudoku.solver;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.concurrent.CancellationException;
|
import java.util.concurrent.CancellationException;
|
||||||
|
|
||||||
import sudoku.structure.MultiDoku;
|
import sudoku.structure.MultiDoku;
|
||||||
@@ -8,9 +9,9 @@ import sudoku.structure.Sudoku;
|
|||||||
/**
|
/**
|
||||||
* Class de test non utilisé
|
* Class de test non utilisé
|
||||||
*/
|
*/
|
||||||
public class StupidSolver {
|
public class StupidSolver implements Solver{
|
||||||
|
|
||||||
private static boolean solve(Sudoku sudoku, int index) {
|
private boolean solve(Sudoku sudoku, int index, List<SolverStep> steps) {
|
||||||
// mécanisme d'abandon
|
// mécanisme d'abandon
|
||||||
if (Thread.interrupted())
|
if (Thread.interrupted())
|
||||||
throw new CancellationException("User wants to stop the solver");
|
throw new CancellationException("User wants to stop the solver");
|
||||||
@@ -21,27 +22,30 @@ public class StupidSolver {
|
|||||||
|
|
||||||
// si la case n'est pas modifiable, on passe à la suivante
|
// si la case n'est pas modifiable, on passe à la suivante
|
||||||
if (!sudoku.getCell(index).isMutable())
|
if (!sudoku.getCell(index).isMutable())
|
||||||
return solve(sudoku, index + 1);
|
return solve(sudoku, index + 1, steps);
|
||||||
|
|
||||||
for (int symbol = 0; symbol < sudoku.getSize(); symbol++) {
|
for (int symbol = 0; symbol < sudoku.getSize(); symbol++) {
|
||||||
if (sudoku.getCell(index).trySetValue(symbol)) {
|
if (sudoku.getCell(index).trySetValue(symbol)) {
|
||||||
|
addStep(sudoku.getCell(index), steps);
|
||||||
// on tente de placer sur la case suivante
|
// on tente de placer sur la case suivante
|
||||||
if (solve(sudoku, index + 1)) {
|
if (solve(sudoku, index + 1, steps)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// on a tout essayé et rien n'a fonctionné
|
// on a tout essayé et rien n'a fonctionné
|
||||||
sudoku.getCell(index).empty();
|
sudoku.getCell(index).empty();
|
||||||
|
addStep(sudoku.getCell(index), steps);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean solve(MultiDoku doku) {
|
@Override
|
||||||
|
public boolean solve(MultiDoku doku, List<SolverStep> steps) {
|
||||||
if (doku.isSolved())
|
if (doku.isSolved())
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
for (Sudoku sudoku : doku.getSubGrids()) {
|
for (Sudoku sudoku : doku.getSubGrids()) {
|
||||||
if (!solve(sudoku, 0))
|
if (!solve(sudoku, 0, steps))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in New Issue
Block a user