Compare commits
8 Commits
tests
...
71666a3883
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
71666a3883 | ||
| 3f1ef93323 | |||
| a580321bd0 | |||
| d7d7dfe787 | |||
|
|
2fb3874a99 | ||
|
|
591e4f977a | ||
|
|
c481f66b0c | ||
|
|
aafb025874 |
@@ -1,13 +1,18 @@
|
||||
# Sudoku 🧩
|
||||
|
||||
Une application de génération et résolution de MultiDoku.
|
||||
|
||||
## Features 🌟
|
||||
|
||||
- MultiDoku solvers
|
||||
- Graphical User Interface (GUI)
|
||||
- Sudoku saves
|
||||
- Multiplayer
|
||||
|
||||
## Develop ☝🤓
|
||||
|
||||
Pour plus de détails, voir le dossier de conception
|
||||
|
||||
### Run 🏃
|
||||
|
||||
```sh
|
||||
|
||||
@@ -3,6 +3,9 @@ package sudoku.constraint;
|
||||
import sudoku.structure.Block;
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
/**
|
||||
* Contrainte de bloc
|
||||
*/
|
||||
public class BlockConstraint implements IConstraint{
|
||||
|
||||
@Override
|
||||
|
||||
@@ -3,6 +3,9 @@ package sudoku.constraint;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
/**
|
||||
* Contrainte de colonne
|
||||
*/
|
||||
public class ColumnConstraint implements IConstraint {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package sudoku.constraint;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public enum Constraint {
|
||||
|
||||
Block("Bloc", new BlockConstraint()),
|
||||
@@ -19,14 +19,6 @@ public enum Constraint {
|
||||
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() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@ package sudoku.constraint;
|
||||
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
/**
|
||||
* Contrainte de diagonale
|
||||
*/
|
||||
public class DiagonalConstraint implements IConstraint {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,22 +1,12 @@
|
||||
package sudoku.constraint;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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);
|
||||
|
||||
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,6 +2,9 @@ package sudoku.constraint;
|
||||
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
/**
|
||||
* Contrainte de ligne
|
||||
*/
|
||||
public class LineConstraint implements IConstraint {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -11,22 +11,35 @@ import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ConsoleInterface {
|
||||
public Scanner reader = new Scanner(System.in);
|
||||
private final 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() {
|
||||
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("Do you have a save sudoku you would like to continue? (y/n, default n)");
|
||||
if (reader.next().equalsIgnoreCase("y")) {
|
||||
useSavedDoku();
|
||||
} else {
|
||||
createDoku();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Demande à l'utilisateur un fichier de sauvegarde et le laisse jouer au
|
||||
* MultiDoku.
|
||||
* qui y est sauvegardé
|
||||
*/
|
||||
private void useSavedDoku() {
|
||||
System.out.println("What save should we use? Please enter the save number.");
|
||||
MultiDoku md = saveChoice();
|
||||
MultiDoku md = getSavedDoku();
|
||||
int blockWidth = md.getSubGrid(0).getBlockWidth();
|
||||
int blockHeight = md.getSubGrid(0).getBlocks().getFirst().getCells().size() / blockWidth;
|
||||
List<String> listSymbols = pickSymbols(blockWidth * blockHeight);
|
||||
@@ -38,7 +51,10 @@ public class ConsoleInterface {
|
||||
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.");
|
||||
int width = getBlockWidth();
|
||||
int height = getBlockHeight();
|
||||
@@ -58,7 +74,7 @@ public class ConsoleInterface {
|
||||
System.out.println("Your sudoku will look like this:");
|
||||
showMultidoku(doku, listSymbols, width, height);
|
||||
System.out.println(
|
||||
"You can now manually fill this sudoku ('fill'), or generate a playable one ('generate', default):");
|
||||
"You can now manually fill this sudoku ('fill'), or generate a playable one from this template ('generate', default):");
|
||||
if (reader.next().equalsIgnoreCase("fill")) {
|
||||
findSolution(doku, listSymbols, width, height);
|
||||
} else {
|
||||
@@ -66,6 +82,16 @@ 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) {
|
||||
System.out.println("We will now fill this sudoku.");
|
||||
System.out.println("What level of difficulty would you like?" +
|
||||
@@ -74,6 +100,7 @@ public class ConsoleInterface {
|
||||
if (difficulty.equals("full")) {
|
||||
generateFullDoku(doku);
|
||||
System.out.println("Here's your sudoku !");
|
||||
showMultidoku(doku, listSymbols, width, height);
|
||||
exit();
|
||||
} else {
|
||||
generatePartialDoku(doku, difficulty);
|
||||
@@ -86,36 +113,64 @@ 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 {
|
||||
turn(doku, listSymbols, width, height);
|
||||
} while (!doku.isSolved());
|
||||
System.out.println("This doku can be solved like this :");
|
||||
showMultidoku(doku, listSymbols, width, height);
|
||||
exit();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Message de félicitation quand l'utilisateur a rempli son doku.
|
||||
*/
|
||||
private void congrats() {
|
||||
System.out.println("Congrats! You've solved this sudoku! We hope this was fun! Let's play together again!");
|
||||
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;
|
||||
MultiDoku md = null;
|
||||
do {
|
||||
nbSave = reader.nextInt();
|
||||
if (nbSave == -1) {
|
||||
System.exit(0);
|
||||
}
|
||||
try {
|
||||
md = SudokuSerializer.getSavedMultiDoku(nbSave);
|
||||
} catch (Exception e) {
|
||||
System.out.println("There seems to be a problem with this save, please try again.");
|
||||
System.out.println(
|
||||
"There seems to be a problem with this save, please try again or write '-1' to abort.");
|
||||
}
|
||||
} while (md == null);
|
||||
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: ");
|
||||
int widthBlock = reader.nextInt();
|
||||
checkValidSize(widthBlock);
|
||||
@@ -127,7 +182,12 @@ public class ConsoleInterface {
|
||||
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: ");
|
||||
int heightBlock = reader.nextInt();
|
||||
checkValidSize(heightBlock);
|
||||
@@ -139,10 +199,23 @@ public class ConsoleInterface {
|
||||
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) {
|
||||
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) {
|
||||
System.out.println("Would you like to pick the " + numberOfSymbols
|
||||
+ " symbols from the sudoku? (y/n, default 'no')");
|
||||
@@ -171,6 +244,12 @@ 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() {
|
||||
List<IConstraint> listConstraints = SudokuFactory.DEFAULT_CONSTRAINTS;
|
||||
System.out.println(
|
||||
@@ -181,6 +260,12 @@ public class ConsoleInterface {
|
||||
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) {
|
||||
Difficulty difficulty;
|
||||
switch (difficultyName) {
|
||||
@@ -200,32 +285,73 @@ public class ConsoleInterface {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remplit entièrement le doku passé en paramètre.
|
||||
*
|
||||
* @param doku MultiDoku, doku à remplir
|
||||
*/
|
||||
private void generateFullDoku(MultiDoku 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) {
|
||||
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) {
|
||||
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) {
|
||||
System.out.println("Number of the file to overwrite ('-1' or unused save file number to create a new save) :");
|
||||
int n = reader.nextInt();
|
||||
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) {
|
||||
System.out.println(
|
||||
"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').");
|
||||
"You can now put a number in a cell ('play', default), show a solution ('solution'), save the grid ('save') or exit the program ('exit').");
|
||||
switch (reader.next()) {
|
||||
case "save":
|
||||
saveMultiDoku(doku);
|
||||
break;
|
||||
|
||||
case "solution":
|
||||
solve(doku, listSymbols, width, height);
|
||||
break;
|
||||
@@ -238,10 +364,26 @@ public class ConsoleInterface {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Applique l'étape passée en paramètre.
|
||||
*
|
||||
* @param step SolverStep, étape à appliquer
|
||||
*/
|
||||
private void applyStep(SolverStep step) {
|
||||
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) {
|
||||
System.out.println("Here is the step : \n");
|
||||
showMultidoku(doku, listSymbols, width, height);
|
||||
@@ -252,22 +394,39 @@ public class ConsoleInterface {
|
||||
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)");
|
||||
doku.getStateManager().popState();
|
||||
switch (reader.next()) {
|
||||
case "y":
|
||||
if (reader.next().equalsIgnoreCase("y")) {
|
||||
int stepCount = 0;
|
||||
while(stepCount < steps.size() && showStep(doku, listSymbols, width, height, steps.get(stepCount))){stepCount++;}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
while (stepCount < steps.size() && showStep(doku, listSymbols, width, height, steps.get(stepCount))) {
|
||||
stepCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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').");
|
||||
/**
|
||||
* Résout le doku en fonction du solver que choisit l'utilisateur.
|
||||
*
|
||||
* @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<>();
|
||||
doku.getStateManager().pushState();
|
||||
switch (reader.next()) {
|
||||
@@ -281,9 +440,19 @@ public class ConsoleInterface {
|
||||
new RandomSolver().solve(doku, steps);
|
||||
break;
|
||||
}
|
||||
showMultidoku(doku, listSymbols, width, height);
|
||||
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) {
|
||||
int x, y;
|
||||
RenderableMultidoku rdoku = RenderableMultidoku.fromMultidoku(doku);
|
||||
@@ -292,8 +461,8 @@ public class ConsoleInterface {
|
||||
y = reader.nextInt();
|
||||
System.out.println("Column of the cell to fill:");
|
||||
x = reader.nextInt();
|
||||
} while (!isValidCoordinates(rdoku, width, height, x-1, y-1));
|
||||
Cell cell = rdoku.getCell(x-1, y-1);
|
||||
} while (!isValidCoordinates(rdoku, width, height, x - 1, y - 1));
|
||||
Cell cell = rdoku.getCell(x - 1, y - 1);
|
||||
System.out.println("Character to put in the (" + x + ", " + y + ") cell:");
|
||||
String character = reader.next();
|
||||
while (!isValidSymbol(character, listSymbols, width * height)) {
|
||||
@@ -304,22 +473,49 @@ public class ConsoleInterface {
|
||||
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) {
|
||||
if (doku.getCell(x, y) != null) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
Cell cell = doku.getCell(x, y);
|
||||
return ((cell != null) && cell.isMutable());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
for (int i = 0; i < nbSymbols; i++) {
|
||||
if (listSymbols.get(i).equals(symbol)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
return Cell.NOSYMBOL;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (listSymbols.get(i).equals(symbol)) {
|
||||
@@ -329,6 +525,9 @@ public class ConsoleInterface {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Affiche un message d'aurevoir et ferme l'application.
|
||||
*/
|
||||
private void exit() {
|
||||
System.out.println("Thank you for playing!");
|
||||
System.exit(0);
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package sudoku.io;
|
||||
|
||||
public class SudokuFile {
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package sudoku.io;
|
||||
|
||||
public class SudokuSave {
|
||||
|
||||
public static enum AlgoResolution {
|
||||
Backtracking,
|
||||
NoBacktring
|
||||
}
|
||||
|
||||
// private final MultiDoku sudoku;
|
||||
// private final AlgoResolution resolution;
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -18,8 +18,17 @@ import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
/**
|
||||
* Classe permettant d'effectuer des opérations sur les sudokus afin de les
|
||||
* charger/sauvegarder
|
||||
*/
|
||||
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) {
|
||||
List<Cell> cellIds = new ArrayList<>();
|
||||
List<Block> blockIds = new ArrayList<>();
|
||||
@@ -127,6 +136,14 @@ public class SudokuSerializer {
|
||||
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.
|
||||
*
|
||||
@@ -134,6 +151,7 @@ public class SudokuSerializer {
|
||||
* @return String, the path of the save.
|
||||
*/
|
||||
public static String saveMultiDoku(final MultiDoku doku) {
|
||||
createSaveDir();
|
||||
JSONObject jsonRoot = serializeSudoku(doku);
|
||||
|
||||
File f = new File("save", "save.json");
|
||||
@@ -151,6 +169,7 @@ public class SudokuSerializer {
|
||||
}
|
||||
|
||||
public static String saveMultiDoku(final MultiDoku doku, final int saveToOverwrite) {
|
||||
createSaveDir();
|
||||
File f;
|
||||
if (saveToOverwrite == 0) {
|
||||
f = new File("save", "save.json");
|
||||
@@ -173,7 +192,7 @@ public class SudokuSerializer {
|
||||
* Get a MultiDoku from a pre-existing json save file.
|
||||
*
|
||||
* @param numberSave int, number of the save file to open.
|
||||
* @return MultiDoku, MultoDoku contained in the file.
|
||||
* @return MultiDoku, MultiDoku contained in the file.
|
||||
* @throws Exception when the given save file does not exist.
|
||||
*/
|
||||
public static MultiDoku getSavedMultiDoku(int numberSave) throws Exception {
|
||||
@@ -195,11 +214,21 @@ 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) {
|
||||
JSONObject jsonRoot = new JSONObject(json);
|
||||
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) {
|
||||
|
||||
List<Cell> cells = new ArrayList<>();
|
||||
|
||||
@@ -9,11 +9,17 @@ import java.util.Random;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
|
||||
//TODO
|
||||
public class HintHelper {
|
||||
|
||||
public static record Hint(Cell cell, int newValue) {}
|
||||
public 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) {
|
||||
doku.getStateManager().pushState();
|
||||
doku.clearMutableCells();
|
||||
|
||||
@@ -201,7 +201,6 @@ public class MultiDoku {
|
||||
}
|
||||
|
||||
public MultiDoku clone() {
|
||||
// TODO: C'est pas dingue de le faire comme ça...
|
||||
return SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(this));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
package sudoku.structure;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import sudoku.constraint.Constraint;
|
||||
import sudoku.constraint.IConstraint;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @class Sudoku
|
||||
* @brief Représent un Sudoku
|
||||
|
||||
@@ -1,20 +1,19 @@
|
||||
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.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
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 {
|
||||
|
||||
/**
|
||||
@@ -358,7 +357,7 @@ public class SudokuFactory {
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforme des Constraint en IConstraint correspondants
|
||||
* Transforme des Constraints en IConstraints correspondants.
|
||||
* @param constraints List<Constraints>
|
||||
* @return List<IConstraints>
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user