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

@@ -11,11 +11,11 @@ public class Solver {
/**
* Résout le multidoku passé en paramètre si c'est possible.
* En testant toutes les possibilités avec un algorithme de backtracking.
* En testant toutes les possibilités, de manière aléatoire, avec un algorithme de backtracking.
*
* @param doku Multidoku, à résoudre
* @param rand random pour tester aléatoirement les symboles
* @return boolean, true s'il est résolut ou false s'il ne l'est pas.
* @param rand Random, pour tester aléatoirement les symboles
* @return boolean, true s'il est résolu ou false s'il ne l'est pas.
*/
public static boolean solveRandom(MultiDoku doku, Random rand) {
if (Thread.interrupted())
@@ -39,15 +39,40 @@ public class Solver {
cellToFill.setSymbolIndex(nextSymbol);
if (Solver.solveRandom(doku, rand)) {
return true;
} else {
cellToFill.setSymbolIndex(Cell.NOSYMBOL);
possibleSymbols.remove(nextPossibleSymbolIndex);
}
cellToFill.setSymbolIndex(Cell.NOSYMBOL);
possibleSymbols.remove(nextPossibleSymbolIndex);
}
return false;
}
public static int countSolution(MultiDoku doku) {
int result = 0;
if (doku.isValid()) {
return 1;
}
Cell cellToFill = doku.getFirstEmptyCell();
if (cellToFill == null) {
return 0;
}
List<Integer> possibleSymbols = doku.getPossibleSymbolsOfCell(cellToFill);
for (int symbol : possibleSymbols) {
cellToFill.setSymbolIndex(symbol);
if (Solver.solve(doku)) {
result++;
}
cellToFill.setSymbolIndex(Cell.NOSYMBOL);
}
return result;
}
public static boolean solve(MultiDoku doku) {
if (Thread.interrupted())
throw new CancellationException("User wants to stop the solver");