feat : Solver.solve()

This commit is contained in:
Melvyn
2025-01-23 16:28:41 +01:00
parent e19a9c7b27
commit fc7ae02387
5 changed files with 80 additions and 9 deletions

View File

@@ -72,6 +72,10 @@ public abstract class Cell {
}
*/
public boolean isEmpty() {
return this.symbolIndex == Cell.NOSYMBOL;
}
public boolean equalsValue(Cell otherCell) {
return otherCell.getSymbolIndex() == this.getSymbolIndex();
}

View File

@@ -3,17 +3,9 @@
*/
package sudoku;
import sudoku.constraint.BlockConstraint;
import sudoku.constraint.ColumnConstraint;
import sudoku.constraint.IConstraint;
import sudoku.constraint.LineConstraint;
import sudoku.io.SudokuPrinter;
import sudoku.io.SudokuSerializer;
import sudoku.solver.Solver;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public String getGreeting() {

View File

@@ -50,4 +50,23 @@ public class MultiDoku {
sb.append("\n}");
return sb.toString();
}
public MutableCell getFirstEmptyMutableCell() {
for (Sudoku sudoku : this.subGrids) {
MutableCell cellTmp = sudoku.getFirstEmptyMutableCell();
if (cellTmp.isEmpty()) {
return cellTmp;
}
}
return null;
}
public List<Integer> getPossibleSymbolsOfCell(MutableCell cellToFill) {
for (Sudoku sudoku : this.subGrids) {
if (sudoku.contains(cellToFill)) {
return sudoku.getPossibleSymbolsOfCell(cellToFill);
}
}
return new ArrayList<>();
}
}

View File

@@ -162,4 +162,31 @@ public class Sudoku {
sb.append("\n}");
return sb.toString();
}
public MutableCell getFirstEmptyMutableCell() {
for (MutableCell cell : this.getMutableCells()) {
if (cell.isEmpty()) {
return cell;
}
}
return null;
}
public List<Integer> getPossibleSymbolsOfCell(MutableCell cellToFill) {
List<Integer> result = new ArrayList<>();
Coordinate cellCoordinates;
try {
cellCoordinates = this.getCoordinateCell(cellToFill);
} catch (Exception e) {
return result;
}
for (IConstraint constraint : this.constraints) {
if (result.isEmpty()) {
result.addAll(constraint.getPossibleSymbols(this, cellCoordinates.getX(), cellCoordinates.getY()));
} else {
result.retainAll(constraint.getPossibleSymbols(this, cellCoordinates.getX(), cellCoordinates.getY()));
}
}
return result;
}
}

View File

@@ -10,14 +10,41 @@ import java.util.Random;
import java.util.Stack;
public class Solver {
Stack<MutableCell> stack;
public Solver() {}
/**
* Résout le multidoku passé en paramètre si c'est possible.
* En testant toutes les possibilités avec un algorithme de backtracking.
* @param doku Multidouke, à résoudre
* @return boolean, true s'il est résolut ou false s'il ne l'est pas.
*/
public boolean solve(MultiDoku doku) {
MutableCell cellToFill = doku.getFirstEmptyMutableCell();
if (cellToFill == null) {
return true;
}
List<Integer> possibleSymbols = doku.getPossibleSymbolsOfCell(cellToFill);
if (possibleSymbols.isEmpty()) {
return false;
}
for (int symbol : possibleSymbols) {
cellToFill.setSymbolIndex(symbol);
return this.solve(doku);
}
return false;
}
/*
Ancien algo abandonné pour le moment
private void rollBack() {
stack.pop();
}
public MultiDoku solve(MultiDoku doku, List<IConstraint> constraints) throws Exception {
List<MutableCell> allMutableCells = doku.getMutableCells();
List<MutableCell> remainingCellsToCheck = new ArrayList<>(allMutableCells);
@@ -39,4 +66,6 @@ public class Solver {
}
return doku;
}
*/
}