8 Commits

Author SHA1 Message Date
ba65cb9ff5 rainbow
All checks were successful
Linux arm64 / Build (push) Successful in 5m42s
2025-01-22 22:41:47 +01:00
396abb30b3 Merge branch 'master' into gui 2025-01-22 20:51:12 +01:00
fl.du.pr Grens
e19a9c7b27 update validity dokus
All checks were successful
Linux arm64 / Build (push) Successful in 1m5s
2025-01-21 22:05:16 +01:00
fl.du.pr Grens
38d1bc466f feat: added method to fill sudoku from integer list
All checks were successful
Linux arm64 / Build (push) Successful in 1m4s
2025-01-21 21:42:39 +01:00
f44311fd5c checking boudaries
All checks were successful
Linux arm64 / Build (push) Successful in 1m0s
2025-01-21 21:30:08 +01:00
dabcbe1d9a fix setCell
All checks were successful
Linux arm64 / Build (push) Successful in 34s
2025-01-21 21:01:30 +01:00
a981034030 setCell function
All checks were successful
Linux arm64 / Build (push) Successful in 34s
2025-01-21 20:58:24 +01:00
fl.du.pr Grens
25df004c3c test
All checks were successful
Linux arm64 / Build (push) Successful in 52s
2025-01-21 20:44:02 +01:00
7 changed files with 209 additions and 79 deletions

View File

@@ -0,0 +1,70 @@
package gui;
import java.util.ArrayList;
import java.util.List;
public class ColorGenerator {
public static class Color {
public float r, g, b;
public Color(float r, float g, float b) {
this.r = r;
this.g = g;
this.b = b;
}
}
public static List<Color> greatPalette(int colorCount) {
List<Color> colors = greatScheme(colorCount);
List<Color> newOrder = new ArrayList<>();
int newIndex = 0;
while (!colors.isEmpty()) {
int randomIndex = newIndex % colors.size();
newOrder.add(colors.get(randomIndex));
colors.remove(randomIndex);
newIndex += Math.sqrt(colorCount) + 1;
}
return newOrder;
}
public static List<Color> greatScheme(int colorCount) {
List<Color> colors = new ArrayList<>();
for (int i = 0; i < colorCount; i++) {
colors.add(hslToRgb((float) (i) / (float) colorCount, 0.9f, 0.4f));
}
return colors;
}
public static Color hslToRgb(float h, float s, float l){
float r, g, b;
if (s == 0f) {
r = g = b = l; // achromatic
} else {
float q = l < 0.5f ? l * (1 + s) : l + s - l * s;
float p = 2 * l - q;
r = hueToRgb(p, q, h + 1f/3f);
g = hueToRgb(p, q, h);
b = hueToRgb(p, q, h - 1f/3f);
}
return new Color(r, g, b);
}
/** Helper method that converts hue to rgb */
public static float hueToRgb(float p, float q, float t) {
if (t < 0f)
t += 1f;
if (t > 1f)
t -= 1f;
if (t < 1f / 6f)
return p + (q - p) * 6f * t;
if (t < 1f / 2f)
return q;
if (t < 2f / 3f)
return p + (q - p) * (2f / 3f - t) * 6f;
return p;
}
}

View File

