8 Commits

Author SHA1 Message Date
Melvyn
71666a3883 refactor : mini fixs
All checks were successful
Linux arm64 / Build (push) Successful in 44s
2025-02-02 22:51:33 +01:00
3f1ef93323 doc: serializer
All checks were successful
Linux arm64 / Build (push) Successful in 38s
2025-02-02 22:29:52 +01:00
a580321bd0 doc: constraints
All checks were successful
Linux arm64 / Build (push) Successful in 42s
2025-02-02 22:25:28 +01:00
d7d7dfe787 restore console save
All checks were successful
Linux arm64 / Build (push) Successful in 39s
2025-02-02 22:06:45 +01:00
Janet-Doe
2fb3874a99 update ConsoleInterface
All checks were successful
Linux arm64 / Build (push) Successful in 37s
2025-02-02 21:46:29 +01:00
Janet-Doe
591e4f977a update ConsoleInterface
All checks were successful
Linux arm64 / Build (push) Successful in 42s
2025-02-02 21:31:32 +01:00
Janet-Doe
c481f66b0c add doc + delete unused class
All checks were successful
Linux arm64 / Build (push) Successful in 37s
2025-02-02 21:03:36 +01:00
Janet-Doe
aafb025874 doc ConsoleInterface
All checks were successful
Linux arm64 / Build (push) Successful in 38s
2025-02-02 17:50:15 +01:00
15 changed files with 303 additions and 95 deletions

View File

@@ -1,13 +1,18 @@
# 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

View File

@@ -3,6 +3,9 @@ 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

View File

@@ -3,6 +3,9 @@ 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

View File

@@ -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.
import sudoku.structure.Sudoku; * Un sudoku peut tout de même contenir des contraintes qui ne sont pas dans cette énumération.
*/
public enum Constraint { public enum Constraint {
Block("Bloc", new BlockConstraint()), Block("Bloc", new BlockConstraint()),
@@ -19,14 +19,6 @@ 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;
} }

View File

@@ -2,6 +2,9 @@ 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

View File

@@ -1,22 +1,12 @@
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;
}
} }

View File

@@ -2,6 +2,9 @@ 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

View File

@@ -11,22 +11,35 @@ import java.util.List;
import java.util.Scanner; import java.util.Scanner;
public class ConsoleInterface { 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() { 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 = saveChoice(); MultiDoku md = getSavedDoku();
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);
@@ -38,7 +51,10 @@ 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();
@@ -58,7 +74,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 ('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")) { if (reader.next().equalsIgnoreCase("fill")) {
findSolution(doku, listSymbols, width, height); findSolution(doku, listSymbols, width, height);
} else { } 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) { 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?" +
@@ -74,6 +100,7 @@ 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);
@@ -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 { 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("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); } 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);
@@ -127,7 +182,12 @@ 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);
@@ -139,10 +199,23 @@ 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')");
@@ -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() { private List<IConstraint> getListConstraints() {
List<IConstraint> listConstraints = SudokuFactory.DEFAULT_CONSTRAINTS; List<IConstraint> listConstraints = SudokuFactory.DEFAULT_CONSTRAINTS;
System.out.println( System.out.println(
@@ -181,6 +260,12 @@ 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) {
@@ -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) { 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), 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()) { 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;
@@ -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) { 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);
@@ -252,22 +394,39 @@ 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();
switch (reader.next()) { if (reader.next().equalsIgnoreCase("y")) {
case "y": int stepCount = 0;
int stepCount = 0; while (stepCount < steps.size() && showStep(doku, listSymbols, width, height, steps.get(stepCount))) {
while(stepCount < steps.size() && showStep(doku, listSymbols, width, height, steps.get(stepCount))){stepCount++;} stepCount++;
break; }
default:
break;
} }
} }
private void solve(MultiDoku doku, List<String> listSymbols, int width, int height){ /**
System.out.println("Pick a solver to use : random ('random', default), human ('human') or mixed solver ('mixed')."); * 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<>(); List<SolverStep> steps = new ArrayList<>();
doku.getStateManager().pushState(); doku.getStateManager().pushState();
switch (reader.next()) { switch (reader.next()) {
@@ -281,9 +440,19 @@ 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);
@@ -292,8 +461,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)) {
@@ -304,22 +473,49 @@ 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) {
if (doku.getCell(x, y) != null) { Cell cell = doku.getCell(x, y);
return true; return ((cell != null) && cell.isMutable());
}
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 -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) { 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)) {
@@ -329,6 +525,9 @@ 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);

View File

@@ -1,7 +0,0 @@
package sudoku.io;
public class SudokuFile {
}

View File

@@ -1,15 +0,0 @@
package sudoku.io;
public class SudokuSave {
public static enum AlgoResolution {
Backtracking,
NoBacktring
}
// private final MultiDoku sudoku;
// private final AlgoResolution resolution;
}

View File

@@ -18,8 +18,17 @@ 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<>();
@@ -127,6 +136,14 @@ 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.
* *
@@ -134,6 +151,7 @@ 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");
@@ -151,6 +169,7 @@ 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");
@@ -173,7 +192,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, MultoDoku contained in the file. * @return MultiDoku, MultiDoku 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 {
@@ -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) { 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<>();

View File

@@ -9,11 +9,17 @@ 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 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) { public static Hint getHint(MultiDoku doku, Solver solver) {
doku.getStateManager().pushState(); doku.getStateManager().pushState();
doku.clearMutableCells(); doku.clearMutableCells();

View File

@@ -201,7 +201,6 @@ public class MultiDoku {
} }
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));
} }

View File

@@ -1,11 +1,10 @@
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

View File

@@ -1,20 +1,19 @@
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 {
/** /**
@@ -358,7 +357,7 @@ public class SudokuFactory {
} }
/** /**
* Transforme des Constraint en IConstraint correspondants * Transforme des Constraints en IConstraints correspondants.
* @param constraints List<Constraints> * @param constraints List<Constraints>
* @return List<IConstraints> * @return List<IConstraints>
*/ */