17 Commits

Author SHA1 Message Date
a060cde5b2 feat: implement main menu for Sudoku and Multidoku selection (basic for multidoku) 2025-02-09 10:09:32 +01:00
a3e60c1c93 refactor: improve code comments and app menu 2025-02-09 09:45:45 +01:00
09776489fa refactor: remove unused classes and methods, improve code clarity 2025-02-09 08:52:30 +01:00
436967da03 feat: add tests for Sudoku with emojis, letters, and strings 2025-02-08 19:09:44 +01:00
0f72113d67 feat: add error handling for unsolvable Sudoku and rename test class for clarity 2025-02-08 18:44:59 +01:00
690be279bd feat: add tests for multidoku functionality 2025-02-08 18:35:54 +01:00
b836670cda refactor: fix warnings 2025-02-08 17:39:20 +01:00
f002667e54 feat: better shared contraints and backtrace working ! 2025-02-08 17:34:46 +01:00
bf9bfc8323 feat: implement multidoku resolution with shared constraints (without case propagation) 2025-02-07 18:25:02 +01:00
dd21b9a13a fix: better padding for console rendering 2025-02-07 17:56:27 +01:00
a36960c57a feat: colors in multidokus ! 2025-02-07 10:16:34 +01:00
1c1da09e1f feat: multidoku rendu seems to work 2025-02-07 10:19:42 +01:00
7e068d0910 feat: one different color for every bloc 2025-02-06 18:32:01 +01:00
909219a8f8 feat: update test of app 2025-02-06 18:28:36 +01:00
1ad9c0500e feat: rename test names 2025-02-06 18:10:35 +01:00
b0b4152ec4 feat: enhance menu, colors, sudoku, all is working, now multidoku ^^ 2025-02-06 17:54:17 +01:00
7bc99258ec Squashed commit of the following:
commit 1791846882ffc30b5fb052b1619093a5a74a409c
Author: ROGER <gwendal.roger@etu.univ-lyon1.fr>
Date:   Mon Feb 3 10:23:53 2025 +0100

    03/02/2025 : Ajout d'un mecanisme pour la création de bloc mannuels

commit 39892a169561e4827a9e14fdb52cdf870ef30013
Author: ROGER <gwendal.roger@etu.univ-lyon1.fr>
Date:   Mon Feb 3 10:21:06 2025 +0100

    03/02/2025 : Ajout d'un mecanisme pour la création de bloc mannuels

commit ec5101a213c6a5fc24997eab3375def7595a637a
Author: ROGER <gwendal.roger@etu.univ-lyon1.fr>
Date:   Thu Jan 30 10:57:46 2025 +0100

    30/01/2025 : Ajout d'un menu utilisateur + ajout de système de vérification des saisies

commit 8d77fd725f65ba946c46b344186aa56d53448b1a
Author: ROGER <gwendal.roger@etu.univ-lyon1.fr>
Date:   Mon Jan 20 13:01:12 2025 +0100

    20/01/2025 : Ajout de test + création d'un menu utilisateur

commit af2dcfe2c46eefa0e49e3bf88ea51327690c660d
Author: Morph01 <thibaut6969delastreet@gmail.com>
Date:   Mon Jan 20 12:57:32 2025 +0100

    Squashed commit of the following:

    commit 3586ae4c15
    Author: Morph01 <thibaut6969delastreet@gmail.com>
    Date:   Mon Jan 20 12:53:23 2025 +0100

        Squashed commit of the following:

        commit 862ff6e08d
        Author: Morph01 <thibaut6969delastreet@gmail.com>
        Date:   Mon Jan 20 12:49:38 2025 +0100

            feat: affichage de la grille colorée et vérification de tt les contraintes

        commit b1dde68ec1
        Author: Morph01 <thibaut6969delastreet@gmail.com>
        Date:   Sun Jan 5 11:16:34 2025 +0100

            feat: add constraint management and validation to Sudoku class

        commit d8486a3bd7
        Author: Morph01 <thibaut6969delastreet@gmail.com>
        Date:   Sun Jan 5 11:13:19 2025 +0100

            feat: implement constraint classes for Sudoku (row, column, block)

        commit 8945072074
        Author: Morph01 <thibaut6969delastreet@gmail.com>
        Date:   Sun Jan 5 11:12:59 2025 +0100

            refactor: rename methods to French for consistency in the Sudoku application

        commit e35123e9fe
        Author: Morph01 <thibaut6969delastreet@gmail.com>
        Date:   Sun Jan 5 11:11:40 2025 +0100

            fix: update build.gradle to use testRuntimeOnly for JUnit Jupiter engine

    commit 22e88a899f
    Author: Morph01 <thibaut6969delastreet@gmail.com>
    Date:   Sat Jan 4 15:56:23 2025 +0000

        Gwendal in main : add test on futur constraints and enhance input symbol method (#1)

        Co-authored-by: ROGER <gwendal.roger@etu.univ-lyon1.fr>
        Reviewed-on: #1

commit 3e6aa821fc
Author: Morph01 <thibaut6969delastreet@gmail.com>
Date:   Sat Jan 4 16:51:12 2025 +0100

    feat: enhance symbol input validation and improve console messaging

commit 7c4fa8cbe8
Author: Morph01 <thibaut6969delastreet@gmail.com>
Date:   Sat Jan 4 16:49:41 2025 +0100

    chore: update Gradle wrapper to version 8.3

commit c226eeebae
Author: ROGER <gwendal.roger@etu.univ-lyon1.fr>
Date:   Fri Jan 3 22:58:54 2025 +0100

    03/01/2025 : Intégration d'un mécanisme permettant de contrôler que le symbole choisi ne figure pas déjà dans la liste de symboles possibles. Permet d'éviter les doublons

commit b553fd2be9
Author: ROGER <gwendal.roger@etu.univ-lyon1.fr>
Date:   Fri Jan 3 17:12:10 2025 +0100

    03/01/2025 : Création d'un second sudoku avec intégration automatique des symboles

commit a1b130b968
Author: ROGER <gwendal.roger@etu.univ-lyon1.fr>
Date:   Fri Jan 3 16:35:02 2025 +0100

    03/01/2025 : Ajout des tests lignes, colonne et bloc

commit 7c29041ca4
Author: ROGER <gwendal.roger@etu.univ-lyon1.fr>
Date:   Thu Dec 26 20:27:49 2024 +0100

    Création de branche / 1er COMMIT de test
2025-02-03 11:03:11 +01:00
33 changed files with 1978 additions and 464 deletions

3
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

2
.idea/compiler.xml generated
View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="CompilerConfiguration"> <component name="CompilerConfiguration">
<bytecodeTargetLevel target="21" /> <bytecodeTargetLevel target="1.8" />
</component> </component>
</project> </project>

1
.idea/gradle.xml generated
View File

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings"> <component name="GradleSettings">
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>

20
.idea/jarRepositories.xml generated Normal file
View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="MavenRepo" />
<option name="name" value="MavenRepo" />
<option name="url" value="https://repo.maven.apache.org/maven2/" />
</remote-repository>
</component>
</project>

2
.idea/misc.xml generated
View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK" /> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK" />
</project> </project>

124
.idea/uiDesigner.xml generated Normal file
View File

@@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

BIN
Sudoku_menuGrille.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 KiB

View File

@@ -1,8 +1,5 @@
package sudoku; package sudoku;
import java.util.ArrayList;
import java.util.Arrays;
public class App { public class App {
public String getGreeting() { public String getGreeting() {
return "Hello World!"; return "Hello World!";
@@ -11,47 +8,11 @@ public class App {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(new App().getGreeting()); System.out.println(new App().getGreeting());
// Create a new Sudoku try{
Sudoku sudoku = new Sudoku(9); Sudoku.menu();
sudoku.getGrille().setSymbolesPossibles(new ArrayList<>(Arrays.asList( } catch (Exception e){
Symbole.of(1), System.err.println("Une erreur fatale est survenue : " + e.getMessage());
Symbole.of(2), e.printStackTrace();
Symbole.of(3), }
Symbole.of(4),
Symbole.of(5),
Symbole.of(6),
Symbole.of(7),
Symbole.of(8),
Symbole.of(9))));
sudoku.getGrille().setCase(0, 0, Symbole.of(1));
sudoku.getGrille().setCase(6, 1, Symbole.of(2));
sudoku.getGrille().setCase(2, 2, Symbole.of(3));
sudoku.getGrille().setCase(0, 3, Symbole.of(4));
sudoku.getGrille().setCase(4, 4, Symbole.of(5));
sudoku.getGrille().setCase(0, 5, Symbole.of(6));
sudoku.getGrille().setCase(5, 6, Symbole.of(7));
sudoku.getGrille().setCase(0, 7, Symbole.of(8));
sudoku.getGrille().setCase(4, 8, Symbole.of(9));
sudoku.getGrille().creerBlocCarre();
System.out.println("Sudoku :");
System.out.println(sudoku.getGrille().toString());
System.out.println("Blocs :");
sudoku.getGrille().printBlocs();
System.out.println("Symboles possibles :");
sudoku.getGrille().printSymbolesPossibles();
//Création d'un second sudoku
System.out.println("Création d'un second SUDOKU");
Sudoku sudoku2 = new Sudoku(9);
sudoku2.getGrille().askSetSymbolesPossibles();
System.out.println("Symboles possibles :");
sudoku2.getGrille().printSymbolesPossibles();
} }
} }

View File

@@ -18,31 +18,16 @@ public class Bloc {
cases.add(c); cases.add(c);
} }
public boolean contains(Symbole s) {
for (Case c : cases) {
if (c.getSymbole().equals(s)) {
return true;
}
}
return false;
}
public String getCouleur() { public String getCouleur() {
return couleur; return couleur;
} }
public int getCouleurIndex() {
return couleurIndex;
}
public List<Case> getCases() { public List<Case> getCases() {
return cases; return cases;
} }
@Override @Override
public String toString() { public String toString() {
return "Bloc{" + return "Bloc [cases=" + cases + ", couleur=" + couleur + ", couleurIndex=" + couleurIndex + " \u001B[0m]";
"cases=" + cases +
'}';
} }
} }

View File

