Files
Sudoku/app/src/main/java/sudoku/solver/Solver.java
Janet-Doe 5a07f9347c
Some checks failed
Linux arm64 / Build (push) Failing after 23s
update clear function in multidoku + solver soolve
2025-01-10 17:17:21 +01:00

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;
}
}