1 Commits

Author SHA1 Message Date
abf6f6c7c3 fix tests
Some checks failed
Linux arm64 / Build (push) Failing after 29s
2025-02-02 17:26:54 +01:00
17 changed files with 144 additions and 343 deletions

View File

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

View File

@@ -3,9 +3,6 @@ package sudoku.constraint;
import sudoku.structure.Block;
import sudoku.structure.Sudoku;
/**
* Contrainte de bloc
*/
public class BlockConstraint implements IConstraint{
@Override

View File

@@ -3,9 +3,6 @@ package sudoku.constraint;
import sudoku.structure.Cell;
import sudoku.structure.Sudoku;
/**
* Contrainte de colonne
*/
public class ColumnConstraint implements IConstraint {
@Override

View File

@@ -1,9 +1,9 @@
package sudoku.constraint;
/**
* 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 java.util.List;
import sudoku.structure.Sudoku;
public enum Constraint {
Block("Bloc", new BlockConstraint()),
@@ -19,6 +19,14 @@ 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;
}

View File

@@ -2,9 +2,6 @@ package sudoku.constraint;
import sudoku.structure.Sudoku;
/**
* Contrainte de diagonale
*/
public class DiagonalConstraint implements IConstraint {
@Override

View File

@@ -1,12 +1,22 @@
package sudoku.constraint;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import sudoku.structure.Sudoku;
/**
* 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 {
public interface IConstraint extends Serializable {
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,9 +2,6 @@ package sudoku.constraint;
import sudoku.structure.Sudoku;
/**
* Contrainte de ligne
*/
public class LineConstraint implements IConstraint {
@Override

View File

@@ -11,35 +11,22 @@ import java.util.List;
import java.util.Scanner;
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() {
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 = getSavedDoku();
MultiDoku md = saveChoice();
int blockWidth = md.getSubGrid(0).getBlockWidth();
int blockHeight = md.getSubGrid(0).getBlocks().getFirst().getCells().size() / blockWidth;
List<String> listSymbols = pickSymbols(blockWidth * blockHeight);
@@ -51,10 +38,7 @@ public class ConsoleInterface {
congrats();
}
/**
* Demande à l'utilisateur les paramètres du doku à générer.
*/
private void createDoku() {
public 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();
@@ -74,7 +58,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 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")) {
findSolution(doku, listSymbols, width, height);
} 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) {
System.out.println("We will now fill this sudoku.");
System.out.println("What level of difficulty would you like?" +
@@ -100,7 +74,6 @@ 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);
@@ -113,64 +86,36 @@ public class ConsoleInterface {
}
}
/**
* 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) {
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);
}
/**
* 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() {
private MultiDoku saveChoice() {
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 or write '-1' to abort.");
System.out.println("There seems to be a problem with this save, please try again.");
}
} while (md == null);
return md;
}
/**
* Demande à l'utilisateur la largeur d'un bloc du sudoku à générer.
*
* @return int, largeur d'un bloc du sudoku
*/
private int getBlockWidth() {
public int getBlockWidth() {
System.out.println("Width of a block: ");
int widthBlock = reader.nextInt();
checkValidSize(widthBlock);
@@ -182,12 +127,7 @@ public class ConsoleInterface {
return widthBlock;
}
/**
* Demande à l'utilisateur la hauteur d'un bloc du sudoku à générer.
*
* @return int, hauteur d'un bloc du sudoku
*/
private int getBlockHeight() {
public int getBlockHeight() {
System.out.println("Height of a block: ");
int heightBlock = reader.nextInt();
checkValidSize(heightBlock);
@@ -199,23 +139,10 @@ 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')");
@@ -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() {
List<IConstraint> listConstraints = SudokuFactory.DEFAULT_CONSTRAINTS;
System.out.println(
@@ -260,12 +181,6 @@ 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) {
@@ -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) {
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), 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()) {
case "save":
saveMultiDoku(doku);
break;
case "solution":
solve(doku, listSymbols, width, height);
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) {
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);
@@ -394,39 +252,22 @@ public class ConsoleInterface {
return reader.next().equals("y");
}
/**
* 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) {
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();
if (reader.next().equalsIgnoreCase("y")) {
int stepCount = 0;
while (stepCount < steps.size() && showStep(doku, listSymbols, width, height, steps.get(stepCount))) {
stepCount++;
}
switch (reader.next()) {
case "y":
int stepCount = 0;
while(stepCount < steps.size() && showStep(doku, listSymbols, width, height, steps.get(stepCount))){stepCount++;}
break;
default:
break;
}
}
/**
* 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').");
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()) {
@@ -440,19 +281,9 @@ 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);
@@ -461,8 +292,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)) {
@@ -473,49 +304,22 @@ 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) {
Cell cell = doku.getCell(x, y);
return ((cell != null) && cell.isMutable());
if (doku.getCell(x, y) != null) {
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) {
for (int i = 0; i < nbSymbols; i++) {
if (listSymbols.get(i).equals(symbol)) {
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) {
for (int i = 0; i < size; i++) {
if (listSymbols.get(i).equals(symbol)) {
@@ -525,9 +329,6 @@ 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);

View File

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

View File

@@ -1,13 +1,13 @@
package sudoku.io;
import java.util.List;
import gui.RenderableMultidoku;
import gui.constants.Symbols;
import sudoku.structure.Cell;
import sudoku.structure.MultiDoku;
import sudoku.structure.Sudoku;
import java.util.List;
public class SudokuPrinter {
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_RED = "\u001B[31m";
@@ -47,7 +47,7 @@ public class SudokuPrinter {
List<String> listSymbols) {
StringBuilder header = new StringBuilder("");
header.append(" ");
for (int x = 0; x < blockWidth*blockHeight; x++) {
for (int x = 0; x < blockWidth * blockHeight; x++) {
header.append(x + 1).append(" ");
if (x % blockWidth == blockWidth - 1 && x != blockWidth * blockHeight - 1) {
header.append(" ");
@@ -77,11 +77,12 @@ public class SudokuPrinter {
}
}
public static void printMultiDoku(final RenderableMultidoku rm, Symbols symbols, int blockWidth, int blockHeight) {
printMultiDoku(rm, symbols.getSymbols(), blockWidth, blockHeight);
public static String printMultiDoku(final RenderableMultidoku rm, Symbols symbols, int blockWidth,
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) {
StringBuilder line = new StringBuilder("\n");
int nBlockInWidth = rm.getWidth() / blockWidth;
@@ -108,7 +109,8 @@ public class SudokuPrinter {
line.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,
@@ -127,7 +129,7 @@ public class SudokuPrinter {
if (y % blockHeight == 0) {
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++) {
if (x % blockWidth == 0 && x > 0) {
line.append("| ");
@@ -182,12 +184,16 @@ public class SudokuPrinter {
return result.toString();
}
public static void printMultiDoku(final MultiDoku doku, int blockWidth, int blockHeight, Symbols symbols) {
if (doku.getNbSubGrids() == 1) {
printRectangleSudoku(doku.getSubGrid(0), blockWidth, blockHeight, symbols);
} else {
printMultiDoku(RenderableMultidoku.fromMultidoku(doku), symbols, blockWidth, blockHeight);
}
public static String printMultiDoku(final MultiDoku doku) {
int blockWidth = doku.getSubGrid(0).getBlockWidth();
if (blockWidth == 0)
return printMultiDoku(doku, 0, 0, Symbols.Numbers);
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) {

View 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;
}

View File

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

View File

@@ -9,17 +9,11 @@ import java.util.Random;
import sudoku.structure.Cell;
import sudoku.structure.MultiDoku;
//TODO
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) {
doku.getStateManager().pushState();
doku.clearMutableCells();

View File

@@ -1,5 +1,6 @@
package sudoku.structure;
import sudoku.io.SudokuPrinter;
import sudoku.io.SudokuSerializer;
import java.util.ArrayList;
@@ -182,7 +183,7 @@ public class MultiDoku {
if (sudoku.getSize() != otherSudoku.getSize())
return false;
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;
}
}
@@ -191,16 +192,11 @@ public class MultiDoku {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Multidoku {");
for (Sudoku sudoku : subGrids) {
sb.append("\n\t").append(sudoku.toString());
}
sb.append("\n}");
return sb.toString();
return SudokuPrinter.printMultiDoku(this);
}
public MultiDoku clone() {
// TODO: C'est pas dingue de le faire comme ça...
return SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(this));
}

View File

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

View File

@@ -1,19 +1,20 @@
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 {
/**
@@ -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 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>
* @return List<IConstraints>
*/

View File

@@ -42,13 +42,13 @@ class SolverTest {
ns, 0, 1, ns);
assertTrue(test.setImmutableCellsSymbol(immutableCells));
List<Integer> correctCells = List.of(
1, 2, 3, 0,
0, 3, 2, 1,
2, 1, 0, 3,
3, 0, 1, 2);
assertTrue(result.setCellsSymbol(correctCells));
assertTrue(result.isSolved());
1, 2, 3, 0,
0, 3, 2, 1,
2, 1, 0, 3,
3, 0, 1, 2);
assertTrue(result.setCellsSymbol(correctCells));
assertTrue(result.isSolved());
assertNotEquals(mdResult, mdTest);
solver.solve(mdTest);
assertTrue(mdTest.isSolved());
@@ -101,24 +101,32 @@ class SolverTest {
MultiDoku mdResult = SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(mdTest));
assertFalse(mdTest.isSolved());
assertFalse(mdResult.isSolved());
assertEquals(mdTest, mdResult);
assertTrue(solver.solve(mdTest));
assertTrue(mdTest.isSolved());
assertFalse(mdResult.isSolved());
System.out.println(mdTest);
System.out.println(mdResult);
assertNotEquals(mdTest, mdResult);
solver.solve(mdResult);
assertTrue(solver.solve(mdResult));
assertEquals(mdTest, mdResult);
}
@Test
void solveTest() {
initializeSolvers();
testSize2(h);
testSize3(h);
testSize2(m);
testSize3(m);
testMDSize3(m);
testSize2(r);
testSize3(r);
testMDSize3(r);
for (int i = 0; i < 100; i++) {
testSize2(h);
testSize3(h);
testSize2(m);
testSize3(m);
testMDSize3(m);
testSize2(r);
testSize3(r);
testMDSize3(r);
}
}
}