This commit is contained in:
@@ -20,4 +20,27 @@ public class SudokuPrinter {
|
||||
System.out.println(line);
|
||||
}
|
||||
}
|
||||
|
||||
public static String toStringRectangleSudoku(final Sudoku s, int blockWidth, int blockHeight) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (int y = 0; y < s.getSize(); y++) {
|
||||
// Ajouter une ligne vide entre les blocs horizontaux
|
||||
if (y % blockHeight == 0 && y > 0) {
|
||||
result.append("\n");
|
||||
}
|
||||
StringBuilder line = new StringBuilder("[ ");
|
||||
for (int x = 0; x < s.getSize(); x++) {
|
||||
// Ajouter la valeur de la cellule
|
||||
line.append((s.getCell(x, y).getSymbolIndex() + 1)).append(" ");
|
||||
|
||||
// Ajouter un séparateur vertical entre les blocs
|
||||
if (x % blockWidth == blockWidth - 1 && x != s.getSize() - 1) {
|
||||
line.append("| ");
|
||||
}
|
||||
}
|
||||
line.append("]");
|
||||
result.append(line).append("\n");
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
package sudoku.solver;
|
||||
|
||||
import sudoku.io.SudokuPrinter;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Solver {
|
||||
private static final Logger logger = Logger.getLogger("SolverLogger");
|
||||
|
||||
/**
|
||||
* Résout le multidoku passé en paramètre si c'est possible.
|
||||
@@ -21,6 +26,9 @@ public class Solver {
|
||||
if (Thread.interrupted())
|
||||
throw new CancellationException("User wants to stop the solver");
|
||||
|
||||
Sudoku sudoku = doku.getSubGrid(0);
|
||||
logger.log(Level.INFO, '\n'+SudokuPrinter.toStringRectangleSudoku(sudoku, sudoku.getSize(), sudoku.getSize()));
|
||||
|
||||
if (doku.isValid()) {
|
||||
return true;
|
||||
}
|
||||
@@ -32,7 +40,7 @@ public class Solver {
|
||||
|
||||
List<Integer> possibleSymbols = doku.getPossibleSymbolsOfCell(cellToFill);
|
||||
|
||||
while (!possibleSymbols.isEmpty()){
|
||||
while (!possibleSymbols.isEmpty()) {
|
||||
int nextPossibleSymbolIndex = rand.nextInt(possibleSymbols.size());
|
||||
int nextSymbol = possibleSymbols.get(nextPossibleSymbolIndex);
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package sudoku.structure;
|
||||
|
||||
import sudoku.constraint.IConstraint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -60,12 +62,20 @@ public class MultiDoku {
|
||||
}
|
||||
|
||||
public List<Integer> getPossibleSymbolsOfCell(Cell cellToFill) {
|
||||
for (Sudoku sudoku : this.subGrids) {
|
||||
if (sudoku.contains(cellToFill)) {
|
||||
return sudoku.getPossibleSymbolsOfCell(cellToFill);
|
||||
}
|
||||
}
|
||||
return new ArrayList<>();
|
||||
List<Integer> result = new ArrayList<>();
|
||||
boolean hasBeenFill = false;
|
||||
|
||||
for (Sudoku sudoku : this.subGrids) {
|
||||
if (sudoku.contains(cellToFill)) {
|
||||
if (!hasBeenFill) {
|
||||
result.addAll(sudoku.getPossibleSymbolsOfCell(cellToFill));
|
||||
hasBeenFill = true;
|
||||
} else {
|
||||
result.retainAll(sudoku.getPossibleSymbolsOfCell(cellToFill));
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public List<Sudoku> getSubGrids() {
|
||||
|
||||
@@ -152,7 +152,6 @@ public class SudokuFactory {
|
||||
*/
|
||||
|
||||
Sudoku sudoku1 = createSquareSudoku(size);
|
||||
|
||||
Sudoku sudoku2 = createSquareSudoku(size);
|
||||
Sudoku sudoku3 = createSquareSudoku(size);
|
||||
Sudoku sudoku4 = createSquareSudoku(size);
|
||||
|
||||
Reference in New Issue
Block a user