Compare commits
1 Commits
71666a3883
...
tests
| Author | SHA1 | Date | |
|---|---|---|---|
| abf6f6c7c3 |
@@ -1,18 +1,13 @@
|
|||||||
# Sudoku 🧩
|
# Sudoku 🧩
|
||||||
|
|
||||||
Une application de génération et résolution de MultiDoku.
|
|
||||||
|
|
||||||
## Features 🌟
|
## Features 🌟
|
||||||
|
|
||||||
- MultiDoku solvers
|
|
||||||
- Graphical User Interface (GUI)
|
- Graphical User Interface (GUI)
|
||||||
- Sudoku saves
|
- Sudoku saves
|
||||||
- Multiplayer
|
- Multiplayer
|
||||||
|
|
||||||
## Develop ☝🤓
|
## Develop ☝🤓
|
||||||
|
|
||||||
Pour plus de détails, voir le dossier de conception
|
|
||||||
|
|
||||||
### Run 🏃
|
### Run 🏃
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
|
|||||||
@@ -3,9 +3,6 @@ package sudoku.constraint;
|
|||||||
import sudoku.structure.Block;
|
import sudoku.structure.Block;
|
||||||
import sudoku.structure.Sudoku;
|
import sudoku.structure.Sudoku;
|
||||||
|
|
||||||
/**
|
|
||||||
* Contrainte de bloc
|
|
||||||
*/
|
|
||||||
public class BlockConstraint implements IConstraint{
|
public class BlockConstraint implements IConstraint{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -3,9 +3,6 @@ package sudoku.constraint;
|
|||||||
import sudoku.structure.Cell;
|
import sudoku.structure.Cell;
|
||||||
import sudoku.structure.Sudoku;
|
import sudoku.structure.Sudoku;
|
||||||
|
|
||||||
/**
|
|
||||||
* Contrainte de colonne
|
|
||||||
*/
|
|
||||||
public class ColumnConstraint implements IConstraint {
|
public class ColumnConstraint implements IConstraint {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
package sudoku.constraint;
|
package sudoku.constraint;
|
||||||
|
|
||||||
/**
|
import java.util.List;
|
||||||
* Enumération utilisée afin de simplifier l'affichage en graphique.
|
|
||||||
* Un sudoku peut tout de même contenir des contraintes qui ne sont pas dans cette énumération.
|
import sudoku.structure.Sudoku;
|
||||||
*/
|
|
||||||
public enum Constraint {
|
public enum Constraint {
|
||||||
|
|
||||||
Block("Bloc", new BlockConstraint()),
|
Block("Bloc", new BlockConstraint()),
|
||||||
@@ -19,6 +19,14 @@ public enum Constraint {
|
|||||||
this.displayName = displayName;
|
this.displayName = displayName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean canBePlaced(Sudoku s, int x, int y, int newValue) {
|
||||||
|
return getConstraint().canBePlaced(s, x, y, newValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Integer> getPossibleSymbols(final Sudoku s, int x, int y) {
|
||||||
|
return getConstraint().getPossibleSymbols(s, x, y);
|
||||||
|
}
|
||||||
|
|
||||||
public String getDisplayName() {
|
public String getDisplayName() {
|
||||||
return displayName;
|
return displayName;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,6 @@ package sudoku.constraint;
|
|||||||
|
|
||||||
import sudoku.structure.Sudoku;
|
import sudoku.structure.Sudoku;
|
||||||
|
|
||||||
/**
|
|
||||||
* Contrainte de diagonale
|
|
||||||
*/
|
|
||||||
public class DiagonalConstraint implements IConstraint {
|
public class DiagonalConstraint implements IConstraint {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,12 +1,22 @@
|
|||||||
package sudoku.constraint;
|
package sudoku.constraint;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import sudoku.structure.Sudoku;
|
import sudoku.structure.Sudoku;
|
||||||
|
|
||||||
/**
|
public interface IConstraint extends Serializable {
|
||||||
* Interface de base pour la déclaration d'une contrainte
|
|
||||||
* Elle est en théorie assez générique pour implémenter n'importe quelle
|
|
||||||
* contrainte
|
|
||||||
*/
|
|
||||||
public interface IConstraint {
|
|
||||||
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) {
|
||||||
|
List<Integer> possibilities = new ArrayList<>();
|
||||||
|
for (int i = 0; i < s.getSize(); i++) {
|
||||||
|
if (canBePlaced(s, x, y, i)) {
|
||||||
|
possibilities.add(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return possibilities;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,6 @@ package sudoku.constraint;
|
|||||||
|
|
||||||
import sudoku.structure.Sudoku;
|
import sudoku.structure.Sudoku;
|
||||||
|
|
||||||
/**
|
|
||||||
* Contrainte de ligne
|
|
||||||
*/
|
|
||||||
public class LineConstraint implements IConstraint {
|
public class LineConstraint implements IConstraint {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -11,35 +11,22 @@ import java.util.List;
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class ConsoleInterface {
|
public class ConsoleInterface {
|
||||||
private final Scanner reader = new Scanner(System.in);
|
public Scanner reader = new Scanner(System.in);
|
||||||
|
|
||||||
/**
|
|
||||||
* Début de la séquence console, affiche un message de bienvenue et les crédits
|
|
||||||
* du projet
|
|
||||||
* puis donne à l'utilisateur le choix de récupérer un doku sauvegardé
|
|
||||||
* ou d'en créer un nouveau.
|
|
||||||
*/
|
|
||||||
public void welcome() {
|
public void welcome() {
|
||||||
System.out.println("Welcome to our Sudoku Solver!");
|
System.out.println("Welcome to our Sudoku Solver!");
|
||||||
System.out.println("This is the project of Melvyn Bauvent, Lilas Grenier and Simon Pribylski.");
|
System.out.println("This is the project of Melvyn Bauvent, Lilas Grenier and Simon Pribylski.");
|
||||||
|
|
||||||
System.out.println("Do you have a save sudoku you would like to continue? (y/n, default n)");
|
System.out.println("Do you have a save sudoku you would like to continue? (y/n, default n)");
|
||||||
if (reader.next().equalsIgnoreCase("y")) {
|
if (reader.next().equalsIgnoreCase("y")) {
|
||||||
useSavedDoku();
|
useSavedDoku();
|
||||||
} else {
|
} else {
|
||||||
createDoku();
|
createDoku();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Demande à l'utilisateur un fichier de sauvegarde et le laisse jouer au
|
|
||||||
* MultiDoku.
|
|
||||||
* qui y est sauvegardé
|
|
||||||
*/
|
|
||||||
private void useSavedDoku() {
|
private void useSavedDoku() {
|
||||||
System.out.println("What save should we use? Please enter the save number.");
|
System.out.println("What save should we use? Please enter the save number.");
|
||||||
MultiDoku md = getSavedDoku();
|
MultiDoku md = saveChoice();
|
||||||
int blockWidth = md.getSubGrid(0).getBlockWidth();
|
int blockWidth = md.getSubGrid(0).getBlockWidth();
|
||||||
int blockHeight = md.getSubGrid(0).getBlocks().getFirst().getCells().size() / blockWidth;
|
int blockHeight = md.getSubGrid(0).getBlocks().getFirst().getCells().size() / blockWidth;
|
||||||
List<String> listSymbols = pickSymbols(blockWidth * blockHeight);
|
List<String> listSymbols = pickSymbols(blockWidth * blockHeight);
|
||||||
@@ -51,10 +38,7 @@ public class ConsoleInterface {
|
|||||||
congrats();
|
congrats();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public void createDoku() {
|
||||||
* Demande à l'utilisateur les paramètres du doku à générer.
|
|
||||||
*/
|
|
||||||
private void createDoku() {
|
|
||||||
System.out.println("First of all, you need to tell me the size of the sudoku you want to generate.");
|
System.out.println("First of all, you need to tell me the size of the sudoku you want to generate.");
|
||||||
int width = getBlockWidth();
|
int width = getBlockWidth();
|
||||||
int height = getBlockHeight();
|
int height = getBlockHeight();
|
||||||
@@ -74,7 +58,7 @@ public class ConsoleInterface {
|
|||||||
System.out.println("Your sudoku will look like this:");
|
System.out.println("Your sudoku will look like this:");
|
||||||
showMultidoku(doku, listSymbols, width, height);
|
showMultidoku(doku, listSymbols, width, height);
|
||||||
System.out.println(
|
System.out.println(
|
||||||
"You can now manually fill this sudoku ('fill'), or generate a playable one from this template ('generate', default):");
|
"You can now manually fill this sudoku ('fill'), or generate a playable one ('generate', default):");
|
||||||
if (reader.next().equalsIgnoreCase("fill")) {
|
if (reader.next().equalsIgnoreCase("fill")) {
|
||||||
findSolution(doku, listSymbols, width, height);
|
findSolution(doku, listSymbols, width, height);
|
||||||
} else {
|
} else {
|
||||||
@@ -82,16 +66,6 @@ public class ConsoleInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Remplit un doku vide en fonction de la difficulté que l'utilisateur
|
|
||||||
* renseigne,
|
|
||||||
* et le laisse jouer.
|
|
||||||
*
|
|
||||||
* @param doku MultiDoku, MultiDoku vide à remplir
|
|
||||||
* @param listSymbols List<String>, liste des symboles à utiliser
|
|
||||||
* @param width int, largeur d'un bloc du sudoku
|
|
||||||
* @param height int, hauteur d'un bloc du sudoku
|
|
||||||
*/
|
|
||||||
private void playableDoku(MultiDoku doku, List<String> listSymbols, int width, int height) {
|
private void playableDoku(MultiDoku doku, List<String> listSymbols, int width, int height) {
|
||||||
System.out.println("We will now fill this sudoku.");
|
System.out.println("We will now fill this sudoku.");
|
||||||
System.out.println("What level of difficulty would you like?" +
|
System.out.println("What level of difficulty would you like?" +
|
||||||
@@ -100,7 +74,6 @@ public class ConsoleInterface {
|
|||||||
if (difficulty.equals("full")) {
|
if (difficulty.equals("full")) {
|
||||||
generateFullDoku(doku);
|
generateFullDoku(doku);
|
||||||
System.out.println("Here's your sudoku !");
|
System.out.println("Here's your sudoku !");
|
||||||
showMultidoku(doku, listSymbols, width, height);
|
|
||||||
exit();
|
exit();
|
||||||
} else {
|
} else {
|
||||||
generatePartialDoku(doku, difficulty);
|
generatePartialDoku(doku, difficulty);
|
||||||
@@ -113,64 +86,36 @@ public class ConsoleInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private void findSolution(MultiDoku doku, List<String> listSymbols, int width, int height){
|
||||||
* Permet à l'utilisateur de remplir manuellement un sudoku vide, et de le
|
|
||||||
* remplir
|
|
||||||
* quand souhaité.
|
|
||||||
*
|
|
||||||
* @param doku MultiDoku, MultiDoku vide à remplir
|
|
||||||
* @param listSymbols List<String>, liste des symboles à utiliser
|
|
||||||
* @param width int, largeur d'un bloc du sudoku
|
|
||||||
* @param height int, hauteur d'un bloc du sudoku
|
|
||||||
*/
|
|
||||||
private void findSolution(MultiDoku doku, List<String> listSymbols, int width, int height) {
|
|
||||||
do {
|
do {
|
||||||
turn(doku, listSymbols, width, height);
|
turn(doku, listSymbols, width, height);
|
||||||
} while (!doku.isSolved());
|
} while (!doku.isSolved());
|
||||||
System.out.println("This doku can be solved like this :");
|
System.out.println("This doku can be solved like this :");
|
||||||
showMultidoku(doku, listSymbols, width, height);
|
showMultidoku(doku, listSymbols, width, height);
|
||||||
exit();
|
exit();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Message de félicitation quand l'utilisateur a rempli son doku.
|
|
||||||
*/
|
|
||||||
private void congrats() {
|
private void congrats() {
|
||||||
System.out.println("Congrats! You've solved this sudoku! We hope this was fun! Let's play together again!");
|
System.out.println("Congrats! You've solved this sudoku! We hope this was fun! Let's play together again!");
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private MultiDoku saveChoice() {
|
||||||
* Renvoie un MultiDoku préenregistré, dont le numéro de sauvegarde est
|
|
||||||
* renseigné
|
|
||||||
* par l'utilisateur.
|
|
||||||
*
|
|
||||||
* @return Mutidoku, multidoku enregistré à la sauveagrde de numéro donné.
|
|
||||||
*/
|
|
||||||
private MultiDoku getSavedDoku() {
|
|
||||||
int nbSave;
|
int nbSave;
|
||||||
MultiDoku md = null;
|
MultiDoku md = null;
|
||||||
do {
|
do {
|
||||||
nbSave = reader.nextInt();
|
nbSave = reader.nextInt();
|
||||||
if (nbSave == -1) {
|
|
||||||
System.exit(0);
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
md = SudokuSerializer.getSavedMultiDoku(nbSave);
|
md = SudokuSerializer.getSavedMultiDoku(nbSave);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println(
|
System.out.println("There seems to be a problem with this save, please try again.");
|
||||||
"There seems to be a problem with this save, please try again or write '-1' to abort.");
|
|
||||||
}
|
}
|
||||||
} while (md == null);
|
} while (md == null);
|
||||||
return md;
|
return md;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public int getBlockWidth() {
|
||||||
* Demande à l'utilisateur la largeur d'un bloc du sudoku à générer.
|
|
||||||
*
|
|
||||||
* @return int, largeur d'un bloc du sudoku
|
|
||||||
*/
|
|
||||||
private int getBlockWidth() {
|
|
||||||
System.out.println("Width of a block: ");
|
System.out.println("Width of a block: ");
|
||||||
int widthBlock = reader.nextInt();
|
int widthBlock = reader.nextInt();
|
||||||
checkValidSize(widthBlock);
|
checkValidSize(widthBlock);
|
||||||
@@ -182,12 +127,7 @@ public class ConsoleInterface {
|
|||||||
return widthBlock;
|
return widthBlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public int getBlockHeight() {
|
||||||
* Demande à l'utilisateur la hauteur d'un bloc du sudoku à générer.
|
|
||||||
*
|
|
||||||
* @return int, hauteur d'un bloc du sudoku
|
|
||||||
*/
|
|
||||||
private int getBlockHeight() {
|
|
||||||
System.out.println("Height of a block: ");
|
System.out.println("Height of a block: ");
|
||||||
int heightBlock = reader.nextInt();
|
int heightBlock = reader.nextInt();
|
||||||
checkValidSize(heightBlock);
|
checkValidSize(heightBlock);
|
||||||
@@ -199,23 +139,10 @@ public class ConsoleInterface {
|
|||||||
return heightBlock;
|
return heightBlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Vérifie si la taille passée en paramètres est une taille valide.
|
|
||||||
*
|
|
||||||
* @param size int, longueur à vérifier
|
|
||||||
* @return true si size>0, false sinon.
|
|
||||||
*/
|
|
||||||
private Boolean checkValidSize(int size) {
|
private Boolean checkValidSize(int size) {
|
||||||
return (size > 0);
|
return (size > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Permet à l'utilisateur de choisir les symboles qu'il souhaite utiliser pour
|
|
||||||
* l'affichage.
|
|
||||||
*
|
|
||||||
* @param numberOfSymbols int, nombre de symboles à choisir
|
|
||||||
* @return LIst<String>, liste des symboles à utiliser
|
|
||||||
*/
|
|
||||||
private List<String> pickSymbols(int numberOfSymbols) {
|
private List<String> pickSymbols(int numberOfSymbols) {
|
||||||
System.out.println("Would you like to pick the " + numberOfSymbols
|
System.out.println("Would you like to pick the " + numberOfSymbols
|
||||||
+ " symbols from the sudoku? (y/n, default 'no')");
|
+ " symbols from the sudoku? (y/n, default 'no')");
|
||||||
@@ -244,12 +171,6 @@ public class ConsoleInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Permet à l'utilisateur de choisir les contraintes qu'il souhaite utiliser
|
|
||||||
* pour son sudoku.
|
|
||||||
*
|
|
||||||
* @return List<IConstraint>, liste des contraintes à utiliser
|
|
||||||
*/
|
|
||||||
private List<IConstraint> getListConstraints() {
|
private List<IConstraint> getListConstraints() {
|
||||||
List<IConstraint> listConstraints = SudokuFactory.DEFAULT_CONSTRAINTS;
|
List<IConstraint> listConstraints = SudokuFactory.DEFAULT_CONSTRAINTS;
|
||||||
System.out.println(
|
System.out.println(
|
||||||
@@ -260,12 +181,6 @@ public class ConsoleInterface {
|
|||||||
return listConstraints;
|
return listConstraints;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Remplit un sudoku selon la difficulté passée en paramètre.
|
|
||||||
*
|
|
||||||
* @param doku MultiDoku, doku vide à remplir selon la difficulté.
|
|
||||||
* @param difficultyName String, difficulté de résolution du doku à remplir.
|
|
||||||
*/
|
|
||||||
private void generatePartialDoku(MultiDoku doku, String difficultyName) {
|
private void generatePartialDoku(MultiDoku doku, String difficultyName) {
|
||||||
Difficulty difficulty;
|
Difficulty difficulty;
|
||||||
switch (difficultyName) {
|
switch (difficultyName) {
|
||||||
@@ -285,73 +200,32 @@ public class ConsoleInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Remplit entièrement le doku passé en paramètre.
|
|
||||||
*
|
|
||||||
* @param doku MultiDoku, doku à remplir
|
|
||||||
*/
|
|
||||||
private void generateFullDoku(MultiDoku doku) {
|
private void generateFullDoku(MultiDoku doku) {
|
||||||
new RandomSolver().solve(doku);
|
new RandomSolver().solve(doku);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Affiche le doku passé en paramètre.
|
|
||||||
*
|
|
||||||
* @param doku MultiDoku, MultiDoku à afficher
|
|
||||||
* @param listSymbols List<String>, liste des symboles à utiliser
|
|
||||||
* @param width int, largeur d'un bloc du sudoku
|
|
||||||
* @param height int, hauteur d'un bloc du sudoku
|
|
||||||
*/
|
|
||||||
private void showMultidoku(MultiDoku doku, List<String> listSymbols, int width, int height) {
|
private void showMultidoku(MultiDoku doku, List<String> listSymbols, int width, int height) {
|
||||||
showMultiDoku(RenderableMultidoku.fromMultidoku(doku), listSymbols, width, height);
|
showMultiDoku(RenderableMultidoku.fromMultidoku(doku), listSymbols, width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Affiche le doku passé en paramètre.
|
|
||||||
*
|
|
||||||
* @param doku RenderableMultiDoku, MultiDoku à afficher
|
|
||||||
* @param listSymbols List<String>, liste des symboles à utiliser
|
|
||||||
* @param width int, largeur d'un bloc du sudoku
|
|
||||||
* @param height int, hauteur d'un bloc du sudoku
|
|
||||||
*/
|
|
||||||
private void showMultiDoku(RenderableMultidoku doku, List<String> listSymbols, int width, int height) {
|
private void showMultiDoku(RenderableMultidoku doku, List<String> listSymbols, int width, int height) {
|
||||||
SudokuPrinter.printMultiDokuWithIndex(doku, listSymbols, width, height);
|
SudokuPrinter.printMultiDokuWithIndex(doku, listSymbols, width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Permet à l'utilisateur de sauvegarder l'état de son doku, soit dans un
|
|
||||||
* nouveau fichier
|
|
||||||
* de sauvegarde, soit en écrasant une sauvegarde précédente.
|
|
||||||
*
|
|
||||||
* @param doku MultiDoku, MultiDoku à sauvegarder
|
|
||||||
*/
|
|
||||||
private void saveMultiDoku(MultiDoku doku) {
|
private void saveMultiDoku(MultiDoku doku) {
|
||||||
System.out.println("Number of the file to overwrite ('-1' or unused save file number to create a new save) :");
|
System.out.println("Number of the file to overwrite ('-1' or unused save file number to create a new save) :");
|
||||||
int n = reader.nextInt();
|
int n = reader.nextInt();
|
||||||
String path = SudokuSerializer.saveMultiDoku(doku, n);
|
String path = SudokuSerializer.saveMultiDoku(doku, n);
|
||||||
System.out.println("The path to your save is: " + path);
|
System.out.println("The path to your save is:" + path);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Tour de jeu de l'utilisateur, présenté avec les choix de remplir une case du
|
|
||||||
* doku,
|
|
||||||
* de sauvegarder son état actuel dans un fichier de sauvegarde,
|
|
||||||
* de le résoudre tel qu'il est,
|
|
||||||
* ou de quitter l'application.
|
|
||||||
*
|
|
||||||
* @param doku MultiDoku, MultiDoku actuel
|
|
||||||
* @param listSymbols List<String>, liste des symboles à utiliser
|
|
||||||
* @param width int, largeur d'un bloc du sudoku
|
|
||||||
* @param height int, hauteur d'un bloc du sudoku
|
|
||||||
*/
|
|
||||||
private void turn(MultiDoku doku, List<String> listSymbols, int width, int height) {
|
private void turn(MultiDoku doku, List<String> listSymbols, int width, int height) {
|
||||||
System.out.println(
|
System.out.println(
|
||||||
"You can now put a number in a cell ('play', default), show a solution ('solution'), save the grid ('save') or exit the program ('exit').");
|
"You can now put a number in a cell ('play', default), save the state of the doku ('save'), show a solution ('solution') or exit the program ('exit').");
|
||||||
switch (reader.next()) {
|
switch (reader.next()) {
|
||||||
case "save":
|
case "save":
|
||||||
saveMultiDoku(doku);
|
saveMultiDoku(doku);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "solution":
|
case "solution":
|
||||||
solve(doku, listSymbols, width, height);
|
solve(doku, listSymbols, width, height);
|
||||||
break;
|
break;
|
||||||
@@ -364,26 +238,10 @@ public class ConsoleInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Applique l'étape passée en paramètre.
|
|
||||||
*
|
|
||||||
* @param step SolverStep, étape à appliquer
|
|
||||||
*/
|
|
||||||
private void applyStep(SolverStep step) {
|
private void applyStep(SolverStep step) {
|
||||||
step.getCell().setSymbolIndex(step.getNewValue());
|
step.getCell().setSymbolIndex(step.getNewValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Permet d'afficher une étape de résolution du doku complété.
|
|
||||||
*
|
|
||||||
* @param doku MultiDoku, MultiDoku actuel
|
|
||||||
* @param listSymbols List<String>, liste des symboles à utiliser
|
|
||||||
* @param width int, largeur d'un bloc du sudoku
|
|
||||||
* @param height int, hauteur d'un bloc du sudoku
|
|
||||||
* @param step SolverStep, étape de résolution à afficher
|
|
||||||
* @return boolean, valant true si l'utilisateur veut afficher l'étape, false
|
|
||||||
* sinon
|
|
||||||
*/
|
|
||||||
private boolean showStep(MultiDoku doku, List<String> listSymbols, int width, int height, SolverStep step) {
|
private boolean showStep(MultiDoku doku, List<String> listSymbols, int width, int height, SolverStep step) {
|
||||||
System.out.println("Here is the step : \n");
|
System.out.println("Here is the step : \n");
|
||||||
showMultidoku(doku, listSymbols, width, height);
|
showMultidoku(doku, listSymbols, width, height);
|
||||||
@@ -394,39 +252,22 @@ public class ConsoleInterface {
|
|||||||
return reader.next().equals("y");
|
return reader.next().equals("y");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private void showSolveSteps(MultiDoku doku, List<String> listSymbols, int width, int height, List<SolverStep> steps) {
|
||||||
* Permet d'afficher les étapes de résolution du doku complété si l'utilisateur
|
|
||||||
* le souhaite.
|
|
||||||
*
|
|
||||||
* @param doku MultiDoku, MultiDoku actuel
|
|
||||||
* @param listSymbols List<String>, liste des symboles à utiliser
|
|
||||||
* @param width int, largeur d'un bloc du sudoku
|
|
||||||
* @param height int, hauteur d'un bloc du sudoku
|
|
||||||
* @param steps List<SolverStep>, liste des étapes de résolution
|
|
||||||
*/
|
|
||||||
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)");
|
System.out.println("Would you like to see the steps of the solver ? (y/n, default n)");
|
||||||
doku.getStateManager().popState();
|
doku.getStateManager().popState();
|
||||||
if (reader.next().equalsIgnoreCase("y")) {
|
switch (reader.next()) {
|
||||||
int stepCount = 0;
|
case "y":
|
||||||
while (stepCount < steps.size() && showStep(doku, listSymbols, width, height, steps.get(stepCount))) {
|
int stepCount = 0;
|
||||||
stepCount++;
|
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){
|
||||||
* Résout le doku en fonction du solver que choisit l'utilisateur.
|
System.out.println("Pick a solver to use : random ('random', default), human ('human') or mixed solver ('mixed').");
|
||||||
*
|
|
||||||
* @param doku MultiDoku, MultiDoku actuel
|
|
||||||
* @param listSymbols List<String>, liste des symboles à utiliser
|
|
||||||
* @param width int, largeur d'un bloc du sudoku
|
|
||||||
* @param height int, hauteur d'un bloc du sudoku
|
|
||||||
*/
|
|
||||||
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<>();
|
List<SolverStep> steps = new ArrayList<>();
|
||||||
doku.getStateManager().pushState();
|
doku.getStateManager().pushState();
|
||||||
switch (reader.next()) {
|
switch (reader.next()) {
|
||||||
@@ -440,19 +281,9 @@ public class ConsoleInterface {
|
|||||||
new RandomSolver().solve(doku, steps);
|
new RandomSolver().solve(doku, steps);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
showMultidoku(doku, listSymbols, width, height);
|
|
||||||
showSolveSteps(doku, listSymbols, width, height, steps);
|
showSolveSteps(doku, listSymbols, width, height, steps);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Remplissage d'une Cell du doku en fonction des coordonnées et du symboles que
|
|
||||||
* l'utilisateur choisit.
|
|
||||||
*
|
|
||||||
* @param doku MultiDoku, MultiDoku actuel
|
|
||||||
* @param listSymbols List<String>, liste des symboles à utiliser
|
|
||||||
* @param width int, largeur d'un bloc du sudoku
|
|
||||||
* @param height int, hauteur d'un bloc du sudoku
|
|
||||||
*/
|
|
||||||
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);
|
||||||
@@ -461,8 +292,8 @@ public class ConsoleInterface {
|
|||||||
y = reader.nextInt();
|
y = reader.nextInt();
|
||||||
System.out.println("Column of the cell to fill:");
|
System.out.println("Column of the cell to fill:");
|
||||||
x = reader.nextInt();
|
x = reader.nextInt();
|
||||||
} while (!isValidCoordinates(rdoku, width, height, x - 1, y - 1));
|
} while (!isValidCoordinates(rdoku, width, height, x-1, y-1));
|
||||||
Cell cell = rdoku.getCell(x - 1, y - 1);
|
Cell cell = rdoku.getCell(x-1, y-1);
|
||||||
System.out.println("Character to put in the (" + x + ", " + y + ") cell:");
|
System.out.println("Character to put in the (" + x + ", " + y + ") cell:");
|
||||||
String character = reader.next();
|
String character = reader.next();
|
||||||
while (!isValidSymbol(character, listSymbols, width * height)) {
|
while (!isValidSymbol(character, listSymbols, width * height)) {
|
||||||
@@ -473,49 +304,22 @@ public class ConsoleInterface {
|
|||||||
showMultiDoku(rdoku, listSymbols, width, height);
|
showMultiDoku(rdoku, listSymbols, width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Vérifie que la Cell identifiée par les coordonées x et y dans le
|
|
||||||
* RenderableMultiDOku fourni
|
|
||||||
* existe et est modifiable.
|
|
||||||
*
|
|
||||||
* @param doku RenderableMultiDoku, MultiDoku actuel
|
|
||||||
* @param width int, largeur d'un bloc du sudoku
|
|
||||||
* @param height int, hauteur d'un bloc du sudoku
|
|
||||||
* @param x int, abscisse de la Cell à vérifier
|
|
||||||
* @param y int, ordonnée de la Cell à vérifier
|
|
||||||
* @return Boolean true si la Cell aux coordonéees données peut être modifiée,
|
|
||||||
* false sinon
|
|
||||||
*/
|
|
||||||
private boolean isValidCoordinates(RenderableMultidoku doku, int width, int height, int x, int y) {
|
private boolean isValidCoordinates(RenderableMultidoku doku, int width, int height, int x, int y) {
|
||||||
Cell cell = doku.getCell(x, y);
|
if (doku.getCell(x, y) != null) {
|
||||||
return ((cell != null) && cell.isMutable());
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Renvoie l'index du symbole passé en paramètre.
|
|
||||||
*
|
|
||||||
* @param symbol String, symbole dont on veut l'index
|
|
||||||
* @param listSymbols List<String>, liste des symboles possibles
|
|
||||||
* @param nbSymbols int, nombre de symboles possibles
|
|
||||||
* @return int, index du symbole si celui-ci est valide, Cell.NOSYMBOL sinon.
|
|
||||||
*/
|
|
||||||
private int indexOfSymbol(String symbol, List<String> listSymbols, int nbSymbols) {
|
private int indexOfSymbol(String symbol, List<String> listSymbols, int nbSymbols) {
|
||||||
for (int i = 0; i < nbSymbols; i++) {
|
for (int i = 0; i < nbSymbols; i++) {
|
||||||
if (listSymbols.get(i).equals(symbol)) {
|
if (listSymbols.get(i).equals(symbol)) {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Cell.NOSYMBOL;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Vérifie que le symbol passé en paramètre est valide.
|
|
||||||
*
|
|
||||||
* @param symbol String, symbole dont on vérifie la validité
|
|
||||||
* @param listSymbols List<String>, liste des symboles possibles
|
|
||||||
* @param size int, nombre de symboles possibles
|
|
||||||
* @return boolean, valant true si le symbole est valide, false sinon.
|
|
||||||
*/
|
|
||||||
private boolean isValidSymbol(String symbol, List<String> listSymbols, int size) {
|
private boolean isValidSymbol(String symbol, List<String> listSymbols, int size) {
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
if (listSymbols.get(i).equals(symbol)) {
|
if (listSymbols.get(i).equals(symbol)) {
|
||||||
@@ -525,9 +329,6 @@ public class ConsoleInterface {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Affiche un message d'aurevoir et ferme l'application.
|
|
||||||
*/
|
|
||||||
private void exit() {
|
private void exit() {
|
||||||
System.out.println("Thank you for playing!");
|
System.out.println("Thank you for playing!");
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
|
|||||||
7
app/src/main/java/sudoku/io/SudokuFile.java
Normal file
7
app/src/main/java/sudoku/io/SudokuFile.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package sudoku.io;
|
||||||
|
|
||||||
|
public class SudokuFile {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
package sudoku.io;
|
package sudoku.io;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import gui.RenderableMultidoku;
|
import gui.RenderableMultidoku;
|
||||||
import gui.constants.Symbols;
|
import gui.constants.Symbols;
|
||||||
import sudoku.structure.Cell;
|
import sudoku.structure.Cell;
|
||||||
import sudoku.structure.MultiDoku;
|
import sudoku.structure.MultiDoku;
|
||||||
import sudoku.structure.Sudoku;
|
import sudoku.structure.Sudoku;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class SudokuPrinter {
|
public class SudokuPrinter {
|
||||||
public static final String ANSI_RESET = "\u001B[0m";
|
public static final String ANSI_RESET = "\u001B[0m";
|
||||||
public static final String ANSI_RED = "\u001B[31m";
|
public static final String ANSI_RED = "\u001B[31m";
|
||||||
@@ -47,7 +47,7 @@ public class SudokuPrinter {
|
|||||||
List<String> listSymbols) {
|
List<String> listSymbols) {
|
||||||
StringBuilder header = new StringBuilder("");
|
StringBuilder header = new StringBuilder("");
|
||||||
header.append(" ");
|
header.append(" ");
|
||||||
for (int x = 0; x < blockWidth*blockHeight; x++) {
|
for (int x = 0; x < blockWidth * blockHeight; x++) {
|
||||||
header.append(x + 1).append(" ");
|
header.append(x + 1).append(" ");
|
||||||
if (x % blockWidth == blockWidth - 1 && x != blockWidth * blockHeight - 1) {
|
if (x % blockWidth == blockWidth - 1 && x != blockWidth * blockHeight - 1) {
|
||||||
header.append(" ");
|
header.append(" ");
|
||||||
@@ -77,11 +77,12 @@ public class SudokuPrinter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void printMultiDoku(final RenderableMultidoku rm, Symbols symbols, int blockWidth, int blockHeight) {
|
public static String printMultiDoku(final RenderableMultidoku rm, Symbols symbols, int blockWidth,
|
||||||
printMultiDoku(rm, symbols.getSymbols(), blockWidth, blockHeight);
|
int blockHeight) {
|
||||||
|
return printMultiDoku(rm, symbols.getSymbols(), blockWidth, blockHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void printMultiDoku(final RenderableMultidoku rm, List<String> listSymbols, int blockWidth,
|
public static String printMultiDoku(final RenderableMultidoku rm, List<String> listSymbols, int blockWidth,
|
||||||
int blockHeight) {
|
int blockHeight) {
|
||||||
StringBuilder line = new StringBuilder("\n");
|
StringBuilder line = new StringBuilder("\n");
|
||||||
int nBlockInWidth = rm.getWidth() / blockWidth;
|
int nBlockInWidth = rm.getWidth() / blockWidth;
|
||||||
@@ -108,7 +109,8 @@ public class SudokuPrinter {
|
|||||||
line.append("]\n");
|
line.append("]\n");
|
||||||
}
|
}
|
||||||
line.append("__".repeat(Math.max(0, rm.getWidth() + nBlockInWidth))).append("_\n");
|
line.append("__".repeat(Math.max(0, rm.getWidth() + nBlockInWidth))).append("_\n");
|
||||||
System.out.println(line);
|
// System.out.println(line);
|
||||||
|
return line.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void printMultiDokuWithIndex(final RenderableMultidoku rm, List<String> listSymbols, int blockWidth,
|
public static void printMultiDokuWithIndex(final RenderableMultidoku rm, List<String> listSymbols, int blockWidth,
|
||||||
@@ -127,7 +129,7 @@ public class SudokuPrinter {
|
|||||||
if (y % blockHeight == 0) {
|
if (y % blockHeight == 0) {
|
||||||
line.append(" ").append("__".repeat(Math.max(0, rm.getWidth() + nBlockInWidth))).append("_\n");
|
line.append(" ").append("__".repeat(Math.max(0, rm.getWidth() + nBlockInWidth))).append("_\n");
|
||||||
}
|
}
|
||||||
line.append(y+1).append(" [ ");
|
line.append(y + 1).append(" [ ");
|
||||||
for (int x = 0; x < rm.getWidth(); x++) {
|
for (int x = 0; x < rm.getWidth(); x++) {
|
||||||
if (x % blockWidth == 0 && x > 0) {
|
if (x % blockWidth == 0 && x > 0) {
|
||||||
line.append("| ");
|
line.append("| ");
|
||||||
@@ -182,12 +184,16 @@ public class SudokuPrinter {
|
|||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void printMultiDoku(final MultiDoku doku, int blockWidth, int blockHeight, Symbols symbols) {
|
public static String printMultiDoku(final MultiDoku doku) {
|
||||||
if (doku.getNbSubGrids() == 1) {
|
int blockWidth = doku.getSubGrid(0).getBlockWidth();
|
||||||
printRectangleSudoku(doku.getSubGrid(0), blockWidth, blockHeight, symbols);
|
if (blockWidth == 0)
|
||||||
} else {
|
return printMultiDoku(doku, 0, 0, Symbols.Numbers);
|
||||||
printMultiDoku(RenderableMultidoku.fromMultidoku(doku), symbols, blockWidth, blockHeight);
|
else
|
||||||
}
|
return printMultiDoku(doku, blockWidth, doku.getSubGrid(0).getSize() / blockWidth, Symbols.Letters);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String printMultiDoku(final MultiDoku doku, int blockWidth, int blockHeight, Symbols symbols) {
|
||||||
|
return printMultiDoku(RenderableMultidoku.fromMultidoku(doku), symbols, blockWidth, blockHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void printMultiDokuWithIndex(final MultiDoku doku, int blockWidth, int blockHeight, Symbols symbols) {
|
public static void printMultiDokuWithIndex(final MultiDoku doku, int blockWidth, int blockHeight, Symbols symbols) {
|
||||||
|
|||||||
15
app/src/main/java/sudoku/io/SudokuSave.java
Normal file
15
app/src/main/java/sudoku/io/SudokuSave.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package sudoku.io;
|
||||||
|
|
||||||
|
public class SudokuSave {
|
||||||
|
|
||||||
|
public static enum AlgoResolution {
|
||||||
|
Backtracking,
|
||||||
|
NoBacktring
|
||||||
|
}
|
||||||
|
|
||||||
|
// private final MultiDoku sudoku;
|
||||||
|
// private final AlgoResolution resolution;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -18,17 +18,8 @@ import sudoku.structure.Cell;
|
|||||||
import sudoku.structure.MultiDoku;
|
import sudoku.structure.MultiDoku;
|
||||||
import sudoku.structure.Sudoku;
|
import sudoku.structure.Sudoku;
|
||||||
|
|
||||||
/**
|
|
||||||
* Classe permettant d'effectuer des opérations sur les sudokus afin de les
|
|
||||||
* charger/sauvegarder
|
|
||||||
*/
|
|
||||||
public class SudokuSerializer {
|
public class SudokuSerializer {
|
||||||
|
|
||||||
/**
|
|
||||||
* Convertit un sudoku en object JSON
|
|
||||||
* @param multidoku le sudoku à sérialiser
|
|
||||||
* @return le JSON
|
|
||||||
*/
|
|
||||||
public static JSONObject serializeSudoku(final MultiDoku multidoku) {
|
public static JSONObject serializeSudoku(final MultiDoku multidoku) {
|
||||||
List<Cell> cellIds = new ArrayList<>();
|
List<Cell> cellIds = new ArrayList<>();
|
||||||
List<Block> blockIds = new ArrayList<>();
|
List<Block> blockIds = new ArrayList<>();
|
||||||
@@ -136,14 +127,6 @@ public class SudokuSerializer {
|
|||||||
return jsonRoot;
|
return jsonRoot;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Crée le répertoire save afin d'y sauvegarder les sudokus
|
|
||||||
*/
|
|
||||||
private static void createSaveDir() {
|
|
||||||
File f = new File("save");
|
|
||||||
f.mkdir();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save a serialized MultiDoku in a JSON file.
|
* Save a serialized MultiDoku in a JSON file.
|
||||||
*
|
*
|
||||||
@@ -151,7 +134,6 @@ public class SudokuSerializer {
|
|||||||
* @return String, the path of the save.
|
* @return String, the path of the save.
|
||||||
*/
|
*/
|
||||||
public static String saveMultiDoku(final MultiDoku doku) {
|
public static String saveMultiDoku(final MultiDoku doku) {
|
||||||
createSaveDir();
|
|
||||||
JSONObject jsonRoot = serializeSudoku(doku);
|
JSONObject jsonRoot = serializeSudoku(doku);
|
||||||
|
|
||||||
File f = new File("save", "save.json");
|
File f = new File("save", "save.json");
|
||||||
@@ -169,7 +151,6 @@ public class SudokuSerializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String saveMultiDoku(final MultiDoku doku, final int saveToOverwrite) {
|
public static String saveMultiDoku(final MultiDoku doku, final int saveToOverwrite) {
|
||||||
createSaveDir();
|
|
||||||
File f;
|
File f;
|
||||||
if (saveToOverwrite == 0) {
|
if (saveToOverwrite == 0) {
|
||||||
f = new File("save", "save.json");
|
f = new File("save", "save.json");
|
||||||
@@ -192,7 +173,7 @@ public class SudokuSerializer {
|
|||||||
* Get a MultiDoku from a pre-existing json save file.
|
* Get a MultiDoku from a pre-existing json save file.
|
||||||
*
|
*
|
||||||
* @param numberSave int, number of the save file to open.
|
* @param numberSave int, number of the save file to open.
|
||||||
* @return MultiDoku, MultiDoku contained in the file.
|
* @return MultiDoku, MultoDoku contained in the file.
|
||||||
* @throws Exception when the given save file does not exist.
|
* @throws Exception when the given save file does not exist.
|
||||||
*/
|
*/
|
||||||
public static MultiDoku getSavedMultiDoku(int numberSave) throws Exception {
|
public static MultiDoku getSavedMultiDoku(int numberSave) throws Exception {
|
||||||
@@ -214,21 +195,11 @@ public class SudokuSerializer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Construit un sudoku à partir d'un String en JSON
|
|
||||||
* @param json le sudoku sérialisé
|
|
||||||
* @return le sudoku désérialisé
|
|
||||||
*/
|
|
||||||
public static MultiDoku deserializeSudoku(final String json) {
|
public static MultiDoku deserializeSudoku(final String json) {
|
||||||
JSONObject jsonRoot = new JSONObject(json);
|
JSONObject jsonRoot = new JSONObject(json);
|
||||||
return deserializeSudoku(jsonRoot);
|
return deserializeSudoku(jsonRoot);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Désérialise un sudoku d'un objet JSON
|
|
||||||
* @param jsonObject l'objet à désérialiser
|
|
||||||
* @return le sudoku correspondant
|
|
||||||
*/
|
|
||||||
public static MultiDoku deserializeSudoku(final JSONObject jsonObject) {
|
public static MultiDoku deserializeSudoku(final JSONObject jsonObject) {
|
||||||
|
|
||||||
List<Cell> cells = new ArrayList<>();
|
List<Cell> cells = new ArrayList<>();
|
||||||
|
|||||||
@@ -9,17 +9,11 @@ import java.util.Random;
|
|||||||
import sudoku.structure.Cell;
|
import sudoku.structure.Cell;
|
||||||
import sudoku.structure.MultiDoku;
|
import sudoku.structure.MultiDoku;
|
||||||
|
|
||||||
|
//TODO
|
||||||
public class HintHelper {
|
public class HintHelper {
|
||||||
|
|
||||||
public record Hint(Cell cell, int newValue) {}
|
public static record Hint(Cell cell, int newValue) {}
|
||||||
|
|
||||||
/**
|
|
||||||
* Si possible, donne un indice sur la résolution du doku passé en paramètre,
|
|
||||||
* selon la méthode de résolution rensaignée.
|
|
||||||
* @param doku MultiDoku, multidoku pour lequel on veut un indice
|
|
||||||
* @param solver Solver, méthode de résolution souhaitée
|
|
||||||
* @return Hint, indice sur une case à remplir, valant null si le doku n'a pas de solution.
|
|
||||||
*/
|
|
||||||
public static Hint getHint(MultiDoku doku, Solver solver) {
|
public static Hint getHint(MultiDoku doku, Solver solver) {
|
||||||
doku.getStateManager().pushState();
|
doku.getStateManager().pushState();
|
||||||
doku.clearMutableCells();
|
doku.clearMutableCells();
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package sudoku.structure;
|
package sudoku.structure;
|
||||||
|
|
||||||
|
import sudoku.io.SudokuPrinter;
|
||||||
import sudoku.io.SudokuSerializer;
|
import sudoku.io.SudokuSerializer;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -182,7 +183,7 @@ public class MultiDoku {
|
|||||||
if (sudoku.getSize() != otherSudoku.getSize())
|
if (sudoku.getSize() != otherSudoku.getSize())
|
||||||
return false;
|
return false;
|
||||||
for (int j = 0; j < sudoku.getSize() * sudoku.getSize(); j++) {
|
for (int j = 0; j < sudoku.getSize() * sudoku.getSize(); j++) {
|
||||||
if (sudoku.getCell(i).getSymbolIndex() != otherSudoku.getCell(i).getSymbolIndex())
|
if (sudoku.getCell(j).getSymbolIndex() != otherSudoku.getCell(j).getSymbolIndex())
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -191,16 +192,11 @@ public class MultiDoku {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
return SudokuPrinter.printMultiDoku(this);
|
||||||
sb.append("Multidoku {");
|
|
||||||
for (Sudoku sudoku : subGrids) {
|
|
||||||
sb.append("\n\t").append(sudoku.toString());
|
|
||||||
}
|
|
||||||
sb.append("\n}");
|
|
||||||
return sb.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public MultiDoku clone() {
|
public MultiDoku clone() {
|
||||||
|
// TODO: C'est pas dingue de le faire comme ça...
|
||||||
return SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(this));
|
return SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
package sudoku.structure;
|
package sudoku.structure;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import sudoku.constraint.Constraint;
|
import sudoku.constraint.Constraint;
|
||||||
import sudoku.constraint.IConstraint;
|
import sudoku.constraint.IConstraint;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class Sudoku
|
* @class Sudoku
|
||||||
* @brief Représent un Sudoku
|
* @brief Représent un Sudoku
|
||||||
|
|||||||
@@ -1,19 +1,20 @@
|
|||||||
package sudoku.structure;
|
package sudoku.structure;
|
||||||
|
|
||||||
import sudoku.constraint.Constraint;
|
|
||||||
import sudoku.constraint.IConstraint;
|
|
||||||
import sudoku.io.SudokuSerializer;
|
|
||||||
import sudoku.solver.RandomSolver;
|
|
||||||
import sudoku.solver.Solver;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
|
import sudoku.constraint.Constraint;
|
||||||
|
import sudoku.constraint.IConstraint;
|
||||||
|
import sudoku.io.SudokuSerializer;
|
||||||
|
import sudoku.solver.RandomSolver;
|
||||||
|
import sudoku.solver.Solver;
|
||||||
|
|
||||||
public class SudokuFactory {
|
public class SudokuFactory {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -327,7 +328,7 @@ public class SudokuFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rempli un MultiDoku donné par rapport à un difficulté.
|
* Remplit un MultiDoku donné par rapport à une difficulté.
|
||||||
*
|
*
|
||||||
* @param doku MultiDoku, vide.
|
* @param doku MultiDoku, vide.
|
||||||
* @param difficulty Difficulty, qui correspond au pourcentage de cases à enlever.
|
* @param difficulty Difficulty, qui correspond au pourcentage de cases à enlever.
|
||||||
@@ -357,7 +358,7 @@ public class SudokuFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transforme des Constraints en IConstraints correspondants.
|
* Transforme des Constraint en IConstraint correspondants
|
||||||
* @param constraints List<Constraints>
|
* @param constraints List<Constraints>
|
||||||
* @return List<IConstraints>
|
* @return List<IConstraints>
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -42,12 +42,12 @@ class SolverTest {
|
|||||||
ns, 0, 1, ns);
|
ns, 0, 1, ns);
|
||||||
assertTrue(test.setImmutableCellsSymbol(immutableCells));
|
assertTrue(test.setImmutableCellsSymbol(immutableCells));
|
||||||
List<Integer> correctCells = List.of(
|
List<Integer> correctCells = List.of(
|
||||||
1, 2, 3, 0,
|
1, 2, 3, 0,
|
||||||
0, 3, 2, 1,
|
0, 3, 2, 1,
|
||||||
2, 1, 0, 3,
|
2, 1, 0, 3,
|
||||||
3, 0, 1, 2);
|
3, 0, 1, 2);
|
||||||
assertTrue(result.setCellsSymbol(correctCells));
|
assertTrue(result.setCellsSymbol(correctCells));
|
||||||
assertTrue(result.isSolved());
|
assertTrue(result.isSolved());
|
||||||
|
|
||||||
assertNotEquals(mdResult, mdTest);
|
assertNotEquals(mdResult, mdTest);
|
||||||
solver.solve(mdTest);
|
solver.solve(mdTest);
|
||||||
@@ -101,24 +101,32 @@ class SolverTest {
|
|||||||
MultiDoku mdResult = SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(mdTest));
|
MultiDoku mdResult = SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(mdTest));
|
||||||
assertFalse(mdTest.isSolved());
|
assertFalse(mdTest.isSolved());
|
||||||
assertFalse(mdResult.isSolved());
|
assertFalse(mdResult.isSolved());
|
||||||
|
assertEquals(mdTest, mdResult);
|
||||||
|
|
||||||
assertTrue(solver.solve(mdTest));
|
assertTrue(solver.solve(mdTest));
|
||||||
|
|
||||||
assertTrue(mdTest.isSolved());
|
assertTrue(mdTest.isSolved());
|
||||||
assertFalse(mdResult.isSolved());
|
assertFalse(mdResult.isSolved());
|
||||||
|
System.out.println(mdTest);
|
||||||
|
System.out.println(mdResult);
|
||||||
assertNotEquals(mdTest, mdResult);
|
assertNotEquals(mdTest, mdResult);
|
||||||
solver.solve(mdResult);
|
|
||||||
|
assertTrue(solver.solve(mdResult));
|
||||||
|
|
||||||
assertEquals(mdTest, mdResult);
|
assertEquals(mdTest, mdResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void solveTest() {
|
void solveTest() {
|
||||||
initializeSolvers();
|
for (int i = 0; i < 100; i++) {
|
||||||
testSize2(h);
|
testSize2(h);
|
||||||
testSize3(h);
|
testSize3(h);
|
||||||
testSize2(m);
|
testSize2(m);
|
||||||
testSize3(m);
|
testSize3(m);
|
||||||
testMDSize3(m);
|
testMDSize3(m);
|
||||||
testSize2(r);
|
testSize2(r);
|
||||||
testSize3(r);
|
testSize3(r);
|
||||||
testMDSize3(r);
|
testMDSize3(r);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user