This commit is contained in:
@@ -36,4 +36,20 @@ public abstract class Cell {
|
||||
return otherCell.getSymbolIndex() == this.getSymbolIndex();
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("| ");
|
||||
if (this.symbolIndex != NOSYMBOLE){
|
||||
sb.append(this.symbolIndex);
|
||||
}
|
||||
else {
|
||||
sb.append(" ");
|
||||
}
|
||||
|
||||
sb.append(" |");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
33
app/src/main/java/sudoku/Coordinate.java
Normal file
33
app/src/main/java/sudoku/Coordinate.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package sudoku;
|
||||
|
||||
public class Coordinate {
|
||||
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
public Coordinate(int row, int col) {
|
||||
this.x = row;
|
||||
this.y = col;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public int calculateIndex(int size) {
|
||||
return this.y * size + this.x;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -42,4 +42,20 @@ public class MultiDoku {
|
||||
return mutableCells;
|
||||
}
|
||||
|
||||
public void updateSymbolsPossibilities() throws Exception {
|
||||
for (Sudoku sudoku : subGrids){
|
||||
sudoku.updateSymbolsPossibilities();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Multidoku {");
|
||||
for (Sudoku sudoku : subGrids){
|
||||
sb.append("\n\t").append(sudoku.toString());
|
||||
}
|
||||
sb.append("\n}");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,4 +55,64 @@ public class Sudoku {
|
||||
}
|
||||
return mutableCells;
|
||||
}
|
||||
|
||||
public boolean contains(Cell cell) {
|
||||
return this.cells.contains(cell);
|
||||
}
|
||||
|
||||
private Coordinate getCoordinateCell(Cell c) throws Exception {
|
||||
int x=0, y=0;
|
||||
int size = this.getSize();
|
||||
|
||||
if (!this.contains(c)) {
|
||||
throw new Exception("The given cell is not in this sudoku.");
|
||||
}
|
||||
|
||||
for (Cell cell : this.cells) {
|
||||
if (cell == c){
|
||||
return new Coordinate(x, y);
|
||||
}
|
||||
if (x == size - 1){
|
||||
y+=1;
|
||||
x=0;
|
||||
}
|
||||
else {
|
||||
x+=1;
|
||||
}
|
||||
}
|
||||
return new Coordinate(x, y);
|
||||
}
|
||||
|
||||
public void updateSymbolsPossibilities() throws Exception {
|
||||
for (IConstraint constraint : constraints) {
|
||||
List<MutableCell> mutableCells = this.getMutableCells();
|
||||
for (MutableCell cell : mutableCells) {
|
||||
Coordinate coord = null;
|
||||
try {
|
||||
coord = this.getCoordinateCell(cell);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
constraint.getPossibleSymbols(this, coord.getX(), coord.getY());
|
||||
if (cell.getPossibleSymbols().isEmpty()){
|
||||
throw new Exception("Rollback bitch");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String toString (){
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Sudoku {");
|
||||
for (int i = 0; i < getSize(); i++) {
|
||||
sb.append("\n\t");
|
||||
for (int j = 0; j < getSize(); j++) {
|
||||
Cell cell = getCell(i, j);
|
||||
sb.append(cell.toString()).append(" ");
|
||||
}
|
||||
}
|
||||
sb.append("\n}");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -38,10 +38,10 @@ public class SudokuFactory {
|
||||
return blocs;
|
||||
}
|
||||
|
||||
public static MultiDoku createBasicEmptyRectangleSudoku(int width, int height) {
|
||||
int symbolCount = width * height;
|
||||
public static MultiDoku createBasicEmptyRectangleSudoku(int widthBlock, int heightBlock) {
|
||||
int symbolCount = widthBlock * heightBlock;
|
||||
List<Cell> cases = initCells(symbolCount);
|
||||
List<Block> blocs = initRectangleBlocs(cases, width, height);
|
||||
List<Block> blocs = initRectangleBlocs(cases, widthBlock, heightBlock);
|
||||
List<IConstraint> constraints = new ArrayList<>();
|
||||
constraints.add(new ColumnConstraint());
|
||||
constraints.add(new LineConstraint());
|
||||
|
||||
@@ -32,6 +32,11 @@ public class Solver {
|
||||
int symbol = currentCell.getPossibleSymbols().get(0);
|
||||
currentCell.setSymbolIndex(symbol);
|
||||
stack.push(currentCell);
|
||||
try {
|
||||
doku.updateSymbolsPossibilities();
|
||||
} catch (Exception e) {
|
||||
//TODO rollback
|
||||
}
|
||||
//TODO check constraints integrity in sudoku
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user