This commit is contained in:
@@ -68,10 +68,9 @@ public class Solver {
|
||||
* @param oldDoku MultiDoku, MultiDoku dont on veut le nombre de solutions.
|
||||
* @return int, nombre de solutions possibles.
|
||||
*/
|
||||
public static int countSolution(MultiDoku oldDoku) {
|
||||
public static int countSolution(MultiDoku doku) {
|
||||
int result = 0;
|
||||
|
||||
MultiDoku doku = oldDoku.clone();
|
||||
|
||||
if (doku.isSolved()) {
|
||||
return 1;
|
||||
@@ -83,11 +82,12 @@ public class Solver {
|
||||
List<Integer> possibleSymbols = cellToFill.getPossibleSymbols();
|
||||
|
||||
for (int symbol : possibleSymbols) {
|
||||
doku.getStateManager().pushState();
|
||||
cellToFill.setSymbolIndex(symbol);
|
||||
if (Solver.solve(doku) != null) {
|
||||
if (Solver.solve(doku)) {
|
||||
result++;
|
||||
}
|
||||
cellToFill.setSymbolIndex(Cell.NOSYMBOL);
|
||||
doku.getStateManager().popState();
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -98,37 +98,35 @@ public class Solver {
|
||||
* @param doku MultiDoku, MultiDoku à résoudre.
|
||||
* @return boolean, valant true si le MultiDoku est résolu, false sinon.
|
||||
*/
|
||||
public static MultiDoku solve(MultiDoku oldDoku) {
|
||||
public static boolean solve(MultiDoku doku) {
|
||||
if (Thread.interrupted())
|
||||
throw new CancellationException("User wants to stop the solver");
|
||||
|
||||
MultiDoku doku = oldDoku.clone();
|
||||
|
||||
if (doku.isSolved()) {
|
||||
return doku;
|
||||
return true;
|
||||
}
|
||||
|
||||
Cell cellToFill = doku.getFirstEmptyCell();
|
||||
if (cellToFill == null) {
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
|
||||
List<Integer> possibleSymbols = cellToFill.getPossibleSymbols();
|
||||
if (possibleSymbols.isEmpty()) {
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int symbol : possibleSymbols) {
|
||||
|
||||
cellToFill.setSymbolIndex(symbol);
|
||||
if (Solver.solve(doku) != null) {
|
||||
return doku;
|
||||
if (Solver.solve(doku)) {
|
||||
return true;
|
||||
} else {
|
||||
cellToFill.setSymbolIndex(Cell.NOSYMBOL);
|
||||
}
|
||||
|
||||
}
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,13 +19,11 @@ public class MultiDoku {
|
||||
*/
|
||||
private final List<Sudoku> subGrids;
|
||||
|
||||
private final StateManager stateManager;
|
||||
|
||||
public MultiDoku(List<Sudoku> subGrids) {
|
||||
this.subGrids = subGrids;
|
||||
}
|
||||
|
||||
public MultiDoku clone() {
|
||||
// TODO: ahhhhhhhhhhhhhhhhhhhhhhh
|
||||
return SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(this));
|
||||
this.stateManager = new StateManager(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -167,4 +165,9 @@ public class MultiDoku {
|
||||
filledCell.setImmutable();
|
||||
}
|
||||
}
|
||||
|
||||
public StateManager getStateManager() {
|
||||
return stateManager;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
41
app/src/main/java/sudoku/structure/StateManager.java
Normal file
41
app/src/main/java/sudoku/structure/StateManager.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package sudoku.structure;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Stack;
|
||||
|
||||
//TODO: doc
|
||||
public class StateManager {
|
||||
|
||||
private final Stack<Map<Cell, Integer>> states;
|
||||
private final MultiDoku doku;
|
||||
|
||||
public StateManager(MultiDoku doku) {
|
||||
this.states = new Stack<>();
|
||||
this.doku = doku;
|
||||
}
|
||||
|
||||
public void pushState() {
|
||||
states.add(new HashMap<>());
|
||||
saveState();
|
||||
}
|
||||
|
||||
public void popState() {
|
||||
assert (states.size() > 0);
|
||||
restoreState();
|
||||
states.pop();
|
||||
}
|
||||
|
||||
private void restoreState() {
|
||||
for (var entry : this.states.getLast().entrySet()) {
|
||||
entry.getKey().setSymbolIndex(entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
private void saveState() {
|
||||
for (Cell cell : this.doku.getCells()) {
|
||||
states.getLast().put(cell, cell.getSymbolIndex());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user