3 Commits

Author SHA1 Message Date
a616ab63e4 gui: save sudoku
All checks were successful
Linux arm64 / Build (push) Successful in 42s
2025-01-29 15:08:30 +01:00
00866256a7 gui: revert suoku scrolling 2025-01-29 15:07:34 +01:00
ed9b636b58 tests: make it faster 2025-01-29 14:59:06 +01:00
4 changed files with 27 additions and 19 deletions

View File

@@ -77,7 +77,7 @@ public class SudokuRenderer {
if (offsetX > 0) {
ImGui.setCursorPosX(offsetX);
}
ImGui.beginChild(1, new ImVec2(cellSize.x * doku.getWidth(), cellSize.y * doku.getHeight()), ImGuiWindowFlags.HorizontalScrollbar);
ImGui.beginChild(1, new ImVec2(cellSize.x * doku.getWidth(), cellSize.y * doku.getHeight()));
ImGui.pushStyleVar(ImGuiStyleVar.FrameBorderSize, 2.0f);
ImGui.pushStyleVar(ImGuiStyleVar.ItemSpacing, new ImVec2(0.0f, 0.0f));

View File

@@ -6,6 +6,7 @@ import java.util.concurrent.CancellationException;
import gui.SudokuRenderer;
import imgui.ImGui;
import imgui.ImGuiStyle;
import sudoku.io.SudokuSerializer;
import sudoku.solver.Solver;
import sudoku.structure.MultiDoku;
@@ -14,6 +15,7 @@ public class SudokuView extends BaseView {
private final SudokuRenderer sudokuRenderer;
private Thread resolveThread;
private final MultiDoku doku;
private String lastSavePath = null;
private boolean resolved = false;
@@ -94,10 +96,23 @@ public class SudokuView extends BaseView {
}
}
private void renderSaveButton() {
if (ImGui.button("Sauvegarder l'état de la grille")) {
lastSavePath = SudokuSerializer.saveMultiDoku(doku);
ImGui.openPopup("saveDone");
}
if (ImGui.beginPopup("saveDone")) {
ImGui.text("Sudoku sauvegardé dans ");
ImGui.text(lastSavePath);
ImGui.endPopup();
}
}
@Override
public void render() {
sudokuRenderer.render();
renderSolveButton();
renderSaveButton();
renderCancelButton();
renderReturnButton();
}

View File

@@ -106,9 +106,9 @@ public class SudokuSerializer {
/**
* Save a serialized MultiDoku in a JSON file.
* @param doku MultiDoku, MultiDoku to save.
* @return int, number of the save.
* @return String, the path of the save.
*/
public static int saveMultiDoku(final MultiDoku doku) {
public static String saveMultiDoku(final MultiDoku doku) {
JSONObject jsonRoot = serializeSudoku(doku);
@@ -123,9 +123,9 @@ public class SudokuSerializer {
try (FileWriter file = new FileWriter(f)) {
file.write(jsonRoot.toString(3));
} catch (IOException e) {
e.fillInStackTrace();
e.printStackTrace();
}
return i;
return f.getAbsolutePath();
}
/**

View File

@@ -1,18 +1,14 @@
package sudoku;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Random;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import sudoku.io.SudokuPrinter;
import sudoku.io.SudokuSerializer;
import sudoku.structure.MultiDoku;
import sudoku.structure.SudokuFactory;
import java.util.Random;
public class SudokuSerializerTest {
void testSerializeWithSize(int blockWidth, int blockHeight) {
@@ -24,10 +20,10 @@ public class SudokuSerializerTest {
void testSaveWithSize(int blockWidth, int blockHeight) {
MultiDoku doku = SudokuFactory.createBasicEmptyRectangleSudoku(blockWidth, blockHeight);
int saveNumber = SudokuSerializer.saveMultiDoku(doku);
String savePath = SudokuSerializer.saveMultiDoku(doku);
MultiDoku otherDoku = null;
try {
otherDoku = SudokuSerializer.getSavedMultiDoku(saveNumber);
otherDoku = SudokuFactory.fromfile(savePath);
assert (otherDoku != null);
} catch (Exception e) {
e.printStackTrace();
@@ -41,18 +37,15 @@ public class SudokuSerializerTest {
Random r = new Random();
int testCount = 5;
for (int i = 0; i < testCount; i++) {
int blockWidth = r.nextInt(20) + 1;
int blockHeight = r.nextInt(20) + 1;
int blockWidth = r.nextInt(10) + 1;
int blockHeight = r.nextInt(10) + 1;
testSerializeWithSize(blockWidth, blockHeight);
}
for (int i = 0; i < testCount; i++) {
int blockWidth = r.nextInt(20) + 1;
int blockHeight = r.nextInt(20) + 1;
int blockWidth = r.nextInt(10) + 1;
int blockHeight = r.nextInt(10) + 1;
testSaveWithSize(blockWidth, blockHeight);
}
}
}