12 Commits

Author SHA1 Message Date
b05351bc20 more menus
Some checks failed
Linux arm64 / Build (push) Failing after 14m21s
2025-01-23 15:50:10 +01:00
7ab1173fb3 change BaseMenu to BaseView
Some checks failed
Linux arm64 / Build (push) Failing after 5m2s
2025-01-23 14:41:40 +01:00
749f5c831d add menu logic
Some checks failed
Linux arm64 / Build (push) Failing after 5m1s
2025-01-23 12:50:00 +01:00
48fc88d8ab fix constraints
All checks were successful
Linux arm64 / Build (push) Successful in 5m12s
2025-01-23 10:37:50 +01:00
6c8bfad18d add SudokuRenderer
Some checks failed
Linux arm64 / Build (push) Has been cancelled
2025-01-23 10:33:59 +01:00
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
ea5afa66df basic grid display
All checks were successful
Linux arm64 / Build (push) Successful in 33s
2025-01-22 16:09:00 +01:00
a8359d2f69 comic
All checks were successful
Linux arm64 / Build (push) Successful in 53s
2025-01-21 22:12:23 +01:00
2d980ac816 non
All checks were successful
Linux arm64 / Build (push) Successful in 24s
2025-01-21 20:51:03 +01:00
fl.du.pr Grens
42d5bdc8d6 update title window
All checks were successful
Linux arm64 / Build (push) Successful in 28s
2025-01-21 20:50:33 +01:00
121c60ddef feneeeeeeeeeeetre
All checks were successful
Linux arm64 / Build (push) Successful in 26s
2025-01-21 19:59:24 +01:00
19 changed files with 550 additions and 18 deletions

2
.gitignore vendored
View File

@@ -8,3 +8,5 @@ build
.idea
output.puml
imgui.ini

View File

@@ -29,11 +29,13 @@ dependencies {
implementation 'org.json:json:20250107'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.18.2'
implementation "io.github.spair:imgui-java-app:1.88.0"
}
application {
// Define the main class for the application.
mainClass = 'sudoku.Main'
mainClass = 'gui.Main'
}
tasks.named('test') {

BIN
app/comic.ttf Normal file

Binary file not shown.

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

@@ -0,0 +1,34 @@
package gui;
import gui.menu.MainMenu;
import gui.menu.StateMachine;
import imgui.ImGui;
import imgui.app.Application;
import imgui.app.Configuration;
public class Main extends Application {
private final StateMachine stateMachine = new StateMachine();
@Override
protected void configure(Configuration config) {
config.setTitle("Let's play sudoku!");
}
@Override
protected void initImGui(Configuration config) {
super.initImGui(config);
ImGui.getIO().getFonts().addFontFromFileTTF("comic.ttf", 50.0f);
stateMachine.pushState(new MainMenu(stateMachine));
}
@Override
public void process() {
stateMachine.render();
// ImGui.showDemoWindow();
}
public static void main(String[] args) {
launch(new Main());
}
}

View File

@@ -0,0 +1,79 @@
package gui;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import gui.ColorGenerator.Color;
import imgui.ImGui;
import imgui.ImVec2;
import imgui.ImVec4;
import imgui.flag.ImGuiCol;
import imgui.flag.ImGuiStyleVar;
import sudoku.Block;
import sudoku.Cell;
import sudoku.Sudoku;
public class SudokuRenderer {
private final Sudoku sudoku;
private int currentIndex = -1;
private final Map<Block, Color> colorPalette;
public SudokuRenderer(Sudoku sudoku) {
this.sudoku = sudoku;
this.colorPalette = new HashMap<>();
initColors();
}
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++;
}
}
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();
}
}
public void render() {
ImGui.begin("Sudoku Window");
for (int y = 0; y < sudoku.getSize(); y++) {
for (int x = 0; x < sudoku.getSize(); x++) {
if (x > 0)
ImGui.sameLine();
int index = y * sudoku.getSize() + x;
Cell cell = sudoku.getCell(x, y);
int symbol = cell.getSymbolIndex();
Color blockColor = colorPalette.get(cell.getBlock());
ImGui.pushStyleVar(ImGuiStyleVar.SelectableTextAlign, new ImVec2(0.5f, 0.5f));
ImGui.pushStyleColor(ImGuiCol.Header, new ImVec4(blockColor.r, blockColor.g, blockColor.b, 1.0f));
String cellText = "";
if (symbol != -1)
cellText += Integer.toString(symbol + 1);
if (ImGui.selectable(cellText + "##" + index, true, 0, new ImVec2(50, 50))) {
ImGui.openPopup("editPopup");
currentIndex = index;
}
ImGui.popStyleVar();
ImGui.popStyleColor();
}
}
renderPopup();
ImGui.end();
}
}

View File

@@ -0,0 +1,25 @@
package gui.menu;
import imgui.ImGui;
public abstract class BaseView {
protected final StateMachine stateMachine;
public BaseView(StateMachine stateMachine) {
this.stateMachine = stateMachine;
}
public abstract void render();
public void closeMenu() {
this.stateMachine.popState();
}
protected void renderReturnButton() {
if (ImGui.button("Retour")) {
closeMenu();
}
}
}

