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

@@ -77,4 +77,10 @@ public class Cell {
public boolean isMutable() {
return this.isMutable;
}
public int empty() {
int oldSymbol = this.symbolIndex;
this.symbolIndex = Cell.NOSYMBOL;
return oldSymbol;
}
}

View File

@@ -98,5 +98,21 @@ public class MultiDoku {
return true;
}
public List<Cell> getFilledCells() {
List<Cell> result = new ArrayList<>();
for (Cell c : this.getCells()){
if (!c.isEmpty()) {
result.add(c);
}
}
return result;
}
public void empty(Cell cell) {
List<Cell> cells = getCells();
Cell cellToEmpty = cells.get(cells.indexOf(cell));
cellToEmpty.setSymbolIndex(Cell.NOSYMBOL);
}
}

View File

@@ -287,4 +287,8 @@ public class Sudoku {
return true;
}
public void empty(Cell cell) {
}
}

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