Merge branch 'master' into gui

This commit is contained in:
2025-01-22 20:51:12 +01:00
5 changed files with 82 additions and 65 deletions

View File

@@ -12,6 +12,8 @@ import sudoku.io.SudokuSerializer;
import sudoku.solver.Solver; import sudoku.solver.Solver;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main { public class Main {
public String getGreeting() { public String getGreeting() {
@@ -20,41 +22,20 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(new Main().getGreeting()); System.out.println(new Main().getGreeting());
int blockWidth = 2; int blockWidth = 2;
int blockHeight = 2; int blockHeight = 2;
var multidoku = SudokuFactory.createBasicEmptyRectangleSudoku(blockWidth, blockHeight); var multidoku = SudokuFactory.createBasicEmptyRectangleSudoku(blockWidth, blockHeight);
var sudoku = multidoku.getSubGrid(0); var sudoku = multidoku.getSubGrid(0);
//SudokuPrinter.printRectangleSudoku(sudoku, blockWidth , blockHeight); if(!sudoku.setCellsSymbol(Arrays.asList(0,1,2,3, 2,3,1,1, 1,0,3,2, 3,2,1,1))){
//Line 1: System.out.println("At least one of those values does not respect the constraints.");
((MutableCell)sudoku.getCell(0)).setSymbolIndex(0); }
((MutableCell)sudoku.getCell(1)).setSymbolIndex(1);
((MutableCell)sudoku.getCell(2)).setSymbolIndex(2);
((MutableCell)sudoku.getCell(3)).setSymbolIndex(3); //sudoku.setCellSymbol(8,3,0);
//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);
SudokuPrinter.printRectangleSudoku(multidoku.getSubGrid(0), blockWidth , blockHeight); SudokuPrinter.printRectangleSudoku(multidoku.getSubGrid(0), blockWidth , blockHeight);
ArrayList<IConstraint> 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(); Solver solver = new Solver();
ArrayList<IConstraint> constraints = new ArrayList<>(); ArrayList<IConstraint> constraints = new ArrayList<>();

View File

@@ -26,14 +26,6 @@ public class MultiDoku {
return subGrids.get(i); return subGrids.get(i);
} }
public boolean isValid(List<IConstraint> constraints){
for (Sudoku sudoku : subGrids){
if (!sudoku.isValid(constraints))
return false;
}
return true;
}
public List<MutableCell> getMutableCells(){ public List<MutableCell> getMutableCells(){
List<MutableCell> mutableCells = new ArrayList<>(); List<MutableCell> mutableCells = new ArrayList<>();
for (Sudoku sudoku : subGrids){ for (Sudoku sudoku : subGrids){

View File

@@ -21,9 +21,53 @@ 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
*
* @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<Integer> 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) { 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);
} }
@@ -39,12 +83,6 @@ public class Sudoku {
return this.blocks.size(); return this.blocks.size();
} }
public boolean isValid(List<IConstraint> constraints) {
//not implemented
//for eachcase check contraintes
return false;
}
public List<Cell> getCells() { public List<Cell> getCells() {
return this.cells; return this.cells;
} }
@@ -56,7 +94,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);
} }
} }
@@ -68,7 +106,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)) {
@@ -76,43 +114,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++) {

View File

@@ -19,9 +19,6 @@ public class Solver {
} }
public MultiDoku solve(MultiDoku doku, List<IConstraint> constraints) throws Exception { public MultiDoku solve(MultiDoku doku, List<IConstraint> constraints) throws Exception {
if (!doku.isValid(constraints)) {
throw new Exception("Invalid doku");
}
List<MutableCell> allMutableCells = doku.getMutableCells(); List<MutableCell> allMutableCells = doku.getMutableCells();
List<MutableCell> remainingCellsToCheck = new ArrayList<>(allMutableCells); List<MutableCell> remainingCellsToCheck = new ArrayList<>(allMutableCells);
Random rand = new Random(); Random rand = new Random();

10
imgui.ini Normal file
View File

@@ -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