From 8596781ce31d934a71fd3064b22bb4b1037c9c91 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Thu, 30 Jan 2025 14:51:21 +0100 Subject: [PATCH] Fixes #14 --- app/src/main/java/gui/SudokuSelector.java | 33 ++++------ app/src/main/java/gui/SudokuType.java | 62 +++++++++++++++++++ .../java/sudoku/structure/SudokuFactory.java | 24 +++++++ 3 files changed, 97 insertions(+), 22 deletions(-) create mode 100644 app/src/main/java/gui/SudokuType.java diff --git a/app/src/main/java/gui/SudokuSelector.java b/app/src/main/java/gui/SudokuSelector.java index 343a47f..0d46f51 100644 --- a/app/src/main/java/gui/SudokuSelector.java +++ b/app/src/main/java/gui/SudokuSelector.java @@ -86,7 +86,6 @@ public class SudokuSelector { } public void render() { - ImGui.combo("Type de Sudoku", sudokuType, sudokuTypes); ImGui.combo("Difficulté", difficulty, Difficulty.getDifficultyNames()); if (ImGui.treeNode("Constraintes")) { for (Constraint cons : Constraint.values()) { @@ -94,46 +93,36 @@ public class SudokuSelector { } ImGui.treePop(); } - switch (sudokuType.get()) { - case SQUARE: + ImGui.combo("Type de Sudoku", sudokuType, SudokuType.getTypeNames()); + SudokuType currentType = SudokuType.values()[sudokuType.get()]; + switch (currentType.getMakerParamCount()) { + case 1: ImGui.inputInt("Taille", sudokuSize); if (ImGui.button("Résoudre un sudoku")) { - selectSudoku(SudokuFactory.createBasicEmptySquareDoku(sudokuSize.get(), getConstraints()), false); + selectSudoku(currentType.createDoku(getConstraints(), sudokuSize.get()), false); } if (canGenEmptyGrid && ImGui.button("Générer une grille vide")) { - selectSudoku(SudokuFactory.createBasicEmptySquareDoku(sudokuSize.get(), getConstraints()), true); + selectSudoku(currentType.createDoku(getConstraints(), sudokuSize.get()), true); } break; - case RECTANGLE: + case 2: ImGui.inputInt("Largeur", sudokuHeight); ImGui.inputInt("Longueur", sudokuWidth); if (ImGui.button("Résoudre un sudoku")) { - selectSudoku( - SudokuFactory.createBasicEmptyRectangleDoku(sudokuWidth.get(), sudokuHeight.get(), - getConstraints()), + selectSudoku(currentType.createDoku(getConstraints(), sudokuWidth.get(), sudokuHeight.get()), false); } if (canGenEmptyGrid && ImGui.button("Générer une grille vide")) { - selectSudoku( - SudokuFactory.createBasicEmptyRectangleDoku(sudokuWidth.get(), sudokuHeight.get(), - getConstraints()), - true); + selectSudoku(currentType.createDoku(getConstraints(), sudokuWidth.get(), sudokuHeight.get()), true); } break; - case MULTIDOKU: - ImGui.inputInt("Taille", sudokuSize); - if (ImGui.button("Résoudre un sudoku")) { - selectSudoku(SudokuFactory.createBasicXShapedMultidoku(sudokuSize.get(), getConstraints()), false); - } - if (canGenEmptyGrid && ImGui.button("Générer une grille vide")) { - selectSudoku(SudokuFactory.createBasicXShapedMultidoku(sudokuSize.get(), getConstraints()), true); - } - default: + assert (false); break; } + if (ImGui.button("À partir d'un fichier")) { ImGuiFileDialog.openDialog("browse-sudoku", "Choisissez un fichier", ".json", "."); } diff --git a/app/src/main/java/gui/SudokuType.java b/app/src/main/java/gui/SudokuType.java new file mode 100644 index 0000000..c8f3d8e --- /dev/null +++ b/app/src/main/java/gui/SudokuType.java @@ -0,0 +1,62 @@ +package gui; + +import java.util.List; + +import sudoku.constraint.Constraint; +import sudoku.structure.MultiDoku; +import sudoku.structure.SudokuFactory;; + +public enum SudokuType { + + Square("Carré", 1, + (constraints, params) -> SudokuFactory.createBasicEmptySquareDoku(params[0], constraints)), + Rectangle("Rectangle", 2, + (constraints, params) -> SudokuFactory.createBasicEmptyRectangleDoku(params[0], params[1], constraints)), + RandomBloc("Blocs aléatoires", 1, + (constraints, params) -> SudokuFactory.createBasicEmptyRandomBlockDoku(params[0], constraints)), + MultiDokuSquare("Multidoku carré (X)", 1, + (constraints, params) -> SudokuFactory.createBasicXShapedMultidoku(params[0], constraints)), + MultidokuRectangle("Multidoku rectangle (X)", 2, + (constraints, params) -> SudokuFactory.createBasicXShapedMultidoku(params[0], params[1], constraints)); + + String displayName; + SudokuMaker maker; + int paramCount; + + private SudokuType(String displayName, int paramCount, SudokuMaker maker) { + this.displayName = displayName; + this.maker = maker; + this.paramCount = paramCount; + } + + public String getDisplayName() { + return this.displayName; + } + + public MultiDoku createDoku(List constraints, int... params) { + return maker.makeSudoku(constraints, params); + } + + public int getMakerParamCount() { + return this.paramCount; + } + + private static interface SudokuMaker { + MultiDoku makeSudoku(List constraints, int... params); + } + + private static final String[] dokuNames; + + static { + SudokuType[] types = SudokuType.values(); + dokuNames = new String[types.length]; + for (int i = 0; i < types.length; i++) { + dokuNames[i] = types[i].getDisplayName(); + } + } + + public static String[] getTypeNames() { + return dokuNames; + } + +} diff --git a/app/src/main/java/sudoku/structure/SudokuFactory.java b/app/src/main/java/sudoku/structure/SudokuFactory.java index 4b64402..4d970d1 100644 --- a/app/src/main/java/sudoku/structure/SudokuFactory.java +++ b/app/src/main/java/sudoku/structure/SudokuFactory.java @@ -291,4 +291,28 @@ public class SudokuFactory { return null; } } + + public static MultiDoku createBasicEmptyRandomBlockDoku(int blockSize, List constraints) { + int blockCellCount = blockSize * blockSize; + List cells = initCells(blockCellCount); + List homeLessCells = new ArrayList<>(); + homeLessCells.addAll(cells); + List blocks = new ArrayList<>(); + Random r = new Random(); + for (int i = 0 ; i < blockCellCount; i++) { + Block b = new Block(); + for (int j = 0; j < blockCellCount; j++) { + int cellIndex = r.nextInt(homeLessCells.size()); + Cell cell = homeLessCells.remove(cellIndex); + b.addCell(cell); + cell.setBlock(b); + } + blocks.add(b); + } + Sudoku sudoku = new Sudoku(cells, blocks, constraints); + for (Block block : blocks) { + block.getSudokus().add(sudoku); + } + return new MultiDoku(Arrays.asList(sudoku)); + } }