This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user