Compare commits
4 Commits
f3bbfd9e6c
...
createPlus
| Author | SHA1 | Date | |
|---|---|---|---|
| 1e67e7a9d4 | |||
|
|
70eef1646d | ||
| 5dfe4382fe | |||
|
|
059886c2a4 |
@@ -2,10 +2,15 @@ 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 {
|
||||||
|
|
||||||
@@ -41,6 +46,13 @@ public class Main extends Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -90,29 +90,23 @@ public class RenderableMultidoku {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static Coordinate getMaxSudokuCoordinate(Map<Sudoku, Coordinate> sudokusOffset) {
|
private static Coordinate getMaxSudokuCoordinate(Map<Sudoku, Coordinate> sudokusOffset) {
|
||||||
Coordinate maxCoordinate = null;
|
int maxX = 0;
|
||||||
Sudoku maxSudoku = null;
|
int maxY = 0;
|
||||||
float maxDistanceSquared = 0;
|
Sudoku lastSudoku = null;
|
||||||
for (var entry : sudokusOffset.entrySet()) {
|
for (var entry : sudokusOffset.entrySet()) {
|
||||||
Coordinate coordinate = entry.getValue();
|
Coordinate coordinate = entry.getValue();
|
||||||
float distanceSquared = coordinate.getX() * coordinate.getX() + coordinate.getY() * coordinate.getY();
|
if (coordinate.getX() > maxX)
|
||||||
if (maxCoordinate == null) {
|
maxX = coordinate.getX();
|
||||||
maxCoordinate = coordinate;
|
if (coordinate.getY() > maxY)
|
||||||
maxDistanceSquared = distanceSquared;
|
maxY = coordinate.getY();
|
||||||
maxSudoku = entry.getKey();
|
lastSudoku = entry.getKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (distanceSquared > maxDistanceSquared) {
|
Coordinate maxCoordinate = new Coordinate(maxX, maxY);
|
||||||
maxDistanceSquared = distanceSquared;
|
// tous les sudokus sont censés faire la même taille
|
||||||
maxSudoku = entry.getKey();
|
int sudokuSize = lastSudoku.getSize();
|
||||||
maxCoordinate = coordinate;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int blockWidth = maxSudoku.getBlockWidth();
|
return new Coordinate(maxCoordinate.getX() + sudokuSize, maxCoordinate.getY() + sudokuSize);
|
||||||
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,10 +14,14 @@ 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)),
|
||||||
MultiDokuSquare("Multidoku carré (X)", 1,
|
MultiDokuXSquare("Multidoku carré (X)", 1,
|
||||||
(constraints, params) -> SudokuFactory.createBasicXShapedMultidoku(params[0], constraints)),
|
(constraints, params) -> SudokuFactory.createBasicXShapedMultidoku(params[0], constraints)),
|
||||||
MultidokuRectangle("Multidoku rectangle (X)", 2,
|
MultidokuXRectangle("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;
|
||||||
|
|||||||
@@ -227,7 +227,7 @@ public class ConsoleInterface {
|
|||||||
saveMultiDoku(doku);
|
saveMultiDoku(doku);
|
||||||
break;
|
break;
|
||||||
case "solution":
|
case "solution":
|
||||||
solve(doku, listSymbols, width, height);
|
solve(doku);
|
||||||
break;
|
break;
|
||||||
case "exit":
|
case "exit":
|
||||||
exit();
|
exit();
|
||||||
@@ -238,50 +238,19 @@ public class ConsoleInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void applyStep(SolverStep step) {
|
private void solve(MultiDoku doku){
|
||||||
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){
|
|
||||||
System.out.println("Pick a solver to use : random ('random', default), human ('human') or mixed solver ('mixed').");
|
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()) {
|
switch (reader.next()) {
|
||||||
case "human":
|
case "human":
|
||||||
new HumanSolver().solve(doku, steps);
|
new HumanSolver().solve(doku);
|
||||||
break;
|
break;
|
||||||
case "mixed":
|
case "mixed":
|
||||||
new MixedSolver().solve(doku, steps);
|
new MixedSolver().solve(doku);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
new RandomSolver().solve(doku, steps);
|
new RandomSolver().solve(doku);
|
||||||
break;
|
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) {
|
||||||
|
|||||||
@@ -2,9 +2,13 @@ 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 {
|
||||||
|
|
||||||
@@ -19,6 +23,13 @@ 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,9 +3,13 @@ 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 {
|
||||||
|
|
||||||
@@ -24,6 +28,14 @@ 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,9 +3,13 @@ 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 {
|
||||||
|
|
||||||
@@ -24,6 +28,13 @@ 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,24 +234,7 @@ 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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -287,6 +270,55 @@ 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);
|
||||||
@@ -309,8 +341,7 @@ 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<>();
|
List<Cell> homeLessCells = new ArrayList<>(cells);
|
||||||
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