Compare commits
5 Commits
ed1dc0e742
...
6c3389dd2d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c3389dd2d | ||
|
|
7904d0a27d | ||
|
|
277eb76e60 | ||
|
|
e5618b70c1 | ||
|
|
5a07f9347c |
@@ -3,11 +3,11 @@ package sudoku;
|
|||||||
public abstract class Cell {
|
public abstract class Cell {
|
||||||
|
|
||||||
protected static int NOSYMBOLE = -1;
|
protected static int NOSYMBOLE = -1;
|
||||||
protected int symboleIndex;
|
protected int symbolIndex;
|
||||||
protected Block block = null;
|
protected Block block = null;
|
||||||
|
|
||||||
public Cell(int symboleIndex) {
|
public Cell(int symbolIndex) {
|
||||||
this.symboleIndex = symboleIndex;
|
this.symbolIndex = symbolIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -17,8 +17,8 @@ public abstract class Cell {
|
|||||||
this(NOSYMBOLE);
|
this(NOSYMBOLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSymboleIndex() {
|
public int getSymbolIndex() {
|
||||||
return symboleIndex;
|
return symbolIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Block getBlock() {
|
public Block getBlock() {
|
||||||
@@ -33,7 +33,23 @@ public abstract class Cell {
|
|||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (obj instanceof Cell otherCell)
|
if (obj instanceof Cell otherCell)
|
||||||
return otherCell.getSymboleIndex() == this.getSymboleIndex();
|
return otherCell.getSymbolIndex() == this.getSymbolIndex();
|
||||||
return false;
|
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;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,33 +5,38 @@ import java.util.List;
|
|||||||
|
|
||||||
public class MutableCell extends Cell{
|
public class MutableCell extends Cell{
|
||||||
|
|
||||||
private final List<Integer> hintsSymbolIndex;
|
private final List<Integer> possibleSymbols;
|
||||||
|
|
||||||
public MutableCell() {
|
public MutableCell() {
|
||||||
super();
|
super();
|
||||||
this.hintsSymbolIndex = new ArrayList<>();
|
this.possibleSymbols = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public MutableCell(int symboleIndex) {
|
public MutableCell(int symbolIndex) {
|
||||||
super(symboleIndex);
|
super(symbolIndex);
|
||||||
this.hintsSymbolIndex = new ArrayList<>();
|
this.possibleSymbols = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSymboleIndex(int symboleIndex) {
|
public void setSymbolIndex(int symbolIndex) {
|
||||||
this.symboleIndex = symboleIndex;
|
this.symbolIndex = symbolIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clear() {
|
/**
|
||||||
setSymboleIndex(NOSYMBOLE);
|
* Remove the current symbolIndex and returns it
|
||||||
|
* @return integer symbolIndex cleared
|
||||||
|
*/
|
||||||
|
public int clearCurrentSymbol() {
|
||||||
|
int i = this.symbolIndex;
|
||||||
|
setSymbolIndex(NOSYMBOLE);
|
||||||
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clear(int indexSymbol) {
|
public void removeSymbolFromPossibilities(int indexSymbol) {
|
||||||
hintsSymbolIndex.remove(indexSymbol);
|
possibleSymbols.remove(indexSymbol);
|
||||||
this.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Integer> getHints() {
|
public List<Integer> getPossibleSymbols() {
|
||||||
return this.hintsSymbolIndex;
|
return this.possibleSymbols;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,10 +14,12 @@ public class Sudoku {
|
|||||||
|
|
||||||
private final List<Block> blocks;
|
private final List<Block> blocks;
|
||||||
private final List<Cell> cells;
|
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.cells = cells;
|
||||||
this.blocks = blocks;
|
this.blocks = blocks;
|
||||||
|
this.constraints = new ArrayList<>(constraints);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Cell getCell(int x, int y) {
|
public Cell getCell(int x, int y) {
|
||||||
@@ -30,6 +32,10 @@ public class Sudoku {
|
|||||||
return this.cells.get(i);
|
return this.cells.get(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<IConstraint> getConstraints() {
|
||||||
|
return constraints;
|
||||||
|
}
|
||||||
|
|
||||||
public int getSize() {
|
public int getSize() {
|
||||||
return this.blocks.size();
|
return this.blocks.size();
|
||||||
}
|
}
|
||||||
@@ -49,4 +55,63 @@ public class Sudoku {
|
|||||||
}
|
}
|
||||||
return mutableCells;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
package sudoku;
|
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.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -33,14 +38,18 @@ public class SudokuFactory {
|
|||||||
return blocs;
|
return blocs;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MultiDoku createBasicEmptyRectangleSudoku(int width, int height) {
|
public static MultiDoku createBasicEmptyRectangleSudoku(int widthBlock, int heightBlock) {
|
||||||
int symbolCount = width * height;
|
int symbolCount = widthBlock * heightBlock;
|
||||||
List<Cell> cases = initCells(symbolCount);
|
List<Cell> cases = initCells(symbolCount);
|
||||||
List<Block> blocs = initRectangleBlocs(cases, width, height);
|
List<Block> blocs = initRectangleBlocs(cases, widthBlock, heightBlock);
|
||||||
Sudoku s = new Sudoku(cases, blocs);
|
List<IConstraint> constraints = new ArrayList<>();
|
||||||
List<Sudoku> ss = new ArrayList<>();
|
constraints.add(new ColumnConstraint());
|
||||||
ss.add(s);
|
constraints.add(new LineConstraint());
|
||||||
return new MultiDoku(ss);
|
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) {
|
public static MultiDoku createBasicEmptySquareSudoku(int size) {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package sudoku.constraint;
|
|||||||
|
|
||||||
import sudoku.Block;
|
import sudoku.Block;
|
||||||
import sudoku.Cell;
|
import sudoku.Cell;
|
||||||
|
import sudoku.MutableCell;
|
||||||
import sudoku.Sudoku;
|
import sudoku.Sudoku;
|
||||||
|
|
||||||
public class BlockConstraint implements IConstraint{
|
public class BlockConstraint implements IConstraint{
|
||||||
@@ -9,7 +10,7 @@ public class BlockConstraint implements IConstraint{
|
|||||||
@Override
|
@Override
|
||||||
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
|
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
|
||||||
Block bloc = s.getCell(x, y).getBlock();
|
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
|
@Override
|
||||||
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
|
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
|
||||||
for (int i = 0; i < s.getSize(); i++) {
|
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 false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ public class LineConstraint implements IConstraint {
|
|||||||
@Override
|
@Override
|
||||||
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
|
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
|
||||||
for (int i = 0; i < s.getSize(); i++) {
|
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 false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -1,17 +1,16 @@
|
|||||||
package sudoku.solver;
|
package sudoku.solver;
|
||||||
|
|
||||||
import sudoku.Cell;
|
|
||||||
import sudoku.MultiDoku;
|
import sudoku.MultiDoku;
|
||||||
import sudoku.MutableCell;
|
import sudoku.MutableCell;
|
||||||
import sudoku.Sudoku;
|
|
||||||
import sudoku.constraint.IConstraint;
|
import sudoku.constraint.IConstraint;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
public class Solver {
|
public class Solver {
|
||||||
Caretaker stack;
|
Stack<MutableCell> stack;
|
||||||
|
|
||||||
public Solver() {
|
public Solver() {
|
||||||
}
|
}
|
||||||
@@ -26,12 +25,26 @@ public class Solver {
|
|||||||
while (!remainingCellsToCheck.isEmpty()) {
|
while (!remainingCellsToCheck.isEmpty()) {
|
||||||
int indexCurrentCell = rand.nextInt(remainingCellsToCheck.size());
|
int indexCurrentCell = rand.nextInt(remainingCellsToCheck.size());
|
||||||
MutableCell currentCell = remainingCellsToCheck.get(indexCurrentCell);
|
MutableCell currentCell = remainingCellsToCheck.get(indexCurrentCell);
|
||||||
if (currentCell.getHints().isEmpty()){
|
if (currentCell.getPossibleSymbols().isEmpty()){
|
||||||
MutableCell modify = allMutableCells.get(stack.undo());
|
MutableCell modify = stack.pop();
|
||||||
modify.clear(modify.getSymboleIndex());
|
modify.removeSymbolFromPossibilities(modify.clearCurrentSymbol());
|
||||||
|
} else {
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
remainingCellsToCheck.remove(indexCurrentCell);
|
remainingCellsToCheck.remove(indexCurrentCell);
|
||||||
}
|
}
|
||||||
return doku;
|
return doku;
|
||||||
|
|||||||
Reference in New Issue
Block a user