View File

@@ -0,0 +1,17 @@
package gui.menu;
import imgui.ImGui;
public class CreateGameMenu extends BaseView {
public CreateGameMenu(StateMachine stateMachine) {
super(stateMachine);
}
@Override
public void render() {
ImGui.text("Créer");
renderReturnButton();
}
}

View File

@@ -0,0 +1,17 @@
package gui.menu;
import imgui.ImGui;
public class JoinGameMenu extends BaseView {
public JoinGameMenu(StateMachine stateMachine) {
super(stateMachine);
}
@Override
public void render() {
ImGui.text("Rejoindre");
renderReturnButton();
}
}

View File

@@ -0,0 +1,37 @@
package gui.menu;
import imgui.ImGui;
import imgui.ImVec2;
public class MainMenu extends BaseView {
public MainMenu(StateMachine stateMachine) {
super(stateMachine);
}
@Override
public void render() {
var displaySize = ImGui.getIO().getDisplaySize();
ImVec2 buttonSize = new ImVec2(500, 150);
int paddingHeight = 20;
float centerX = displaySize.x / 2.0f - buttonSize.x / 2.0f;
float centerY = displaySize.y / 2.0f - buttonSize.y / 2.0f;
ImGui.setCursorPos(centerX, centerY - buttonSize.y - paddingHeight);
if (ImGui.button("Mode histoire", buttonSize)) {
this.stateMachine.pushState(new SoloMenu(this.stateMachine));
}
ImGui.setCursorPos(centerX, centerY);
if (ImGui.button("Multijoueur", buttonSize)) {
this.stateMachine.pushState(new MultiMenu(this.stateMachine));
}
ImGui.setCursorPos(centerX, centerY + buttonSize.y + paddingHeight);
if (ImGui.button("Options", buttonSize)) {
this.stateMachine.pushState(new OptionsMenu(this.stateMachine));
}
}
}

View File

@@ -0,0 +1,56 @@
package gui.menu;
import imgui.ImGui;
import imgui.ImVec2;
import imgui.type.ImInt;
import imgui.type.ImString;
public class MultiMenu extends BaseView {
private final ImInt port = new ImInt(25565);
private final ImString address = new ImString("localhost");
public MultiMenu(StateMachine stateMachine) {
super(stateMachine);
}
private void renderCreate() {
ImVec2 displaySize = ImGui.getIO().getDisplaySize();
ImGui.beginChild("##CreateGame", new ImVec2(displaySize.x / 2.0f, displaySize.y * 8.0f / 9.0f));
ImGui.inputInt("Port", port);
if (ImGui.button("Créer")) {
// TODO: create game
}
ImGui.endChild();
}
private void renderJoin() {
ImVec2 displaySize = ImGui.getIO().getDisplaySize();
ImGui.beginChild("##JoinGame", new ImVec2(displaySize.x / 2.0f, displaySize.y * 8.0f / 9.0f));
ImGui.inputText("Adresse", address);
ImGui.inputInt("Port", port);
if (ImGui.button("Rejoindre")) {
// TODO: join game
}
ImGui.endChild();
}
@Override
public void render() {
renderCreate();
ImGui.sameLine();
renderJoin();
ImVec2 displaySize = ImGui.getIO().getDisplaySize();
ImVec2 buttonSize = new ImVec2(500, 70.0f);
float centerX = displaySize.x / 2.0f - buttonSize.x / 2.0f;
ImGui.setCursorPosX(centerX);
if (ImGui.button("Retour", buttonSize)) {
closeMenu();
}
}
}

View File

@@ -0,0 +1,18 @@
package gui.menu;
import imgui.ImGui;
public class OptionsMenu extends BaseView {
public OptionsMenu(StateMachine stateMachine) {
super(stateMachine);
}
@Override
public void render() {
// TODO Auto-generated method stub
ImGui.text("Options");
renderReturnButton();
}
}

View File

@@ -0,0 +1,47 @@
package gui.menu;
import imgui.ImGui;
import imgui.type.ImInt;
public class SoloMenu extends BaseView {
private final ImInt sudokuType = new ImInt(0);
private static final String[] sudokuTypes = { "Carré", "Rectangle" };
private static final int SQUARE = 0, RECTANGLE = 1;
private final ImInt sudokuSize = new ImInt(3);
private final ImInt sudokuWidth = new ImInt(3);
private final ImInt sudokuHeight = new ImInt(3);
public SoloMenu(StateMachine stateMachine) {
super(stateMachine);
}
@Override
public void render() {
ImGui.text("Solo");
ImGui.combo("Type de Sudoku", sudokuType, sudokuTypes);
switch (sudokuType.get()) {
case SQUARE:
ImGui.inputInt("Taille", sudokuSize);
if (ImGui.button("Résoudre un sudoku")) {
this.stateMachine.pushState(new SudokuView(stateMachine, sudokuSize.get(), sudokuSize.get()));
}
break;
case RECTANGLE:
ImGui.inputInt("Largeur", sudokuHeight);
ImGui.inputInt("Longueur", sudokuWidth);
if (ImGui.button("Résoudre un sudoku")) {
this.stateMachine.pushState(new SudokuView(stateMachine, sudokuWidth.get(), sudokuHeight.get()));
}
break;
default:
break;
}
renderReturnButton();
}
}

