This commit is contained in:
@@ -2,7 +2,7 @@ package gui;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import sudoku.constraint.Constraint;
|
import sudoku.constraint.IConstraint;
|
||||||
import sudoku.structure.MultiDoku;
|
import sudoku.structure.MultiDoku;
|
||||||
import sudoku.structure.SudokuFactory;;
|
import sudoku.structure.SudokuFactory;;
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ public enum SudokuType {
|
|||||||
return this.displayName;
|
return this.displayName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MultiDoku createDoku(List<Constraint> constraints, int... params) {
|
public MultiDoku createDoku(List<IConstraint> constraints, int... params) {
|
||||||
return maker.makeSudoku(constraints, params);
|
return maker.makeSudoku(constraints, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ public enum SudokuType {
|
|||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
private static interface SudokuMaker {
|
private static interface SudokuMaker {
|
||||||
MultiDoku makeSudoku(List<Constraint> constraints, int... params);
|
MultiDoku makeSudoku(List<IConstraint> constraints, int... params);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final String[] dokuNames;
|
private static final String[] dokuNames;
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import imgui.extension.imguifiledialog.flag.ImGuiFileDialogFlags;
|
|||||||
import imgui.type.ImBoolean;
|
import imgui.type.ImBoolean;
|
||||||
import imgui.type.ImInt;
|
import imgui.type.ImInt;
|
||||||
import sudoku.constraint.Constraint;
|
import sudoku.constraint.Constraint;
|
||||||
|
import sudoku.constraint.IConstraint;
|
||||||
import sudoku.structure.Difficulty;
|
import sudoku.structure.Difficulty;
|
||||||
import sudoku.structure.MultiDoku;
|
import sudoku.structure.MultiDoku;
|
||||||
import sudoku.structure.SudokuFactory;
|
import sudoku.structure.SudokuFactory;
|
||||||
@@ -40,18 +41,18 @@ public class SudokuSelector {
|
|||||||
initConstraints();
|
initConstraints();
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Constraint> getConstraints() {
|
private List<IConstraint> getConstraints() {
|
||||||
List<Constraint> constraints = new ArrayList<>();
|
List<IConstraint> constraints = new ArrayList<>();
|
||||||
for (int i = 0; i < this.contraints.size(); i++) {
|
for (int i = 0; i < this.contraints.size(); i++) {
|
||||||
if (this.contraints.get(i).get())
|
if (this.contraints.get(i).get())
|
||||||
constraints.add(Constraint.values()[i]);
|
constraints.add(Constraint.values()[i].getConstraint());
|
||||||
}
|
}
|
||||||
return constraints;
|
return constraints;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initConstraints() {
|
private void initConstraints() {
|
||||||
for (Constraint cons : Constraint.values()) {
|
for (Constraint cons : Constraint.values()) {
|
||||||
contraints.add(new ImBoolean(SudokuFactory.DEFAULT_CONSTRAINTS.contains(cons)));
|
contraints.add(new ImBoolean(SudokuFactory.DEFAULT_CONSTRAINTS.contains(cons.getConstraint())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
package sudoku.constraint;
|
package sudoku.constraint;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import sudoku.structure.Sudoku;
|
import sudoku.structure.Sudoku;
|
||||||
|
|
||||||
public interface IConstraint {
|
public interface IConstraint extends Serializable {
|
||||||
|
|
||||||
boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex);
|
boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex);
|
||||||
|
|
||||||
default List<Integer> getPossibleSymbols(final Sudoku s, int x, int y) {
|
default List<Integer> getPossibleSymbols(final Sudoku s, int x, int y) {
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ public class ConsoleInterface {
|
|||||||
System.out.println("Your sudoku will have blocks of a " + width + " x " + height + " format.");
|
System.out.println("Your sudoku will have blocks of a " + width + " x " + height + " format.");
|
||||||
int numberOfSymbols = width * height;
|
int numberOfSymbols = width * height;
|
||||||
List<String> listSymbols = pickSymbols(numberOfSymbols);
|
List<String> listSymbols = pickSymbols(numberOfSymbols);
|
||||||
List<Constraint> listConstraints = getListConstraints();
|
List<IConstraint> listConstraints = getListConstraints();
|
||||||
System.out.println("Now that we have the size of our sudoku, " +
|
System.out.println("Now that we have the size of our sudoku, " +
|
||||||
"would you rather have a single grid ('one', default), " +
|
"would you rather have a single grid ('one', default), " +
|
||||||
"or a a multidoku composed of 5 subgrids ('multi') ?");
|
"or a a multidoku composed of 5 subgrids ('multi') ?");
|
||||||
@@ -171,12 +171,12 @@ public class ConsoleInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Constraint> getListConstraints() {
|
private List<IConstraint> getListConstraints() {
|
||||||
List<Constraint> listConstraints = SudokuFactory.DEFAULT_CONSTRAINTS;
|
List<IConstraint> listConstraints = SudokuFactory.DEFAULT_CONSTRAINTS;
|
||||||
System.out.println(
|
System.out.println(
|
||||||
"The sudoku have constraints of blocks, lines and columns. Would you like to add the diagonal constraints ? (y/n, default 'no')");
|
"The sudoku have constraints of blocks, lines and columns. Would you like to add the diagonal constraints ? (y/n, default 'no')");
|
||||||
if (reader.next().equalsIgnoreCase("y")) {
|
if (reader.next().equalsIgnoreCase("y")) {
|
||||||
listConstraints.add(Constraint.Diagonal);
|
listConstraints.add(Constraint.Diagonal.getConstraint());
|
||||||
}
|
}
|
||||||
return listConstraints;
|
return listConstraints;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import org.json.JSONArray;
|
|||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import sudoku.constraint.Constraint;
|
import sudoku.constraint.Constraint;
|
||||||
|
import sudoku.constraint.IConstraint;
|
||||||
import sudoku.structure.Block;
|
import sudoku.structure.Block;
|
||||||
import sudoku.structure.Cell;
|
import sudoku.structure.Cell;
|
||||||
import sudoku.structure.MultiDoku;
|
import sudoku.structure.MultiDoku;
|
||||||
@@ -44,7 +45,7 @@ public class SudokuSerializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int blockID = blockIds.indexOf(block);
|
int blockID = blockIds.indexOf(block);
|
||||||
assert(blockID >= 0);
|
assert (blockID >= 0);
|
||||||
int symbolIndex = cell.getSymbolIndex();
|
int symbolIndex = cell.getSymbolIndex();
|
||||||
|
|
||||||
JSONObject cellJsonObject = new JSONObject();
|
JSONObject cellJsonObject = new JSONObject();
|
||||||
@@ -99,8 +100,17 @@ public class SudokuSerializer {
|
|||||||
|
|
||||||
// serialize constraints
|
// serialize constraints
|
||||||
|
|
||||||
for (Constraint cons : sudoku.getConstraints()) {
|
for (IConstraint cons : sudoku.getConstraints()) {
|
||||||
constraintsJsonArray.put(cons.ordinal());
|
boolean constraintSerialized = false;
|
||||||
|
for (Constraint enumCons : Constraint.values()) {
|
||||||
|
if (cons.getClass().isAssignableFrom(enumCons.getConstraint().getClass())) {
|
||||||
|
constraintSerialized = true;
|
||||||
|
constraintsJsonArray.put(enumCons.ordinal());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!constraintSerialized) {
|
||||||
|
System.out.println("La contrainte " + cons.getClass() + " n'a pas pu être sérialisé !");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonSudoku.put("constraints", constraintsJsonArray);
|
jsonSudoku.put("constraints", constraintsJsonArray);
|
||||||
@@ -125,6 +135,7 @@ public class SudokuSerializer {
|
|||||||
*/
|
*/
|
||||||
public static String saveMultiDoku(final MultiDoku doku) {
|
public static String saveMultiDoku(final MultiDoku doku) {
|
||||||
JSONObject jsonRoot = serializeSudoku(doku);
|
JSONObject jsonRoot = serializeSudoku(doku);
|
||||||
|
|
||||||
File f = new File("save", "save.json");
|
File f = new File("save", "save.json");
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (f.exists()) {
|
while (f.exists()) {
|
||||||
@@ -143,14 +154,12 @@ public class SudokuSerializer {
|
|||||||
File f;
|
File f;
|
||||||
if (saveToOverwrite == 0) {
|
if (saveToOverwrite == 0) {
|
||||||
f = new File("save", "save.json");
|
f = new File("save", "save.json");
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
f = new File("save", "save-" + saveToOverwrite + ".json");
|
f = new File("save", "save-" + saveToOverwrite + ".json");
|
||||||
}
|
}
|
||||||
if (!f.exists()) {
|
if (!f.exists()) {
|
||||||
return saveMultiDoku(doku);
|
return saveMultiDoku(doku);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
try (FileWriter file = new FileWriter(f)) {
|
try (FileWriter file = new FileWriter(f)) {
|
||||||
file.write(serializeSudoku(doku).toString(3));
|
file.write(serializeSudoku(doku).toString(3));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -234,7 +243,7 @@ public class SudokuSerializer {
|
|||||||
|
|
||||||
List<Cell> sudokuCells = new ArrayList<>();
|
List<Cell> sudokuCells = new ArrayList<>();
|
||||||
List<Block> sudokuBlocks = new ArrayList<>();
|
List<Block> sudokuBlocks = new ArrayList<>();
|
||||||
List<Constraint> sudokuConstraints = new ArrayList<>();
|
List<IConstraint> sudokuConstraints = new ArrayList<>();
|
||||||
|
|
||||||
for (int j = 0; j < sudokuCellsJsonArray.length(); j++) {
|
for (int j = 0; j < sudokuCellsJsonArray.length(); j++) {
|
||||||
int cellID = sudokuCellsJsonArray.getInt(j);
|
int cellID = sudokuCellsJsonArray.getInt(j);
|
||||||
@@ -248,7 +257,7 @@ public class SudokuSerializer {
|
|||||||
|
|
||||||
for (int j = 0; j < sudokuConstraintsJsonArray.length(); j++) {
|
for (int j = 0; j < sudokuConstraintsJsonArray.length(); j++) {
|
||||||
int constraintID = sudokuConstraintsJsonArray.getInt(j);
|
int constraintID = sudokuConstraintsJsonArray.getInt(j);
|
||||||
sudokuConstraints.add(Constraint.values()[constraintID]);
|
sudokuConstraints.add(Constraint.values()[constraintID].getConstraint());
|
||||||
}
|
}
|
||||||
|
|
||||||
Sudoku s = new Sudoku(sudokuCells, sudokuBlocks, sudokuConstraints);
|
Sudoku s = new Sudoku(sudokuCells, sudokuBlocks, sudokuConstraints);
|
||||||
|
|||||||
@@ -193,7 +193,7 @@ public class MultiDoku {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public MultiDoku clone() {
|
public MultiDoku clone() {
|
||||||
//TODO: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah
|
// TODO: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah
|
||||||
return SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(this));
|
return SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,13 +25,14 @@ public class Sudoku {
|
|||||||
/**
|
/**
|
||||||
* Liste des contraintes (TODO) du Sudoku.
|
* Liste des contraintes (TODO) du Sudoku.
|
||||||
*/
|
*/
|
||||||
private final List<Constraint> constraints;
|
private final List<IConstraint> constraints;
|
||||||
/**
|
/**
|
||||||
* Largeur des Blocks s'ils sont rectangulaires, valant 0 si ce n'est pas le cas.
|
* Largeur des Blocks s'ils sont rectangulaires, valant 0 si ce n'est pas le
|
||||||
|
* cas.
|
||||||
*/
|
*/
|
||||||
private int blockWidth;
|
private int blockWidth;
|
||||||
|
|
||||||
public Sudoku(List<Cell> cells, List<Block> blocks, List<Constraint> constraints) {
|
public Sudoku(List<Cell> cells, List<Block> blocks, List<IConstraint> constraints) {
|
||||||
this.cells = cells;
|
this.cells = cells;
|
||||||
this.blocks = blocks;
|
this.blocks = blocks;
|
||||||
this.constraints = constraints;
|
this.constraints = constraints;
|
||||||
@@ -49,6 +50,7 @@ public class Sudoku {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Transforme des coordonées d'une Cell en index.
|
* Transforme des coordonées d'une Cell en index.
|
||||||
|
*
|
||||||
* @param x int, abscisse.
|
* @param x int, abscisse.
|
||||||
* @param y int, ordonnée.
|
* @param y int, ordonnée.
|
||||||
* @return int, index correspondant.
|
* @return int, index correspondant.
|
||||||
@@ -59,7 +61,9 @@ public class Sudoku {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Vérifie que des coordonnées correspondent bien à une Cell dans le Sudoku.
|
* Vérifie que des coordonnées correspondent bien à une Cell dans le Sudoku.
|
||||||
* @return boolean, valant true si les coordonnées sont dans les bornes du Sudoku, false sinon.
|
*
|
||||||
|
* @return boolean, valant true si les coordonnées sont dans les bornes du
|
||||||
|
* Sudoku, false sinon.
|
||||||
*/
|
*/
|
||||||
public boolean isValidCoords(int x, int y) {
|
public boolean isValidCoords(int x, int y) {
|
||||||
int index = toIndex(x, y);
|
int index = toIndex(x, y);
|
||||||
@@ -68,21 +72,25 @@ public class Sudoku {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Vérifie que l'index correspond bien à une Cell dans le Sudoku.
|
* Vérifie que l'index correspond bien à une Cell dans le Sudoku.
|
||||||
* @return boolean, valant true si l'index est dans les bornes du Sudoku, false sinon.
|
*
|
||||||
|
* @return boolean, valant true si l'index est dans les bornes du Sudoku, false
|
||||||
|
* sinon.
|
||||||
*/
|
*/
|
||||||
public boolean isValidCoords(int index) {
|
public boolean isValidCoords(int index) {
|
||||||
return index < getSize() * getSize();
|
return index < getSize() * getSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Teste si on peut placer la value dans la Cell aux coordonnées x, y d'après les contraintes du Sudoku.
|
* Teste si on peut placer la value dans la Cell aux coordonnées x, y d'après
|
||||||
|
* les contraintes du Sudoku.
|
||||||
|
*
|
||||||
* @param x int, abscisse de la Cell voulue.
|
* @param x int, abscisse de la Cell voulue.
|
||||||
* @param y int, ordonnée de la Cell voulue.
|
* @param y int, ordonnée de la Cell voulue.
|
||||||
* @param value int, index du symbole qu'on veut placer.
|
* @param value int, index du symbole qu'on veut placer.
|
||||||
* @return boolean, true si on peut la placer et false sinon.
|
* @return boolean, true si on peut la placer et false sinon.
|
||||||
*/
|
*/
|
||||||
public boolean canBePlaced(int x, int y, int value) {
|
public boolean canBePlaced(int x, int y, int value) {
|
||||||
for (Constraint constraint : this.constraints) {
|
for (IConstraint constraint : this.constraints) {
|
||||||
if (!constraint.canBePlaced(this, x, y, value)) {
|
if (!constraint.canBePlaced(this, x, y, value)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -92,6 +100,7 @@ public class Sudoku {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Vide la Cell dotn les coordonnées sont renseignées de son symbole.
|
* Vide la Cell dotn les coordonnées sont renseignées de son symbole.
|
||||||
|
*
|
||||||
* @param x int, abscisse de la Cell voulue.
|
* @param x int, abscisse de la Cell voulue.
|
||||||
* @param y int, coordonnée de la Cell voulue.
|
* @param y int, coordonnée de la Cell voulue.
|
||||||
*/
|
*/
|
||||||
@@ -119,6 +128,7 @@ public class Sudoku {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Place le symbole d'index value dans la Cell de coordonnées précisées.
|
* Place le symbole d'index value dans la Cell de coordonnées précisées.
|
||||||
|
*
|
||||||
* @param x int, abscisse de la Cell voulue.
|
* @param x int, abscisse de la Cell voulue.
|
||||||
* @param y int, coordonnée de la Cell voulue.
|
* @param y int, coordonnée de la Cell voulue.
|
||||||
* @param value int, index du symbole à placer.
|
* @param value int, index du symbole à placer.
|
||||||
@@ -126,7 +136,7 @@ public class Sudoku {
|
|||||||
*/
|
*/
|
||||||
public Cell setCellSymbol(int x, int y, int value) {
|
public Cell setCellSymbol(int x, int y, int value) {
|
||||||
assert (isValidCoords(x, y));
|
assert (isValidCoords(x, y));
|
||||||
for (Constraint constraint : this.constraints) {
|
for (IConstraint constraint : this.constraints) {
|
||||||
if (!constraint.canBePlaced(this, x, y, value)) {
|
if (!constraint.canBePlaced(this, x, y, value)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -138,6 +148,7 @@ public class Sudoku {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Place les symboles d'index contenus dans values dans les cases du Sudoku.
|
* Place les symboles d'index contenus dans values dans les cases du Sudoku.
|
||||||
|
*
|
||||||
* @param values List<Integer>, liste des index des symboles à placer.
|
* @param values List<Integer>, liste des index des symboles à placer.
|
||||||
* @return boolean, vaut true si les symboles ont été placés, false sinon.
|
* @return boolean, vaut true si les symboles ont été placés, false sinon.
|
||||||
*/
|
*/
|
||||||
@@ -155,7 +166,9 @@ public class Sudoku {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Place les symboles d'index contenus dans values dans les cases du Sudoku et rend ces cases immuables.
|
* Place les symboles d'index contenus dans values dans les cases du Sudoku et
|
||||||
|
* rend ces cases immuables.
|
||||||
|
*
|
||||||
* @param values List<Integer>, liste des index des symboles à placer.
|
* @param values List<Integer>, liste des index des symboles à placer.
|
||||||
* @return boolean, vaut true si les symboles ont été placés, false sinon.
|
* @return boolean, vaut true si les symboles ont été placés, false sinon.
|
||||||
*/
|
*/
|
||||||
@@ -192,7 +205,7 @@ public class Sudoku {
|
|||||||
return this.cells.get(i);
|
return this.cells.get(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Constraint> getConstraints() {
|
public List<IConstraint> getConstraints() {
|
||||||
return constraints;
|
return constraints;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,6 +223,7 @@ public class Sudoku {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Vérifie si une Cell appartient au Sudoku.
|
* Vérifie si une Cell appartient au Sudoku.
|
||||||
|
*
|
||||||
* @param cell Cell, cellule dont on veut vérifier l'appartenance au Sudoku.
|
* @param cell Cell, cellule dont on veut vérifier l'appartenance au Sudoku.
|
||||||
* @return boolean, vaut true si la Cell appartient au Sudoku.
|
* @return boolean, vaut true si la Cell appartient au Sudoku.
|
||||||
*/
|
*/
|
||||||
@@ -233,6 +247,7 @@ public class Sudoku {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Renvoie la 1re Cell vide du Sudoku.
|
* Renvoie la 1re Cell vide du Sudoku.
|
||||||
|
*
|
||||||
* @return Cell, une Cell vide, ou null s'il n'y en a pas.
|
* @return Cell, une Cell vide, ou null s'il n'y en a pas.
|
||||||
*/
|
*/
|
||||||
public Cell getFirstEmptyCell() {
|
public Cell getFirstEmptyCell() {
|
||||||
@@ -245,7 +260,9 @@ public class Sudoku {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Vérifie si le Sudoku est résolu, c'est à dire complet et cohérent avec ses contraintes.
|
* Vérifie si le Sudoku est résolue, soit complet et cohérent avec ses
|
||||||
|
* contraintes.
|
||||||
|
*
|
||||||
* @return boolean, valant true si le Sudoku est résolu, false sinon.
|
* @return boolean, valant true si le Sudoku est résolu, false sinon.
|
||||||
*/
|
*/
|
||||||
public boolean isSolved() {
|
public boolean isSolved() {
|
||||||
@@ -256,6 +273,7 @@ public class Sudoku {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Vérifie que le Sudoku est complet, soit qu'il n'y ait aucune case vide.
|
* Vérifie que le Sudoku est complet, soit qu'il n'y ait aucune case vide.
|
||||||
|
*
|
||||||
* @return boolean, true si le Sudoku est complet, false sinon.
|
* @return boolean, true si le Sudoku est complet, false sinon.
|
||||||
*/
|
*/
|
||||||
private boolean isComplete() {
|
private boolean isComplete() {
|
||||||
@@ -263,7 +281,9 @@ public class Sudoku {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Vérifie si le Sudoku est valide, soit qu'il est cohérent avec ses contraintes.
|
* Vérifie si le Sudoku est valide, soit qu'il est cohérent avec ses
|
||||||
|
* contraintes.
|
||||||
|
*
|
||||||
* @return bollean, true si le Sudoku est valide, false sinon
|
* @return bollean, true si le Sudoku est valide, false sinon
|
||||||
*/
|
*/
|
||||||
private boolean isValid() {
|
private boolean isValid() {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import java.util.Map;
|
|||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import sudoku.constraint.Constraint;
|
import sudoku.constraint.Constraint;
|
||||||
|
import sudoku.constraint.IConstraint;
|
||||||
import sudoku.io.SudokuSerializer;
|
import sudoku.io.SudokuSerializer;
|
||||||
import sudoku.solver.RandomSolver;
|
import sudoku.solver.RandomSolver;
|
||||||
import sudoku.solver.Solver;
|
import sudoku.solver.Solver;
|
||||||
@@ -25,11 +26,13 @@ public class SudokuFactory {
|
|||||||
* Liste des contraintes par défaut d'un Multi- ou Sudoku.
|
* Liste des contraintes par défaut d'un Multi- ou Sudoku.
|
||||||
* Comprend les contraintes de blocs, de lignes, et de colonnes.
|
* Comprend les contraintes de blocs, de lignes, et de colonnes.
|
||||||
*/
|
*/
|
||||||
public static List<Constraint> DEFAULT_CONSTRAINTS = Arrays.asList(Constraint.Block, Constraint.Column,
|
public static List<IConstraint> DEFAULT_CONSTRAINTS = SudokuFactory
|
||||||
Constraint.Line);
|
.fromConstraints(Arrays.asList(Constraint.Block, Constraint.Column,
|
||||||
|
Constraint.Line));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Créée des Cells et les met dans une liste de taille size.
|
* Créée des Cells et les met dans une liste de taille size.
|
||||||
|
*
|
||||||
* @param size int, nombre de Cells à initialiser.
|
* @param size int, nombre de Cells à initialiser.
|
||||||
* @return List<Cell>, liste des Cells initialisées.
|
* @return List<Cell>, liste des Cells initialisées.
|
||||||
*/
|
*/
|
||||||
@@ -42,9 +45,11 @@ public class SudokuFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Créée des Blocks de taille width par height à partir des cellules données, et les met dans une liste.
|
* Créée des Blocks de taille width par height à partir des cellules données, et
|
||||||
* @param cells List<Cell>, liste des Cells à découper en Blocks.
|
* les met dans une liste.
|
||||||
* @param width int, largeur des Blocks à créer.
|
*
|
||||||
|
* @param cells List<Cell>, liste des Cells à découper en Blocks.
|
||||||
|
* @param width int, largeur des Blocks à créer.
|
||||||
* @param height int, hauteur des Blocks à créer.
|
* @param height int, hauteur des Blocks à créer.
|
||||||
* @return List<Block>, liste des Blocks créés.
|
* @return List<Block>, liste des Blocks créés.
|
||||||
*/
|
*/
|
||||||
@@ -72,29 +77,36 @@ public class SudokuFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Créée un MultiDoku vide dont les Blocks sont de taille widthBlock par heightBlock.
|
* Créée un MultiDoku vide dont les Blocks sont de taille widthBlock par
|
||||||
* @param widthBlock int, largeur des Blocks.
|
* heightBlock.
|
||||||
|
*
|
||||||
|
* @param widthBlock int, largeur des Blocks.
|
||||||
* @param heightBlock int, hauteur des Blocks.
|
* @param heightBlock int, hauteur des Blocks.
|
||||||
* @return MultiDoku, MultiDoku vide.
|
* @return MultiDoku, MultiDoku vide.
|
||||||
*/
|
*/
|
||||||
public static MultiDoku createBasicEmptyRectangleDoku(int widthBlock, int heightBlock,
|
public static MultiDoku createBasicEmptyRectangleDoku(int widthBlock, int heightBlock,
|
||||||
List<Constraint> constraints) {
|
List<IConstraint> constraints) {
|
||||||
return new MultiDoku(Arrays.asList(createRectangleSudoku(widthBlock, heightBlock, constraints)));
|
return new MultiDoku(Arrays.asList(createRectangleSudoku(widthBlock, heightBlock, constraints)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Créée un MultiDoku vide dont les Blocks sont carrés de longueur size.
|
* Créée un MultiDoku vide dont les Blocks sont carrés de longueur size.
|
||||||
|
*
|
||||||
* @param size int, taille des Blocks.
|
* @param size int, taille des Blocks.
|
||||||
* @return MultiDoku, MultiDoku vide.
|
* @return MultiDoku, MultiDoku vide.
|
||||||
*/
|
*/
|
||||||
public static MultiDoku createBasicEmptySquareDoku(int size, List<Constraint> constraints) {
|
public static MultiDoku createBasicEmptySquareDoku(int size, List<IConstraint> constraints) {
|
||||||
return new MultiDoku(Arrays.asList(createSquareSudoku(size, constraints)));
|
return new MultiDoku(Arrays.asList(createSquareSudoku(size, constraints)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Place des Cells immutables de valeurs fournies, aux Coordinate fournies dans le MultiDoku doku fourni.
|
* Place des Cells immutables de valeurs fournies, aux Coordinate fournies dans
|
||||||
|
* le MultiDoku doku fourni.
|
||||||
|
*
|
||||||
* @param doku MultiDoku, MultiDoku à remplir.
|
* @param doku MultiDoku, MultiDoku à remplir.
|
||||||
* @param immutableCells Map<Coordinate, Integer>, association de Coordinate coordonnées et Integer valeurs, correspondant aux cases à remplir.
|
* @param immutableCells Map<Coordinate, Integer>, association de Coordinate
|
||||||
|
* coordonnées et Integer valeurs, correspondant aux cases
|
||||||
|
* à remplir.
|
||||||
*/
|
*/
|
||||||
public static void setImmutableCells(MultiDoku doku, Map<Coordinate, Integer> immutableCells) {
|
public static void setImmutableCells(MultiDoku doku, Map<Coordinate, Integer> immutableCells) {
|
||||||
immutableCells.forEach((coordinate, symbol) -> {
|
immutableCells.forEach((coordinate, symbol) -> {
|
||||||
@@ -113,8 +125,10 @@ public class SudokuFactory {
|
|||||||
*
|
*
|
||||||
* @param doku MultiDoku, MultiDoku dont on doit vider des Cells.
|
* @param doku MultiDoku, MultiDoku dont on doit vider des Cells.
|
||||||
* @param nbCellsToEmpty int, nombre de cases à retirer.
|
* @param nbCellsToEmpty int, nombre de cases à retirer.
|
||||||
* @return boolean, valant true si un MultiDoku de difficulté donnée peut être créée, false sinon.
|
* @return boolean, valant true si un MultiDoku de difficulté donnée peut être
|
||||||
* @throws Exception si la difficulté n'est pas compatible avec la taille du MultiDoku.
|
* créée, false sinon.
|
||||||
|
* @throws Exception si la difficulté n'est pas compatible avec la taille du
|
||||||
|
* MultiDoku.
|
||||||
*/
|
*/
|
||||||
public static boolean newDokuFromFilledOne(MultiDoku doku, int nbCellsToEmpty, Solver solver) throws Exception {
|
public static boolean newDokuFromFilledOne(MultiDoku doku, int nbCellsToEmpty, Solver solver) throws Exception {
|
||||||
|
|
||||||
@@ -149,12 +163,14 @@ public class SudokuFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Créée un Sudoku vide dont les Blocks sont de taille widthBlock par heightBlock.
|
* Créée un Sudoku vide dont les Blocks sont de taille widthBlock par
|
||||||
|
* heightBlock.
|
||||||
|
*
|
||||||
* @param widthBlock int, largeur des Blocks.
|
* @param widthBlock int, largeur des Blocks.
|
||||||
* @param heightBlock int, hauteur des Blocks.
|
* @param heightBlock int, hauteur des Blocks.
|
||||||
* @return Sudoku, Sudoku vide.
|
* @return Sudoku, Sudoku vide.
|
||||||
*/
|
*/
|
||||||
private static Sudoku createRectangleSudoku(int widthBlock, int heightBlock, List<Constraint> constraints) {
|
private static Sudoku createRectangleSudoku(int widthBlock, int heightBlock, List<IConstraint> constraints) {
|
||||||
int symbolCount = widthBlock * heightBlock;
|
int symbolCount = widthBlock * heightBlock;
|
||||||
List<Cell> cases = initCells(symbolCount);
|
List<Cell> cases = initCells(symbolCount);
|
||||||
List<Block> blocs = initRectangleBlocs(cases, widthBlock, heightBlock);
|
List<Block> blocs = initRectangleBlocs(cases, widthBlock, heightBlock);
|
||||||
@@ -168,15 +184,17 @@ public class SudokuFactory {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Créée un Sudoku vide dont les Blocks sont carrés de longueur size.
|
* Créée un Sudoku vide dont les Blocks sont carrés de longueur size.
|
||||||
|
*
|
||||||
* @param size int, taille des Blocks.
|
* @param size int, taille des Blocks.
|
||||||
* @return Sudoku, Sudoku vide.
|
* @return Sudoku, Sudoku vide.
|
||||||
*/
|
*/
|
||||||
private static Sudoku createSquareSudoku(int size, List<Constraint> constraints) {
|
private static Sudoku createSquareSudoku(int size, List<IConstraint> constraints) {
|
||||||
return createRectangleSudoku(size, size, constraints);
|
return createRectangleSudoku(size, size, constraints);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connecte deux Sudokus selon la décalage offset fourni.
|
* Connecte deux Sudokus selon la décalage offset fourni.
|
||||||
|
*
|
||||||
* @param sudoku1 Sudoku, premier sudoku à connecter.
|
* @param sudoku1 Sudoku, premier sudoku à connecter.
|
||||||
* @param sudoku2 Sudoku, second sudoku à connecter.
|
* @param sudoku2 Sudoku, second sudoku à connecter.
|
||||||
* @param offset Coordinate, décalage entre les deux Sudokus.
|
* @param offset Coordinate, décalage entre les deux Sudokus.
|
||||||
@@ -213,16 +231,19 @@ public class SudokuFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
* 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.
|
* @param size int, largeur des Blocks unitraires des Sudokus à crééer.
|
||||||
* @return MultiDoku, MultiDoku de forme X.
|
* @return MultiDoku, MultiDoku de forme X.
|
||||||
*/
|
*/
|
||||||
public static MultiDoku createBasicXShapedMultidoku(int size, List<Constraint> constraints) {
|
public static MultiDoku createBasicXShapedMultidoku(int size, List<IConstraint> constraints) {
|
||||||
assert (size > 1);
|
assert (size > 1);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 2 3
|
* 2 3
|
||||||
* 1
|
* 1
|
||||||
* 4 5
|
* 4 5
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -241,15 +262,16 @@ public class SudokuFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Créée un MultiDoku de Blocks rectangulaires de forme width par height composé de cinq Sudokus,
|
* 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
|
* dont un central qui partage chacun de ses Blocks d'angle avec un autre
|
||||||
* Sudoku.
|
* Sudoku.
|
||||||
*
|
*
|
||||||
* @param width int, largeur des Blocks unitraires des Sudokus à crééer.
|
* @param width int, largeur des Blocks unitraires des Sudokus à crééer.
|
||||||
* @param height int, hauteur des Blocks unitraires des Sudokus à crééer.
|
* @param height int, hauteur des Blocks unitraires des Sudokus à crééer.
|
||||||
* @return MultiDoku, MultiDoku de forme X.
|
* @return MultiDoku, MultiDoku de forme X.
|
||||||
*/
|
*/
|
||||||
public static MultiDoku createBasicXShapedMultidoku(int width, int height, List<Constraint> constraints) {
|
public static MultiDoku createBasicXShapedMultidoku(int width, int height, List<IConstraint> constraints) {
|
||||||
assert (width > 1 && height > 1);
|
assert (width > 1 && height > 1);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -291,14 +313,14 @@ public class SudokuFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MultiDoku createBasicEmptyRandomBlockDoku(int blockSize, List<Constraint> 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<>();
|
||||||
homeLessCells.addAll(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++) {
|
||||||
Block b = new Block();
|
Block b = new Block();
|
||||||
for (int j = 0; j < blockCellCount; j++) {
|
for (int j = 0; j < blockCellCount; j++) {
|
||||||
int cellIndex = r.nextInt(homeLessCells.size());
|
int cellIndex = r.nextInt(homeLessCells.size());
|
||||||
@@ -314,4 +336,12 @@ public class SudokuFactory {
|
|||||||
}
|
}
|
||||||
return new MultiDoku(Arrays.asList(sudoku));
|
return new MultiDoku(Arrays.asList(sudoku));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<IConstraint> fromConstraints(List<Constraint> constraints) {
|
||||||
|
List<IConstraint> iconstraints = new ArrayList<>();
|
||||||
|
for (Constraint cons : constraints) {
|
||||||
|
iconstraints.add(cons.getConstraint());
|
||||||
|
}
|
||||||
|
return iconstraints;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user