feat : generation de doku par rapport à une difficulté
Some checks are pending
Linux arm64 / Build (push) Waiting to run

This commit is contained in:
Melvyn
2025-01-27 16:17:22 +01:00
parent 584a1c8b42
commit 4c02be3d39
5 changed files with 92 additions and 7 deletions

View File

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