Fixes #14
All checks were successful
Linux arm64 / Build (push) Successful in 40s

This commit is contained in:
2025-01-30 14:51:21 +01:00
parent d6c3504bc7
commit 8596781ce3
3 changed files with 97 additions and 22 deletions

View File

@@ -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", ".");
}

View File

@@ -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<Constraint> constraints, int... params) {
return maker.makeSudoku(constraints, params);
}
public int getMakerParamCount() {
return this.paramCount;
}
private static interface SudokuMaker {
MultiDoku makeSudoku(List<Constraint> 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;
}
}

View File

@@ -291,4 +291,28 @@ public class SudokuFactory {
return null;
}
}
public static MultiDoku createBasicEmptyRandomBlockDoku(int blockSize, List<Constraint> constraints) {
int blockCellCount = blockSize * blockSize;
List<Cell> cells = initCells(blockCellCount);
List<Cell> homeLessCells = new ArrayList<>();
homeLessCells.addAll(cells);
List<Block> 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));
}
}