@@ -1,46 +1,91 @@
package gui;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import gui.ColorGenerator.Color;
import imgui.ImGui;
import imgui.ImVec2;
import imgui.ImVec4;
import imgui.app.Application;
import imgui.app.Configuration;
import imgui.flag.ImGuiCol;
import sudoku.Block;
import sudoku.Cell;
import sudoku.MultiDoku;
import sudoku.Sudoku;
import sudoku.SudokuFactory;
public class Main extends Application {
// temp thing
private static int[] values = new int[9 * 9];
private Sudoku sudoku;
private int currentIndex = -1;
private Map<Block, Color> colorPalette = new HashMap<>();
private static final int SUDOKU_SIZE = 5;
@Override
protected void configure(Configuration config) {
config.setTitle("Let's play sudoku!");
}
private void initColors() {
List<Color> colors = ColorGenerator.greatPalette(sudoku.getSize());
int index = 0;
for (Block block : sudoku.getBlocks()) {
colorPalette.put(block, colors.get(index));
index++;
}
}
@Override
protected void initImGui(Configuration config) {
super.initImGui(config);
ImGui.getIO().getFonts().addFontFromFileTTF("comic.ttf", 50.0f);
Random r = new Random();
for (int i = 0; i < 9 * 9; i++) {
values[i] = r.nextInt(9) + 1;
MultiDoku doku = SudokuFactory.createBasicEmptySquareSudoku(SUDOKU_SIZE);
sudoku = doku.getSubGrid(0);
initColors();
}
private void renderPopup() {
if (ImGui.beginPopup("editPopup")) {
for (int i = 1; i < sudoku.getSize() + 1; i++) {
if(i % (int) (Math.sqrt(sudoku.getSize())) != 1)
ImGui.sameLine();
if(ImGui.button(Integer.toString(i), new ImVec2(50, 50))){
this.sudoku.setCellSymbol(currentIndex % sudoku.getSize(), currentIndex / sudoku.getSize(), i - 1);
ImGui.closeCurrentPopup();
}
}
ImGui.endPopup();
}
}
@Override
public void process() {
ImGui.begin("Sudoku Window");
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++) {
for (int y = 0; y < sudoku.getSize(); y++) {
for (int x = 0; x < sudoku.getSize(); x++) {
if (x > 0)
ImGui.sameLine();
ImGui.pushID(y * 9 + x);
ImGui.selectable(Integer.toString(values[y * 9 + x]), false, 0, new ImVec2(50, 50));
ImGui.popID();
int index = y * sudoku.getSize() + x;
Cell cell = sudoku.getCell(x, y);
int symbol = cell.getSymbolIndex();
Color blockColor = colorPalette.get(cell.getBlock());
ImGui.pushStyleColor(ImGuiCol.Header, new ImVec4(blockColor.r, blockColor.g, blockColor.b, 1.0f));
if (ImGui.selectable(Integer.toString(symbol + 1) + "##" + index, true, 0, new ImVec2(50, 50))) {
ImGui.openPopup("editPopup");
currentIndex = index;
}
ImGui.popStyleColor();
}
}
renderPopup();
ImGui.end();
// ImGui.showDemoWindow();
ImGui.showDemoWindow();
}
public static void main(String[] args) {

View File

@@ -3,15 +3,9 @@
*/
package sudoku;
import sudoku.constraint.BlockConstraint;
import sudoku.constraint.ColumnConstraint;
import sudoku.constraint.IConstraint;
import sudoku.constraint.LineConstraint;
import sudoku.io.SudokuPrinter;
import sudoku.io.SudokuSerializer;
import sudoku.solver.Solver;
import java.util.Arrays;
import java.util.ArrayList;
import sudoku.io.SudokuPrinter;
public class Main {
public String getGreeting() {
@@ -20,41 +14,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<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();
ArrayList<IConstraint> constraints = new ArrayList<>();

View File

@@ -26,14 +26,6 @@ public class MultiDoku {
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(){
List<MutableCell> mutableCells = new ArrayList<>();
for (Sudoku sudoku : subGrids){

View File

@@ -21,9 +21,52 @@ 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<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) {
int index = y * getSize() + x;
assert(index < getSize() * getSize());
assert (isValidCoords(x, y));
return this.cells.get(index);
}
@@ -40,8 +83,8 @@ public class Sudoku {
}
public boolean isValid(List<IConstraint> constraints) {
//not implemented
//for eachcase check contraintes
// not implemented
// for eachcase check contraintes
return false;
}
@@ -56,7 +99,7 @@ public class Sudoku {
public List<MutableCell> getMutableCells() {
List<MutableCell> mutableCells = new ArrayList<>();
for (Cell cell : this.cells) {
if (cell instanceof MutableCell){
if (cell instanceof MutableCell) {
mutableCells.add((MutableCell) cell);
}
}
@@ -68,7 +111,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,15 +119,14 @@ 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);
@@ -105,14 +147,15 @@ public class Sudoku {
cell.setPossibleSymbols(newPossibleSymbols);
if (cell.getPossibleSymbols().isEmpty()){
if (cell.getPossibleSymbols().isEmpty()) {
throw new Exception("Rollback bitch");
}
}
}
}
public String toString () {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Sudoku {");
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 {
if (!doku.isValid(constraints)) {
throw new Exception("Invalid doku");
}
List<MutableCell> allMutableCells = doku.getMutableCells();
List<MutableCell> remainingCellsToCheck = new ArrayList<>(allMutableCells);
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