Compare commits
2 Commits
e19a9c7b27
...
6902321101
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6902321101 | ||
|
|
fc7ae02387 |
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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<>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,14 +10,39 @@ 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 static 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 Solver.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 +64,6 @@ public class Solver {
|
||||
}
|
||||
return doku;
|
||||
}
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user