40 lines
1.1 KiB
Java
40 lines
1.1 KiB
Java
package sudoku.solver;
|
|
|
|
import sudoku.Cell;
|
|
import sudoku.MultiDoku;
|
|
import sudoku.MutableCell;
|
|
import sudoku.Sudoku;
|
|
import sudoku.constraint.IConstraint;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Random;
|
|
|
|
public class Solver {
|
|
Caretaker stack;
|
|
|
|
public Solver() {
|
|
}
|
|
|
|
public MultiDoku solve(MultiDoku doku, List<IConstraint> constraints) throws Exception {
|
|
if (!doku.isValid(constraints)) {
|
|
throw new Exception("Invalid doku");
|
|
}
|
|
List<MutableCell> allMutableCells = doku.getMutableCells();
|
|
List<MutableCell> remainingCellsToCheck = new ArrayList<>(allMutableCells);
|
|
Random rand = new Random();
|
|
while (!remainingCellsToCheck.isEmpty()) {
|
|
int indexCurrentCell = rand.nextInt(remainingCellsToCheck.size());
|
|
MutableCell currentCell = remainingCellsToCheck.get(indexCurrentCell);
|
|
if (currentCell.getHints().isEmpty()){
|
|
MutableCell modify = stack.undo();
|
|
modify.removeHint(modify.clear());
|
|
}
|
|
|
|
|
|
remainingCellsToCheck.remove(indexCurrentCell);
|
|
}
|
|
return doku;
|
|
}
|
|
}
|