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