Compare commits
3 Commits
createPlus
...
4ee29d8f50
| Author | SHA1 | Date | |
|---|---|---|---|
| 4ee29d8f50 | |||
| eda2a1afae | |||
| f3bbfd9e6c |
@@ -2,15 +2,10 @@ package gui;
|
|||||||
|
|
||||||
import gui.constants.Fonts;
|
import gui.constants.Fonts;
|
||||||
import gui.constants.Images;
|
import gui.constants.Images;
|
||||||
import gui.constants.Symbols;
|
|
||||||
import gui.menu.MainMenu;
|
import gui.menu.MainMenu;
|
||||||
import gui.menu.StateMachine;
|
import gui.menu.StateMachine;
|
||||||
import imgui.app.Application;
|
import imgui.app.Application;
|
||||||
import imgui.app.Configuration;
|
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 {
|
public class Main extends Application {
|
||||||
|
|
||||||
@@ -46,13 +41,6 @@ public class Main extends Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
MultiDoku doku = SudokuFactory.createBasicPlusShapedMultidoku(3, 3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
launch(new Main());
|
||||||
try {
|
|
||||||
SudokuFactory.fillDoku(doku, Difficulty.Easy);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException();
|
|
||||||
}
|
|
||||||
SudokuPrinter.printMultiDoku(doku, 3, 3, Symbols.Numbers);
|
|
||||||
//launch(new Main());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -90,23 +90,29 @@ public class RenderableMultidoku {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static Coordinate getMaxSudokuCoordinate(Map<Sudoku, Coordinate> sudokusOffset) {
|
private static Coordinate getMaxSudokuCoordinate(Map<Sudoku, Coordinate> sudokusOffset) {
|
||||||
int maxX = 0;
|
Coordinate maxCoordinate = null;
|
||||||
int maxY = 0;
|
Sudoku maxSudoku = null;
|
||||||
Sudoku lastSudoku = null;
|
float maxDistanceSquared = 0;
|
||||||
for (var entry : sudokusOffset.entrySet()) {
|
for (var entry : sudokusOffset.entrySet()) {
|
||||||
Coordinate coordinate = entry.getValue();
|
Coordinate coordinate = entry.getValue();
|
||||||
if (coordinate.getX() > maxX)
|
float distanceSquared = coordinate.getX() * coordinate.getX() + coordinate.getY() * coordinate.getY();
|
||||||
maxX = coordinate.getX();
|
if (maxCoordinate == null) {
|
||||||
if (coordinate.getY() > maxY)
|
maxCoordinate = coordinate;
|
||||||
maxY = coordinate.getY();
|
maxDistanceSquared = distanceSquared;
|
||||||
lastSudoku = entry.getKey();
|
maxSudoku = entry.getKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
Coordinate maxCoordinate = new Coordinate(maxX, maxY);
|
if (distanceSquared > maxDistanceSquared) {
|
||||||
// tous les sudokus sont censés faire la même taille
|
maxDistanceSquared = distanceSquared;
|
||||||
int sudokuSize = lastSudoku.getSize();
|
maxSudoku = entry.getKey();
|
||||||
|
maxCoordinate = coordinate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return new Coordinate(maxCoordinate.getX() + sudokuSize, maxCoordinate.getY() + sudokuSize);
|
int blockWidth = maxSudoku.getBlockWidth();
|
||||||
|
int blockHeight = maxSudoku.getSize() / blockWidth;
|
||||||
|
|
||||||
|
return new Coordinate(maxCoordinate.getX() + maxSudoku.getSize(), maxCoordinate.getY() + maxSudoku.getSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static RenderableMultidoku fromMultidoku(MultiDoku doku) {
|
public static RenderableMultidoku fromMultidoku(MultiDoku doku) {
|
||||||
|
|||||||
@@ -14,14 +14,10 @@ public enum SudokuType {
|
|||||||
(constraints, params) -> SudokuFactory.createBasicEmptyRectangleDoku(params[0], params[1], constraints)),
|
(constraints, params) -> SudokuFactory.createBasicEmptyRectangleDoku(params[0], params[1], constraints)),
|
||||||
RandomBloc("Blocs aléatoires", 1,
|
RandomBloc("Blocs aléatoires", 1,
|
||||||
(constraints, params) -> SudokuFactory.createBasicEmptyRandomBlockDoku(params[0], constraints)),
|
(constraints, params) -> SudokuFactory.createBasicEmptyRandomBlockDoku(params[0], constraints)),
|
||||||
MultiDokuXSquare("Multidoku carré (X)", 1,
|
MultiDokuSquare("Multidoku carré (X)", 1,
|
||||||
(constraints, params) -> SudokuFactory.createBasicXShapedMultidoku(params[0], constraints)),
|
(constraints, params) -> SudokuFactory.createBasicXShapedMultidoku(params[0], constraints)),
|
||||||
MultidokuXRectangle("Multidoku rectangle (X)", 2,
|
MultidokuRectangle("Multidoku rectangle (X)", 2,
|
||||||
(constraints, params) -> SudokuFactory.createBasicXShapedMultidoku(params[0], params[1], constraints)),
|
(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;
|
String displayName;
|
||||||
SudokuMaker maker;
|
SudokuMaker maker;
|
||||||
|
|||||||
@@ -92,6 +92,7 @@ public class SudokuView extends BaseView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void startSolve(Solver solver) {
|
private void startSolve(Solver solver) {
|
||||||
|
this.doku.clearMutableCells();
|
||||||
resolveThread = new Thread(() -> {
|
resolveThread = new Thread(() -> {
|
||||||
List<SolverStep> steps = new ArrayList<>();
|
List<SolverStep> steps = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
@@ -158,6 +159,8 @@ public class SudokuView extends BaseView {
|
|||||||
private void renderClearButton() {
|
private void renderClearButton() {
|
||||||
if (centeredButton("Effacer")) {
|
if (centeredButton("Effacer")) {
|
||||||
this.doku.clearMutableCells();
|
this.doku.clearMutableCells();
|
||||||
|
this.resolved = false;
|
||||||
|
this.unresolved = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -227,7 +227,7 @@ public class ConsoleInterface {
|
|||||||
saveMultiDoku(doku);
|
saveMultiDoku(doku);
|
||||||
break;
|
break;
|
||||||
case "solution":
|
case "solution":
|
||||||
solve(doku);
|
solve(doku, listSymbols, width, height);
|
||||||
break;
|
break;
|
||||||
case "exit":
|
case "exit":
|
||||||
exit();
|
exit();
|
||||||
@@ -238,21 +238,52 @@ public class ConsoleInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void solve(MultiDoku doku){
|
private void applyStep(SolverStep step) {
|
||||||
System.out.println("Pick a solver to use : random ('random', default), human ('human') or mixed solver ('mixed').");
|
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()) {
|
switch (reader.next()) {
|
||||||
case "human":
|
case "y":
|
||||||
new HumanSolver().solve(doku);
|
int stepCount = 0;
|
||||||
break;
|
while(stepCount < steps.size() && showStep(doku, listSymbols, width, height, steps.get(stepCount))){stepCount++;}
|
||||||
case "mixed":
|
|
||||||
new MixedSolver().solve(doku);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
new RandomSolver().solve(doku);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void solve(MultiDoku doku, List<String> listSymbols, int width, int height){
|
||||||
|
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);
|
||||||
|
break;
|
||||||
|
case "mixed":
|
||||||
|
new MixedSolver().solve(doku, steps);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
new RandomSolver().solve(doku, steps);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
showSolveSteps(doku, listSymbols, width, height, steps);
|
||||||
|
}
|
||||||
|
|
||||||
private void play(MultiDoku doku, List<String> listSymbols, int width, int height) {
|
private void play(MultiDoku doku, List<String> listSymbols, int width, int height) {
|
||||||
int x, y;
|
int x, y;
|
||||||
RenderableMultidoku rdoku = RenderableMultidoku.fromMultidoku(doku);
|
RenderableMultidoku rdoku = RenderableMultidoku.fromMultidoku(doku);
|
||||||
|
|||||||
@@ -2,13 +2,9 @@ package sudoku.solver;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CancellationException;
|
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.Cell;
|
||||||
import sudoku.structure.MultiDoku;
|
import sudoku.structure.MultiDoku;
|
||||||
import sudoku.structure.Sudoku;
|
|
||||||
|
|
||||||
public class HumanSolver implements Solver {
|
public class HumanSolver implements Solver {
|
||||||
|
|
||||||
@@ -23,13 +19,6 @@ public class HumanSolver implements Solver {
|
|||||||
if (Thread.interrupted())
|
if (Thread.interrupted())
|
||||||
throw new CancellationException("User wants to stop the 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()) {
|
if (doku.isSolved()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,13 +3,9 @@ package sudoku.solver;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.CancellationException;
|
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.Cell;
|
||||||
import sudoku.structure.MultiDoku;
|
import sudoku.structure.MultiDoku;
|
||||||
import sudoku.structure.Sudoku;
|
|
||||||
|
|
||||||
public class MixedSolver implements Solver {
|
public class MixedSolver implements Solver {
|
||||||
|
|
||||||
@@ -28,14 +24,6 @@ public class MixedSolver implements Solver {
|
|||||||
throw new CancellationException("User wants to stop the 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()) {
|
if (doku.isSolved()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,13 +3,9 @@ package sudoku.solver;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.CancellationException;
|
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.Cell;
|
||||||
import sudoku.structure.MultiDoku;
|
import sudoku.structure.MultiDoku;
|
||||||
import sudoku.structure.Sudoku;
|
|
||||||
|
|
||||||
public class RandomSolver implements Solver {
|
public class RandomSolver implements Solver {
|
||||||
|
|
||||||
@@ -28,13 +24,6 @@ public class RandomSolver implements Solver {
|
|||||||
if (Thread.interrupted())
|
if (Thread.interrupted())
|
||||||
throw new CancellationException("User wants to stop the 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()) {
|
if (doku.isSolved()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -234,7 +234,24 @@ public class SudokuFactory {
|
|||||||
public static MultiDoku createBasicXShapedMultidoku(int size, List<IConstraint> constraints) {
|
public static MultiDoku createBasicXShapedMultidoku(int size, List<IConstraint> constraints) {
|
||||||
assert (size > 1);
|
assert (size > 1);
|
||||||
|
|
||||||
return createBasicXShapedMultidoku(size, size, constraints);
|
/*
|
||||||
|
* 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));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -270,55 +287,6 @@ public class SudokuFactory {
|
|||||||
return new MultiDoku(Arrays.asList(sudoku1, sudoku2, sudoku3, sudoku4, sudoku5));
|
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 {
|
public static void fillDoku(MultiDoku doku, Difficulty difficulty) throws Exception {
|
||||||
Solver solver = new RandomSolver();
|
Solver solver = new RandomSolver();
|
||||||
solver.solve(doku);
|
solver.solve(doku);
|
||||||
@@ -341,7 +309,8 @@ public class SudokuFactory {
|
|||||||
public static MultiDoku createBasicEmptyRandomBlockDoku(int blockSize, List<IConstraint> constraints) {
|
public static MultiDoku createBasicEmptyRandomBlockDoku(int blockSize, List<IConstraint> constraints) {
|
||||||
int blockCellCount = blockSize * blockSize;
|
int blockCellCount = blockSize * blockSize;
|
||||||
List<Cell> cells = initCells(blockCellCount);
|
List<Cell> cells = initCells(blockCellCount);
|
||||||
List<Cell> homeLessCells = new ArrayList<>(cells);
|
List<Cell> homeLessCells = new ArrayList<>();
|
||||||
|
homeLessCells.addAll(cells);
|
||||||
List<Block> blocks = new ArrayList<>();
|
List<Block> blocks = new ArrayList<>();
|
||||||
Random r = new Random();
|
Random r = new Random();
|
||||||
for (int i = 0; i < blockCellCount; i++) {
|
for (int i = 0; i < blockCellCount; i++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user