Merge branch 'master' of git.ale-pri.com:Ryuk/Sudoku
Some checks failed
Linux arm64 / Build (push) Has been cancelled
Some checks failed
Linux arm64 / Build (push) Has been cancelled
This commit is contained in:
@@ -77,4 +77,10 @@ public class Cell {
|
||||
public boolean isMutable() {
|
||||
return this.isMutable;
|
||||
}
|
||||
|
||||
public int empty() {
|
||||
int oldSymbol = this.symbolIndex;
|
||||
this.symbolIndex = Cell.NOSYMBOL;
|
||||
return oldSymbol;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,5 +98,21 @@ public class MultiDoku {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<Cell> getFilledCells() {
|
||||
List<Cell> result = new ArrayList<>();
|
||||
for (Cell c : this.getCells()){
|
||||
if (!c.isEmpty()) {
|
||||
result.add(c);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void empty(Cell cell) {
|
||||
List<Cell> cells = getCells();
|
||||
Cell cellToEmpty = cells.get(cells.indexOf(cell));
|
||||
cellToEmpty.setSymbolIndex(Cell.NOSYMBOL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -242,8 +242,9 @@ public class Sudoku {
|
||||
} catch (Exception e) {
|
||||
return result;
|
||||
}
|
||||
for (IConstraint constraint : this.constraints) {
|
||||
if (result.isEmpty()) {
|
||||
for (int i = 0; i < this.constraints.size(); i++) {
|
||||
IConstraint constraint = this.constraints.get(i);
|
||||
if (i == 0) {
|
||||
result.addAll(constraint.getPossibleSymbols(this, cellCoordinates.getX(), cellCoordinates.getY()));
|
||||
} else {
|
||||
result.retainAll(constraint.getPossibleSymbols(this, cellCoordinates.getX(), cellCoordinates.getY()));
|
||||
|
||||
@@ -4,14 +4,17 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import sudoku.constraint.BlockConstraint;
|
||||
import sudoku.constraint.ColumnConstraint;
|
||||
import sudoku.constraint.IConstraint;
|
||||
import sudoku.constraint.LineConstraint;
|
||||
import sudoku.solver.Solver;
|
||||
|
||||
public class SudokuFactory {
|
||||
|
||||
private static final Random random = new Random();
|
||||
public static List<IConstraint> DEFAULT_CONSTRAINTS = Arrays.asList(new BlockConstraint(), new LineConstraint(), new ColumnConstraint());
|
||||
|
||||
private static List<Cell> initCells(int size) {
|
||||
@@ -66,6 +69,37 @@ public class SudokuFactory {
|
||||
});
|
||||
}
|
||||
|
||||
public static boolean newDokuFromFilledOne (MultiDoku doku, int difficulty) throws Exception {
|
||||
|
||||
if (difficulty > doku.getCells().size()) {
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
if (difficulty == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
List<Cell> cellsThatCanBeEmptied = doku.getFilledCells();
|
||||
|
||||
while (!cellsThatCanBeEmptied.isEmpty()) {
|
||||
int index = random.nextInt(cellsThatCanBeEmptied.size());
|
||||
Cell cellToEmpty = cellsThatCanBeEmptied.get(index);
|
||||
|
||||
int oldSymbol = cellToEmpty.empty();
|
||||
|
||||
if (Solver.countSolution(doku) == 1) {
|
||||
if (newDokuFromFilledOne(doku, --difficulty)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
cellToEmpty.setSymbolIndex(oldSymbol);
|
||||
cellsThatCanBeEmptied.remove(cellToEmpty);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static Sudoku createRectangleSudoku(int width, int height) {
|
||||
int symbolCount = width * height;
|
||||
List<Cell> cases = initCells(symbolCount);
|
||||
|
||||
Reference in New Issue
Block a user