fix : MixedSolver
All checks were successful
Linux arm64 / Build (push) Successful in 40s

This commit is contained in:
Melvyn
2025-01-31 18:26:44 +01:00
parent f47e4cc309
commit b7f9ca8a98
2 changed files with 26 additions and 39 deletions

View File

@@ -17,8 +17,6 @@ public class MixedSolver implements Solver{
* backtracking. * backtracking.
* *
* @param doku MultiDoku, MultiDoku à résoudre. * @param doku MultiDoku, MultiDoku à résoudre.
* @param rand Random, pour tester aléatoirement les symboles, lors du
* backtracking.
* @return boolean, valant true si le MultiDoku est résolu, false sinon. * @return boolean, valant true si le MultiDoku est résolu, false sinon.
*/ */
@Override @Override
@@ -40,29 +38,20 @@ public class MixedSolver implements Solver{
return true; return true;
} }
List<Cell> cellsToFill = doku.getEmptyCells(); Cell cellToFill = doku.getFirstEmptyCell();
if (cellsToFill.isEmpty()) { if (cellToFill == null) {
return false; return false;
} }
// Règles de déduction
for (Cell cellToFill : cellsToFill) {
List<Integer> possibleSymbols = cellToFill.getPossibleSymbols();
if (possibleSymbols.size() != 1) {
continue;
}
cellToFill.setSymbolIndex(possibleSymbols.getFirst());
return this.solve(doku);
}
// Si ça ne marche pas
// On fait du backtracking
Cell cellToFill = doku.getRandomEmptyCell(rand);
List<Integer> possibleSymbols = cellToFill.getPossibleSymbols(); List<Integer> possibleSymbols = cellToFill.getPossibleSymbols();
if (possibleSymbols.size() == 1) {
cellToFill.setSymbolIndex(possibleSymbols.getFirst());
if (this.solve(doku)) {
return true;
}
}
while (!possibleSymbols.isEmpty()) { while (!possibleSymbols.isEmpty()) {
int nextPossibleSymbolIndex = rand.nextInt(possibleSymbols.size()); int nextPossibleSymbolIndex = rand.nextInt(possibleSymbols.size());
int nextSymbol = possibleSymbols.get(nextPossibleSymbolIndex); int nextSymbol = possibleSymbols.get(nextPossibleSymbolIndex);
@@ -71,9 +60,9 @@ public class MixedSolver implements Solver{
if (this.solve(doku)) { if (this.solve(doku)) {
return true; return true;
} }
cellToFill.setSymbolIndex(Cell.NOSYMBOL); cellToFill.setSymbolIndex(Cell.NOSYMBOL);
possibleSymbols.remove(nextPossibleSymbolIndex); possibleSymbols.remove(nextPossibleSymbolIndex);
} }
return false; return false;

View File

@@ -145,7 +145,7 @@ public class SudokuFactory {
cellsThatCanBeEmptied.remove(cellToEmpty); cellsThatCanBeEmptied.remove(cellToEmpty);
} }
return newDokuFromFilledOne(doku, --nbCellsToEmpty, solver); return false;
} }
/** /**
@@ -181,21 +181,22 @@ public class SudokuFactory {
* @param sudoku2 Sudoku, second sudoku à connecter. * @param sudoku2 Sudoku, second sudoku à connecter.
* @param offset Coordinate, décalage entre les deux Sudokus. * @param offset Coordinate, décalage entre les deux Sudokus.
*/ */
private static void linkSquareSudokus(Sudoku sudoku1, Sudoku sudoku2, Coordinate offset) { private static void linkRectangleSudokus(Sudoku sudoku1, Sudoku sudoku2, Coordinate offset) {
int blockWidth = sudoku1.getBlockWidth(); int blockWidth = sudoku1.getBlockWidth();
for (int dx = 0; dx < blockWidth; dx++) { int blockHeight = sudoku1.getSize() / blockWidth;
for (int dx = 0; dx < blockHeight; dx++) {
for (int dy = 0; dy < blockWidth; dy++) { for (int dy = 0; dy < blockWidth; dy++) {
int block1X = dx + offset.getX(); int block1X = dx + offset.getX();
int block1Y = dy + offset.getY(); int block1Y = dy + offset.getY();
int block2X = dx; int block2X = dx;
int block2Y = dy; int block2Y = dy;
if ((block1X < blockWidth) && (block1X >= 0) && (block1Y >= 0) && (block1Y < blockWidth)) { if ((block1X < blockHeight) && (block1X >= 0) && (block1Y >= 0) && (block1Y < blockWidth)) {
Block block1 = sudoku1.getBlocks().get(block1Y * blockWidth + block1X); Block block1 = sudoku1.getBlocks().get(block1Y * blockHeight + block1X);
Block block2 = sudoku2.getBlocks().get(block2Y * blockWidth + block2X); Block block2 = sudoku2.getBlocks().get(block2Y * blockHeight + block2X);
// on remplace le bloc // on remplace le bloc
sudoku2.getBlocks().set(block2Y * blockWidth + block2X, block1); sudoku2.getBlocks().set(block2Y * blockHeight + block2X, block1);
block1.getSudokus().add(sudoku2); block1.getSudokus().add(sudoku2);
// on remplace les cellules // on remplace les cellules
@@ -231,10 +232,10 @@ public class SudokuFactory {
Sudoku sudoku4 = createSquareSudoku(size, constraints); Sudoku sudoku4 = createSquareSudoku(size, constraints);
Sudoku sudoku5 = createSquareSudoku(size, constraints); Sudoku sudoku5 = createSquareSudoku(size, constraints);
linkSquareSudokus(sudoku1, sudoku2, new Coordinate(1 - size, 1 - size)); linkRectangleSudokus(sudoku1, sudoku2, new Coordinate(1 - size, 1 - size));
linkSquareSudokus(sudoku1, sudoku3, new Coordinate(size - 1, 1 - size)); linkRectangleSudokus(sudoku1, sudoku3, new Coordinate(size - 1, 1 - size));
linkSquareSudokus(sudoku1, sudoku4, new Coordinate(1 - size, size - 1)); linkRectangleSudokus(sudoku1, sudoku4, new Coordinate(1 - size, size - 1));
linkSquareSudokus(sudoku1, sudoku5, new Coordinate(size - 1, size - 1)); linkRectangleSudokus(sudoku1, sudoku5, new Coordinate(size - 1, size - 1));
return new MultiDoku(Arrays.asList(sudoku1, sudoku2, sudoku3, sudoku4, sudoku5)); return new MultiDoku(Arrays.asList(sudoku1, sudoku2, sudoku3, sudoku4, sudoku5));
} }
@@ -263,10 +264,10 @@ public class SudokuFactory {
Sudoku sudoku4 = createRectangleSudoku(width, height, constraints); Sudoku sudoku4 = createRectangleSudoku(width, height, constraints);
Sudoku sudoku5 = createRectangleSudoku(width, height, constraints); Sudoku sudoku5 = createRectangleSudoku(width, height, constraints);
linkSquareSudokus(sudoku1, sudoku2, new Coordinate(1 - width, 1 - height)); linkRectangleSudokus(sudoku1, sudoku2, new Coordinate(1 - height, 1 - width));
linkSquareSudokus(sudoku1, sudoku3, new Coordinate(width - 1, 1 - height)); linkRectangleSudokus(sudoku1, sudoku3, new Coordinate(height - 1, 1 - width));
linkSquareSudokus(sudoku1, sudoku4, new Coordinate(1 - width, height - 1)); linkRectangleSudokus(sudoku1, sudoku4, new Coordinate(1 - height, width - 1));
linkSquareSudokus(sudoku1, sudoku5, new Coordinate(width - 1, height - 1)); linkRectangleSudokus(sudoku1, sudoku5, new Coordinate(height - 1, width - 1));
return new MultiDoku(Arrays.asList(sudoku1, sudoku2, sudoku3, sudoku4, sudoku5)); return new MultiDoku(Arrays.asList(sudoku1, sudoku2, sudoku3, sudoku4, sudoku5));
} }
@@ -276,9 +277,6 @@ public class SudokuFactory {
solver.solve(doku); solver.solve(doku);
int nbCellsToEmpty = (int) (difficulty.getFactor() * doku.getNbCells()); int nbCellsToEmpty = (int) (difficulty.getFactor() * doku.getNbCells());
boolean successfull = newDokuFromFilledOne(doku, nbCellsToEmpty, solver); boolean successfull = newDokuFromFilledOne(doku, nbCellsToEmpty, solver);
if (!successfull) {
throw new Exception("Canno't create this doku with this difficulty");
}
doku.setFilledCellsImmutable(); doku.setFilledCellsImmutable();
} }