39
app/src/main/java/sudoku/solver/Solver.java
Normal file
39
app/src/main/java/sudoku/solver/Solver.java
Normal file
@@ -0,0 +1,39 @@
|
||||
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 = allMutableCells.get(stack.undo());
|
||||
modify.clear(modify.getSymboleIndex());
|
||||
}
|
||||
|
||||
|
||||
remainingCellsToCheck.remove(indexCurrentCell);
|
||||
}
|
||||
return doku;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user