From 25df004c3cf335a4f62844a3ddb9a158de94ef37 Mon Sep 17 00:00:00 2001 From: "fl.du.pr Grens" Date: Tue, 21 Jan 2025 20:44:02 +0100 Subject: [PATCH 1/6] test From a9810340300482c8b7799589c22bcc0bce8eafca Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Tue, 21 Jan 2025 20:58:24 +0100 Subject: [PATCH 2/6] setCell function --- app/src/main/java/sudoku/Sudoku.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/src/main/java/sudoku/Sudoku.java b/app/src/main/java/sudoku/Sudoku.java index e43350f..91a4bd6 100644 --- a/app/src/main/java/sudoku/Sudoku.java +++ b/app/src/main/java/sudoku/Sudoku.java @@ -21,6 +21,19 @@ public class Sudoku { this.constraints = constraints; } + /** + * 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) { + for (IConstraint constraint : this.constraints) { + if (!constraint.canBePlaced(this, x, y, value)) { + return false; + } + } + return true; + } + public Cell getCell(int x, int y) { int index = y * getSize() + x; assert(index < getSize() * getSize()); From dabcbe1d9a29595f7012917655d3e394f618d645 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Tue, 21 Jan 2025 21:01:30 +0100 Subject: [PATCH 3/6] fix setCell --- app/src/main/java/sudoku/Sudoku.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/sudoku/Sudoku.java b/app/src/main/java/sudoku/Sudoku.java index 91a4bd6..686ad94 100644 --- a/app/src/main/java/sudoku/Sudoku.java +++ b/app/src/main/java/sudoku/Sudoku.java @@ -31,7 +31,12 @@ public class Sudoku { return false; } } - return true; + Cell cell = getCell(x, y); + if (cell instanceof MutableCell mCell) { + mCell.setSymbolIndex(value); + return true; + } + return false; } public Cell getCell(int x, int y) { From f44311fd5ca25c0545c42a3078e0ca246c953a64 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Tue, 21 Jan 2025 21:30:08 +0100 Subject: [PATCH 4/6] checking boudaries --- app/src/main/java/sudoku/Sudoku.java | 53 ++++++++++++++++------------ 1 file changed, 31 insertions(+), 22 deletions(-) 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++) { From 38d1bc466f0e9a21ff90422aca2ece232868b167 Mon Sep 17 00:00:00 2001 From: "fl.du.pr Grens" Date: Tue, 21 Jan 2025 21:42:39 +0100 Subject: [PATCH 5/6] feat: added method to fill sudoku from integer list --- app/src/main/java/sudoku/Main.java | 32 +++++++++------------------- app/src/main/java/sudoku/Sudoku.java | 17 ++++++++++++++- imgui.ini | 10 +++++++++ 3 files changed, 36 insertions(+), 23 deletions(-) create mode 100644 imgui.ini diff --git a/app/src/main/java/sudoku/Main.java b/app/src/main/java/sudoku/Main.java index b5ae811..d4a1e25 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,34 +22,18 @@ 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); - + sudoku.setCellsSymbol(Arrays.asList(0,1,2,3, 2,3,0,1, 1,0,3,2, 3,2,1,0)); SudokuPrinter.printRectangleSudoku(multidoku.getSubGrid(0), blockWidth , blockHeight); + //sudoku.setCellSymbol(8,3,0); + + + ArrayList constraints = new ArrayList<>(); constraints.add(new LineConstraint()); constraints.add(new ColumnConstraint()); @@ -55,6 +41,8 @@ public class Main { System.out.println(sudoku.isValid(constraints)); + + /* Solver solver = new Solver(); ArrayList constraints = new ArrayList<>(); diff --git a/app/src/main/java/sudoku/Sudoku.java b/app/src/main/java/sudoku/Sudoku.java index b8b4f8f..6ea68d5 100644 --- a/app/src/main/java/sudoku/Sudoku.java +++ b/app/src/main/java/sudoku/Sudoku.java @@ -31,7 +31,7 @@ public class Sudoku { /** * 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) { @@ -49,6 +49,21 @@ public class Sudoku { 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(); + if (!this.setCellSymbol(x, y, values.get(i))) { + return false; + } + } + return true; + } + + public Cell getCell(int x, int y) { int index = y * getSize() + x; assert (isValidCoords(x, y)); 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 + From e19a9c7b2727f6ca40f861e21495337f02494e5a Mon Sep 17 00:00:00 2001 From: "fl.du.pr Grens" Date: Tue, 21 Jan 2025 22:05:16 +0100 Subject: [PATCH 6/6] update validity dokus --- app/src/main/java/sudoku/Main.java | 17 +++++------------ app/src/main/java/sudoku/MultiDoku.java | 8 -------- app/src/main/java/sudoku/Sudoku.java | 9 ++------- app/src/main/java/sudoku/solver/Solver.java | 3 --- 4 files changed, 7 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/sudoku/Main.java b/app/src/main/java/sudoku/Main.java index d4a1e25..fe57633 100644 --- a/app/src/main/java/sudoku/Main.java +++ b/app/src/main/java/sudoku/Main.java @@ -27,21 +27,14 @@ public class Main { int blockHeight = 2; var multidoku = SudokuFactory.createBasicEmptyRectangleSudoku(blockWidth, blockHeight); var sudoku = multidoku.getSubGrid(0); - sudoku.setCellsSymbol(Arrays.asList(0,1,2,3, 2,3,0,1, 1,0,3,2, 3,2,1,0)); - SudokuPrinter.printRectangleSudoku(multidoku.getSubGrid(0), blockWidth , blockHeight); + 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); - - - ArrayList constraints = new ArrayList<>(); - constraints.add(new LineConstraint()); - constraints.add(new ColumnConstraint()); - constraints.add(new BlockConstraint()); - - System.out.println(sudoku.isValid(constraints)); - - + SudokuPrinter.printRectangleSudoku(multidoku.getSubGrid(0), blockWidth , blockHeight); /* Solver solver = new Solver(); 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 6ea68d5..e0401e4 100644 --- a/app/src/main/java/sudoku/Sudoku.java +++ b/app/src/main/java/sudoku/Sudoku.java @@ -56,7 +56,8 @@ public class Sudoku { for (int i = 0; i < values.size(); i++) { int x = i%this.blocks.size(); int y = (i-x)/this.blocks.size(); - if (!this.setCellSymbol(x, y, values.get(i))) { + int value = values.get(i); + if (!this.setCellSymbol(x, y, value)) { return false; } } @@ -82,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; } 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();