@@ -1,10 +1,17 @@
|
||||
package sudoku;
|
||||
|
||||
import org.checkerframework.checker.units.qual.C;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class Cell {
|
||||
|
||||
protected static int NOSYMBOLE = -1;
|
||||
protected static int NOSYMBOL = -1;
|
||||
protected int symbolIndex;
|
||||
protected Block block = null;
|
||||
//protected List<Block> blockContainers = null;
|
||||
//protected List<Sudoku> sudokuContainers = null;
|
||||
|
||||
public Cell(int symbolIndex) {
|
||||
this.symbolIndex = symbolIndex;
|
||||
@@ -14,9 +21,25 @@ public abstract class Cell {
|
||||
* @brief Default constructor, empty square
|
||||
*/
|
||||
public Cell() {
|
||||
this(NOSYMBOLE);
|
||||
this(NOSYMBOL);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
public Cell(Cell cell) {
|
||||
this.symbolIndex = cell.symbolIndex;
|
||||
this.sudokuContainers = cell.sudokuContainers;
|
||||
this.blockContainers = cell.blockContainers;
|
||||
}
|
||||
|
||||
public Cell(List<Sudoku> sudokuContainers, List<Block> blockContainers) {
|
||||
super();
|
||||
this.sudokuContainers = new ArrayList<>(sudokuContainers);
|
||||
this.blockContainers = new ArrayList<>(blockContainers);
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
public int getSymbolIndex() {
|
||||
return symbolIndex;
|
||||
}
|
||||
@@ -25,11 +48,30 @@ public abstract class Cell {
|
||||
return this.block;
|
||||
}
|
||||
|
||||
/*
|
||||
public List<Block> getBlockContainers() {
|
||||
return this.blockContainers;
|
||||
}
|
||||
|
||||
public void setBlockContainers(List<Block> block) {
|
||||
this.blockContainers = block;
|
||||
}
|
||||
|
||||
public List<Sudoku> getSudokuContainers() {
|
||||
return this.sudokuContainers;
|
||||
}
|
||||
*/
|
||||
// only SudokuFactory and SudokuSerializer should access this
|
||||
public void setBlock(Block block) {
|
||||
this.block = block;
|
||||
}
|
||||
|
||||
/*
|
||||
public void setSudokuContainers(List<Sudoku> sudoku) {
|
||||
this.sudokuContainers = sudoku;
|
||||
}
|
||||
*/
|
||||
|
||||
public boolean equalsValue(Cell otherCell) {
|
||||
return otherCell.getSymbolIndex() == this.getSymbolIndex();
|
||||
}
|
||||
@@ -38,7 +80,7 @@ public abstract class Cell {
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("| ");
|
||||
if (this.symbolIndex != NOSYMBOLE){
|
||||
if (this.symbolIndex != NOSYMBOL){
|
||||
sb.append(this.symbolIndex);
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -2,8 +2,8 @@ package sudoku;
|
||||
|
||||
public class ImmutableCell extends Cell {
|
||||
|
||||
public ImmutableCell(int symboleIndex) {
|
||||
super(symboleIndex);
|
||||
public ImmutableCell(int symbolIndex) {
|
||||
super(symbolIndex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,8 +3,15 @@
|
||||
*/
|
||||
package sudoku;
|
||||
|
||||
import sudoku.constraint.BlockConstraint;
|
||||
import sudoku.constraint.ColumnConstraint;
|
||||
import sudoku.constraint.IConstraint;
|
||||
import sudoku.constraint.LineConstraint;
|
||||
import sudoku.io.SudokuPrinter;
|
||||
import sudoku.io.SudokuSerializer;
|
||||
import sudoku.solver.Solver;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Main {
|
||||
public String getGreeting() {
|
||||
@@ -13,5 +20,54 @@ public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Main().getGreeting());
|
||||
int blockWidth = 2;
|
||||
int blockHeight = 2;
|
||||
var multidoku = SudokuFactory.createBasicEmptyRectangleSudoku(blockWidth, blockHeight);
|
||||
var sudoku = multidoku.getSubGrid(0);
|
||||
//SudokuPrinter.printRectangleSudoku(sudoku, blockWidth , blockHeight);
|
||||
//Line 1:
|
||||
((MutableCell)sudoku.getCell(0)).setSymbolIndex(0);
|
||||
((MutableCell)sudoku.getCell(1)).setSymbolIndex(1);
|
||||
((MutableCell)sudoku.getCell(2)).setSymbolIndex(2);
|
||||
((MutableCell)sudoku.getCell(3)).setSymbolIndex(3);
|
||||
//Line 2:
|
||||
((MutableCell)sudoku.getCell(4)).setSymbolIndex(2);
|
||||
((MutableCell)sudoku.getCell(5)).setSymbolIndex(3);
|
||||
((MutableCell)sudoku.getCell(6)).setSymbolIndex(0);
|
||||
((MutableCell)sudoku.getCell(7)).setSymbolIndex(1);
|
||||
//Line 3:
|
||||
((MutableCell)sudoku.getCell(8)).setSymbolIndex(1);
|
||||
((MutableCell)sudoku.getCell(9)).setSymbolIndex(0);
|
||||
((MutableCell)sudoku.getCell(10)).setSymbolIndex(3);
|
||||
((MutableCell)sudoku.getCell(11)).setSymbolIndex(2);
|
||||
// Line 4
|
||||
((MutableCell)sudoku.getCell(12)).setSymbolIndex(3);
|
||||
((MutableCell)sudoku.getCell(13)).setSymbolIndex(2);
|
||||
((MutableCell)sudoku.getCell(14)).setSymbolIndex(1);
|
||||
((MutableCell)sudoku.getCell(15)).setSymbolIndex(0);
|
||||
|
||||
SudokuPrinter.printRectangleSudoku(multidoku.getSubGrid(0), blockWidth , blockHeight);
|
||||
|
||||
ArrayList<IConstraint> constraints = new ArrayList<>();
|
||||
constraints.add(new LineConstraint());
|
||||
constraints.add(new ColumnConstraint());
|
||||
constraints.add(new BlockConstraint());
|
||||
|
||||
System.out.println(sudoku.isValid(constraints));
|
||||
|
||||
/*
|
||||
Solver solver = new Solver();
|
||||
ArrayList<IConstraint> constraints = new ArrayList<>();
|
||||
constraints.add(new LineConstraint());
|
||||
constraints.add(new ColumnConstraint());
|
||||
constraints.add(new BlockConstraint());
|
||||
try {
|
||||
solver.solve(multidoku, constraints);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,17 +17,27 @@ public class MutableCell extends Cell{
|
||||
this.possibleSymbols = new ArrayList<>();
|
||||
}
|
||||
|
||||
public MutableCell(MutableCell currentCell) {
|
||||
super(currentCell);
|
||||
this.possibleSymbols = currentCell.getPossibleSymbols();
|
||||
}
|
||||
|
||||
public void setSymbolIndex(int symbolIndex) {
|
||||
this.symbolIndex = symbolIndex;
|
||||
}
|
||||
|
||||
public void setPossibleSymbols(List<Integer> possibleSymbols) {
|
||||
this.possibleSymbols.clear();
|
||||
this.possibleSymbols.addAll(possibleSymbols);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the current symbolIndex and returns it
|
||||
* @return integer symbolIndex cleared
|
||||
*/
|
||||
public int clearCurrentSymbol() {
|
||||
int i = this.symbolIndex;
|
||||
setSymbolIndex(NOSYMBOLE);
|
||||
setSymbolIndex(NOSYMBOL);
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* @class Sudoku
|
||||
* @brief Représente une grille de sudoku avec N symboles
|
||||
* @brief Represents a sudoku
|
||||
*/
|
||||
public class Sudoku {
|
||||
|
||||
@@ -39,10 +39,10 @@ public class Sudoku {
|
||||
return this.blocks.size();
|
||||
}
|
||||
|
||||
public Boolean isValid(List<IConstraint> constraints) {
|
||||
public boolean isValid(List<IConstraint> constraints) {
|
||||
//not implemented
|
||||
//for eachcase check contraintes
|
||||
throw new Error("Function isValid() not implemented");
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<Cell> getCells() {
|
||||
@@ -100,7 +100,11 @@ public class Sudoku {
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
constraint.getPossibleSymbols(this, coord.getX(), coord.getY());
|
||||
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");
|
||||
}
|
||||
|
||||
@@ -29,7 +29,9 @@ public class SudokuFactory {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,15 @@ 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 MutableCell(newSymbolIndex));
|
||||
|
||||
/*
|
||||
boolean result = true;
|
||||
for (Block block : s.getCell(x, y).getBlockContainers()) {
|
||||
result = result && (!block.getCells().contains(new MutableCell(newSymbolIndex)));
|
||||
}
|
||||
*/
|
||||
return !s.getBlocks().getCells().contains(new MutableCell(newSymbolIndex));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,16 +7,16 @@ public class SudokuPrinter {
|
||||
public static void printRectangleSudoku(final Sudoku s, int blockWidth, int blockHeight) {
|
||||
for (int y = 0; y < s.getSize(); y++) {
|
||||
if (y % blockHeight == 0 && y > 0) {
|
||||
System.out.println("");
|
||||
System.out.println();
|
||||
}
|
||||
String line = "[ ";
|
||||
StringBuilder line = new StringBuilder("[ ");
|
||||
for (int x = 0; x < s.getSize(); x++) {
|
||||
line += (s.getCell(x, y).getSymbolIndex() + 1) + " ";
|
||||
line.append((s.getCell(x, y).getSymbolIndex() + 1)).append(" ");
|
||||
if (x % blockWidth == blockWidth - 1 && x != blockWidth * blockHeight - 1) {
|
||||
line += "| ";
|
||||
line.append("| ");
|
||||
}
|
||||
}
|
||||
line += "]";
|
||||
line.append("]");
|
||||
System.out.println(line);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,10 @@ import java.util.Stack;
|
||||
public class Solver {
|
||||
Stack<MutableCell> stack;
|
||||
|
||||
public Solver() {
|
||||
public Solver() {}
|
||||
|
||||
private void rollBack() {
|
||||
stack.pop();
|
||||
}
|
||||
|
||||
public MultiDoku solve(MultiDoku doku, List<IConstraint> constraints) throws Exception {
|
||||
@@ -25,26 +28,16 @@ public class Solver {
|
||||
while (!remainingCellsToCheck.isEmpty()) {
|
||||
int indexCurrentCell = rand.nextInt(remainingCellsToCheck.size());
|
||||
MutableCell currentCell = remainingCellsToCheck.get(indexCurrentCell);
|
||||
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);
|
||||
try {
|
||||
doku.updateSymbolsPossibilities();
|
||||
} catch (Exception e) {
|
||||
//TODO rollback
|
||||
}
|
||||
//TODO check constraints integrity in sudoku
|
||||
|
||||
|
||||
int symbol = currentCell.getPossibleSymbols().get(0);
|
||||
currentCell.setSymbolIndex(symbol);
|
||||
stack.push(new MutableCell(currentCell));
|
||||
try {
|
||||
doku.updateSymbolsPossibilities();
|
||||
} catch (Exception e) {
|
||||
this.rollBack();
|
||||
System.out.println(this.stack);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
remainingCellsToCheck.remove(indexCurrentCell);
|
||||
}
|
||||
return doku;
|
||||
|
||||
Reference in New Issue
Block a user