This commit is contained in:
@@ -3,11 +3,11 @@ package sudoku;
|
||||
public abstract class Cell {
|
||||
|
||||
protected static int NOSYMBOLE = -1;
|
||||
protected int symboleIndex;
|
||||
protected int symbolIndex;
|
||||
protected Block block = null;
|
||||
|
||||
public Cell(int symboleIndex) {
|
||||
this.symboleIndex = symboleIndex;
|
||||
public Cell(int symbolIndex) {
|
||||
this.symbolIndex = symbolIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -17,8 +17,8 @@ public abstract class Cell {
|
||||
this(NOSYMBOLE);
|
||||
}
|
||||
|
||||
public int getSymboleIndex() {
|
||||
return symboleIndex;
|
||||
public int getSymbolIndex() {
|
||||
return symbolIndex;
|
||||
}
|
||||
|
||||
public Block getBlock() {
|
||||
@@ -33,7 +33,7 @@ public abstract class Cell {
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof Cell otherCell)
|
||||
return otherCell.getSymboleIndex() == this.getSymboleIndex();
|
||||
return otherCell.getSymbolIndex() == this.getSymbolIndex();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,38 +5,38 @@ import java.util.List;
|
||||
|
||||
public class MutableCell extends Cell{
|
||||
|
||||
private final List<Integer> hintsSymbolIndex;
|
||||
private final List<Integer> possibleSymbols;
|
||||
|
||||
public MutableCell() {
|
||||
super();
|
||||
this.hintsSymbolIndex = new ArrayList<>();
|
||||
this.possibleSymbols = new ArrayList<>();
|
||||
}
|
||||
|
||||
public MutableCell(int symboleIndex) {
|
||||
super(symboleIndex);
|
||||
this.hintsSymbolIndex = new ArrayList<>();
|
||||
public MutableCell(int symbolIndex) {
|
||||
super(symbolIndex);
|
||||
this.possibleSymbols = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void setSymboleIndex(int symboleIndex) {
|
||||
this.symboleIndex = symboleIndex;
|
||||
public void setSymbolIndex(int symbolIndex) {
|
||||
this.symbolIndex = symbolIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the current symboleIndex and returns it
|
||||
* @return integer symboleIndex cleared
|
||||
* Remove the current symbolIndex and returns it
|
||||
* @return integer symbolIndex cleared
|
||||
*/
|
||||
public int clear() {
|
||||
int i = this.symboleIndex;
|
||||
setSymboleIndex(NOSYMBOLE);
|
||||
public int clearCurrentSymbol() {
|
||||
int i = this.symbolIndex;
|
||||
setSymbolIndex(NOSYMBOLE);
|
||||
return i;
|
||||
}
|
||||
|
||||
public void removeHint(int indexSymbol) {
|
||||
hintsSymbolIndex.remove(indexSymbol);
|
||||
public void removeSymbolFromPossibilities(int indexSymbol) {
|
||||
possibleSymbols.remove(indexSymbol);
|
||||
}
|
||||
|
||||
public List<Integer> getHints() {
|
||||
return this.hintsSymbolIndex;
|
||||
public List<Integer> getPossibleSymbols() {
|
||||
return this.possibleSymbols;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,10 +14,12 @@ public class Sudoku {
|
||||
|
||||
private final List<Block> blocks;
|
||||
private final List<Cell> cells;
|
||||
private final List<IConstraint> constraints;
|
||||
|
||||
public Sudoku(List<Cell> cells, List<Block> blocks) {
|
||||
public Sudoku(List<Cell> cells, List<Block> blocks, List<IConstraint> constraints) {
|
||||
this.cells = cells;
|
||||
this.blocks = blocks;
|
||||
this.constraints = new ArrayList<>(constraints);
|
||||
}
|
||||
|
||||
public Cell getCell(int x, int y) {
|
||||
@@ -30,6 +32,10 @@ public class Sudoku {
|
||||
return this.cells.get(i);
|
||||
}
|
||||
|
||||
public List<IConstraint> getConstraints() {
|
||||
return constraints;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return this.blocks.size();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package sudoku;
|
||||
|
||||
import sudoku.constraint.BlockConstraint;
|
||||
import sudoku.constraint.ColumnConstraint;
|
||||
import sudoku.constraint.IConstraint;
|
||||
import sudoku.constraint.LineConstraint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -37,10 +42,14 @@ public class SudokuFactory {
|
||||
int symbolCount = width * height;
|
||||
List<Cell> cases = initCells(symbolCount);
|
||||
List<Block> blocs = initRectangleBlocs(cases, width, height);
|
||||
Sudoku s = new Sudoku(cases, blocs);
|
||||
List<Sudoku> ss = new ArrayList<>();
|
||||
ss.add(s);
|
||||
return new MultiDoku(ss);
|
||||
List<IConstraint> constraints = new ArrayList<>();
|
||||
constraints.add(new ColumnConstraint());
|
||||
constraints.add(new LineConstraint());
|
||||
constraints.add(new BlockConstraint());
|
||||
Sudoku s = new Sudoku(cases, blocs, constraints);
|
||||
List<Sudoku> subSudoku = new ArrayList<>();
|
||||
subSudoku.add(s);
|
||||
return new MultiDoku(subSudoku);
|
||||
}
|
||||
|
||||
public static MultiDoku createBasicEmptySquareSudoku(int size) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package sudoku.constraint;
|
||||
|
||||
import sudoku.Block;
|
||||
import sudoku.Cell;
|
||||
import sudoku.MutableCell;
|
||||
import sudoku.Sudoku;
|
||||
|
||||
public class BlockConstraint implements IConstraint{
|
||||
@@ -9,7 +10,7 @@ public class BlockConstraint implements IConstraint{
|
||||
@Override
|
||||
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
|
||||
Block bloc = s.getCell(x, y).getBlock();
|
||||
return !bloc.getCells().contains(new Cell(newSymbolIndex));
|
||||
return !bloc.getCells().contains(new MutableCell(newSymbolIndex));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ public class ColumnConstraint implements IConstraint {
|
||||
@Override
|
||||
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
|
||||
for (int i = 0; i < s.getSize(); i++) {
|
||||
if (s.getCell(x, newSymbolIndex).getSymboleIndex() == newSymbolIndex)
|
||||
if (s.getCell(x, newSymbolIndex).getSymbolIndex() == newSymbolIndex)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -7,7 +7,7 @@ public class LineConstraint implements IConstraint {
|
||||
@Override
|
||||
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
|
||||
for (int i = 0; i < s.getSize(); i++) {
|
||||
if (s.getCell(newSymbolIndex, y).getSymboleIndex() == newSymbolIndex)
|
||||
if (s.getCell(newSymbolIndex, y).getSymbolIndex() == newSymbolIndex)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
package sudoku.solver;
|
||||
|
||||
import sudoku.Cell;
|
||||
import sudoku.MultiDoku;
|
||||
import sudoku.MutableCell;
|
||||
import sudoku.Sudoku;
|
||||
import sudoku.constraint.IConstraint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.Stack;
|
||||
|
||||
public class Solver {
|
||||
Caretaker stack;
|
||||
Stack<MutableCell> stack;
|
||||
|
||||
public Solver() {
|
||||
}
|
||||
@@ -26,12 +25,21 @@ public class Solver {
|
||||
while (!remainingCellsToCheck.isEmpty()) {
|
||||
int indexCurrentCell = rand.nextInt(remainingCellsToCheck.size());
|
||||
MutableCell currentCell = remainingCellsToCheck.get(indexCurrentCell);
|
||||
if (currentCell.getHints().isEmpty()){
|
||||
MutableCell modify = stack.undo();
|
||||
modify.removeHint(modify.clear());
|
||||
if (currentCell.getPossibleSymbols().isEmpty()){
|
||||
MutableCell modify = stack.pop();
|
||||
modify.removeSymbolFromPossibilities(modify.clearCurrentSymbol());
|
||||
} else {
|
||||
int symbol = currentCell.getPossibleSymbols().get(0);
|
||||
currentCell.setSymbolIndex(symbol);
|
||||
stack.push(currentCell);
|
||||
//TODO check constraints integrity in sudoku
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
remainingCellsToCheck.remove(indexCurrentCell);
|
||||
}
|
||||
return doku;
|
||||
|
||||
Reference in New Issue
Block a user