4 Commits

Author SHA1 Message Date
1e67e7a9d4 fix renderable multidoku max coords
All checks were successful
Linux arm64 / Build (push) Successful in 29s
2025-02-02 00:37:07 +01:00
Melvyn
70eef1646d refactor
All checks were successful
Linux arm64 / Build (push) Successful in 31s
2025-02-02 00:16:27 +01:00
5dfe4382fe feat: render new sudoku types
All checks were successful
Linux arm64 / Build (push) Successful in 27s
2025-02-02 00:10:25 +01:00
Melvyn
059886c2a4 feat : createPlusMMultidoku
All checks were successful
Linux arm64 / Build (push) Successful in 30s
2025-02-02 00:02:47 +01:00
19 changed files with 147 additions and 190 deletions

View File

@@ -2,10 +2,15 @@ package gui;
import gui.constants.Fonts;
import gui.constants.Images;
import gui.constants.Symbols;
import gui.menu.MainMenu;
import gui.menu.StateMachine;
import imgui.app.Application;
import imgui.app.Configuration;
import sudoku.io.SudokuPrinter;
import sudoku.structure.Difficulty;
import sudoku.structure.MultiDoku;
import sudoku.structure.SudokuFactory;
public class Main extends Application {
@@ -31,7 +36,7 @@ public class Main extends Application {
@Override
protected void preRun() {
super.preRun();
Images.reloadImages();
Images.loadImages();
}
@Override
@@ -41,6 +46,13 @@ public class Main extends Application {
}
public static void main(String[] args) {
launch(new Main());
MultiDoku doku = SudokuFactory.createBasicPlusShapedMultidoku(3, 3, SudokuFactory.DEFAULT_CONSTRAINTS);
try {
SudokuFactory.fillDoku(doku, Difficulty.Easy);
} catch (Exception e) {
throw new RuntimeException();
}
SudokuPrinter.printMultiDoku(doku, 3, 3, Symbols.Numbers);
//launch(new Main());
}
}

View File

@@ -90,29 +90,23 @@ public class RenderableMultidoku {
}
private static Coordinate getMaxSudokuCoordinate(Map<Sudoku, Coordinate> sudokusOffset) {
Coordinate maxCoordinate = null;
Sudoku maxSudoku = null;
float maxDistanceSquared = 0;
int maxX = 0;
int maxY = 0;
Sudoku lastSudoku = null;
for (var entry : sudokusOffset.entrySet()) {
Coordinate coordinate = entry.getValue();
float distanceSquared = coordinate.getX() * coordinate.getX() + coordinate.getY() * coordinate.getY();
if (maxCoordinate == null) {
maxCoordinate = coordinate;
maxDistanceSquared = distanceSquared;
maxSudoku = entry.getKey();
if (coordinate.getX() > maxX)
maxX = coordinate.getX();
if (coordinate.getY() > maxY)
maxY = coordinate.getY();
lastSudoku = entry.getKey();
}
if (distanceSquared > maxDistanceSquared) {
maxDistanceSquared = distanceSquared;
maxSudoku = entry.getKey();
maxCoordinate = coordinate;
}
}
Coordinate maxCoordinate = new Coordinate(maxX, maxY);
// tous les sudokus sont censés faire la même taille
int sudokuSize = lastSudoku.getSize();
int blockWidth = maxSudoku.getBlockWidth();
int blockHeight = maxSudoku.getSize() / blockWidth;
return new Coordinate(maxCoordinate.getX() + maxSudoku.getSize(), maxCoordinate.getY() + maxSudoku.getSize());
return new Coordinate(maxCoordinate.getX() + sudokuSize, maxCoordinate.getY() + sudokuSize);
}
public static RenderableMultidoku fromMultidoku(MultiDoku doku) {

View File

@@ -32,8 +32,8 @@ public class Images {
return textureID;
}
public static void reloadImages() {
BACKGROUND = loadTexture(Options.BackgroundPath);
public static void loadImages() {
BACKGROUND = loadTexture("background.png");
}
}

View File

@@ -4,6 +4,5 @@ public class Options {
public static Symbols Symboles = Symbols.Numbers;
public static float BackgroundSpeed = 1.0f;
public static String BackgroundPath = "background.png";
}

View File

@@ -14,10 +14,14 @@ public enum SudokuType {
(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,
MultiDokuXSquare("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));
MultidokuXRectangle("Multidoku rectangle (X)", 2,
(constraints, params) -> SudokuFactory.createBasicXShapedMultidoku(params[0], params[1], constraints)),
MultiDokuPSquare("Multidoku carré (+)", 1,
(constraints, params) -> SudokuFactory.createBasicPlusShapedMultidoku(params[0], constraints)),
MultiDokuPRectangle("Multidoku rectangle (+)", 2,
(constraints, params) -> SudokuFactory.createBasicPlusShapedMultidoku(params[0], params[1], constraints));
String displayName;
SudokuMaker maker;

View File

@@ -14,12 +14,12 @@ public class ConnexionStatusView extends BaseView {
private String displayText = "Connecting ...";
public ConnexionStatusView(StateMachine stateMachine, String pseudo, String address, short port)
public ConnexionStatusView(StateMachine stateMachine, String address, short port)
throws UnknownHostException, IOException {
super(stateMachine);
Thread t = new Thread(() -> {
try {
this.client = new Client(pseudo, address, port);
this.client = new Client(address, port);
bindListeners();
} catch (IOException e) {
e.printStackTrace();
@@ -29,13 +29,12 @@ public class ConnexionStatusView extends BaseView {
t.start();
}
public ConnexionStatusView(StateMachine stateMachine, String pseudo, short port)
throws UnknownHostException, IOException {
public ConnexionStatusView(StateMachine stateMachine, short port) throws UnknownHostException, IOException {
super(stateMachine);
Thread t = new Thread(() -> {
try {
this.server = new Server(port);
this.client = new Client(pseudo, "localhost", port);
this.client = new Client("localhost", port);
bindListeners();
} catch (IOException e) {
e.printStackTrace();

View File

@@ -1,7 +1,6 @@
package gui.menu;
import java.io.IOException;
import java.util.Random;
import imgui.ImGui;
import imgui.ImVec2;
@@ -12,7 +11,6 @@ public class MultiMenu extends BaseView {
private final ImInt port = new ImInt(25565);
private final ImString address = new ImString("localhost");
private final ImString pseudo = new ImString("Joueur" + new Random().nextInt());
public MultiMenu(StateMachine stateMachine) {
super(stateMachine);
@@ -24,10 +22,9 @@ public class MultiMenu extends BaseView {
ImGui.beginChild("##CreateGame", new ImVec2(displaySize.x / 2.0f, displaySize.y * 8.0f / 9.0f));
if (ImGui.inputInt("Port", port))
port.set(Math.clamp(port.get(), 1, 65535));
ImGui.inputText("Pseudo", pseudo);
if (ImGui.button("Créer")) {
try {
this.stateMachine.pushState(new ConnexionStatusView(stateMachine, pseudo.get(), (short) port.get()));
this.stateMachine.pushState(new ConnexionStatusView(stateMachine, (short) port.get()));
} catch (IOException e) {
e.printStackTrace();
}
@@ -41,7 +38,6 @@ public class MultiMenu extends BaseView {
ImGui.inputText("Adresse", address);
if (ImGui.inputInt("Port", port))
port.set(Math.clamp(port.get(), 1, 65535));
ImGui.inputText("Pseudo", pseudo);
if (ImGui.button("Rejoindre")) {
try {
this.stateMachine.pushState(new ConnexionStatusView(stateMachine, address.get(), (short) port.get()));

View File

@@ -78,9 +78,4 @@ public class MultiPlayerView extends BaseView {
renderGameStatus();
}
@Override
public void cleanResources() {
this.selector.clean();
}
}

View File

@@ -1,11 +1,8 @@
package gui.menu;
import gui.constants.Images;
import gui.constants.Options;
import gui.constants.Symbols;
import imgui.ImGui;
import imgui.extension.imguifiledialog.ImGuiFileDialog;
import imgui.extension.imguifiledialog.flag.ImGuiFileDialogFlags;
import imgui.type.ImInt;
public class OptionsMenu extends BaseView {
@@ -17,30 +14,6 @@ public class OptionsMenu extends BaseView {
super(stateMachine);
}
private void renderImageSelectDialog() {
if (ImGuiFileDialog.display("browse-img", ImGuiFileDialogFlags.None)) {
if (ImGuiFileDialog.isOk()) {
var selection = ImGuiFileDialog.getSelection();
for (var entry : selection.entrySet()) {
try {
String filePath = entry.getValue();
Options.BackgroundPath = filePath;
Images.reloadImages();
} catch (Exception e) {
e.printStackTrace();
}
}
}
ImGuiFileDialog.close();
}
}
private void renderImageSelectButton() {
if (ImGui.button("Changer de fond d'écran"))
ImGuiFileDialog.openDialog("browse-img", "Choisissez un fichier", ".png,.jpg,.jpeg", ".");
renderImageSelectDialog();
}
@Override
public void render() {
ImGui.text("Options");
@@ -50,7 +23,6 @@ public class OptionsMenu extends BaseView {
if(ImGui.sliderFloat("Vitesse d'animation de l'arrière plan", backgroundSpeed, 0.0f, 10.0f)){
Options.BackgroundSpeed = backgroundSpeed[0];
}
renderImageSelectButton();
renderReturnButton();
}

View File

@@ -25,9 +25,4 @@ public class SoloMenu extends BaseView {
renderReturnButton();
}
@Override
public void cleanResources() {
this.sudokuSelector.clean();
}
}

View File

@@ -92,7 +92,6 @@ public class SudokuView extends BaseView {
}
private void startSolve(Solver solver) {
this.doku.clearMutableCells();
resolveThread = new Thread(() -> {
List<SolverStep> steps = new ArrayList<>();
try {
@@ -159,8 +158,6 @@ public class SudokuView extends BaseView {
private void renderClearButton() {
if (centeredButton("Effacer")) {
this.doku.clearMutableCells();
this.resolved = false;
this.unresolved = false;
}
}

View File

@@ -9,22 +9,13 @@ public class SmoothProgressBar {
private final float speed = 2.0f;
private final float clipConstant = 0.001f;
private void updateProgress(float newProgress) {
float delta = newProgress - lastProgress;
public void render(String label, ImVec2 size, float progress) {
float delta = progress - lastProgress;
if (Math.abs(delta) < clipConstant)
lastProgress = newProgress;
lastProgress = progress;
else
lastProgress = lastProgress + delta * ImGui.getIO().getDeltaTime() * speed;
}
public void render(String label, ImVec2 size, float progress) {
updateProgress(progress);
ImGui.progressBar(lastProgress, size, label);
}
public void render(float progress) {
updateProgress(progress);
ImGui.progressBar(lastProgress);
}
}

View File

@@ -35,15 +35,10 @@ public class SudokuSelector {
private final String confirmMessage;
private Thread genThread = null;
private final SmoothProgressBar genProgressBar;
public SudokuSelector(boolean canGenEmptyGrid, String confirmMessage) {
this.canGenEmptyGrid = canGenEmptyGrid;
this.confirmMessage = confirmMessage;
initConstraints();
this.genProgressBar = new SmoothProgressBar();
}
private List<IConstraint> getConstraints() {
@@ -61,39 +56,16 @@ public class SudokuSelector {
}
}
private void stopGenThread() {
if (this.genThread != null) {
this.genThread.interrupt();
this.genThread = null;
}
}
private void renderGenProgress() {
if (ImGui.beginPopup("genProgress")) {
ImGui.text("Loading ...");
int filled = this.doku.getFilledCells().size();
int total = this.doku.getCells().size();
this.genProgressBar.render(filled / (float) total);
ImGui.endPopup();
} else {
stopGenThread();
}
}
private void selectSudoku(MultiDoku doku, boolean empty) {
this.doku = doku;
ImGui.openPopup("genProgress");
this.genThread = new Thread(() -> {
if (!empty) {
try {
SudokuFactory.fillDoku(doku, Difficulty.values()[difficulty.get()]);
this.onSelect.emit(this.doku);
} catch (Exception e) {
e.printStackTrace();
}
}
});
this.genThread.start();
this.onSelect.emit(this.doku);
}
public void renderFileDialog() {
@@ -159,12 +131,7 @@ public class SudokuSelector {
if (ImGui.button("À partir d'un fichier")) {
ImGuiFileDialog.openDialog("browse-sudoku", "Choisissez un fichier", ".json", ".");
}
renderGenProgress();
renderFileDialog();
}
public void clean() {
stopGenThread();
}
}

View File

@@ -28,10 +28,12 @@ public class Client {
String disconnectReason = null;
public Client(String pseudo, String address, short port) throws UnknownHostException, IOException {
public Client(String address, short port) throws UnknownHostException, IOException {
this.clientConnection = new ClientConnexion(address, port, this);
this.game = new Game();
login(pseudo);
// temp
Random r = new Random();
login("Player" + r.nextInt());
}
public void login(String pseudo) {

View File

@@ -227,7 +227,7 @@ public class ConsoleInterface {
saveMultiDoku(doku);
break;
case "solution":
solve(doku, listSymbols, width, height);
solve(doku);
break;
case "exit":
exit();
@@ -238,50 +238,19 @@ public class ConsoleInterface {
}
}
private void applyStep(SolverStep step) {
step.getCell().setSymbolIndex(step.getNewValue());
}
private boolean showStep(MultiDoku doku, List<String> listSymbols, int width, int height, SolverStep step) {
System.out.println("Here is the step : \n");
showMultidoku(doku, listSymbols, width, height);
applyStep(step);
System.out.println("\nTurns into :\n");
showMultidoku(doku, listSymbols, width, height);
System.out.println("Do you want to see the next step ? (y/n, default n)");
return reader.next().equals("y");
}
private void showSolveSteps(MultiDoku doku, List<String> listSymbols, int width, int height, List<SolverStep> steps) {
System.out.println("Would you like to see the steps of the solver ? (y/n, default n)");
doku.getStateManager().popState();
switch (reader.next()) {
case "y":
int stepCount = 0;
while(stepCount < steps.size() && showStep(doku, listSymbols, width, height, steps.get(stepCount))){stepCount++;}
break;
default:
break;
}
}
private void solve(MultiDoku doku, List<String> listSymbols, int width, int height){
private void solve(MultiDoku doku){
System.out.println("Pick a solver to use : random ('random', default), human ('human') or mixed solver ('mixed').");
List<SolverStep> steps = new ArrayList<>();
doku.getStateManager().pushState();
switch (reader.next()) {
case "human":
new HumanSolver().solve(doku, steps);
new HumanSolver().solve(doku);
break;
case "mixed":
new MixedSolver().solve(doku, steps);
new MixedSolver().solve(doku);
break;
default:
new RandomSolver().solve(doku, steps);
new RandomSolver().solve(doku);
break;
}
showSolveSteps(doku, listSymbols, width, height, steps);
}
private void play(MultiDoku doku, List<String> listSymbols, int width, int height) {

View File

@@ -2,9 +2,13 @@ package sudoku.solver;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.logging.Level;
import gui.constants.Symbols;
import sudoku.io.SudokuPrinter;
import sudoku.structure.Cell;
import sudoku.structure.MultiDoku;
import sudoku.structure.Sudoku;
public class HumanSolver implements Solver {
@@ -19,6 +23,13 @@ public class HumanSolver implements Solver {
if (Thread.interrupted())
throw new CancellationException("User wants to stop the solver");
Sudoku sudoku = doku.getSubGrid(0);
logger.log(Level.FINE,
'\n' + SudokuPrinter.toStringRectangleSudoku(sudoku,
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getBlockWidth(),
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getSize() / sudoku.getBlockWidth(),
Symbols.Numbers));
if (doku.isSolved()) {
return true;
}

View File

@@ -3,9 +3,13 @@ package sudoku.solver;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CancellationException;
import java.util.logging.Level;
import gui.constants.Symbols;
import sudoku.io.SudokuPrinter;
import sudoku.structure.Cell;
import sudoku.structure.MultiDoku;
import sudoku.structure.Sudoku;
public class MixedSolver implements Solver {
@@ -24,6 +28,14 @@ public class MixedSolver implements Solver {
throw new CancellationException("User wants to stop the solver");
}
Sudoku sudoku = doku.getSubGrid(0);
logger.log(Level.FINE,
'\n' + SudokuPrinter.toStringRectangleSudoku(
sudoku,
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getBlockWidth(),
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getSize() / sudoku.getBlockWidth(),
Symbols.Numbers));
if (doku.isSolved()) {
return true;
}

View File

@@ -3,9 +3,13 @@ package sudoku.solver;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CancellationException;
import java.util.logging.Level;
import gui.constants.Symbols;
import sudoku.io.SudokuPrinter;
import sudoku.structure.Cell;
import sudoku.structure.MultiDoku;
import sudoku.structure.Sudoku;
public class RandomSolver implements Solver {
@@ -24,6 +28,13 @@ public class RandomSolver implements Solver {
if (Thread.interrupted())
throw new CancellationException("User wants to stop the solver");
Sudoku sudoku = doku.getSubGrid(0);
logger.log(Level.FINE,
'\n' + SudokuPrinter.toStringRectangleSudoku(sudoku,
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getBlockWidth(),
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getSize() / sudoku.getBlockWidth(),
Symbols.Numbers));
if (doku.isSolved()) {
return true;
}

View File

@@ -234,24 +234,7 @@ public class SudokuFactory {
public static MultiDoku createBasicXShapedMultidoku(int size, List<IConstraint> constraints) {
assert (size > 1);
/*
* 2 3
* 1
* 4 5
*/
Sudoku sudoku1 = createSquareSudoku(size, constraints);
Sudoku sudoku2 = createSquareSudoku(size, constraints);
Sudoku sudoku3 = createSquareSudoku(size, constraints);
Sudoku sudoku4 = createSquareSudoku(size, constraints);
Sudoku sudoku5 = createSquareSudoku(size, constraints);
linkRectangleSudokus(sudoku1, sudoku2, new Coordinate(1 - size, 1 - size));
linkRectangleSudokus(sudoku1, sudoku3, new Coordinate(size - 1, 1 - size));
linkRectangleSudokus(sudoku1, sudoku4, new Coordinate(1 - size, size - 1));
linkRectangleSudokus(sudoku1, sudoku5, new Coordinate(size - 1, size - 1));
return new MultiDoku(Arrays.asList(sudoku1, sudoku2, sudoku3, sudoku4, sudoku5));
return createBasicXShapedMultidoku(size, size, constraints);
}
/**
@@ -287,6 +270,55 @@ public class SudokuFactory {
return new MultiDoku(Arrays.asList(sudoku1, sudoku2, sudoku3, sudoku4, sudoku5));
}
/**
* TODO
* Créée un MultiDoku de Blocks carrés de taille size composé de cinq Sudokus,
* dont un central qui partage chacun de ses Blockss d'angle avec un autre
* Sudoku.
*
* @param size int, largeur des Blocks unitraires des Sudokus à crééer.
* @return MultiDoku, MultiDoku de forme X.
*/
public static MultiDoku createBasicPlusShapedMultidoku(int size, List<IConstraint> constraints) {
assert (size > 1);
return createBasicPlusShapedMultidoku(size, size, constraints);
}
/**
* TODO
* Créée un MultiDoku de Blocks rectangulaires de forme width par height composé
* de cinq Sudokus,
* dont un central qui partage chacun de ses Blocks d'angle avec un autre
* Sudoku.
*
* @param width int, largeur des Blocks unitraires des Sudokus à crééer.
* @param height int, hauteur des Blocks unitraires des Sudokus à crééer.
* @return MultiDoku, MultiDoku de forme X.
*/
public static MultiDoku createBasicPlusShapedMultidoku(int width, int height, List<IConstraint> constraints) {
assert (width > 1 && height > 1);
/*
* 3
* 2 1 4
* 5
*/
Sudoku sudoku1 = createRectangleSudoku(width, height, constraints);
Sudoku sudoku2 = createRectangleSudoku(width, height, constraints);
Sudoku sudoku3 = createRectangleSudoku(width, height, constraints);
Sudoku sudoku4 = createRectangleSudoku(width, height, constraints);
Sudoku sudoku5 = createRectangleSudoku(width, height, constraints);
linkRectangleSudokus(sudoku1, sudoku2, new Coordinate(1 - height, 0));
linkRectangleSudokus(sudoku1, sudoku3, new Coordinate(0, 1 - width));
linkRectangleSudokus(sudoku1, sudoku4, new Coordinate(height - 1, 0));
linkRectangleSudokus(sudoku1, sudoku5, new Coordinate(0, width - 1));
return new MultiDoku(Arrays.asList(sudoku1, sudoku2, sudoku3, sudoku4, sudoku5));
}
public static void fillDoku(MultiDoku doku, Difficulty difficulty) throws Exception {
Solver solver = new RandomSolver();
solver.solve(doku);
@@ -309,8 +341,7 @@ public class SudokuFactory {
public static MultiDoku createBasicEmptyRandomBlockDoku(int blockSize, List<IConstraint> constraints) {
int blockCellCount = blockSize * blockSize;
List<Cell> cells = initCells(blockCellCount);
List<Cell> homeLessCells = new ArrayList<>();
homeLessCells.addAll(cells);
List<Cell> homeLessCells = new ArrayList<>(cells);
List<Block> blocks = new ArrayList<>();
Random r = new Random();
for (int i = 0; i < blockCellCount; i++) {