package sudoku.solver; import java.util.List; import java.util.Random; import java.util.concurrent.CancellationException; import sudoku.structure.Cell; import sudoku.structure.MultiDoku; public class RandomSolver implements Solver { /** * Résout, si possible, le multidoku passé en paramètre * en testant toutes les possibilités, de manière aléatoire, avec un algorithme * de backtracking. * * @param doku Multidoku, à résoudre * @return boolean, true s'il est résolu ou false s'il ne l'est pas. */ @Override public boolean solve(MultiDoku doku, List steps) { Random rand = new Random(); if (Thread.interrupted()) throw new CancellationException("User wants to stop the solver"); if (doku.isSolved()) { return true; } Cell cellToFill = doku.getFirstEmptyCell(); if (cellToFill == null) { return false; } List possibleSymbols = cellToFill.getPossibleSymbols(); while (!possibleSymbols.isEmpty()) { int nextPossibleSymbolIndex = rand.nextInt(possibleSymbols.size()); int nextSymbol = possibleSymbols.get(nextPossibleSymbolIndex); cellToFill.setSymbolIndex(nextSymbol); addStep(cellToFill, steps); if (this.solve(doku, steps)) { return true; } cellToFill.setSymbolIndex(Cell.NOSYMBOL); addStep(cellToFill, steps); possibleSymbols.remove(nextPossibleSymbolIndex); } return false; } }