@@ -2,16 +2,20 @@ package sudoku;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Scanner; import java.util.Scanner;
import sudoku.core.Console; import sudoku.core.Console;
import sudoku.gui.ColorGenerator;
public class Grille { public class Grille {
private final int taille; private final int taille;
private final Case[][] cases; private final Case[][] cases;
private final ArrayList<Bloc> blocs; private final ArrayList<Bloc> blocs;
private ArrayList<Symbole> symbolesPossibles; private ArrayList<Symbole> symbolesPossibles;
private final Sudoku sudoku; private final Sudoku sudoku; // Référence à Sudoku
private Multidoku multidoku; // Référence à Multidoku
private List<String> generatedColors;
public Grille(int taille, Sudoku sudoku) { public Grille(int taille, Sudoku sudoku) {
this.taille = taille; this.taille = taille;
@@ -19,6 +23,7 @@ public class Grille {
this.blocs = new ArrayList<>(); this.blocs = new ArrayList<>();
this.symbolesPossibles = new ArrayList<>(); this.symbolesPossibles = new ArrayList<>();
this.sudoku = sudoku; this.sudoku = sudoku;
initColors();
// Initialiser les cases // Initialiser les cases
for (int i = 0; i < taille; i++) { for (int i = 0; i < taille; i++) {
@@ -28,23 +33,46 @@ public class Grille {
} }
} }
public void setMultidoku(Multidoku multidoku) {
this.multidoku = multidoku;
}
/**
* Génère une palette de couleurs en fonction du nombre de blocs attendus.
* Pour un sudoku classique, la grille est de taille n², et il y a n² blocs,
* on génère donc 'taille' couleurs.
*/
private void initColors() {
// nombre de couleurs = nombre de blocs (pour sudoku classique)
List<ColorGenerator.Color> colors = ColorGenerator.greatScheme(taille);
generatedColors = new ArrayList<>();
for (ColorGenerator.Color color : colors) {
generatedColors.add(convertToAnsi(color));
}
}
private String convertToAnsi(ColorGenerator.Color color) {
int r = Math.round(color.r * 255);
int g = Math.round(color.g * 255);
int b = Math.round(color.b * 255);
return String.format("\u001B[38;2;%d;%d;%dm", r, g, b);
}
public void setCase(int ligne, int colonne, Symbole symbole) { public void setCase(int ligne, int colonne, Symbole symbole) {
try { try {
if (symbole != null && !symbolesPossibles.contains(symbole)) { if (symbole != null && !symbolesPossibles.contains(symbole)) {
throw new IllegalArgumentException("Symbole non autorisé : " + symbole); throw new IllegalArgumentException("Symbole non autorisé : " + symbole);
} }
// Save ancien symbole
Symbole ancienSymbole = cases[ligne][colonne].getSymbole();
// Set nouveau symbole Case currentCase = cases[ligne][colonne];
cases[ligne][colonne].setSymbole(symbole); Symbole ancienSymbole = currentCase.getSymbole();
currentCase.setSymbole(symbole);
// Vérifier les contraintes if (!sudoku.verifierToutesContraintes() ||
if (!sudoku.verifierToutesContraintes()) { (multidoku != null && !multidoku.verifierContraintesPartagees())) {
// Revert to ancien symbole currentCase.setSymbole(ancienSymbole);
cases[ligne][colonne].setSymbole(ancienSymbole); throw new IllegalArgumentException(
throw new IllegalArgumentException("SET CASE: Les contraintes ne sont pas respectées pour la case (" "Les contraintes ne sont pas respectées pour la case (" + ligne + ", " + colonne + ")");
+ ligne + ", " + colonne + ")");
} }
} catch (Exception e) { } catch (Exception e) {
Console.errorln(e.getMessage()); Console.errorln(e.getMessage());
@@ -55,54 +83,12 @@ public class Grille {
return cases[ligne][colonne]; return cases[ligne][colonne];
} }
private static final String[] QUATRE_COULEURS = {
"\u001B[31m", // Rouge
"\u001B[32m", // Vert
"\u001B[34m", // Bleu
"\u001B[33m" // Jaune
};
private int getCouleurDisponible(int blocRow, int blocCol, int blocHeight, int blocWidth) {
List<Integer> couleursUtilisees = new ArrayList<>();
int blocsParLigne = taille / blocWidth;
int blocsParColonne = taille / blocHeight;
// Parcourir les voisins (haut, bas, gauche, droite, et diagonaux)
for (int dRow = -1; dRow <= 1; dRow++) {
for (int dCol = -1; dCol <= 1; dCol++) {
if (dRow == 0 && dCol == 0)
continue; // Ignorer le bloc courant
int voisinRow = blocRow + dRow;
int voisinCol = blocCol + dCol;
// Vérifier si le voisin est dans les limites
if (voisinRow >= 0 && voisinRow < blocsParColonne &&
voisinCol >= 0 && voisinCol < blocsParLigne) {
int blockIndex = voisinRow * blocsParLigne + voisinCol;
if (blockIndex < blocs.size()) {
couleursUtilisees.add(blocs.get(blockIndex).getCouleurIndex());
}
}
}
}
// Trouver une couleur non utilisée
for (int c = 0; c < QUATRE_COULEURS.length; c++) {
if (!couleursUtilisees.contains(c)) {
return c;
}
}
// Retourner une couleur par défaut (ne devrait pas arriver avec 4 couleurs)
return 0;
}
/** /**
* Crée un bloc à partir des positions spécifiées * Crée un bloc personnalisé à partir des positions spécifiées.
* * La couleur du bloc est choisie de façon cyclique dans la palette générée.
* Exemple : *
* sudoku.getGrille().creerBloc(Arrays.asList( * Exemple d'utilisation :
* sudoku.getGrille().creerBlocPersonnalise(Arrays.asList(
* new int[] { 0, 0 }, * new int[] { 0, 0 },
* new int[] { 0, 1 }, * new int[] { 0, 1 },
* new int[] { 0, 2 }, * new int[] { 0, 2 },
@@ -111,10 +97,8 @@ public class Grille {
* new int[] { 1, 2 }, * new int[] { 1, 2 },
* new int[] { 2, 0 }, * new int[] { 2, 0 },
* new int[] { 2, 1 }, * new int[] { 2, 1 },
* new int[] { 2, 2 })); * new int[] { 2, 2 }
* * ));
* @param positions
* @return
*/ */
public void creerBlocPersonnalise(List<int[]> positions) { public void creerBlocPersonnalise(List<int[]> positions) {
try { try {
@@ -125,129 +109,84 @@ public class Grille {
} }
} }
// Collecter les couleurs des blocs voisins // Choisir la couleur suivante en fonction du nombre de blocs déjà créés
List<Integer> couleursVoisines = new ArrayList<>(); setCouleurBloc(positions);
for (int[] pos : positions) {
// Vérifier les cases adjacentes
int[][] directions = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
for (int[] dir : directions) {
int neighborRow = pos[0] + dir[0];
int neighborCol = pos[1] + dir[1];
if (neighborRow >= 0 && neighborRow < taille && neighborCol >= 0 && neighborCol < taille) {
Case neighborCase = cases[neighborRow][neighborCol];
Bloc neighborBloc = findBlocForCase(neighborCase);
if (neighborBloc != null) {
couleursVoisines.add(neighborBloc.getCouleurIndex());
}
}
}
}
// Trouver une couleur disponible
int couleurIndex = -1;
for (int c = 0; c < QUATRE_COULEURS.length; c++) {
if (!couleursVoisines.contains(c)) {
couleurIndex = c;
break;
}
}
if (couleurIndex == -1) {
couleurIndex = 0; // Fallback
}
// Créer et ajouter le bloc
Bloc bloc = new Bloc(QUATRE_COULEURS[couleurIndex], couleurIndex);
for (int[] pos : positions) {
bloc.ajouterCase(cases[pos[0]][pos[1]]);
}
blocs.add(bloc);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
Console.errorln(e.getMessage()); Console.errorln(e.getMessage());
} }
} }
private void setCouleurBloc(List<int[]> positions) {
int couleurIndex = blocs.size() % generatedColors.size();
Bloc bloc = new Bloc(generatedColors.get(couleurIndex), couleurIndex);
for (int[] pos : positions) {
bloc.ajouterCase(cases[pos[0]][pos[1]]);
}
blocs.add(bloc);
}
/**
* Crée des blocs carrés (par exemple pour un sudoku classique).
*/
public void creerBlocCarre() { public void creerBlocCarre() {
try { int blocSize = (int) Math.sqrt(taille);
int blocSize = (int) Math.sqrt(taille); if (blocSize * blocSize != taille) {
if (blocSize * blocSize != taille) { throw new IllegalArgumentException("La taille de la grille doit être un carré parfait.");
throw new IllegalArgumentException("La taille de la grille doit être un carré parfait."); }
}
// Create blocks in 3x3 pattern // Création des blocs en motif (par exemple 3x3 pour un sudoku 9x9)
for (int blocRow = 0; blocRow < blocSize; blocRow++) { for (int blocRow = 0; blocRow < blocSize; blocRow++) {
for (int blocCol = 0; blocCol < blocSize; blocCol++) { for (int blocCol = 0; blocCol < blocSize; blocCol++) {
List<int[]> positions = new ArrayList<>(); List<int[]> positions = new ArrayList<>();
// Add all positions for current block // Ajouter toutes les positions pour le bloc courant
for (int i = 0; i < blocSize; i++) { for (int i = 0; i < blocSize; i++) {
for (int j = 0; j < blocSize; j++) { for (int j = 0; j < blocSize; j++) {
positions.add(new int[] { positions.add(new int[] {
blocRow * blocSize + i, blocRow * blocSize + i,
blocCol * blocSize + j blocCol * blocSize + j
}); });
}
} }
int couleurIndex = getCouleurDisponible(blocRow, blocCol, blocSize, blocSize);
Bloc bloc = new Bloc(QUATRE_COULEURS[couleurIndex], couleurIndex);
for (int[] pos : positions) {
bloc.ajouterCase(cases[pos[0]][pos[1]]);
}
blocs.add(bloc);
} }
setCouleurBloc(positions);
} }
} catch (IllegalArgumentException e) {
Console.errorln(e.getMessage());
} }
} }
/** /**
* Crée des blocs rectangles automatiquement à partir de la taille de la grille * Crée des blocs rectangulaires automatiquement à partir de la taille de la
* Ne fonctionne pas pour les tailles de grilles qui sont des carrés parfaits * grille.
* * Ne fonctionne pas pour les grilles dont la taille est un carré parfait.
* @param blocRow
* @param blocCol
* @param blocHeight
* @param blocWidth
*/ */
public void creerBlocRectangulaire(int blocHeight, int blocWidth) { public void creerBlocRectangulaire(int blocHeight, int blocWidth) {
try { if (taille % blocHeight != 0 || taille % blocWidth != 0) {
if (taille % blocHeight != 0 || taille % blocWidth != 0) { throw new IllegalArgumentException(
throw new IllegalArgumentException( "La taille de la grille doit être divisible par les dimensions des blocs.");
"La taille de la grille doit être divisible par les dimensions des blocs."); }
}
int blocsParLigne = taille / blocWidth; int blocsParLigne = taille / blocWidth;
int blocsParColonne = taille / blocHeight; int blocsParColonne = taille / blocHeight;
// Create blocks in rectangular pattern // Création des blocs en motif rectangulaire
for (int blocRow = 0; blocRow < blocsParColonne; blocRow++) { for (int blocRow = 0; blocRow < blocsParColonne; blocRow++) {
for (int blocCol = 0; blocCol < blocsParLigne; blocCol++) { for (int blocCol = 0; blocCol < blocsParLigne; blocCol++) {
List<int[]> positions = new ArrayList<>(); List<int[]> positions = new ArrayList<>();
// Add all positions for current block // Ajouter toutes les positions pour le bloc courant
for (int i = 0; i < blocHeight; i++) { for (int i = 0; i < blocHeight; i++) {
for (int j = 0; j < blocWidth; j++) { for (int j = 0; j < blocWidth; j++) {
positions.add(new int[] { positions.add(new int[] {
blocRow * blocHeight + i, blocRow * blocHeight + i,
blocCol * blocWidth + j blocCol * blocWidth + j
}); });
}
} }
int couleurIndex = getCouleurDisponible(blocRow, blocCol, blocHeight, blocWidth);
Bloc bloc = new Bloc(QUATRE_COULEURS[couleurIndex], couleurIndex);
for (int[] pos : positions) {
bloc.ajouterCase(cases[pos[0]][pos[1]]);
}
blocs.add(bloc);
} }
setCouleurBloc(positions);
} }
} catch (IllegalArgumentException e) {
Console.errorln(e.getMessage());
} }
} }
@@ -257,40 +196,44 @@ public class Grille {
} }
} }
public void askSetSymbolesPossibles() { public static int choisirTypeSymbole(Scanner scanner) {
Scanner scanner = new Scanner(System.in); while (true) {
try { System.out.println("Choisissez le type de symbole :");
Console.infoln("Choisissez le type de symboles :"); System.out.println("1 : Entiers");
Console.infoln("1. Nombres"); System.out.println("2 : Lettres");
Console.infoln("2. Lettres"); System.out.println("3 : Chaînes de caractères / Emoji");
Console.infoln("3. Texte/Emoji");
int choix = 0; String input = scanner.nextLine();
try { try {
choix = Integer.parseInt(scanner.nextLine()); int choix = Integer.parseInt(input);
if (choix < 1 || choix > 3) { if (choix >= 1 && choix <= 3) {
throw new NumberFormatException("Choix invalide"); return choix;
} else {
System.out.println("Choix invalide. Veuillez entrer 1, 2 ou 3.");
} }
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
Console.errorln("Choix invalide"); System.out.println("Entrée invalide. Veuillez entrer un nombre.");
return;
} }
}
}
public void askSetSymbolesPossibles(int choix, Scanner scanner) {
try {
for (int i = 0; i < taille; i++) { for (int i = 0; i < taille; i++) {
System.out.println(("Entrez le symbole " + (i + 1) + "/" + taille + " :")); System.out.println("Entrez le symbole " + (i + 1) + "/" + taille + " :");
String input = scanner.nextLine(); String input = scanner.nextLine();
switch (choix) { switch (choix) {
case 1: // Nombres case 1: // Nombres
Symbole intTemp = Symbole.of(input); try {
if (intTemp.isInt()) { Symbole intTemp = Symbole.of(Integer.parseInt(input));
if (symbolesPossibles.contains(intTemp)) { if (symbolesPossibles.contains(intTemp)) {
Console.errorln("Ce symbole existe déjà, veuillez entrer un autre symbole"); Console.errorln("Ce symbole existe déjà, veuillez entrer un autre symbole");
i--; i--;
} else { } else {
symbolesPossibles.add(intTemp); symbolesPossibles.add(intTemp);
} }
} else { } catch (NumberFormatException e) {
Console.errorln("Veuillez entrer un nombre valide"); Console.errorln("Veuillez entrer un nombre valide");
i--; i--;
} }
@@ -316,6 +259,9 @@ public class Grille {
if (symbolesPossibles.contains(stringTemp)) { if (symbolesPossibles.contains(stringTemp)) {
Console.errorln("Ce symbole existe déjà, veuillez entrer un autre symbole"); Console.errorln("Ce symbole existe déjà, veuillez entrer un autre symbole");
i--; i--;
} else if (Objects.equals(input, "ESC")) {
Console.errorln("Ce symbole est interdit, veuillez entrer un autre symbole");
i--;
} else { } else {
symbolesPossibles.add(stringTemp); symbolesPossibles.add(stringTemp);
} }
@@ -327,9 +273,7 @@ public class Grille {
} }
} }
} catch (Exception e) { } catch (Exception e) {
System.out.println("Une erreur est survenue : " + e.getMessage()); Console.errorln("Une erreur est survenue : " + e.getMessage());
} finally {
scanner.close();
} }
} }
@@ -346,14 +290,14 @@ public class Grille {
for (Symbole symbole : symbolesPossibles) { for (Symbole symbole : symbolesPossibles) {
sb.append(symbole.toString()).append(" "); sb.append(symbole.toString()).append(" ");
} }
System.out.println(sb.toString()); System.out.println(sb);
} }
public int getTaille() { public int getTaille() {
return taille; return taille;
} }
private Bloc findBlocForCase(Case target) { Bloc findBlocForCase(Case target) {
for (Bloc bloc : blocs) { for (Bloc bloc : blocs) {
if (bloc.getCases().contains(target)) { if (bloc.getCases().contains(target)) {
return bloc; return bloc;
@@ -363,9 +307,9 @@ public class Grille {
} }
/** /**
* Vérifie si toutes les contraintes sont respectées * Vérifie si toutes les contraintes sont respectées.
* S'arrête dès qu'une contrainte n'est pas respectée * S'arrête dès qu'une contrainte n'est pas respectée.
* *
* @param contraintes * @param contraintes
* @return * @return
*/ */
@@ -390,17 +334,6 @@ public class Grille {
return true; return true;
} }
public Case getCaseLibre() {
for (int i = 0; i < taille; i++) {
for (int j = 0; j < taille; j++) {
if (cases[i][j].getSymbole() == null) {
return cases[i][j];
}
}
}
return null;
}
public int getLongueurSymboleLePlusLong() { public int getLongueurSymboleLePlusLong() {
int max = 0; int max = 0;
for (Symbole symbole : symbolesPossibles) { for (Symbole symbole : symbolesPossibles) {
@@ -412,51 +345,35 @@ public class Grille {
} }
@Override @Override
// public String toString() {
// StringBuilder sb = new StringBuilder();
// int plusLongSymbole = this.getLongueurSymboleLePlusLong();
// for (int i = 0; i < taille; i++) {
// for (int j = 0; j < taille; j++) {
// Case currentCase = cases[i][j];
// Bloc bloc = findBlocForCase(currentCase);
// if (bloc != null) {
// sb.append(bloc.getCouleur()) // Couleur du bloc
// .append(" ".repeat(plusLongSymbole - currentCase.toString().length())) //
// Alignement
// .append(currentCase.toString())
// .append("\u001B[0m ") // Réinitialiser la couleur
// .append(" ");
// } else {
// sb.append(currentCase.toString()).append(" ");
// }
// }
// sb.append("\n");
// }
// return sb.toString();
// }
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
int maxLen = getLongueurSymboleLePlusLong(); // longueur maximale des symboles
for (int i = 0; i < taille; i++) { for (int i = 0; i < taille; i++) {
for (int j = 0; j < taille; j++) { for (int j = 0; j < taille; j++) {
Case currentCase = cases[i][j]; Case currentCase = cases[i][j];
String cellStr = currentCase.toString();
// Calculer le nombre d'espaces à ajouter pour atteindre maxLen
int pad = maxLen - cellStr.length();
String padding = " ".repeat(pad);
Bloc bloc = findBlocForCase(currentCase); Bloc bloc = findBlocForCase(currentCase);
if (bloc != null) { if (bloc != null) {
sb.append(bloc.getCouleur()) // Couleur du bloc sb.append(bloc.getCouleur()) // couleur du bloc
.append(currentCase.toString()) // Contenu de la case .append(cellStr) // symbole
.append("\u001B[0m ") // Réinitialiser la couleur .append(padding) // padding pour aligner les symboles
.append("\u001B[0m ") // réinitialiser la couleur
.append(" ");
} else { // pas de bloc, afficher normalement
sb.append(cellStr)
.append(padding)
.append(" "); .append(" ");
} else {
sb.append(currentCase.toString()).append(" ");
} }
} }
sb.append("\n"); sb.append("\n"); // nouvelle ligne
} }
return sb.toString(); return sb.toString();
} }
} }

View File

@@ -0,0 +1,350 @@
package sudoku;
import java.util.*;
public class Multidoku {
private final List<SudokuPlacement> placements = new ArrayList<>();
private final List<ContrainteCasePartagee> contraintesPartagees;
public Multidoku() {
this.contraintesPartagees = new ArrayList<>();
}
public void ajouterSudoku(Sudoku sudoku, int offsetLigne, int offsetColonne) {
placements.add(new SudokuPlacement(sudoku, offsetLigne, offsetColonne));
sudoku.getGrille().setMultidoku(this);
}
public void ajouterCasesPartagees(List<Case> cases) {
ContrainteCasePartagee contrainte = new ContrainteCasePartagee(cases, this);
contraintesPartagees.add(contrainte);
// Vérifier immédiatement la validité après l'ajout
if (!contrainte.estRespectee(null, null)) {
throw new IllegalStateException(
"La contrainte de partage n'est pas respectée après l'ajout des cases partagées");
}
}
public boolean resoudreMultidoku(boolean afficherEtape) {
List<GlobalCell> globalCells = collecterCellulesVides();
return resoudreGlobal(globalCells, 0, afficherEtape);
}
private List<GlobalCell> collecterCellulesVides() {
List<GlobalCell> cells = new ArrayList<>();
// Ajout d'abord des cases partagées
for (ContrainteCasePartagee contrainte : contraintesPartagees) {
for (Case c : contrainte.getCasesLiees()) {
if (c.getSymbole() == null) {
Sudoku s = trouverSudokuPourCase(c);
cells.add(new GlobalCell(s, c.getLigne(), c.getColonne()));
}
}
}
// Ajout ensuite des cases non partagées
for (SudokuPlacement sp : placements) {
Sudoku s = sp.getSudoku();
Grille g = s.getGrille();
int taille = g.getTaille();
for (int i = 0; i < taille; i++) {
for (int j = 0; j < taille; j++) {
Case c = g.getCase(i, j);
if (c.getSymbole() == null && !isSharedCase(c)) {
cells.add(new GlobalCell(s, i, j));
}
}
}
}
return cells;
}
private boolean resoudreGlobal(List<GlobalCell> globalCells, int index, boolean afficherEtape) {
if (index == globalCells.size()) {
return true;
}
GlobalCell gc = globalCells.get(index);
Sudoku s = gc.sudoku;
Grille g = s.getGrille();
Case c = g.getCase(gc.ligne, gc.colonne);
List<Symbole> symboles = g.getSymbolesPossibles();
Symbole symboleOriginal = c.getSymbole();
Map<Case, Symbole> sauvegarde = new HashMap<>();
sauvegarde.put(c, symboleOriginal);
for (Symbole symbole : symboles) {
// Affichage AVANT la tentative d'assignation
if (afficherEtape) {
System.out.println("\nTentative symbole " + symbole + " sur " + gc.sudoku.getNom() + " [" + gc.ligne
+ "," + gc.colonne + "]");
System.out.println(this.toStringCombined()); // Afficher l'état actuel
}
boolean ok;
if (isSharedCase(c)) { // Si la case est partagée, on doit propager la valeur dans les cases
// correspondantes des autres sudokus concernés
ok = setCaseAvecPropagation(gc.ligne, gc.colonne, symbole, s);
} else {
c.setSymbole(symbole);
ok = s.estValide(c);
if (!ok) {
c.setSymbole(null);
}
}
if (ok) {
if (afficherEtape) {
System.out.println("-> Affectation réussie pour " + symbole);
System.out.println(this.toStringCombined());
}
if (globalValide() && resoudreGlobal(globalCells, index + 1, afficherEtape)) {
return true;
}
}
// Backtracking : restauration de l'état sauvegardé
restaurerCases(sauvegarde);
if (afficherEtape) {
System.out.println("Backtracking sur " + gc.sudoku.getNom() + " [" + gc.ligne + "," + gc.colonne + "]");
System.out.println(this.toStringCombined());
}
}
return false;
}
private void restaurerCases(Map<Case, Symbole> sauvegarde) {
for (Map.Entry<Case, Symbole> entry : sauvegarde.entrySet()) {
entry.getKey().setSymbole(entry.getValue());
}
}
private boolean setCaseAvecPropagation(int ligne, int colonne, Symbole symbole, Sudoku s) {
Grille g = s.getGrille();
Case c = g.getCase(ligne, colonne);
Map<Case, Symbole> sauvegarde = new HashMap<>();
// Sauvegarde et définition de la case actuelle
sauvegarde.put(c, c.getSymbole());
c.setSymbole(symbole);
if (!s.estValide(c)) {
restaurerCases(sauvegarde);
return false;
}
// Propagation à la case correspondante dans les autres sudokus concernés
for (ContrainteCasePartagee contrainte : contraintesPartagees) {
if (contrainte.getCasesLiees().contains(c)) {
Case caseCorrespondante = contrainte.getCaseCorrespondante(c);
if (caseCorrespondante != null) {
sauvegarde.put(caseCorrespondante, caseCorrespondante.getSymbole());
caseCorrespondante.setSymbole(symbole);
Sudoku autreSudoku = trouverSudokuPourCase(caseCorrespondante);
if (autreSudoku != null && !autreSudoku.estValide(caseCorrespondante)) {
restaurerCases(sauvegarde);
return false;
}
}
}
}
return true;
}
private Sudoku trouverSudokuPourCase(Case c) {
for (SudokuPlacement sp : placements) {
if (belongsToSudoku(c, sp.getSudoku())) {
return sp.getSudoku();
}
}
return null;
}
private boolean belongsToSudoku(Case c, Sudoku s) {
Grille g = s.getGrille();
int taille = g.getTaille();
for (int i = 0; i < taille; i++) {
for (int j = 0; j < taille; j++) {
if (g.getCase(i, j) == c) {
return true;
}
}
}
return false;
}
private boolean globalValide() {
for (SudokuPlacement sp : placements) {
if (!sp.getSudoku().estValide())
return false;
}
for (Contrainte c : contraintesPartagees) {
if (!c.estRespectee(null, null))
return false;
}
return true;
}
public boolean verifierContraintesPartagees() {
return contraintesPartagees.stream()
.allMatch(c -> c.estRespectee(null, null));
}
public boolean isSharedCase(Case c) {
for (ContrainteCasePartagee contrainte : contraintesPartagees) {
if (contrainte.getCasesLiees().contains(c)) {
return true;
}
}
return false;
}
// Classe utilitaire pour représenter une case vide dans un sudoku particulier
public static class GlobalCell {
public Sudoku sudoku;
public int ligne;
public int colonne;
public GlobalCell(Sudoku sudoku, int ligne, int colonne) {
this.sudoku = sudoku;
this.ligne = ligne;
this.colonne = colonne;
}
}
// Classe interne pour la contrainte de cases partagées
public static class ContrainteCasePartagee implements Contrainte {
private final Map<Case, Case> casesLiees; // Map associant chaque case à sa correspondante
private final Set<Sudoku> sudokusLies;
public ContrainteCasePartagee(List<Case> cases, Multidoku multidoku) {
this.casesLiees = new HashMap<>();
this.sudokusLies = new HashSet<>();
// Les cases sont données dans l'ordre : d'abord celles du premier sudoku,
// puis celles du second dans le même ordre.
int size = cases.size() / 2;
for (int i = 0; i < size; i++) {
Case c1 = cases.get(i);
Case c2 = cases.get(i + size);
casesLiees.put(c1, c2);
casesLiees.put(c2, c1);
Sudoku s1 = multidoku.trouverSudokuPourCase(c1);
Sudoku s2 = multidoku.trouverSudokuPourCase(c2);
if (s1 != null)
sudokusLies.add(s1);
if (s2 != null)
sudokusLies.add(s2);
}
// Propager les valeurs initiales
propagerValeursInitiales();
}
private void propagerValeursInitiales() {
for (Map.Entry<Case, Case> entry : casesLiees.entrySet()) {
Case c1 = entry.getKey();
Case c2 = entry.getValue();
if (c1.getSymbole() != null && c2.getSymbole() == null) {
c2.setSymbole(c1.getSymbole());
} else if (c2.getSymbole() != null && c1.getSymbole() == null) {
c1.setSymbole(c2.getSymbole());
}
}
}
@Override
public boolean estRespectee(Grille grille, Case caseActuelle) {
for (Map.Entry<Case, Case> entry : casesLiees.entrySet()) {
Case c1 = entry.getKey();
Case c2 = entry.getValue();
if (c1.getSymbole() != null && c2.getSymbole() != null
&& !c1.getSymbole().equals(c2.getSymbole())) {
return false;
}
}
return true;
}
public Case getCaseCorrespondante(Case c) {
return casesLiees.get(c);
}
public Set<Case> getCasesLiees() {
return casesLiees.keySet();
}
}
public String toStringCombined() {
// 1. Déterminer la taille globale de la grille combinée et la largeur maximale
int maxLigne = 0, maxColonne = 0;
int globalMaxLen = 0;
for (SudokuPlacement sp : placements) {
int taille = sp.getSudoku().getGrille().getTaille();
maxLigne = Math.max(maxLigne, sp.getOffsetLigne() + taille);
maxColonne = Math.max(maxColonne, sp.getOffsetColonne() + taille);
globalMaxLen = Math.max(globalMaxLen, sp.getSudoku().getGrille().getLongueurSymboleLePlusLong());
}
// On ajoute un espace supplémentaire pour séparer les colonnes
int cellWidth = globalMaxLen + 1;
// 2. Création et initialisation de la matrice globale
String[][] global = new String[maxLigne][maxColonne];
for (int i = 0; i < maxLigne; i++) {
for (int j = 0; j < maxColonne; j++) {
global[i][j] = " ".repeat(cellWidth); // case vide affichée par défaut
}
}
// 3. Pour chaque sudoku, placer l'affichage de chacune de ses cases dans la
// grille globale
for (SudokuPlacement sp : placements) {
Grille grille = sp.getSudoku().getGrille();
int taille = grille.getTaille();
for (int i = 0; i < taille; i++) {
for (int j = 0; j < taille; j++) {
// Coordonnées globales calculées à partir de l'offset
int globalLigne = sp.getOffsetLigne() + i;
int globalColonne = sp.getOffsetColonne() + j;
Case currentCase = grille.getCase(i, j);
String cellStr = currentCase.toString();
int pad = globalMaxLen - cellStr.length();
String padding = " ".repeat(pad);
// Récupérer le bloc associé à la case afin d'obtenir sa couleur
Bloc bloc = grille.findBlocForCase(currentCase);
String cellDisplay;
// Si la case est partagée, on force l'affichage en blanc
if (isSharedCase(currentCase)) {
cellDisplay = "\u001B[37m" + cellStr + padding + "\u001B[0m";
} else {
if (bloc != null) {
cellDisplay = bloc.getCouleur() + cellStr + padding + "\u001B[0m";
} else {
cellDisplay = cellStr + padding;
}
}
// Insertion dans la matrice globale (ajout d'un espace pour séparer les
// colonnes)
global[globalLigne][globalColonne] = cellDisplay + " ";
}
}
}
// 4. Construction de la chaîne d'affichage finale
StringBuilder sb = new StringBuilder();
for (int i = 0; i < maxLigne; i++) {
for (int j = 0; j < maxColonne; j++) {
sb.append(global[i][j]);
}
sb.append("\n");
}
return sb.toString();
}
}

View File

@@ -2,18 +2,18 @@ package sudoku;
import java.util.List; import java.util.List;
import sudoku.core.Console;
public class ResolveurBacktraceSimple implements Resolveur { public class ResolveurBacktraceSimple implements Resolveur {
private final Sudoku sudoku;
public ResolveurBacktraceSimple(Sudoku sudoku) {
this.sudoku = sudoku;
}
@Override @Override
public boolean resoudre(Sudoku s, boolean afficherEtape) { public boolean resoudre(Sudoku s, boolean afficherEtape) {
EtatResolution etat = new EtatResolution(0, 0); EtatResolution etat = new EtatResolution(0, 0);
return resoudre(s, afficherEtape, etat); boolean solved = resoudre(s, afficherEtape, etat);
if (!solved) {
Console.errorln("Ce Sudoku n'a pas de solution");
}
return solved;
} }
private boolean resoudre(Sudoku s, boolean afficherEtape, EtatResolution etat) { private boolean resoudre(Sudoku s, boolean afficherEtape, EtatResolution etat) {

View File

@@ -1,25 +1,36 @@
package sudoku; package sudoku;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Scanner;
import java.util.Set;
import sudoku.core.Console;
public class Sudoku { public class Sudoku {
private final Grille grille; private final Grille grille;
private final List<Contrainte> contraintes; private final List<Contrainte> contraintes;
private String nom;
public Sudoku(int taille) { public Sudoku(int taille) {
this.grille = new Grille(taille, this); this.grille = new Grille(taille, this);
this.contraintes = new ArrayList<>(); this.contraintes = new ArrayList<>();
} }
public void setNom(String nom) {
this.nom = nom;
}
public String getNom() {
return this.nom;
}
public void ajouterContrainte(Contrainte contrainte) { public void ajouterContrainte(Contrainte contrainte) {
contraintes.add(contrainte); contraintes.add(contrainte);
} }
// public void creerBloc(List<int[]> positions, int blocHeight, int blocWidth) {
// grille.creerBloc(positions, blocHeight, blocWidth);
// }
public boolean estValide(Case c) { public boolean estValide(Case c) {
for (Contrainte contrainte : contraintes) { for (Contrainte contrainte : contraintes) {
if (!contrainte.estRespectee(grille, c)) { if (!contrainte.estRespectee(grille, c)) {
@@ -29,11 +40,498 @@ public class Sudoku {
return true; return true;
} }
public boolean estValide() {
for (int i = 0; i < this.getGrille().getTaille(); i++) {
for (int j = 0; j < this.getGrille().getTaille(); j++) {
Case c = this.getGrille().getCase(i, j);
if (c.getSymbole() != null && !this.estValide(c)) {
return false;
}
}
}
return true;
}
public boolean verifierToutesContraintes() { public boolean verifierToutesContraintes() {
return grille.verifierToutesContraintes(contraintes); return grille.verifierToutesContraintes(contraintes);
} }
public Grille getGrille() { public Grille getGrille() {
return grille; return grille;
} }
public static void menu() {
Scanner scanner = new Scanner(System.in);
System.out.println("BIENVENUE DANS LE JEU DE SUDOKU");
while (true) {
System.out.println("\nChoisissez le type de jeu :");
System.out.println("1 : Sudoku classique");
System.out.println("2 : Multidoku (prérempli)");
System.out.println("0 : Quitter");
try {
int choix = Integer.parseInt(scanner.nextLine());
switch (choix) {
case 0:
System.out.println("Au revoir !");
return;
case 1:
menuGrille();
break;
case 2:
menuMultidoku();
break;
default:
System.out.println("Choix invalide. Veuillez réessayer.");
}
} catch (NumberFormatException e) {
System.out.println("Entrée invalide. Veuillez entrer un nombre.");
}
}
}
private static void menuMultidoku() {
final int PAUSE_TIME = 3000; // 3 seconds
System.out.println("MULTIDOKU");
System.out.println("\nChoisissez un type de Multidoku :");
System.out.println("1 : Multidoku avec cases partagées");
System.out.println("2 : Multidoku avec bloc partagé");
System.out.println("3 : Multidoku avec ligne partagée");
System.out.println("4 : Multidoku avec colonne partagée");
Scanner scanner = new Scanner(System.in);
try {
int choix = Integer.parseInt(scanner.nextLine());
Multidoku multidoku = creerMultidoku(choix);
if (multidoku != null) {
System.out.println("\nMultidoku initial :");
System.out.println(multidoku.toStringCombined());
System.out.println("\nDébut de la résolution dans 3 secondes...");
try {
Thread.sleep(PAUSE_TIME);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("\nRésolution du Multidoku...");
if (multidoku.resoudreMultidoku(true)) {
System.out.println("\nMultidoku résolu :");
System.out.println(multidoku.toStringCombined());
} else {
System.out.println("Ce Multidoku n'a pas de solution.");
}
}
} catch (NumberFormatException e) {
System.out.println("Entrée invalide. Veuillez entrer un nombre.");
}
}
private static Multidoku creerMultidoku(int type) {
Multidoku multidoku = new Multidoku();
Sudoku s1 = new Sudoku(9);
Sudoku s2 = new Sudoku(9);
s1.setNom("Sudoku 1");
s2.setNom("Sudoku 2");
// Configuration commune
ArrayList<Symbole> symboles = new ArrayList<>();
for (int i = 1; i <= 9; i++) {
symboles.add(Symbole.of(i));
}
for (Sudoku s : Arrays.asList(s1, s2)) {
s.getGrille().setSymbolesPossibles(symboles);
s.ajouterContrainte(new ContrainteLigne());
s.ajouterContrainte(new ContrainteColonne());
s.ajouterContrainte(new ContrainteBloc());
s.getGrille().creerBlocCarre();
}
switch (type) {
case 1: // Cases partagées
multidoku.ajouterSudoku(s1, 0, 0);
multidoku.ajouterSudoku(s2, 8, 8);
multidoku.ajouterCasesPartagees(Arrays.asList(
s1.getGrille().getCase(8, 8),
s2.getGrille().getCase(0, 0)));
break;
case 2: // Bloc partagé
multidoku.ajouterSudoku(s1, 0, 0);
multidoku.ajouterSudoku(s2, 6, 6);
List<Case> casesBloc = new ArrayList<>();
for (int i = 6; i < 9; i++) {
for (int j = 6; j < 9; j++) {
casesBloc.add(s1.getGrille().getCase(i, j));
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
casesBloc.add(s2.getGrille().getCase(i, j));
}
}
multidoku.ajouterCasesPartagees(casesBloc);
break;
case 3: // Ligne partagée
multidoku.ajouterSudoku(s1, 0, 0);
multidoku.ajouterSudoku(s2, 8, 1);
multidoku.ajouterCasesPartagees(Arrays.asList(s1.getGrille().getCase(8, 1),
s1.getGrille().getCase(8, 2),
s1.getGrille().getCase(8, 3),
s1.getGrille().getCase(8, 4),
s1.getGrille().getCase(8, 5),
s1.getGrille().getCase(8, 6),
s1.getGrille().getCase(8, 7),
s1.getGrille().getCase(8, 8),
s2.getGrille().getCase(0, 0),
s2.getGrille().getCase(0, 1),
s2.getGrille().getCase(0, 2),
s2.getGrille().getCase(0, 3),
s2.getGrille().getCase(0, 4),
s2.getGrille().getCase(0, 5),
s2.getGrille().getCase(0, 6),
s2.getGrille().getCase(0, 7)));
break;
case 4: // Colonne partagée
multidoku.ajouterSudoku(s1, 0, 0);
multidoku.ajouterSudoku(s2, 1, 8);
multidoku.ajouterCasesPartagees(Arrays.asList(
s1.getGrille().getCase(1, 8),
s1.getGrille().getCase(2, 8),
s1.getGrille().getCase(3, 8),
s1.getGrille().getCase(4, 8),
s1.getGrille().getCase(5, 8),
s1.getGrille().getCase(6, 8),
s1.getGrille().getCase(7, 8),
s1.getGrille().getCase(8, 8),
s2.getGrille().getCase(0, 0),
s2.getGrille().getCase(1, 0),
s2.getGrille().getCase(2, 0),
s2.getGrille().getCase(3, 0),
s2.getGrille().getCase(4, 0),
s2.getGrille().getCase(5, 0),
s2.getGrille().getCase(6, 0),
s2.getGrille().getCase(7, 0)));
break;
default:
System.out.println("Type de Multidoku non valide");
return null;
}
return multidoku;
}
public static void menuGrille() {
Scanner scanner = new Scanner(System.in);
// ETAPE 1 : TAILLE GRILLE
System.out.println("ETAPE 1 : Choisir la taille de la grille");
int tailleGrille = setTailleGrille(scanner); // récupère la taille de la grille
Sudoku sudoku = new Sudoku(tailleGrille);
// ETAPE 2 : SYMBOLE POSSIBLE
System.out.println("ETAPE 2 : Choisir les symboles possibles");
int typeSymbole = Grille.choisirTypeSymbole(scanner);
sudoku.getGrille().askSetSymbolesPossibles(typeSymbole, scanner); // demande à l'utilisateur de saisir ses
// symboles
// ETAPE 3 : REMPLIR LA GRILLE
System.out.println("ETAPE 3 : Remplir la grille");
setValeursGrille(sudoku, scanner, tailleGrille, typeSymbole);
System.out.println("Voici votre sudoku rempli :");
System.out.println(sudoku.getGrille().toString());
// ETAPE 4 : CHOIX DES CONTRAINTES
choixContraintes(sudoku, scanner);
// ETAPE 5 : RESOLUTION
System.out.println("ETAPE 5 : Résolution du sudoku");
choixResolution(sudoku, scanner);
}
private static void choixContraintes(Sudoku sudoku, Scanner scanner) {
System.out.println("Choisissez une combinaison entre 0 et 3 contraintes parmi les suivantes :");
System.out.println("1: Contrainte Bloc, 2: Contrainte Ligne, 3: Contrainte Colonne");
System.out.println(
"Entrez les numéros des contraintes séparés par des virgules (par exemple, 1,2,3) ou appuyez sur Entrée pour aucune contrainte :");
String input = scanner.nextLine();
if (input.trim().isEmpty()) {
System.out.println("Aucune contrainte sélectionnée.");
return;
}
String[] choix = input.split(",");
Set<Integer> contraintesChoisies = new HashSet<>();
for (String choixStr : choix) {
try {
int choixInt = Integer.parseInt(choixStr.trim());
if (choixInt >= 1 && choixInt <= 3) {
contraintesChoisies.add(choixInt);
} else {
System.out.println("Choix invalide : " + choixInt);
}
} catch (NumberFormatException e) {
System.out.println("Entrée invalide : " + choixStr);
}
}
for (int choixContrainte : contraintesChoisies) {
switch (choixContrainte) {
case 1:
sudoku.ajouterContrainte(new ContrainteBloc());
System.out.println("Contrainte Bloc ajoutée.");
break;
case 2:
sudoku.ajouterContrainte(new ContrainteLigne());
System.out.println("Contrainte Ligne ajoutée.");
break;
case 3:
sudoku.ajouterContrainte(new ContrainteColonne());
System.out.println("Contrainte Colonne ajoutée.");
break;
default:
System.out.println("Choix invalide : " + choixContrainte);
}
}
}
private void resolutionSudoku(Sudoku sudoku, boolean afficherEtape) {
ResolveurBacktraceSimple resolveur = new ResolveurBacktraceSimple();
resolveur.resoudre(sudoku, afficherEtape);
System.out.println("Sudoku résolu :");
System.out.println(sudoku.getGrille().toString());
}
private static void choixResolution(Sudoku sudoku, Scanner scanner) {
System.out.println("Voulez-vous résoudre le sudoku ? (O/N)");
String choix = scanner.nextLine();
if (choix.equalsIgnoreCase("O")) {
System.out.println("Voulez-vous afficher les étapes de la résolution ? (O/N)");
String choixAffichageEtapes = scanner.nextLine();
if (choixAffichageEtapes.equalsIgnoreCase("O")) {
sudoku.resolutionSudoku(sudoku, true);
} else if (choixAffichageEtapes.equalsIgnoreCase("N")) {
sudoku.resolutionSudoku(sudoku, false);
} else {
System.err.println("Choix invalide.");
}
} else if (choix.equalsIgnoreCase("N")) {
System.err.println("Fin du jeu.");
} else {
System.out.println("Choix invalide.");
choixResolution(sudoku, scanner);
}
}
private static int setTailleGrille(Scanner scanner) {
int tailleGrille = -1;
while (tailleGrille <= 0) {
try {
tailleGrille = Integer.parseInt(scanner.nextLine());
if (tailleGrille <= 0) {
System.out.println("Erreur : Veuillez entrer un entier positif.");
}
} catch (NumberFormatException e) {
System.out.println("Erreur : Entrée invalide. Veuillez entrer un nombre.");
}
}
return tailleGrille;
}
private static void setValeursGrille(Sudoku sudoku, Scanner scanner, int tailleGrille, int typeSymbole) {
while (true) {
try {
System.out.println("Veuillez faire votre choix");
System.out.println("1 : Entrer les blocs manuellement");
System.out.println("2 : Entrer les blocs à l'aide de la longueur et de la largeur");
System.out.println("3 : Générer des blocs carrés");
int choixGenerationBloc = Integer.parseInt(scanner.nextLine());
switch (choixGenerationBloc) {
case 1:
System.out.println("Entrez les blocs manuellement.");
creationBlocManuel(sudoku, scanner, tailleGrille);
break;
case 2:
try {
System.out.println("Entrez les blocs à l'aide de la longueur et de la largeur.");
System.out.println("Entrez la longueur du bloc : ");
int longueurBloc = Integer.parseInt(scanner.nextLine());
System.out.println("Entrez la largeur du bloc :");
int largeurBloc = Integer.parseInt(scanner.nextLine());
sudoku.getGrille().creerBlocRectangulaire(longueurBloc, largeurBloc);
break;
} catch (IllegalArgumentException e) {
Console.errorln("Erreur : " + e.getMessage());
continue;
}
case 3:
try {
sudoku.getGrille().creerBlocCarre();
break;
} catch (IllegalArgumentException e) {
Console.errorln("Erreur : " + e.getMessage());
continue;
}
default:
Console.errorln("Choix invalide.");
continue;
}
break; // Sort de la boucle si la création des blocs a réussi
} catch (NumberFormatException e) {
Console.errorln("Erreur : Veuillez entrer un nombre valide.");
}
}
// DEBUT DU REMPLISSAGE DU SUDOKU
String input;
while (true) {
// Demander et vérifier la ligne
int ligne = -1;
while (true) {
System.out.println("Pour arrêter la saisie, tapez \"esc\".");
System.out.println(
"Entrez le numéro de ligne :");
input = scanner.nextLine(); // Lire la ligne
if (input.equalsIgnoreCase("ESC")) {
break; // Sortie de la boucle si l'utilisateur tape ESC
}
if (input.isEmpty()) {
System.out.println("Veuillez entrer un numéro de ligne valide.");
continue; // Recommencer la saisie de la ligne si l'entrée est vide
}
try {
ligne = Integer.parseInt(input); // Convertir la ligne en entier
break; // Sortir de la boucle si la ligne est valide
} catch (NumberFormatException e) {
System.out.println("Veuillez entrer un numéro de ligne valide (un nombre entier).");
}
}
// ARRET DE LA BOUCLE SI SAISIE DE "ESC"
if (input.equalsIgnoreCase("ESC")) {
break; // Sortie de la boucle principale si l'utilisateur tape ESC
}
// Demander et vérifier la colonne
int colonne;
while (true) {
System.out.println("Entrez le numéro de colonne :");
input = scanner.nextLine(); // Lire la colonne
if (input.isEmpty()) {
System.out.println("Veuillez entrer un numéro de colonne valide.");
continue; // Recommencer la saisie de la colonne si l'entrée est vide
}
try {
colonne = Integer.parseInt(input); // Convertir la colonne en entier
break; // Sortir de la boucle si la colonne est valide
} catch (NumberFormatException e) {
System.out.println("Veuillez entrer un numéro de colonne valide (un nombre entier).");
}
}
// Demander et vérifier le symbole
String symbole;
while (true) {
System.out.println("Entrez le symbole :");
symbole = scanner.nextLine(); // Lire le symbole
if (symbole.isEmpty()) {
System.out.println("Veuillez entrer un symbole valide.");
continue; // Recommencer la saisie du symbole si l'entrée est vide
}
try {
Symbole s;
switch (typeSymbole) {
case 1: // Entiers
if (!Symbole.of(symbole).isInt()) {
throw new IllegalArgumentException("Veuillez entrer un nombre valide.");
}
s = Symbole.of(Integer.parseInt(symbole));
break;
case 2: // Lettres
Symbole tempSymbole = Symbole.of(symbole);
if (!tempSymbole.isLetter()) {
throw new IllegalArgumentException("Veuillez entrer une seule lettre.");
}
s = tempSymbole;
break;
case 3: // Chaînes de caractères
s = Symbole.of(symbole);
break;
default:
throw new IllegalArgumentException("Type de symbole invalide.");
}
sudoku.getGrille().setCase(ligne, colonne, s); // Ajouter le symbole à la grille
System.out.println(sudoku.getGrille().toString());
break; // Sortir de la boucle si le symbole est valide
} catch (IllegalArgumentException e) {
System.out.println("Symbole non valide. " + e.getMessage());
}
}
}
}
private static void creationBlocManuel(Sudoku sudoku, Scanner scanner, int tailleGrille) {
int nombreBloc = tailleGrille; // Nombre de blocs dans la grille
Set<String> toutesLesCoordonnees = new HashSet<>(); // Stocke toutes les coordonnées utilisées
while (nombreBloc != 0) {
List<int[]> listeCases = new ArrayList<>(); // Liste des cases du bloc
System.out.println("Remplissage du bloc " + nombreBloc);
for (int i = 1; i <= tailleGrille; i++) {
int choixLigne, choixColonne;
boolean coordonneesValides;
do {
coordonneesValides = true; // Par défaut, on suppose que la coordonnée est valide
// Demande de saisie
System.out.println("Entrez la ligne de la case " + i + " dans le bloc " + nombreBloc + " (0 à "
+ (tailleGrille - 1) + "):");
choixLigne = scanner.nextInt();
System.out.println("Entrez la colonne de la case " + i + " dans le bloc " + nombreBloc + " (0 à "
+ (tailleGrille - 1) + "):");
choixColonne = scanner.nextInt();
// Vérification des limites
if (choixLigne < 0 || choixLigne >= tailleGrille || choixColonne < 0
|| choixColonne >= tailleGrille) {
System.out.println("Erreur : Coordonnées hors limites ! Veuillez réessayer.");
coordonneesValides = false;
continue;
}
// Vérification si la coordonnée existe déjà dans TOUTE la grille
String coordonneeStr = choixLigne + "-" + choixColonne;
if (toutesLesCoordonnees.contains(coordonneeStr)) {
System.out.println(
"Erreur : Ces coordonnées sont déjà utilisées dans un autre bloc ! Veuillez en entrer une nouvelle.");
coordonneesValides = false;
}
} while (!coordonneesValides); // Tant que la saisie est invalide, on redemande
// Ajout des coordonnées validées
listeCases.add(new int[] { choixLigne, choixColonne });
toutesLesCoordonnees.add(choixLigne + "-" + choixColonne); // Ajout dans l'ensemble global
}
// Création du bloc après la saisie complète
try {
sudoku.getGrille().creerBlocPersonnalise(listeCases);
System.out.println("Bloc " + nombreBloc + " enregistré avec succès !");
} catch (IllegalArgumentException e) {
System.out.println("Erreur lors de la création du bloc : " + e.getMessage());
System.out.println("Veuillez recommencer la saisie de ce bloc.");
continue; // Recommence le bloc en cours
}
nombreBloc--; // Passer au bloc suivant
}
}
} }

View File

@@ -0,0 +1,25 @@
package sudoku;
public class SudokuPlacement {
private final Sudoku sudoku;
private final int offsetLigne;
private final int offsetColonne;
public SudokuPlacement(Sudoku sudoku, int offsetLigne, int offsetColonne) {
this.sudoku = sudoku;
this.offsetLigne = offsetLigne;
this.offsetColonne = offsetColonne;
}
public Sudoku getSudoku() {
return sudoku;
}
public int getOffsetLigne() {
return offsetLigne;
}
public int getOffsetColonne() {
return offsetColonne;
}
}

View File

@@ -48,8 +48,4 @@ public class Symbole {
Symbole symbole = (Symbole) obj; Symbole symbole = (Symbole) obj;
return valeur.equals(symbole.valeur); return valeur.equals(symbole.valeur);
} }
public int getHashCode() {
return valeur.hashCode();
}
} }

View File

@@ -0,0 +1,67 @@
package sudoku.gui;
import java.util.ArrayList;
import java.util.List;
public class ColorGenerator {
public static class Color {
public float r, g, b;
public Color(float r, float g, float b) {
this.r = r;
this.g = g;
this.b = b;
}
}
public static List<Color> greatPalette(int colorCount) {
List<Color> colors = greatScheme(colorCount);
List<Color> newOrder = new ArrayList<>();
int newIndex = 0;
while (!colors.isEmpty()) {
int randomIndex = newIndex % colors.size();
newOrder.add(colors.get(randomIndex));
colors.remove(randomIndex);
newIndex += Math.sqrt(colorCount) + 1;
}
return newOrder;
}
public static List<Color> greatScheme(int colorCount) {
List<Color> colors = new ArrayList<>();
for (int i = 0; i < colorCount; i++) {
colors.add(hslToRgb((float) (i) / (float) colorCount, 0.9f, 0.4f));
}
return colors;
}
public static Color hslToRgb(float h, float s, float l) {
float r, g, b;
if (s == 0f) {
r = g = b = l; // achromatic
} else {
float q = l < 0.5f ? l * (1 + s) : l + s - l * s;
float p = 2 * l - q;
r = hueToRgb(p, q, h + 1f / 3f);
g = hueToRgb(p, q, h);
b = hueToRgb(p, q, h - 1f / 3f);
}
return new Color(r, g, b);
}
public static float hueToRgb(float p, float q, float t) {
if (t < 0f)
t += 1f;
if (t > 1f)
t -= 1f;
if (t < 1f / 6f)
return p + (q - p) * 6f * t;
if (t < 1f / 2f)
return q;
if (t < 2f / 3f)
return p + (q - p) * (2f / 3f - t) * 6f;
return p;
}
}

View File

@@ -1,30 +1,27 @@
package sudoku; package sudoku;
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals;
import sudoku.core.Console;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Scanner;
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test;
public class AppTest { public class AppTest {
/**
* Test the creation of a Sudoku grid with a size of 16
*/
@Test @Test
public void testExample() { public void testExample() {
// Simulate user input with proper line endings // Simulate user input with proper line endings
// First 1 is to select the int type of Symboles and after, Symboles are entered (1 to 16) // First 1 is to select the int type of Symboles and after, Symboles are entered
String simulatedInput = "1\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n"; // (1 to 16)
String simulatedInput = "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n";
System.setIn(new ByteArrayInputStream(simulatedInput.getBytes())); System.setIn(new ByteArrayInputStream(simulatedInput.getBytes()));
// Create a new Sudoku // Create a new Sudoku
Sudoku sudoku = new Sudoku(16); Sudoku sudoku = new Sudoku(16);
sudoku.getGrille().askSetSymbolesPossibles(); sudoku.getGrille().askSetSymbolesPossibles(1, new Scanner(System.in)); // Pass the type of symbol and scanner
// Verify the symbols // Verify the symbols
ArrayList<Symbole> expectedSymbols = new ArrayList<>(Arrays.asList( ArrayList<Symbole> expectedSymbols = new ArrayList<>(Arrays.asList(
@@ -48,79 +45,23 @@ public class AppTest {
// Cleanup // Cleanup
System.setIn(System.in); System.setIn(System.in);
}
@Test int taille = 16;
public void testContraintes() { for (int i = 0; i < taille; i++) {
// Create a new Sudoku sudoku.getGrille().setCase(i, i, expectedSymbols.get(i));
Sudoku sudoku = new Sudoku(16); }
sudoku.getGrille().setSymbolesPossibles(new ArrayList<>(Arrays.asList(
Symbole.of(1), sudoku.getGrille().creerBlocCarre();
Symbole.of(2),
Symbole.of(3),
Symbole.of(4),
Symbole.of(5),
Symbole.of(6),
Symbole.of(7),
Symbole.of(8),
Symbole.of(9),
Symbole.of(10),
Symbole.of(11),
Symbole.of(11),
Symbole.of(12),
Symbole.of(13),
Symbole.of(14),
Symbole.of(15),
Symbole.of(16))));
// Add constraints
sudoku.ajouterContrainte(new ContrainteLigne()); sudoku.ajouterContrainte(new ContrainteLigne());
sudoku.ajouterContrainte(new ContrainteColonne()); sudoku.ajouterContrainte(new ContrainteColonne());
// sudoku.ajouterContrainte(new ContrainteBloc()); sudoku.ajouterContrainte(new ContrainteBloc());
// Fill the grid with valid symbols
sudoku.getGrille().setCase(0, 0, Symbole.of(1));
sudoku.getGrille().setCase(0, 1, Symbole.of(2));
sudoku.getGrille().setCase(0, 2, Symbole.of(3));
sudoku.getGrille().setCase(1, 0, Symbole.of(4));
sudoku.getGrille().setCase(1, 1, Symbole.of(5));
sudoku.getGrille().setCase(1, 2, Symbole.of(6));
sudoku.getGrille().setCase(2, 0, Symbole.of(7));
sudoku.getGrille().setCase(2, 1, Symbole.of(8));
sudoku.getGrille().setCase(2, 2, Symbole.of(9));
sudoku.getGrille().setCase(2, 8, Symbole.of(9));
sudoku.getGrille().setCase(14, 2, Symbole.of(6));
sudoku.getGrille().setCase(3, 3, Symbole.of(5));
// sudoku.getGrille().setCase(2, 2, Symbole.of(13));
// sudoku.getGrille().setCase(2, 2, Symbole.of(14));
// sudoku.getGrille().setCase(2, 2, Symbole.of(15));
// Create square blocks
sudoku.getGrille().creerBlocCarre();
System.out.println(sudoku.getGrille().toString()); System.out.println(sudoku.getGrille().toString());
// Verify that the constraints are respected Resolveur resolveur = new ResolveurBacktraceSimple();
// assertTrue(sudoku.estValide(sudoku.getGrille().getCase(0, 0))); resolveur.resoudre(sudoku, false);
// assertTrue(sudoku.estValide(sudoku.getGrille().getCase(1, 1))); System.out.println("Sudoku résolu :");
// assertTrue(sudoku.estValide(sudoku.getGrille().getCase(2, 2))); System.out.println(sudoku.getGrille().toString());
if (sudoku.verifierToutesContraintes()) {
Console.successln("Toutes les contraintes sont respectées !");
} else {
Console.errorln("Au moins une contrainte n'est pas respectée.");
}
// Add a duplicate symbol in the same row
// sudoku.getGrille().setCase(0, 3, Symbole.of(1));
// assertFalse(sudoku.estValide(sudoku.getGrille().getCase(0, 3)));
// Add a duplicate symbol in the same column
// sudoku.getGrille().setCase(3, 0, Symbole.of(1));
// assertFalse(sudoku.estValide(sudoku.getGrille().getCase(3, 0)));
// Add a duplicate symbol in the same block
// sudoku.getGrille().setCase(1, 1, Symbole.of(1));
// assertFalse(sudoku.estValide(sudoku.getGrille().getCase(1, 1)));
} }
} }

View File

@@ -58,7 +58,7 @@ public class TestBlocParticuliers {
System.out.println("Symboles possibles :"); System.out.println("Symboles possibles :");
sudoku.getGrille().printSymbolesPossibles(); sudoku.getGrille().printSymbolesPossibles();
ResolveurBacktraceSimple resolveur = new ResolveurBacktraceSimple(sudoku); ResolveurBacktraceSimple resolveur = new ResolveurBacktraceSimple();
resolveur.resoudre(sudoku, true); resolveur.resoudre(sudoku, true);
System.out.println("Sudoku résolu :"); System.out.println("Sudoku résolu :");

View File

@@ -34,22 +34,17 @@ public class TestBlocRectangle {
sudoku.getGrille().setCase(0, 3, Symbole.of(9)); sudoku.getGrille().setCase(0, 3, Symbole.of(9));
sudoku.getGrille().setCase(3, 0, Symbole.of(4)); sudoku.getGrille().setCase(3, 0, Symbole.of(4));
sudoku.getGrille().creerBlocRectangulaire(3, 5);
sudoku.getGrille().creerBlocCarre();
System.out.println("Sudoku :"); System.out.println("Sudoku :");
System.out.println(sudoku.getGrille().toString()); System.out.println(sudoku.getGrille().toString());
sudoku.getGrille().creerBlocRectangulaire(3, 5);
System.out.println("Blocs :"); System.out.println("Blocs :");
sudoku.getGrille().printBlocs(); sudoku.getGrille().printBlocs();
System.out.println("Symboles possibles :"); System.out.println("Symboles possibles :");
sudoku.getGrille().printSymbolesPossibles(); sudoku.getGrille().printSymbolesPossibles();
// blocRectangle.resoudre(sudoku, true);
// System.out.println("Sudoku resolu :");
// System.out.println(sudoku.getGrille().toString());
System.out.println("FIN TEST BLOC RECTANGLE"); System.out.println("FIN TEST BLOC RECTANGLE");
} }
} }

View File

@@ -0,0 +1,109 @@
package sudoku;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
public class TestCreationGrilleRectangulaireSansSolution {
@Test
public void testCreationGrilleSansSolution() {
// CREATION D'UN SUDOKU SIMPLE 12*12
Sudoku sudoku = new Sudoku(12);
sudoku.getGrille().setSymbolesPossibles(new ArrayList<>(Arrays.asList(
Symbole.of(1),
Symbole.of(2),
Symbole.of(3),
Symbole.of(4),
Symbole.of(5),
Symbole.of(6),
Symbole.of(7),
Symbole.of(8),
Symbole.of(9),
Symbole.of(10),
Symbole.of(11),
Symbole.of(12))));
sudoku.getGrille().setCase(0, 1, Symbole.of(12));
sudoku.getGrille().setCase(0, 6, Symbole.of(3));
sudoku.getGrille().setCase(0, 7, Symbole.of(8));
sudoku.getGrille().setCase(0, 8, Symbole.of(2));
sudoku.getGrille().setCase(0, 9, Symbole.of(6));
sudoku.getGrille().setCase(1, 0, Symbole.of(7));
sudoku.getGrille().setCase(1, 1, Symbole.of(2));
sudoku.getGrille().setCase(1, 2, Symbole.of(1));
sudoku.getGrille().setCase(1, 5, Symbole.of(4));
sudoku.getGrille().setCase(1, 7, Symbole.of(5));
sudoku.getGrille().setCase(1, 9, Symbole.of(10));
sudoku.getGrille().setCase(1, 11, Symbole.of(9));
sudoku.getGrille().setCase(2, 1, Symbole.of(5));
sudoku.getGrille().setCase(2, 2, Symbole.of(10));
sudoku.getGrille().setCase(2, 5, Symbole.of(2));
sudoku.getGrille().setCase(2, 6, Symbole.of(7));
sudoku.getGrille().setCase(2, 10, Symbole.of(4));
sudoku.getGrille().setCase(3, 5, Symbole.of(12));
sudoku.getGrille().setCase(3, 11, Symbole.of(7));
sudoku.getGrille().setCase(4, 1, Symbole.of(1));
sudoku.getGrille().setCase(4, 3, Symbole.of(2));
sudoku.getGrille().setCase(4, 4, Symbole.of(6));
sudoku.getGrille().setCase(4, 5, Symbole.of(9));
sudoku.getGrille().setCase(4, 6, Symbole.of(5));
sudoku.getGrille().setCase(4, 9, Symbole.of(12));
sudoku.getGrille().setCase(4, 10, Symbole.of(3));
sudoku.getGrille().setCase(4, 11, Symbole.of(8));
sudoku.getGrille().setCase(5, 1, Symbole.of(4));
sudoku.getGrille().setCase(5, 2, Symbole.of(6));
sudoku.getGrille().setCase(5, 3, Symbole.of(12));
sudoku.getGrille().setCase(5, 5, Symbole.of(7));
sudoku.getGrille().setCase(5, 6, Symbole.of(10));
sudoku.getGrille().setCase(5, 9, Symbole.of(5));
sudoku.getGrille().setCase(5, 11, Symbole.of(11));
sudoku.getGrille().setCase(6, 0, Symbole.of(1));
sudoku.getGrille().setCase(6, 1, Symbole.of(8));
sudoku.getGrille().setCase(6, 2, Symbole.of(12));
sudoku.getGrille().setCase(6, 11, Symbole.of(6));
sudoku.getGrille().setCase(7, 2, Symbole.of(2));
sudoku.getGrille().setCase(7, 3, Symbole.of(5));
sudoku.getGrille().setCase(7, 5, Symbole.of(11));
sudoku.getGrille().setCase(7, 6, Symbole.of(8));
sudoku.getGrille().setCase(7, 7, Symbole.of(6));
sudoku.getGrille().setCase(7, 8, Symbole.of(7));
sudoku.getGrille().setCase(7, 10, Symbole.of(1));
sudoku.getGrille().setCase(7, 11, Symbole.of(12));
sudoku.getGrille().setCase(8, 4, Symbole.of(1));
sudoku.getGrille().setCase(8, 7, Symbole.of(10));
sudoku.getGrille().setCase(8, 8, Symbole.of(8));
sudoku.getGrille().setCase(8, 9, Symbole.of(11));
sudoku.getGrille().setCase(9, 5, Symbole.of(8));
sudoku.getGrille().setCase(9, 10, Symbole.of(9));
sudoku.getGrille().setCase(9, 11, Symbole.of(4));
sudoku.getGrille().setCase(10, 0, Symbole.of(2));
sudoku.getGrille().setCase(10, 2, Symbole.of(3));
sudoku.getGrille().setCase(10, 4, Symbole.of(9));
sudoku.getGrille().setCase(11, 0, Symbole.of(4));
sudoku.getGrille().setCase(11, 3, Symbole.of(9));
sudoku.getGrille().setCase(11, 4, Symbole.of(5));
sudoku.getGrille().setCase(11, 6, Symbole.of(11));
sudoku.getGrille().setCase(11, 8, Symbole.of(6));
sudoku.getGrille().setCase(11, 11, Symbole.of(3));
sudoku.getGrille().creerBlocRectangulaire(3, 4);
sudoku.ajouterContrainte(new ContrainteBloc());
sudoku.ajouterContrainte(new ContrainteColonne());
sudoku.ajouterContrainte(new ContrainteLigne());
System.out.println("Sudoku :");
System.out.println(sudoku.getGrille().toString());
System.out.println("Blocs :");
sudoku.getGrille().printBlocs();
System.out.println("Symboles possibles :");
sudoku.getGrille().printSymbolesPossibles();
ResolveurBacktraceSimple resolveur = new ResolveurBacktraceSimple();
boolean solved = resolveur.resoudre(sudoku, false);
System.out.println("Sudoku " + (solved ? "résolu" : "non résolu") + " :");
System.out.println(sudoku.getGrille().toString());
System.out.println("FIN TEST LIGNE");
}
}

View File

@@ -7,10 +7,10 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
public class TestBloc { public class TestDoublonBloc {
@Test @Test
public void testBloc() { public void testBloc() {
System.out.println("TEST BLOC : "); System.out.println("TEST DOUBLON BLOC : ");
System.out.println(new App().getGreeting()); System.out.println(new App().getGreeting());
// Create a new Sudoku // Create a new Sudoku
Sudoku sudoku = new Sudoku(9); Sudoku sudoku = new Sudoku(9);
@@ -51,6 +51,6 @@ public class TestBloc {
System.out.println("Symboles possibles :"); System.out.println("Symboles possibles :");
sudoku.getGrille().printSymbolesPossibles(); sudoku.getGrille().printSymbolesPossibles();
System.out.println("FIN TEST BLOC"); System.out.println("FIN TEST DOUBLON BLOC");
} }
} }

View File

@@ -7,10 +7,10 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
public class TestColonne { public class TestDoublonColonne {
@Test @Test
public void testColonne() { public void testColonne() {
System.out.println("TEST COL : "); System.out.println("TEST DOUBLON COLONNE : ");
System.out.println(new App().getGreeting()); System.out.println(new App().getGreeting());
// Create a new Sudoku // Create a new Sudoku
Sudoku sudoku = new Sudoku(9); Sudoku sudoku = new Sudoku(9);
@@ -50,6 +50,6 @@ public class TestColonne {
System.out.println("Symboles possibles :"); System.out.println("Symboles possibles :");
sudoku.getGrille().printSymbolesPossibles(); sudoku.getGrille().printSymbolesPossibles();
System.out.println("FIN TEST COL"); System.out.println("FIN TEST DOUBLON COLONNE");
} }
} }

View File

@@ -5,10 +5,10 @@ import org.junit.jupiter.api.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
public class TestLigne { public class TestDoublonLigne {
@Test @Test
public void testLigne() { public void testDoublonLigne() {
System.out.println("TEST LIGNE : "); System.out.println("TEST DOUBLON LIGNE : ");
System.out.println(new App().getGreeting()); System.out.println(new App().getGreeting());
// Create a new Sudoku // Create a new Sudoku
Sudoku sudoku = new Sudoku(9); Sudoku sudoku = new Sudoku(9);
@@ -47,6 +47,6 @@ public class TestLigne {
System.out.println("Symboles possibles :"); System.out.println("Symboles possibles :");
sudoku.getGrille().printSymbolesPossibles(); sudoku.getGrille().printSymbolesPossibles();
System.out.println("FIN TEST LIGNE"); System.out.println("FIN TEST DOUBLON LIGNE");
} }
} }

View File

@@ -0,0 +1,91 @@
package sudoku;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
public class TestMultidokuBloc {
@Test
public void testMultidokuBloc() {
Multidoku multidoku = new Multidoku();
Sudoku s1 = new Sudoku(9);
Sudoku s2 = new Sudoku(9);
s1.setNom("Sudoku 1");
s2.setNom("Sudoku 2");
// Placer sudoku1 en haut à gauche (offset (0,0))
multidoku.ajouterSudoku(s1, 0, 0);
// Placer sudoku2 de façon à ce que sa case (0,0) se retrouve en (6,6) de la
// grille globale
multidoku.ajouterSudoku(s2, 6, 6);
ArrayList<Symbole> symboles = new ArrayList<>();
for (int i = 10; i <= 19; i++) {
symboles.add(Symbole.of(i));
}
s1.getGrille().setSymbolesPossibles(symboles);
List<Sudoku> sudokus = Arrays.asList(s1, s2);
for (Sudoku sudoku : sudokus) {
sudoku.getGrille().setSymbolesPossibles(symboles);
sudoku.ajouterContrainte(new ContrainteLigne());
sudoku.ajouterContrainte(new ContrainteColonne());
sudoku.ajouterContrainte(new ContrainteBloc());
sudoku.getGrille().creerBlocCarre();
}
for (int i = 0; i < s1.getGrille().getTaille(); i++) {
s1.getGrille().setCase(i, i, symboles.get(i));
}
// Créer un lien entre la case (6,6) de s1 et (0,0) de s2
List<Case> casesPartagees = Arrays.asList(
s1.getGrille().getCase(6, 6),
s1.getGrille().getCase(6, 7),
s1.getGrille().getCase(6, 8),
s1.getGrille().getCase(7, 6),
s1.getGrille().getCase(7, 7),
s1.getGrille().getCase(7, 8),
s1.getGrille().getCase(8, 6),
s1.getGrille().getCase(8, 7),
s1.getGrille().getCase(8, 8),
s2.getGrille().getCase(0, 0),
s2.getGrille().getCase(0, 1),
s2.getGrille().getCase(0, 2),
s2.getGrille().getCase(1, 0),
s2.getGrille().getCase(1, 1),
s2.getGrille().getCase(1, 2),
s2.getGrille().getCase(2, 0),
s2.getGrille().getCase(2, 1),
s2.getGrille().getCase(2, 2));
multidoku.ajouterCasesPartagees(casesPartagees);
System.out.println("Sudoku 1 :");
System.out.println(s1.getGrille().toString());
System.out.println("Sudoku 2 :");
System.out.println(s2.getGrille().toString());
System.out.println("\nAffichage Multidoku combiné :");
System.out.println(multidoku.toStringCombined());
if (multidoku.resoudreMultidoku(false)) {
System.out.println("Multidoku résolu :");
System.out.println(multidoku.toStringCombined());
} else {
System.out.println("Multidoku non résolu");
}
System.out.println("Sudoku 1 résolu :");
System.out.println(s1.getGrille().toString());
System.out.println("Sudoku 2 résolu :");
System.out.println(s2.getGrille().toString());
}
}

View File

@@ -0,0 +1,73 @@
package sudoku;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
public class TestMultidokuCase {
@Test
public void testMultidokuCase() {
Multidoku multidoku = new Multidoku();
Sudoku s1 = new Sudoku(9);
Sudoku s2 = new Sudoku(9);
s1.setNom("Sudoku 1");
s2.setNom("Sudoku 2");
// Placer sudoku1 en haut à gauche (offset (0,0))
multidoku.ajouterSudoku(s1, 0, 0);
// Placer sudoku2 de façon à ce que sa case (0,0) se retrouve en (8,8) de la
// grille globale
multidoku.ajouterSudoku(s2, 8, 8);
// Créer un lien entre la case (8,8) de s1 et (0,0) de s2
List<Case> casesPartagees = Arrays.asList(
s1.getGrille().getCase(8, 8),
s2.getGrille().getCase(0, 0));
multidoku.ajouterCasesPartagees(casesPartagees);
ArrayList<Symbole> symboles = new ArrayList<>();
for (int i = 10; i <= 19; i++) {
symboles.add(Symbole.of(i));
}
s1.getGrille().setSymbolesPossibles(symboles);
List<Sudoku> sudokus = Arrays.asList(s1, s2);
for (Sudoku sudoku : sudokus) {
sudoku.getGrille().setSymbolesPossibles(symboles);
sudoku.ajouterContrainte(new ContrainteLigne());
sudoku.ajouterContrainte(new ContrainteColonne());
sudoku.ajouterContrainte(new ContrainteBloc());
sudoku.getGrille().creerBlocCarre();
}
for (int i = 0; i < s1.getGrille().getTaille(); i++) {
s1.getGrille().setCase(i, i, symboles.get(i));
}
System.out.println("Sudoku 1 :");
System.out.println(s1.getGrille().toString());
System.out.println("Sudoku 2 :");
System.out.println(s2.getGrille().toString());
System.out.println("\nAffichage Multidoku combiné :");
System.out.println(multidoku.toStringCombined());
if (multidoku.resoudreMultidoku(false)) {
System.out.println("Multidoku résolu !");
System.out.println(multidoku.toStringCombined());
} else {
System.out.println("Échec de la résolution du multidoku.");
}
System.out.println("Sudoku 1 résolu :");
System.out.println(s1.getGrille().toString());
System.out.println("Sudoku 2 résolu :");
System.out.println(s2.getGrille().toString());
}
}

View File

@@ -0,0 +1,86 @@
package sudoku;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
public class TestMultidokuColonne {
@Test
public void testMultidokuColonne() {
Multidoku multidoku = new Multidoku();
Sudoku s1 = new Sudoku(9);
Sudoku s2 = new Sudoku(9);
s1.setNom("Sudoku 1");
s2.setNom("Sudoku 2");
// Placer sudoku1 en haut à gauche (offset (0,0))
multidoku.ajouterSudoku(s1, 0, 0);
multidoku.ajouterSudoku(s2, 1, 8);
ArrayList<Symbole> symboles = new ArrayList<>();
for (int i = 10; i <= 19; i++) {
symboles.add(Symbole.of(i));
}
s1.getGrille().setSymbolesPossibles(symboles);
List<Sudoku> sudokus = Arrays.asList(s1, s2);
for (Sudoku sudoku : sudokus) {
sudoku.getGrille().setSymbolesPossibles(symboles);
sudoku.ajouterContrainte(new ContrainteLigne());
sudoku.ajouterContrainte(new ContrainteColonne());
sudoku.ajouterContrainte(new ContrainteBloc());
sudoku.getGrille().creerBlocCarre();
}
for (int i = 0; i < s1.getGrille().getTaille(); i++) {
s1.getGrille().setCase(i, i, symboles.get(i));
}
List<Case> casesPartagees = Arrays.asList(
s1.getGrille().getCase(1, 8),
s1.getGrille().getCase(2, 8),
s1.getGrille().getCase(3, 8),
s1.getGrille().getCase(4, 8),
s1.getGrille().getCase(5, 8),
s1.getGrille().getCase(6, 8),
s1.getGrille().getCase(7, 8),
s1.getGrille().getCase(8, 8),
s2.getGrille().getCase(0, 0),
s2.getGrille().getCase(1, 0),
s2.getGrille().getCase(2, 0),
s2.getGrille().getCase(3, 0),
s2.getGrille().getCase(4, 0),
s2.getGrille().getCase(5, 0),
s2.getGrille().getCase(6, 0),
s2.getGrille().getCase(7, 0));
multidoku.ajouterCasesPartagees(casesPartagees);
System.out.println("Sudoku 1 :");
System.out.println(s1.getGrille().toString());
System.out.println("Sudoku 2 :");
System.out.println(s2.getGrille().toString());
System.out.println("\nAffichage Multidoku combiné :");
System.out.println(multidoku.toStringCombined());
if (multidoku.resoudreMultidoku(false)) {
System.out.println("Multidoku résolu :");
System.out.println(multidoku.toStringCombined());
} else {
System.out.println("Multidoku non résolu");
}
System.out.println("Sudoku 1 résolu :");
System.out.println(s1.getGrille().toString());
System.out.println("Sudoku 2 résolu :");
System.out.println(s2.getGrille().toString());
}
}

View File

@@ -0,0 +1,86 @@
package sudoku;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
public class TestMultidokuLigne {
@Test
public void testMultidokuLigne() {
Multidoku multidoku = new Multidoku();
Sudoku s1 = new Sudoku(9);
Sudoku s2 = new Sudoku(9);
s1.setNom("Sudoku 1");
s2.setNom("Sudoku 2");
// Placer sudoku1 en haut à gauche (offset (0,0))
multidoku.ajouterSudoku(s1, 0, 0);
multidoku.ajouterSudoku(s2, 8, 1);
ArrayList<Symbole> symboles = new ArrayList<>();
for (int i = 10; i <= 19; i++) {
symboles.add(Symbole.of(i));
}
s1.getGrille().setSymbolesPossibles(symboles);
List<Sudoku> sudokus = Arrays.asList(s1, s2);
for (Sudoku sudoku : sudokus) {
sudoku.getGrille().setSymbolesPossibles(symboles);
sudoku.ajouterContrainte(new ContrainteLigne());
sudoku.ajouterContrainte(new ContrainteColonne());
sudoku.ajouterContrainte(new ContrainteBloc());
sudoku.getGrille().creerBlocCarre();
}
for (int i = 0; i < s1.getGrille().getTaille(); i++) {
s1.getGrille().setCase(i, i, symboles.get(i));
}
List<Case> casesPartagees = Arrays.asList(
s1.getGrille().getCase(8, 1),
s1.getGrille().getCase(8, 2),
s1.getGrille().getCase(8, 3),
s1.getGrille().getCase(8, 4),
s1.getGrille().getCase(8, 5),
s1.getGrille().getCase(8, 6),
s1.getGrille().getCase(8, 7),
s1.getGrille().getCase(8, 8),
s2.getGrille().getCase(0, 0),
s2.getGrille().getCase(0, 1),
s2.getGrille().getCase(0, 2),
s2.getGrille().getCase(0, 3),
s2.getGrille().getCase(0, 4),
s2.getGrille().getCase(0, 5),
s2.getGrille().getCase(0, 6),
s2.getGrille().getCase(0, 7));
multidoku.ajouterCasesPartagees(casesPartagees);
System.out.println("Sudoku 1 :");
System.out.println(s1.getGrille().toString());
System.out.println("Sudoku 2 :");
System.out.println(s2.getGrille().toString());
System.out.println("\nAffichage Multidoku combiné :");
System.out.println(multidoku.toStringCombined());
if (multidoku.resoudreMultidoku(false)) {
System.out.println("Multidoku résolu :");
System.out.println(multidoku.toStringCombined());
} else {
System.out.println("Multidoku non résolu");
}
System.out.println("Sudoku 1 résolu :");
System.out.println(s1.getGrille().toString());
System.out.println("Sudoku 2 résolu :");
System.out.println(s2.getGrille().toString());
}
}

View File

@@ -1,70 +1,70 @@
// package sudoku; package sudoku;
// import java.util.ArrayList; import java.util.ArrayList;
// import java.util.Arrays; import java.util.Arrays;
// import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
// public class TestResolveurBacktraceSimpleSudoku16 { public class TestResolveurBacktraceSimpleSudoku16 {
// @Test @Test
// public void testResolution() { public void testResolution() {
// System.out.println("TEST RESOLVEUR BACKTRACE SIMPLE : "); System.out.println("TEST RESOLVEUR BACKTRACE SIMPLE : ");
// System.out.println(new App().getGreeting()); System.out.println(new App().getGreeting());
// // Create a new Sudoku // Create a new Sudoku
// Sudoku sudoku = new Sudoku(16); Sudoku sudoku = new Sudoku(16);
// sudoku.getGrille().setSymbolesPossibles(new ArrayList<>(Arrays.asList( sudoku.getGrille().setSymbolesPossibles(new ArrayList<>(Arrays.asList(
// Symbole.of(1), Symbole.of(1),
// Symbole.of(2), Symbole.of(2),
// Symbole.of(3), Symbole.of(3),
// Symbole.of(4), Symbole.of(4),
// Symbole.of(5), Symbole.of(5),
// Symbole.of(6), Symbole.of(6),
// Symbole.of(7), Symbole.of(7),
// Symbole.of(8), Symbole.of(8),
// Symbole.of(9), Symbole.of(9),
// Symbole.of(10), Symbole.of(10),
// Symbole.of(11), Symbole.of(11),
// Symbole.of(12), Symbole.of(12),
// Symbole.of(13), Symbole.of(13),
// Symbole.of(14), Symbole.of(14),
// Symbole.of(15), Symbole.of(15),
// Symbole.of(16) Symbole.of(16)
// ))); )));
// sudoku.ajouterContrainte(new ContrainteLigne()); sudoku.ajouterContrainte(new ContrainteLigne());
// sudoku.ajouterContrainte(new ContrainteColonne()); sudoku.ajouterContrainte(new ContrainteColonne());
// sudoku.ajouterContrainte(new ContrainteBloc()); sudoku.ajouterContrainte(new ContrainteBloc());
// sudoku.getGrille().setCase(0, 0, Symbole.of(1)); sudoku.getGrille().setCase(0, 0, Symbole.of(1));
// sudoku.getGrille().setCase(6, 1, Symbole.of(2)); sudoku.getGrille().setCase(6, 1, Symbole.of(2));
// sudoku.getGrille().setCase(2, 2, Symbole.of(3)); sudoku.getGrille().setCase(2, 2, Symbole.of(3));
// sudoku.getGrille().setCase(0, 3, Symbole.of(4)); sudoku.getGrille().setCase(0, 3, Symbole.of(4));
// sudoku.getGrille().setCase(4, 4, Symbole.of(5)); sudoku.getGrille().setCase(4, 4, Symbole.of(5));
// sudoku.getGrille().setCase(0, 5, Symbole.of(6)); sudoku.getGrille().setCase(0, 5, Symbole.of(6));
// sudoku.getGrille().setCase(5, 6, Symbole.of(7)); sudoku.getGrille().setCase(5, 6, Symbole.of(7));
// sudoku.getGrille().setCase(0, 7, Symbole.of(8)); sudoku.getGrille().setCase(0, 7, Symbole.of(8));
// sudoku.getGrille().setCase(4, 8, Symbole.of(9)); sudoku.getGrille().setCase(4, 8, Symbole.of(9));
// sudoku.getGrille().setCase(0, 9, Symbole.of(10)); sudoku.getGrille().setCase(0, 9, Symbole.of(10));
// sudoku.getGrille().setCase(6, 10, Symbole.of(11)); sudoku.getGrille().setCase(6, 10, Symbole.of(11));
// sudoku.getGrille().setCase(2, 11, Symbole.of(12)); sudoku.getGrille().setCase(2, 11, Symbole.of(12));
// sudoku.getGrille().setCase(0, 12, Symbole.of(13)); sudoku.getGrille().setCase(0, 12, Symbole.of(13));
// sudoku.getGrille().creerBlocCarre(); sudoku.getGrille().creerBlocCarre();
// System.out.println("Sudoku :"); System.out.println("Sudoku :");
// System.out.println(sudoku.getGrille().toString()); System.out.println(sudoku.getGrille().toString());
// System.out.println("Blocs :"); System.out.println("Blocs :");
// sudoku.getGrille().printBlocs(); sudoku.getGrille().printBlocs();
// System.out.println("Symboles possibles :"); System.out.println("Symboles possibles :");
// sudoku.getGrille().printSymbolesPossibles(); sudoku.getGrille().printSymbolesPossibles();
// ResolveurBacktraceSimple resolveur = new ResolveurBacktraceSimple(sudoku); ResolveurBacktraceSimple resolveur = new ResolveurBacktraceSimple();
// resolveur.resoudre(sudoku, false); resolveur.resoudre(sudoku, false);
// System.out.println("Sudoku resolu :"); System.out.println("Sudoku resolu :");
// System.out.println(sudoku.getGrille().toString()); System.out.println(sudoku.getGrille().toString());
// System.out.println("FIN TEST RESOLVEUR BACKTRACE SIMPLE"); System.out.println("FIN TEST RESOLVEUR BACKTRACE SIMPLE");
// } }
// } }

View File

@@ -1,3 +1,9 @@
/**
* TestResolveurBacktraceSimpleSudoku25
* Tentative de résolution d'un sudoku 25x25 avec le resolveur backtrace simple
* Commenté car la résolution avec le resolveur backtrace simple est trop longue
*/
// package sudoku; // package sudoku;
// import java.util.ArrayList; // import java.util.ArrayList;
@@ -47,7 +53,7 @@
// System.out.println("Symboles possibles :"); // System.out.println("Symboles possibles :");
// sudoku.getGrille().printSymbolesPossibles(); // sudoku.getGrille().printSymbolesPossibles();
// ResolveurBacktraceSimple resolveur = new ResolveurBacktraceSimple(sudoku); // ResolveurBacktraceSimple resolveur = new ResolveurBacktraceSimple();
// resolveur.resoudre(sudoku, false); // resolveur.resoudre(sudoku, false);
// System.out.println("Sudoku resolu :"); // System.out.println("Sudoku resolu :");
// System.out.println(sudoku.getGrille().toString()); // System.out.println(sudoku.getGrille().toString());

View File

@@ -48,7 +48,7 @@ public class TestResolveurBacktraceSimpleSudoku9 {
System.out.println("Symboles possibles :"); System.out.println("Symboles possibles :");
sudoku.getGrille().printSymbolesPossibles(); sudoku.getGrille().printSymbolesPossibles();
ResolveurBacktraceSimple resolveur = new ResolveurBacktraceSimple(sudoku); ResolveurBacktraceSimple resolveur = new ResolveurBacktraceSimple();
resolveur.resoudre(sudoku, false); resolveur.resoudre(sudoku, false);
System.out.println("Sudoku resolu :"); System.out.println("Sudoku resolu :");
System.out.println(sudoku.getGrille().toString()); System.out.println(sudoku.getGrille().toString());

View File

@@ -0,0 +1,61 @@
package sudoku;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
public class TestSudokuEmojis {
@Test
public void sudokuEmojis() {
System.out.println("TEST SUDOKU EMOJIS : ");
System.out.println(new App().getGreeting());
// Create a new Sudoku
Sudoku sudoku = new Sudoku(9);
List<Symbole> symboles = new ArrayList<>();
symboles.add(Symbole.of("😎"));
symboles.add(Symbole.of("😂"));
symboles.add(Symbole.of("😍"));
symboles.add(Symbole.of("😜"));
symboles.add(Symbole.of("😇"));
symboles.add(Symbole.of("😘"));
symboles.add(Symbole.of("😋"));
symboles.add(Symbole.of("😊"));
symboles.add(Symbole.of("😉"));
sudoku.getGrille()
.setSymbolesPossibles(new ArrayList<>(symboles));
sudoku.ajouterContrainte(new ContrainteLigne());
sudoku.ajouterContrainte(new ContrainteColonne());
sudoku.ajouterContrainte(new ContrainteBloc());
sudoku.getGrille().setCase(0, 0, Symbole.of("😎"));
sudoku.getGrille().setCase(6, 1, Symbole.of("😂"));
sudoku.getGrille().setCase(2, 2, Symbole.of("😍"));
sudoku.getGrille().setCase(0, 3, Symbole.of("😜"));
sudoku.getGrille().setCase(4, 4, Symbole.of("😇"));
sudoku.getGrille().setCase(0, 5, Symbole.of("😘"));
sudoku.getGrille().setCase(5, 6, Symbole.of("😋"));
sudoku.getGrille().setCase(0, 7, Symbole.of("😊"));
sudoku.getGrille().creerBlocCarre();
System.out.println("Sudoku :");
System.out.println(sudoku.getGrille().toString());
System.out.println("Blocs :");
sudoku.getGrille().printBlocs();
System.out.println("Symboles possibles :");
sudoku.getGrille().printSymbolesPossibles();
ResolveurBacktraceSimple resolveur = new ResolveurBacktraceSimple();
resolveur.resoudre(sudoku, false);
System.out.println("Sudoku resolu :");
System.out.println(sudoku.getGrille().toString());
System.out.println("FIN TEST SUDOKU EMOJIS");
}
}

View File

@@ -0,0 +1,60 @@
package sudoku;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
public class TestSudokuLettres {
@Test
public void sudokuLettres() {
System.out.println("TEST SUDOKU LETTRES : ");
System.out.println(new App().getGreeting());
// Create a new Sudoku
Sudoku sudoku = new Sudoku(9);
List<Symbole> symboles = new ArrayList<>();
symboles.add(Symbole.of("L"));
symboles.add(Symbole.of("E"));
symboles.add(Symbole.of("T"));
symboles.add(Symbole.of("T"));
symboles.add(Symbole.of("R"));
symboles.add(Symbole.of("E"));
symboles.add(Symbole.of("S"));
symboles.add(Symbole.of("O"));
symboles.add(Symbole.of("H"));
sudoku.getGrille()
.setSymbolesPossibles(new ArrayList<>(symboles));
sudoku.ajouterContrainte(new ContrainteLigne());
sudoku.ajouterContrainte(new ContrainteColonne());
sudoku.ajouterContrainte(new ContrainteBloc());
sudoku.getGrille().setCase(0, 0, Symbole.of("L"));
sudoku.getGrille().setCase(6, 1, Symbole.of("E"));
sudoku.getGrille().setCase(2, 2, Symbole.of("T"));
sudoku.getGrille().setCase(0, 3, Symbole.of("T"));
sudoku.getGrille().setCase(4, 4, Symbole.of("R"));
sudoku.getGrille().setCase(0, 5, Symbole.of("E"));
sudoku.getGrille().setCase(5, 6, Symbole.of("S"));
sudoku.getGrille().setCase(0, 7, Symbole.of("O"));
sudoku.getGrille().creerBlocCarre();
System.out.println("Sudoku :");
System.out.println(sudoku.getGrille().toString());
System.out.println("Blocs :");
sudoku.getGrille().printBlocs();
System.out.println("Symboles possibles :");
sudoku.getGrille().printSymbolesPossibles();
ResolveurBacktraceSimple resolveur = new ResolveurBacktraceSimple();
boolean solved = resolveur.resoudre(sudoku, false);
System.out.println("Sudoku " + (solved ? "résolu" : "non résolu") + " :");
System.out.println(sudoku.getGrille().toString());
System.out.println("FIN TEST SUDOKU LETTRES");
}
}

View File

@@ -0,0 +1,59 @@
package sudoku;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
public class TestSudokusString {
@Test
public void sudokuString() {
System.out.println("TEST SUDOKU STRING : ");
System.out.println(new App().getGreeting());
// Create a new Sudoku
Sudoku sudoku = new Sudoku(9);
List<Symbole> symboles = new ArrayList<>();
symboles.add(Symbole.of("Sudoku"));
symboles.add(Symbole.of("🗿"));
symboles.add(Symbole.of("Test"));
symboles.add(Symbole.of("😂"));
symboles.add(Symbole.of("1"));
symboles.add(Symbole.of("woW"));
symboles.add(Symbole.of("😎"));
symboles.add(Symbole.of("42"));
symboles.add(Symbole.of("π"));
sudoku.getGrille()
.setSymbolesPossibles(
new ArrayList<>(symboles));
sudoku.ajouterContrainte(new ContrainteLigne());
sudoku.ajouterContrainte(new ContrainteColonne());
sudoku.ajouterContrainte(new ContrainteBloc());
sudoku.getGrille().setCase(0, 0, Symbole.of("Sudoku"));
sudoku.getGrille().setCase(6, 1, Symbole.of("String"));
sudoku.getGrille().setCase(2, 2, Symbole.of("Test"));
sudoku.getGrille().setCase(0, 3, Symbole.of("😂"));
sudoku.getGrille().setCase(4, 4, Symbole.of("1"));
sudoku.getGrille().setCase(0, 5, Symbole.of("woW"));
sudoku.getGrille().creerBlocCarre();
System.out.println("Sudoku :");
System.out.println(sudoku.getGrille().toString());
System.out.println("Blocs :");
sudoku.getGrille().printBlocs();
System.out.println("Symboles possibles :");
sudoku.getGrille().printSymbolesPossibles();
ResolveurBacktraceSimple resolveur = new ResolveurBacktraceSimple();
resolveur.resoudre(sudoku, false);
System.out.println("Sudoku résolu :");
System.out.println(sudoku.getGrille().toString());
System.out.println("FIN TEST SUDOKU STRING");
}
}