feat : generation de doku par rapport à une difficulté
Some checks are pending
Linux arm64 / Build (push) Waiting to run
Some checks are pending
Linux arm64 / Build (push) Waiting to run
This commit is contained in:
@@ -4,14 +4,17 @@ import sudoku.constraint.BlockConstraint;
|
||||
import sudoku.constraint.ColumnConstraint;
|
||||
import sudoku.constraint.IConstraint;
|
||||
import sudoku.constraint.LineConstraint;
|
||||
import sudoku.structure.*;
|
||||
import sudoku.solver.Solver;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
public class SudokuFactory {
|
||||
|
||||
private static final Random random = new Random();
|
||||
|
||||
private static List<Cell> initCells(int size) {
|
||||
List<Cell> cells = new ArrayList<>(size * size);
|
||||
for (int i = 0; i < size * size; i++) {
|
||||
@@ -72,4 +75,35 @@ public class SudokuFactory {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static boolean newDokuFromFilledOne (MultiDoku doku, int difficulty) throws Exception {
|
||||
|
||||
if (difficulty > doku.getCells().size()) {
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
if (difficulty == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
List<Cell> cellsThatCanBeEmptied = doku.getFilledCells();
|
||||
|
||||
while (!cellsThatCanBeEmptied.isEmpty()) {
|
||||
int index = random.nextInt(cellsThatCanBeEmptied.size());
|
||||
Cell cellToEmpty = cellsThatCanBeEmptied.get(index);
|
||||
|
||||
int oldSymbol = cellToEmpty.empty();
|
||||
|
||||
if (Solver.countSolution(doku) == 1) {
|
||||
if (newDokuFromFilledOne(doku, --difficulty)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
cellToEmpty.setSymbolIndex(oldSymbol);
|
||||
cellsThatCanBeEmptied.remove(cellToEmpty);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user