View File

@@ -0,0 +1,43 @@
package gui.menu;
import java.util.Stack;
import imgui.ImGui;
import imgui.ImVec2;
import imgui.flag.ImGuiKey;
import imgui.flag.ImGuiWindowFlags;
public class StateMachine {
private final Stack<BaseView> menus;
public StateMachine() {
this.menus = new Stack<>();
}
public void pushState(BaseView menu) {
menus.add(menu);
}
public void popState() {
menus.pop();
}
private void checkEscape() {
if (ImGui.isKeyPressed(ImGuiKey.Escape) && menus.size() > 1) {
popState();
}
}
public void render() {
var displaySize = ImGui.getIO().getDisplaySize();
ImGui.setNextWindowPos(new ImVec2(0.0f, 0.0f));
ImGui.setNextWindowSize(displaySize);
ImGui.begin("##Main Window", null, ImGuiWindowFlags.NoDecoration | ImGuiWindowFlags.NoMove
| ImGuiWindowFlags.NoSavedSettings | ImGuiWindowFlags.NoBackground);
menus.getLast().render();
ImGui.end();
checkEscape();
}
}

View File

@@ -0,0 +1,87 @@
package gui.menu;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import gui.ColorGenerator;
import gui.ColorGenerator.Color;
import imgui.ImGui;
import imgui.ImVec2;
import imgui.ImVec4;
import imgui.flag.ImGuiCol;
import imgui.flag.ImGuiStyleVar;
import sudoku.Block;
import sudoku.Cell;
import sudoku.MultiDoku;
import sudoku.Sudoku;
import sudoku.SudokuFactory;
public class SudokuView extends BaseView {
private final Sudoku sudoku;
private int currentIndex = -1;
private final Map<Block, Color> colorPalette;
public SudokuView(StateMachine stateMachine, int width, int height) {
super(stateMachine);
MultiDoku doku = SudokuFactory.createBasicEmptyRectangleSudoku(width, height);
this.sudoku = doku.getSubGrid(0);
this.colorPalette = new HashMap<>();
initColors();
}
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++;
}
}
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 render() {
for (int y = 0; y < sudoku.getSize(); y++) {
for (int x = 0; x < sudoku.getSize(); x++) {
if (x > 0)
ImGui.sameLine();
int index = y * sudoku.getSize() + x;
Cell cell = sudoku.getCell(x, y);
int symbol = cell.getSymbolIndex();
Color blockColor = colorPalette.get(cell.getBlock());
ImGui.pushStyleVar(ImGuiStyleVar.SelectableTextAlign, new ImVec2(0.5f, 0.5f));
ImGui.pushStyleColor(ImGuiCol.Header, new ImVec4(blockColor.r, blockColor.g, blockColor.b, 1.0f));
String cellText = "";
if (symbol != -1)
cellText += Integer.toString(symbol + 1);
if (ImGui.selectable(cellText + "##" + index, true, 0, new ImVec2(50, 50))) {
ImGui.openPopup("editPopup");
currentIndex = index;
}
ImGui.popStyleVar();
ImGui.popStyleColor();
}
}
renderPopup();
if (ImGui.button("Résoudre")) {
// TODO: solve
}
renderReturnButton();
}
}

View File

@@ -3,17 +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.ArrayList;
import java.util.Arrays;
import java.util.List;
import sudoku.io.SudokuPrinter;
public class Main {
public String getGreeting() {

View File

@@ -54,16 +54,15 @@ public class Sudoku {
return false;
}
for (int i = 0; i < values.size(); i++) {
int x = i%this.blocks.size();
int y = (i-x)/this.blocks.size();
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;
@@ -83,6 +82,12 @@ public class Sudoku {
return this.blocks.size();
}
public boolean isValid(List<IConstraint> constraints) {
// not implemented
// for eachcase check contraintes
return false;
}
public List<Cell> getCells() {
return this.cells;
}
@@ -149,6 +154,7 @@ public class Sudoku {
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Sudoku {");

View File

@@ -7,7 +7,7 @@ public class ColumnConstraint implements IConstraint {
@Override
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
for (int i = 0; i < s.getSize(); i++) {
if (s.getCell(x, newSymbolIndex).getSymbolIndex() == newSymbolIndex)
if (s.getCell(x, i).getSymbolIndex() == newSymbolIndex)
return false;
}
return true;

View File

@@ -7,7 +7,7 @@ public class LineConstraint implements IConstraint {
@Override
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
for (int i = 0; i < s.getSize(); i++) {
if (s.getCell(newSymbolIndex, y).getSymbolIndex() == newSymbolIndex)
if (s.getCell(i, y).getSymbolIndex() == newSymbolIndex)
return false;
}
return true;