This commit is contained in:
@@ -21,11 +21,21 @@ public class Sudoku {
|
|||||||
this.constraints = constraints;
|
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
|
* Try to place a cell at the given coordinate
|
||||||
|
*
|
||||||
* @return false if it can't be done
|
* @return false if it can't be done
|
||||||
*/
|
*/
|
||||||
public boolean setCellSymbol(int x, int y, int value) {
|
public boolean setCellSymbol(int x, int y, int value) {
|
||||||
|
assert (isValidCoords(x, y));
|
||||||
for (IConstraint constraint : this.constraints) {
|
for (IConstraint constraint : this.constraints) {
|
||||||
if (!constraint.canBePlaced(this, x, y, value)) {
|
if (!constraint.canBePlaced(this, x, y, value)) {
|
||||||
return false;
|
return false;
|
||||||
@@ -41,7 +51,7 @@ public class Sudoku {
|
|||||||
|
|
||||||
public Cell getCell(int x, int y) {
|
public Cell getCell(int x, int y) {
|
||||||
int index = y * getSize() + x;
|
int index = y * getSize() + x;
|
||||||
assert(index < getSize() * getSize());
|
assert (isValidCoords(x, y));
|
||||||
return this.cells.get(index);
|
return this.cells.get(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,8 +68,8 @@ public class Sudoku {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isValid(List<IConstraint> constraints) {
|
public boolean isValid(List<IConstraint> constraints) {
|
||||||
//not implemented
|
// not implemented
|
||||||
//for eachcase check contraintes
|
// for eachcase check contraintes
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +84,7 @@ public class Sudoku {
|
|||||||
public List<MutableCell> getMutableCells() {
|
public List<MutableCell> getMutableCells() {
|
||||||
List<MutableCell> mutableCells = new ArrayList<>();
|
List<MutableCell> mutableCells = new ArrayList<>();
|
||||||
for (Cell cell : this.cells) {
|
for (Cell cell : this.cells) {
|
||||||
if (cell instanceof MutableCell){
|
if (cell instanceof MutableCell) {
|
||||||
mutableCells.add((MutableCell) cell);
|
mutableCells.add((MutableCell) cell);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,7 +96,7 @@ public class Sudoku {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Coordinate getCoordinateCell(Cell c) throws Exception {
|
private Coordinate getCoordinateCell(Cell c) throws Exception {
|
||||||
int x=0, y=0;
|
int x = 0, y = 0;
|
||||||
int size = this.getSize();
|
int size = this.getSize();
|
||||||
|
|
||||||
if (!this.contains(c)) {
|
if (!this.contains(c)) {
|
||||||
@@ -94,43 +104,42 @@ public class Sudoku {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (Cell cell : this.cells) {
|
for (Cell cell : this.cells) {
|
||||||
if (cell == c){
|
if (cell == c) {
|
||||||
return new Coordinate(x, y);
|
return new Coordinate(x, y);
|
||||||
}
|
}
|
||||||
if (x == size - 1){
|
if (x == size - 1) {
|
||||||
y+=1;
|
y += 1;
|
||||||
x=0;
|
x = 0;
|
||||||
}
|
} else {
|
||||||
else {
|
x += 1;
|
||||||
x+=1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new Coordinate(x, y);
|
return new Coordinate(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateSymbolsPossibilities() throws Exception {
|
public void updateSymbolsPossibilities() throws Exception {
|
||||||
for (IConstraint constraint : constraints) {
|
for (IConstraint constraint : constraints) {
|
||||||
List<MutableCell> mutableCells = this.getMutableCells();
|
List<MutableCell> mutableCells = this.getMutableCells();
|
||||||
for (MutableCell cell : mutableCells) {
|
for (MutableCell cell : mutableCells) {
|
||||||
Coordinate coord = null;
|
Coordinate coord = null;
|
||||||
try {
|
try {
|
||||||
coord = this.getCoordinateCell(cell);
|
coord = this.getCoordinateCell(cell);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
List<Integer> newPossibleSymbols = cell.getPossibleSymbols();
|
List<Integer> newPossibleSymbols = cell.getPossibleSymbols();
|
||||||
newPossibleSymbols.retainAll(constraint.getPossibleSymbols(this, coord.getX(), coord.getY()));
|
newPossibleSymbols.retainAll(constraint.getPossibleSymbols(this, coord.getX(), coord.getY()));
|
||||||
|
|
||||||
cell.setPossibleSymbols(newPossibleSymbols);
|
cell.setPossibleSymbols(newPossibleSymbols);
|
||||||
|
|
||||||
if (cell.getPossibleSymbols().isEmpty()){
|
if (cell.getPossibleSymbols().isEmpty()) {
|
||||||
throw new Exception("Rollback bitch");
|
throw new Exception("Rollback bitch");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString () {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("Sudoku {");
|
sb.append("Sudoku {");
|
||||||
for (int i = 0; i < getSize(); i++) {
|
for (int i = 0; i < getSize(); i++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user