diff --git a/app/src/main/java/sudoku/Main.java b/app/src/main/java/sudoku/Main.java index b5ae811..fe57633 100644 --- a/app/src/main/java/sudoku/Main.java +++ b/app/src/main/java/sudoku/Main.java @@ -12,6 +12,8 @@ import sudoku.io.SudokuSerializer; import sudoku.solver.Solver; import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; public class Main { public String getGreeting() { @@ -20,41 +22,20 @@ 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); + if(!sudoku.setCellsSymbol(Arrays.asList(0,1,2,3, 2,3,1,1, 1,0,3,2, 3,2,1,1))){ + System.out.println("At least one of those values does not respect the constraints."); + } + + + //sudoku.setCellSymbol(8,3,0); SudokuPrinter.printRectangleSudoku(multidoku.getSubGrid(0), blockWidth , blockHeight); - ArrayList 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 constraints = new ArrayList<>(); diff --git a/app/src/main/java/sudoku/MultiDoku.java b/app/src/main/java/sudoku/MultiDoku.java index 956f076..0e4a2c5 100644 --- a/app/src/main/java/sudoku/MultiDoku.java +++ b/app/src/main/java/sudoku/MultiDoku.java @@ -26,14 +26,6 @@ public class MultiDoku { return subGrids.get(i); } - public boolean isValid(List constraints){ - for (Sudoku sudoku : subGrids){ - if (!sudoku.isValid(constraints)) - return false; - } - return true; - } - public List getMutableCells(){ List mutableCells = new ArrayList<>(); for (Sudoku sudoku : subGrids){ diff --git a/app/src/main/java/sudoku/Sudoku.java b/app/src/main/java/sudoku/Sudoku.java index e43350f..e0401e4 100644 --- a/app/src/main/java/sudoku/Sudoku.java +++ b/app/src/main/java/sudoku/Sudoku.java @@ -21,9 +21,53 @@ 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; + } + } + Cell cell = getCell(x, y); + if (cell instanceof MutableCell mCell) { + mCell.setSymbolIndex(value); + return true; + } + return false; + } + + public boolean setCellsSymbol(List 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 (!this.setCellSymbol(x, y, value)) { + return false; + } + } + return true; + } + + 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); } @@ -39,12 +83,6 @@ public class Sudoku { return this.blocks.size(); } - public boolean isValid(List constraints) { - //not implemented - //for eachcase check contraintes - return false; - } - public List getCells() { return this.cells; } @@ -56,7 +94,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); } } @@ -68,7 +106,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)) { @@ -76,43 +114,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++) { diff --git a/app/src/main/java/sudoku/solver/Solver.java b/app/src/main/java/sudoku/solver/Solver.java index e338437..59da5eb 100644 --- a/app/src/main/java/sudoku/solver/Solver.java +++ b/app/src/main/java/sudoku/solver/Solver.java @@ -19,9 +19,6 @@ public class Solver { } public MultiDoku solve(MultiDoku doku, List constraints) throws Exception { - if (!doku.isValid(constraints)) { - throw new Exception("Invalid doku"); - } List allMutableCells = doku.getMutableCells(); List remainingCellsToCheck = new ArrayList<>(allMutableCells); Random rand = new Random(); diff --git a/imgui.ini b/imgui.ini new file mode 100644 index 0000000..d7c1d6f --- /dev/null +++ b/imgui.ini @@ -0,0 +1,10 @@ +[Window][Debug##Default] +Pos=60,60 +Size=400,400 +Collapsed=0 + +[Window][Window] +Pos=32,164 +Size=1096,529 +Collapsed=1 +