add multidoku to solo
Some checks failed
Linux arm64 / Build (push) Has been cancelled

This commit is contained in:
2025-01-28 09:56:44 +01:00
parent 0174ec08d7
commit b1ae79a2e8
2 changed files with 56 additions and 97 deletions

View File

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

View File

@@ -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<Block, Color> 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<Color> 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();
}