This commit is contained in:
42
app/src/main/java/sudoku/structure/Block.java
Normal file
42
app/src/main/java/sudoku/structure/Block.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package sudoku.structure;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Block {
|
||||
|
||||
private final List<Cell> cells;
|
||||
|
||||
public Block(List<Cell> cells) {
|
||||
this.cells = cells;
|
||||
}
|
||||
|
||||
public Block() {
|
||||
this.cells = new ArrayList<>();
|
||||
}
|
||||
|
||||
public List<Cell> getCells() {
|
||||
return cells;
|
||||
}
|
||||
|
||||
void addCell(Cell newCell) {
|
||||
this.cells.add(newCell);
|
||||
}
|
||||
|
||||
public boolean containsSymbol(int symbolIndex) {
|
||||
for (Cell cell : getCells()) {
|
||||
if (cell.getSymbolIndex() == symbolIndex)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean containsCell(Cell cell) {
|
||||
for (Cell cellTmp : this.cells) {
|
||||
if (cellTmp.equals(cell)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
80
app/src/main/java/sudoku/structure/Cell.java
Normal file
80
app/src/main/java/sudoku/structure/Cell.java
Normal file
@@ -0,0 +1,80 @@
|
||||
package sudoku.structure;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Cell {
|
||||
|
||||
public static int NOSYMBOL = -1;
|
||||
|
||||
private Block blockContainer;
|
||||
private int symbolIndex = Cell.NOSYMBOL;
|
||||
private final List<Integer> possibleSymbols;
|
||||
private boolean isMutable = true;
|
||||
|
||||
public Cell() {
|
||||
this.possibleSymbols = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Cell(int symbolIndex) {
|
||||
this.symbolIndex = symbolIndex;
|
||||
this.possibleSymbols = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Cell(int symbolIndex, boolean isMutable) {
|
||||
this.symbolIndex = symbolIndex;
|
||||
this.possibleSymbols = new ArrayList<>();
|
||||
this.isMutable = isMutable;
|
||||
}
|
||||
|
||||
public int getSymbolIndex() {
|
||||
return this.symbolIndex;
|
||||
}
|
||||
|
||||
public void setSymbolIndex(int symbolIndex) {
|
||||
this.symbolIndex = symbolIndex;
|
||||
}
|
||||
|
||||
public void setPossibleSymbols(List<Integer> possibleSymbols) {
|
||||
this.possibleSymbols.clear();
|
||||
this.possibleSymbols.addAll(possibleSymbols);
|
||||
}
|
||||
|
||||
public void setImmutable() {
|
||||
this.isMutable = false;
|
||||
}
|
||||
|
||||
public Block getBlock() {
|
||||
return this.blockContainer;
|
||||
}
|
||||
|
||||
public void setBlock(Block block) {
|
||||
this.blockContainer = block;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the current symbolIndex and returns it
|
||||
* @return integer symbolIndex cleared
|
||||
*/
|
||||
public int clearCurrentSymbol() {
|
||||
int i = this.symbolIndex;
|
||||
setSymbolIndex(NOSYMBOL);
|
||||
return i;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return this.symbolIndex == Cell.NOSYMBOL;
|
||||
}
|
||||
|
||||
public void removeSymbolFromPossibilities(int indexSymbol) {
|
||||
possibleSymbols.remove(indexSymbol);
|
||||
}
|
||||
|
||||
public List<Integer> getPossibleSymbols() {
|
||||
return this.possibleSymbols;
|
||||
}
|
||||
|
||||
public boolean isMutable() {
|
||||
return this.isMutable;
|
||||
}
|
||||
}
|
||||
33
app/src/main/java/sudoku/structure/Coordinate.java
Normal file
33
app/src/main/java/sudoku/structure/Coordinate.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package sudoku.structure;
|
||||
|
||||
public class Coordinate {
|
||||
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
public Coordinate(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
102
app/src/main/java/sudoku/structure/MultiDoku.java
Normal file
102
app/src/main/java/sudoku/structure/MultiDoku.java
Normal file
@@ -0,0 +1,102 @@
|
||||
package sudoku.structure;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @class MultiDoku
|
||||
* @brief Représente une grille de Multidoku.
|
||||
* Une grille de sudoku est un multidoku avec un seul sous-sudoku
|
||||
*/
|
||||
public class MultiDoku {
|
||||
|
||||
private final List<Sudoku> subGrids;
|
||||
|
||||
public MultiDoku(List<Sudoku> subGrids) {
|
||||
this.subGrids = subGrids;
|
||||
}
|
||||
|
||||
public int getNbSubGrids(){
|
||||
return subGrids.size();
|
||||
}
|
||||
|
||||
public Sudoku getSubGrid(int i){
|
||||
return subGrids.get(i);
|
||||
}
|
||||
|
||||
public List<Cell> getCells(){
|
||||
List<Cell> cells = new ArrayList<>();
|
||||
for (Sudoku sudoku : subGrids){
|
||||
cells.addAll(sudoku.getCells());
|
||||
}
|
||||
return cells;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
public Cell getFirstEmptyCell() {
|
||||
for (Sudoku sudoku : this.subGrids) {
|
||||
Cell cellTmp = sudoku.getFirstEmptyCell();
|
||||
if (cellTmp != null && cellTmp.isEmpty()) {
|
||||
return cellTmp;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Integer> getPossibleSymbolsOfCell(Cell cellToFill) {
|
||||
for (Sudoku sudoku : this.subGrids) {
|
||||
if (sudoku.contains(cellToFill)) {
|
||||
return sudoku.getPossibleSymbolsOfCell(cellToFill);
|
||||
}
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
public List<Sudoku> getSubGrids() {
|
||||
return this.subGrids;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
boolean result = true;
|
||||
for (Sudoku sudoku : this.subGrids) {
|
||||
result = sudoku.isValid() && result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object) {
|
||||
if (!(object instanceof MultiDoku)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.getNbSubGrids() != ((MultiDoku) object).getNbSubGrids()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < this.getNbSubGrids(); i++) {
|
||||
if (!this.getSubGrid(i).equals(((MultiDoku) object).getSubGrid(i))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
248
app/src/main/java/sudoku/structure/Sudoku.java
Normal file
248
app/src/main/java/sudoku/structure/Sudoku.java
Normal file
@@ -0,0 +1,248 @@
|
||||
package sudoku.structure;
|
||||
|
||||
import sudoku.constraint.IConstraint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @class Sudoku
|
||||
* @brief Represents a sudoku
|
||||
*/
|
||||
public class Sudoku {
|
||||
|
||||
private final List<Block> blocks;
|
||||
private final List<Cell> cells;
|
||||
private final List<IConstraint> constraints;
|
||||
private boolean isMutable;
|
||||
|
||||
public Sudoku(List<Cell> cells, List<Block> blocks, List<IConstraint> constraints) {
|
||||
this.cells = cells;
|
||||
this.blocks = blocks;
|
||||
this.constraints = constraints;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return wether the coords are in the sudoku
|
||||
*/
|
||||
public boolean isValidCoords(int x, int y) {
|
||||
int index = y * getSize() + x;
|
||||
return index < getSize() * getSize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to place a cell at the given coordinate
|
||||
*
|
||||
* @return Cell created or null if it can't be done
|
||||
*/
|
||||
public Cell setCellSymbol(int x, int y, int value) {
|
||||
assert (isValidCoords(x, y));
|
||||
for (IConstraint constraint : this.constraints) {
|
||||
if (!constraint.canBePlaced(this, x, y, value)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Cell cell = getCell(x, y);
|
||||
cell.setSymbolIndex(value);
|
||||
return cell;
|
||||
}
|
||||
|
||||
public boolean setCellsSymbol(List<Integer> values) {
|
||||
if (values.size() > this.cells.size()) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < values.size(); i++) {
|
||||
int x = i % this.blocks.size();
|
||||
int y = (i-x) / this.blocks.size();
|
||||
int value = values.get(i);
|
||||
this.setCellSymbol(x, y, value);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean setImmutableCellsSymbol(List<Integer> values) {
|
||||
if (values.size() > this.cells.size()) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < values.size(); i++) {
|
||||
int x = i%this.blocks.size();
|
||||
int y = (i-x)/this.blocks.size();
|
||||
int value = values.get(i);
|
||||
if (value != Cell.NOSYMBOL) {
|
||||
Cell cellPlaced = this.setCellSymbol(x, y, value);
|
||||
if (cellPlaced == null) {
|
||||
continue;
|
||||
}
|
||||
cellPlaced.setImmutable();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public Cell getCell(int x, int y) {
|
||||
int index = y * getSize() + x;
|
||||
assert (isValidCoords(x, y));
|
||||
try {
|
||||
return this.cells.get(index);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Cell getCell(int i) {
|
||||
return this.cells.get(i);
|
||||
}
|
||||
|
||||
public List<IConstraint> getConstraints() {
|
||||
return constraints;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return this.blocks.size();
|
||||
}
|
||||
|
||||
public List<Cell> getCells() {
|
||||
return this.cells;
|
||||
}
|
||||
|
||||
public List<Block> getBlocks() {
|
||||
return this.blocks;
|
||||
}
|
||||
|
||||
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<Cell> cells = this.getCells();
|
||||
for (Cell cell : cells) {
|
||||
Coordinate coord = null;
|
||||
try {
|
||||
coord = this.getCoordinateCell(cell);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
List<Integer> newPossibleSymbols = cell.getPossibleSymbols();
|
||||
newPossibleSymbols.retainAll(constraint.getPossibleSymbols(this, coord.getX(), coord.getY()));
|
||||
|
||||
cell.setPossibleSymbols(newPossibleSymbols);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
public Cell getFirstEmptyCell() {
|
||||
for (Cell cell : this.cells) {
|
||||
if (cell.isEmpty()) {
|
||||
return cell;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Integer> getPossibleSymbolsOfCell(Cell 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;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
for (Cell cell : this.cells) {
|
||||
if (cell.isMutable()) {
|
||||
if (cell.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
for (IConstraint constraint : this.constraints) {
|
||||
Coordinate coords;
|
||||
try {
|
||||
int symbolPlaced = cell.getSymbolIndex();
|
||||
coords = this.getCoordinateCell(cell);
|
||||
|
||||
cell.setSymbolIndex(Cell.NOSYMBOL);
|
||||
List<Integer> possibleSymbols = constraint.getPossibleSymbols(this, coords.getX(), coords.getY());
|
||||
cell.setSymbolIndex(symbolPlaced);
|
||||
if (possibleSymbols.size() != 1 || possibleSymbols.get(0) != symbolPlaced) {
|
||||
return false;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object) {
|
||||
if (!(object instanceof Sudoku)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.getSize() != ((Sudoku) object).getSize()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < this.getSize(); i++) {
|
||||
if (this.getCell(i).getSymbolIndex() != ((Sudoku) object).getCell(i).getSymbolIndex()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
75
app/src/main/java/sudoku/structure/SudokuFactory.java
Normal file
75
app/src/main/java/sudoku/structure/SudokuFactory.java
Normal file
@@ -0,0 +1,75 @@
|
||||
package sudoku.structure;
|
||||
|
||||
import sudoku.constraint.BlockConstraint;
|
||||
import sudoku.constraint.ColumnConstraint;
|
||||
import sudoku.constraint.IConstraint;
|
||||
import sudoku.constraint.LineConstraint;
|
||||
import sudoku.structure.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class SudokuFactory {
|
||||
|
||||
private static List<Cell> initCells(int size) {
|
||||
List<Cell> cells = new ArrayList<>(size * size);
|
||||
for (int i = 0; i < size * size; i++) {
|
||||
cells.add(new Cell());
|
||||
}
|
||||
return cells;
|
||||
}
|
||||
|
||||
private static List<Block> initRectangleBlocs(List<Cell> cells, int width, int height) {
|
||||
List<Block> blocs = new ArrayList<>();
|
||||
int size = width * height;
|
||||
for (int i = 0; i < size; i++) {
|
||||
Block newBlock = new Block();
|
||||
int blockX = i % height;
|
||||
int blockY = i / height;
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
int index = ((y + blockY * height) * size + (x + blockX * width));
|
||||
Cell blockCell = cells.get(index);
|
||||
blockCell.setBlock(newBlock);
|
||||
// List<Block> blockContainers = new ArrayList<>();
|
||||
// blockContainers.add(newBlock);
|
||||
// blockCell.setBlockContainers(blockContainers);
|
||||
newBlock.addCell(blockCell);
|
||||
}
|
||||
}
|
||||
blocs.add(newBlock);
|
||||
}
|
||||
return blocs;
|
||||
}
|
||||
|
||||
public static MultiDoku createBasicEmptyRectangleSudoku(int widthBlock, int heightBlock) {
|
||||
int symbolCount = widthBlock * heightBlock;
|
||||
List<Cell> cases = initCells(symbolCount);
|
||||
List<Block> blocs = initRectangleBlocs(cases, widthBlock, heightBlock);
|
||||
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) {
|
||||
return createBasicEmptyRectangleSudoku(size, size);
|
||||
}
|
||||
|
||||
public static void setIMMutableCells(MultiDoku doku, Map<Coordinate, Integer> immutableCells) {
|
||||
immutableCells.forEach((coordinate, symbol) -> {
|
||||
for (Sudoku sudoku : doku.getSubGrids()) {
|
||||
Cell cell = sudoku.getCell(coordinate.getX(), coordinate.getY());
|
||||
if (cell != null) {
|
||||
cell.setSymbolIndex(symbol);
|
||||
cell.setImmutable();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user