From b1ae79a2e83fc5f0691b24e62536e5c419a50ce0 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Tue, 28 Jan 2025 09:56:44 +0100 Subject: [PATCH] add multidoku to solo --- app/src/main/java/gui/menu/SoloMenu.java | 18 ++- app/src/main/java/gui/menu/SudokuView.java | 135 +++++++-------------- 2 files changed, 56 insertions(+), 97 deletions(-) diff --git a/app/src/main/java/gui/menu/SoloMenu.java b/app/src/main/java/gui/menu/SoloMenu.java index e9c8e69..52e4ef6 100644 --- a/app/src/main/java/gui/menu/SoloMenu.java +++ b/app/src/main/java/gui/menu/SoloMenu.java @@ -2,12 +2,13 @@ package gui.menu; import imgui.ImGui; import imgui.type.ImInt; +import sudoku.structure.SudokuFactory; 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 static final String[] sudokuTypes = { "Carré", "Rectangle", "Multidoku" }; + private static final int SQUARE = 0, RECTANGLE = 1, MULTIDOKU = 2; private final ImInt sudokuSize = new ImInt(3); @@ -26,7 +27,8 @@ public class SoloMenu extends BaseView { case SQUARE: ImGui.inputInt("Taille", sudokuSize); if (ImGui.button("Résoudre un sudoku")) { - this.stateMachine.pushState(new SudokuView(stateMachine, sudokuSize.get(), sudokuSize.get())); + this.stateMachine.pushState(new SudokuView(stateMachine, + SudokuFactory.createBasicEmptySquareSudoku(sudokuSize.get()))); } break; @@ -34,10 +36,18 @@ public class SoloMenu extends BaseView { 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())); + this.stateMachine.pushState(new SudokuView(stateMachine, + SudokuFactory.createBasicEmptyRectangleSudoku(sudokuWidth.get(), sudokuHeight.get()))); } break; + case MULTIDOKU: + ImGui.inputInt("Taille", sudokuSize); + if (ImGui.button("Résoudre un multidoku")) { + this.stateMachine.pushState(new SudokuView(stateMachine, + SudokuFactory.createBasicSquareMultidoku(sudokuSize.get()))); + } + default: break; } diff --git a/app/src/main/java/gui/menu/SudokuView.java b/app/src/main/java/gui/menu/SudokuView.java index bb02cf9..9f038ea 100644 --- a/app/src/main/java/gui/menu/SudokuView.java +++ b/app/src/main/java/gui/menu/SudokuView.java @@ -1,37 +1,23 @@ package gui.menu; -import java.util.HashMap; -import java.util.List; -import java.util.Map; import java.util.Random; import java.util.concurrent.CancellationException; -import gui.ColorGenerator; -import gui.ColorGenerator.Color; +import gui.SudokuRenderer; import imgui.ImGui; -import imgui.ImVec2; -import imgui.ImVec4; -import imgui.flag.ImGuiCol; -import imgui.flag.ImGuiStyleVar; import sudoku.solver.Solver; -import sudoku.structure.Block; -import sudoku.structure.Cell; import sudoku.structure.MultiDoku; -import sudoku.structure.Sudoku; -import sudoku.structure.SudokuFactory; public class SudokuView extends BaseView { + private final SudokuRenderer sudokuRenderer; + private Thread resolveThread; private final MultiDoku doku; - private int currentIndex = -1; - private final Map colorPalette; - private volatile Thread resolveThread = null; - public SudokuView(StateMachine stateMachine, int width, int height) { + public SudokuView(StateMachine stateMachine, MultiDoku doku) { super(stateMachine); - this.doku = SudokuFactory.createBasicEmptyRectangleSudoku(width, height); - this.colorPalette = new HashMap<>(); - initColors(); + this.doku = doku; + this.sudokuRenderer = new SudokuRenderer(doku); } private void stopResolve() { @@ -41,80 +27,10 @@ public class SudokuView extends BaseView { } } - private void initColors() { - List colors = ColorGenerator.greatPalette(doku.getSubGrid(0).getSize()); - int index = 0; - for (Block block : doku.getSubGrid(0).getBlocks()) { - colorPalette.put(block, colors.get(index)); - index++; - } - } - - private void renderPopup() { - final Sudoku sudoku = doku.getSubGrid(0); - 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))) { - sudoku.setCellSymbol(currentIndex % sudoku.getSize(), currentIndex / sudoku.getSize(), i - 1); - ImGui.closeCurrentPopup(); - } - } - ImGui.endPopup(); - } - } - - @Override - public void render() { - final Sudoku sudoku = doku.getSubGrid(0); - ImGui.pushStyleVar(ImGuiStyleVar.FrameBorderSize, 2.0f); - ImGui.pushStyleVar(ImGuiStyleVar.ItemSpacing, new ImVec2(0.0f, 0.0f)); - ImGui.pushStyleColor(ImGuiCol.Border, new ImVec4(0.0f, 0.0f, 0.0f, 1.0f)); - 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.Button, new ImVec4(blockColor.r, blockColor.g, blockColor.b, 1.0f)); - String cellText = ""; - if (symbol != -1) - cellText += Integer.toString(symbol + 1); - if (ImGui.button(cellText + "##" + index, new ImVec2(50, 50))) { - ImGui.openPopup("editPopup"); - currentIndex = index; - } - ImGui.popStyleVar(); - ImGui.popStyleColor(); - } - } - ImGui.popStyleVar(2); - ImGui.popStyleColor(); - renderPopup(); - if (resolveThread != null) - ImGui.beginDisabled(); - - if (ImGui.button("Résoudre")) { - - resolveThread = new Thread(() -> { - try { - Random rand = new Random(); - doku.getSubGrid(0).clear(); - Solver.solveRandom(doku, rand); - Thread.sleep(200); - } catch (CancellationException | InterruptedException e) { - System.out.println("The user is bored !"); - } - stopResolve(); - }); - } + private void renderCancelButton() { boolean wantsToStop = false; - if (resolveThread != null && resolveThread.isAlive()){ - ImGui.endDisabled(); + if (resolveThread != null && resolveThread.isAlive()) { + // ImGui.endDisabled(); if (ImGui.button("Annuler")) { // we can't stop the Thread right now wantsToStop = true; @@ -125,6 +41,39 @@ public class SudokuView extends BaseView { } if (wantsToStop) stopResolve(); + } + + private void renderSolveButton() { + if (resolveThread != null) + ImGui.beginDisabled(); + + boolean beginSolve = false; + + if (ImGui.button("Résoudre")) { + beginSolve = true; + } + if (resolveThread != null) + ImGui.endDisabled(); + + if (beginSolve) { + resolveThread = new Thread(() -> { + try { + Random rand = new Random(); + Solver.solveRandom(doku, rand); + Thread.sleep(200); + } catch (CancellationException | InterruptedException e) { + System.out.println("The user is bored !"); + } + stopResolve(); + }); + } + } + + @Override + public void render() { + sudokuRenderer.render(); + renderSolveButton(); + renderCancelButton(); renderReturnButton(); }