canellable stupid solver
All checks were successful
Linux arm64 / Build (push) Successful in 24m8s

This commit is contained in:
2025-01-24 21:41:12 +01:00
parent 00941edb19
commit 4b4ed76eb8
4 changed files with 142 additions and 10 deletions

View File

@@ -3,6 +3,7 @@ package gui.menu;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CancellationException;
import gui.ColorGenerator;
import gui.ColorGenerator.Color;
@@ -11,7 +12,7 @@ import imgui.ImVec2;
import imgui.ImVec4;
import imgui.flag.ImGuiCol;
import imgui.flag.ImGuiStyleVar;
import sudoku.solver.Solver;
import sudoku.solver.StupidSolver;
import sudoku.structure.Block;
import sudoku.structure.Cell;
import sudoku.structure.MultiDoku;
@@ -23,6 +24,7 @@ public class SudokuView extends BaseView {
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) {
super(stateMachine);
@@ -31,6 +33,13 @@ public class SudokuView extends BaseView {
initColors();
}
private void stopResolve() {
if (resolveThread != null && resolveThread.isAlive()) {
resolveThread.interrupt();
resolveThread = null;
}
}
private void initColors() {
List<Color> colors = ColorGenerator.greatPalette(doku.getSubGrid(0).getSize());
int index = 0;
@@ -80,10 +89,44 @@ public class SudokuView extends BaseView {
}
}
renderPopup();
if (resolveThread != null)
ImGui.beginDisabled();
if (ImGui.button("Résoudre")) {
Solver.solve(doku);
resolveThread = new Thread(new Runnable() {
@Override
public void run() {
try {
doku.getSubGrid(0).clear();
StupidSolver.solve(doku);
Thread.sleep(200);
} catch (CancellationException | InterruptedException e) {
System.out.println("The user is bored !");
}
stopResolve();
}
});
}
boolean wantsToStop = false;
if (resolveThread != null && resolveThread.isAlive()){
ImGui.endDisabled();
if (ImGui.button("Annuler")) {
// we can't stop the Thread right now
wantsToStop = true;
}
}
if (resolveThread != null && !resolveThread.isAlive()) {
resolveThread.start();
}
if (wantsToStop)
stopResolve();
renderReturnButton();
}
@Override
public void closeMenu() {
super.closeMenu();
stopResolve();
}
}