Compare commits
4 Commits
merge/solv
...
multidoku
| Author | SHA1 | Date | |
|---|---|---|---|
| 7e068d0910 | |||
| 909219a8f8 | |||
| 1ad9c0500e | |||
| b0b4152ec4 |
@@ -7,49 +7,6 @@ public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new App().getGreeting());
|
||||
/*
|
||||
// Create a new Sudoku
|
||||
Sudoku sudoku = new Sudoku(9);
|
||||
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))));
|
||||
|
||||
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();*/
|
||||
|
||||
try{
|
||||
Sudoku.menuGrille();
|
||||
|
||||
@@ -41,8 +41,6 @@ public class Bloc {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Bloc{" +
|
||||
"cases=" + cases +
|
||||
'}';
|
||||
return "Bloc [cases=" + cases + ", couleur=" + couleur + ", couleurIndex=" + couleurIndex + " \u001B[0m]";
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import java.util.Objects;
|
||||
import java.util.Scanner;
|
||||
|
||||
import sudoku.core.Console;
|
||||
import sudoku.gui.ColorGenerator;
|
||||
|
||||
public class Grille {
|
||||
private final int taille;
|
||||
@@ -13,6 +14,7 @@ public class Grille {
|
||||
private final ArrayList<Bloc> blocs;
|
||||
private ArrayList<Symbole> symbolesPossibles;
|
||||
private final Sudoku sudoku;
|
||||
private List<String> generatedColors;
|
||||
|
||||
public Grille(int taille, Sudoku sudoku) {
|
||||
this.taille = taille;
|
||||
@@ -20,6 +22,9 @@ public class Grille {
|
||||
this.blocs = new ArrayList<>();
|
||||
this.symbolesPossibles = new ArrayList<>();
|
||||
this.sudoku = sudoku;
|
||||
// Pour un sudoku classique, on attend "taille" blocs, donc on génère "taille"
|
||||
// couleurs.
|
||||
initColors();
|
||||
|
||||
// Initialiser les cases
|
||||
for (int i = 0; i < taille; i++) {
|
||||
@@ -29,20 +34,41 @@ public class Grille {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
int numberOfColors = taille; // nombre de couleurs = nombre de blocs (pour sudoku classique)
|
||||
List<ColorGenerator.Color> colors = ColorGenerator.greatScheme(numberOfColors);
|
||||
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) {
|
||||
try {
|
||||
if (symbole != null && !symbolesPossibles.contains(symbole)) {
|
||||
throw new IllegalArgumentException("Symbole non autorisé : " + symbole);
|
||||
}
|
||||
// Save ancien symbole
|
||||
// Sauvegarder l'ancien symbole
|
||||
Symbole ancienSymbole = cases[ligne][colonne].getSymbole();
|
||||
|
||||
// Set nouveau symbole
|
||||
// Affecter le nouveau symbole
|
||||
cases[ligne][colonne].setSymbole(symbole);
|
||||
|
||||
// Vérifier les contraintes
|
||||
if (!sudoku.verifierToutesContraintes()) {
|
||||
// Revert to ancien symbole
|
||||
// Revenir sur le changement
|
||||
cases[ligne][colonne].setSymbole(ancienSymbole);
|
||||
throw new IllegalArgumentException("SET CASE: Les contraintes ne sont pas respectées pour la case ("
|
||||
+ ligne + ", " + colonne + ")");
|
||||
@@ -56,54 +82,12 @@ public class Grille {
|
||||
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
|
||||
*
|
||||
* Exemple :
|
||||
* sudoku.getGrille().creerBloc(Arrays.asList(
|
||||
* 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 d'utilisation :
|
||||
* sudoku.getGrille().creerBlocPersonnalise(Arrays.asList(
|
||||
* new int[] { 0, 0 },
|
||||
* new int[] { 0, 1 },
|
||||
* new int[] { 0, 2 },
|
||||
@@ -112,10 +96,8 @@ public class Grille {
|
||||
* new int[] { 1, 2 },
|
||||
* new int[] { 2, 0 },
|
||||
* new int[] { 2, 1 },
|
||||
* new int[] { 2, 2 }));
|
||||
*
|
||||
* @param positions
|
||||
* @return
|
||||
* new int[] { 2, 2 }
|
||||
* ));
|
||||
*/
|
||||
public void creerBlocPersonnalise(List<int[]> positions) {
|
||||
try {
|
||||
@@ -126,38 +108,10 @@ public class Grille {
|
||||
}
|
||||
}
|
||||
|
||||
// Collecter les couleurs des blocs voisins
|
||||
List<Integer> couleursVoisines = new ArrayList<>();
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Choisir la couleur suivante en fonction du nombre de blocs déjà créés
|
||||
int couleurIndex = blocs.size() % generatedColors.size();
|
||||
Bloc bloc = new Bloc(generatedColors.get(couleurIndex), couleurIndex);
|
||||
|
||||
// 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]]);
|
||||
}
|
||||
@@ -168,6 +122,9 @@ public class Grille {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Crée des blocs carrés (par exemple pour un sudoku classique).
|
||||
*/
|
||||
public void creerBlocCarre() {
|
||||
try {
|
||||
int blocSize = (int) Math.sqrt(taille);
|
||||
@@ -175,12 +132,12 @@ public class Grille {
|
||||
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 blocCol = 0; blocCol < blocSize; blocCol++) {
|
||||
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 j = 0; j < blocSize; j++) {
|
||||
positions.add(new int[] {
|
||||
@@ -190,8 +147,9 @@ public class Grille {
|
||||
}
|
||||
}
|
||||
|
||||
int couleurIndex = getCouleurDisponible(blocRow, blocCol, blocSize, blocSize);
|
||||
Bloc bloc = new Bloc(QUATRE_COULEURS[couleurIndex], couleurIndex);
|
||||
// Affectation cyclique d'une couleur issue de la palette générée
|
||||
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]]);
|
||||
@@ -205,13 +163,9 @@ public class Grille {
|
||||
}
|
||||
|
||||
/**
|
||||
* Crée des blocs rectangles automatiquement à partir de la taille de la grille
|
||||
* Ne fonctionne pas pour les tailles de grilles qui sont des carrés parfaits
|
||||
*
|
||||
* @param blocRow
|
||||
* @param blocCol
|
||||
* @param blocHeight
|
||||
* @param blocWidth
|
||||
* Crée des blocs rectangulaires automatiquement à partir de la taille de la
|
||||
* grille.
|
||||
* Ne fonctionne pas pour les grilles dont la taille est un carré parfait.
|
||||
*/
|
||||
public void creerBlocRectangulaire(int blocHeight, int blocWidth) {
|
||||
try {
|
||||
@@ -223,12 +177,12 @@ public class Grille {
|
||||
int blocsParLigne = taille / blocWidth;
|
||||
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 blocCol = 0; blocCol < blocsParLigne; blocCol++) {
|
||||
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 j = 0; j < blocWidth; j++) {
|
||||
positions.add(new int[] {
|
||||
@@ -238,8 +192,8 @@ public class Grille {
|
||||
}
|
||||
}
|
||||
|
||||
int couleurIndex = getCouleurDisponible(blocRow, blocCol, blocHeight, blocWidth);
|
||||
Bloc bloc = new Bloc(QUATRE_COULEURS[couleurIndex], couleurIndex);
|
||||
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]]);
|
||||
@@ -258,47 +212,51 @@ public class Grille {
|
||||
}
|
||||
}
|
||||
|
||||
public void askSetSymbolesPossibles() {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
try {
|
||||
Console.infoln("Choisissez le type de symboles :");
|
||||
Console.infoln("1. Nombres");
|
||||
Console.infoln("2. Lettres");
|
||||
Console.infoln("3. Texte/Emoji");
|
||||
public static int choisirTypeSymbole(Scanner scanner) {
|
||||
while (true) {
|
||||
System.out.println("Choisissez le type de symbole :");
|
||||
System.out.println("1 : Entiers");
|
||||
System.out.println("2 : Lettres");
|
||||
System.out.println("3 : Chaînes de caractères / Emoji");
|
||||
|
||||
int choix = 0;
|
||||
String input = scanner.nextLine();
|
||||
try {
|
||||
choix = Integer.parseInt(scanner.nextLine());
|
||||
if (choix < 1 || choix > 3) {
|
||||
throw new NumberFormatException("Choix invalide");
|
||||
int choix = Integer.parseInt(input);
|
||||
if (choix >= 1 && choix <= 3) {
|
||||
return choix;
|
||||
} else {
|
||||
System.out.println("Choix invalide. Veuillez entrer 1, 2 ou 3.");
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
Console.errorln("Choix invalide");
|
||||
return;
|
||||
System.out.println("Entrée invalide. Veuillez entrer un nombre.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void askSetSymbolesPossibles(int choix, Scanner scanner) {
|
||||
try {
|
||||
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();
|
||||
|
||||
switch (choix) {
|
||||
case 1: // Nombres
|
||||
Symbole intTemp = Symbole.of(input);
|
||||
if (intTemp.isInt()) {
|
||||
try {
|
||||
Symbole intTemp = Symbole.of(Integer.parseInt(input));
|
||||
if (symbolesPossibles.contains(intTemp)) {
|
||||
Console.errorln("Ce symbole existe déjà, veuillez entrer un autre symbole");
|
||||
i--;
|
||||
} else {
|
||||
symbolesPossibles.add(intTemp);
|
||||
}
|
||||
} else {
|
||||
} catch (NumberFormatException e) {
|
||||
Console.errorln("Veuillez entrer un nombre valide");
|
||||
i--;
|
||||
}
|
||||
break;
|
||||
|
||||
case 2: // Lettres
|
||||
if (input.length() == 1 && Symbole.of(input.charAt(0)).isLetter()) {
|
||||
if (input.length() == 1 && Character.isLetter(input.charAt(0))) {
|
||||
Symbole charTemp = Symbole.of(input.charAt(0));
|
||||
if (symbolesPossibles.contains(charTemp)) {
|
||||
Console.errorln("Ce symbole existe déjà, veuillez entrer un autre symbole");
|
||||
@@ -327,10 +285,11 @@ public class Grille {
|
||||
|
||||
default:
|
||||
Console.errorln("Type non supporté");
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("Une erreur est survenue : " + e.getMessage());
|
||||
Console.errorln("Une erreur est survenue : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -364,9 +323,9 @@ public class Grille {
|
||||
}
|
||||
|
||||
/**
|
||||
* Vérifie si toutes les contraintes sont respectées
|
||||
* S'arrête dès qu'une contrainte n'est pas respectée
|
||||
*
|
||||
* Vérifie si toutes les contraintes sont respectées.
|
||||
* S'arrête dès qu'une contrainte n'est pas respectée.
|
||||
*
|
||||
* @param contraintes
|
||||
* @return
|
||||
*/
|
||||
@@ -413,51 +372,35 @@ public class Grille {
|
||||
}
|
||||
|
||||
@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() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int maxLen = getLongueurSymboleLePlusLong(); // longueur maximale des symboles
|
||||
|
||||
for (int i = 0; i < taille; i++) {
|
||||
for (int j = 0; j < taille; 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); // nécessite Java 11 ou ultérieur
|
||||
|
||||
Bloc bloc = findBlocForCase(currentCase);
|
||||
|
||||
if (bloc != null) {
|
||||
sb.append(bloc.getCouleur()) // Couleur du bloc
|
||||
.append(currentCase.toString()) // Contenu de la case
|
||||
.append("\u001B[0m ") // Réinitialiser la couleur
|
||||
sb.append(bloc.getCouleur()) // couleur du bloc
|
||||
.append(cellStr)
|
||||
.append(padding)
|
||||
.append("\u001B[0m ") // réinitialiser la couleur
|
||||
.append(" ");
|
||||
} else {
|
||||
sb.append(currentCase.toString()).append(" ");
|
||||
sb.append(cellStr)
|
||||
.append(padding)
|
||||
.append(" ");
|
||||
}
|
||||
}
|
||||
sb.append("\n");
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -50,16 +50,101 @@ public class Sudoku {
|
||||
Sudoku sudoku = new Sudoku(tailleGrille);
|
||||
// ETAPE 2 : SYMBOLE POSSIBLE
|
||||
System.out.println("ETAPE 2 : Choisir les symboles possibles");
|
||||
sudoku.getGrille().askSetSymbolesPossibles(); // demande à l'utilisateur de saisir ses symboles
|
||||
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);
|
||||
System.out.println("Voici votre sudoku : ");
|
||||
setValeursGrille(sudoku, scanner, tailleGrille, typeSymbole);
|
||||
System.out.println("Voici votre sudoku rempli :");
|
||||
System.out.println(sudoku.getGrille().toString());
|
||||
// System.out.println("ETAPE 4 : Choisir type de grille");
|
||||
// ETAPE 4 : CHOIX DES CONTRAINTES
|
||||
choixContraintes(sudoku, scanner);
|
||||
// ETAPE 5 : RESOLUTION
|
||||
System.out.println("ETAPE 5 : Résolution du sudoku");
|
||||
choixResolution(sudoku, scanner);
|
||||
|
||||
return sudoku;
|
||||
}
|
||||
|
||||
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(sudoku);
|
||||
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("Résolution du sudoku.");
|
||||
// resolutionSudoku(sudoku);
|
||||
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) {
|
||||
@@ -75,14 +160,14 @@ public class Sudoku {
|
||||
return tailleGrille;
|
||||
}
|
||||
|
||||
private static Sudoku setValeursGrille(Sudoku sudoku, Scanner scanner, int tailleGrille) {
|
||||
private static Sudoku setValeursGrille(Sudoku sudoku, Scanner scanner, int tailleGrille, int typeSymbole) {
|
||||
// Etude de la taille de la grille pour choisir le type de génération
|
||||
if (carreParfait(tailleGrille)) {
|
||||
System.out.println(tailleGrille + " est un carré parfait.");
|
||||
sudoku.getGrille().creerBlocCarre();
|
||||
} else {
|
||||
while (true) {
|
||||
System.out.println("Veillez faire votre choix");
|
||||
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");
|
||||
|
||||
@@ -93,16 +178,24 @@ public class Sudoku {
|
||||
// Entrer les blocs manuellement
|
||||
System.out.println("Entrez les blocs manuellement.");
|
||||
creationBlocMannuel(sudoku, scanner, tailleGrille);
|
||||
|
||||
break;
|
||||
case 2:
|
||||
// Entrer les blocs à l'aide de la longueur et de la largeur
|
||||
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 = scanner.nextInt();
|
||||
System.out.println("Entrez la largeur du bloc :");
|
||||
int largeurBloc = scanner.nextInt();
|
||||
sudoku.getGrille().creerBlocRectangulaire(longueurBloc, largeurBloc);
|
||||
boolean validDimensions = false;
|
||||
while (!validDimensions) {
|
||||
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);
|
||||
validDimensions = true; // Sortir de la boucle si aucune exception n'est lancée
|
||||
} catch (IllegalArgumentException e) {
|
||||
Console.errorln("Erreur : " + e.getMessage());
|
||||
Console.errorln("Veuillez entrer des dimensions valides.");
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Console.errorln("Choix invalide.");
|
||||
@@ -111,13 +204,16 @@ public class Sudoku {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// DEBUT DU REMPLISSAGE DU SUDOKU
|
||||
String input;
|
||||
while (true) {
|
||||
// Demander et vérifier la ligne
|
||||
int ligne = -1;
|
||||
while (true) {
|
||||
System.out.println("Entrez le numéro de ligne (ou tapez ESC pour quitter) :");
|
||||
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
|
||||
@@ -158,20 +254,43 @@ public class Sudoku {
|
||||
// Demander et vérifier le symbole
|
||||
String symbole = "";
|
||||
while (true) {
|
||||
System.out.println("Entrez le numéro du symbole :");
|
||||
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
|
||||
}
|
||||
break; // Sortir de la boucle si le symbole est valide
|
||||
try {
|
||||
Symbole s = null;
|
||||
switch (typeSymbole) {
|
||||
case 1:
|
||||
// Entiers
|
||||
s = Symbole.of(Integer.parseInt(symbole));
|
||||
break;
|
||||
case 2:
|
||||
// Lettres
|
||||
if (symbole.length() == 1 && Character.isLetter(symbole.charAt(0))) {
|
||||
s = Symbole.of(symbole.charAt(0));
|
||||
} else {
|
||||
throw new IllegalArgumentException("Veuillez entrer une seule lettre.");
|
||||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
// Assigner la valeur dans la grille
|
||||
sudoku.getGrille().setCase(ligne, colonne, Symbole.of(symbole));
|
||||
System.out.println(sudoku.getGrille().toString());
|
||||
}
|
||||
|
||||
return sudoku;
|
||||
}
|
||||
|
||||
|
||||
67
app/src/main/java/sudoku/gui/ColorGenerator.java
Normal file
67
app/src/main/java/sudoku/gui/ColorGenerator.java
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -1,30 +1,27 @@
|
||||
package sudoku;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import sudoku.core.Console;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.util.ArrayList;
|
||||
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 {
|
||||
|
||||
/**
|
||||
* Test the creation of a Sudoku grid with a size of 16
|
||||
*/
|
||||
@Test
|
||||
public void testExample() {
|
||||
// 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)
|
||||
String simulatedInput = "1\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n";
|
||||
// First 1 is to select the int type of Symboles and after, Symboles are entered
|
||||
// (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()));
|
||||
|
||||
// Create a new Sudoku
|
||||
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
|
||||
ArrayList<Symbole> expectedSymbols = new ArrayList<>(Arrays.asList(
|
||||
@@ -48,79 +45,23 @@ public class AppTest {
|
||||
|
||||
// Cleanup
|
||||
System.setIn(System.in);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContraintes() {
|
||||
// Create a new Sudoku
|
||||
Sudoku sudoku = new Sudoku(16);
|
||||
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(11),
|
||||
Symbole.of(12),
|
||||
Symbole.of(13),
|
||||
Symbole.of(14),
|
||||
Symbole.of(15),
|
||||
Symbole.of(16))));
|
||||
int taille = 16;
|
||||
for (int i = 0; i < taille; i++) {
|
||||
sudoku.getGrille().setCase(i, i, expectedSymbols.get(i));
|
||||
}
|
||||
|
||||
sudoku.getGrille().creerBlocCarre();
|
||||
|
||||
// Add constraints
|
||||
sudoku.ajouterContrainte(new ContrainteLigne());
|
||||
sudoku.ajouterContrainte(new ContrainteColonne());
|
||||
// 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();
|
||||
sudoku.ajouterContrainte(new ContrainteBloc());
|
||||
|
||||
System.out.println(sudoku.getGrille().toString());
|
||||
|
||||
// Verify that the constraints are respected
|
||||
// assertTrue(sudoku.estValide(sudoku.getGrille().getCase(0, 0)));
|
||||
// assertTrue(sudoku.estValide(sudoku.getGrille().getCase(1, 1)));
|
||||
// assertTrue(sudoku.estValide(sudoku.getGrille().getCase(2, 2)));
|
||||
|
||||
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)));
|
||||
Resolveur resolveur = new ResolveurBacktraceSimple(sudoku);
|
||||
resolveur.resoudre(sudoku, false);
|
||||
System.out.println("Sudoku résolu :");
|
||||
System.out.println(sudoku.getGrille().toString());
|
||||
}
|
||||
}
|
||||
@@ -34,13 +34,11 @@ public class TestBlocRectangle {
|
||||
sudoku.getGrille().setCase(0, 3, Symbole.of(9));
|
||||
sudoku.getGrille().setCase(3, 0, Symbole.of(4));
|
||||
|
||||
|
||||
sudoku.getGrille().creerBlocCarre();
|
||||
|
||||
sudoku.getGrille().creerBlocRectangulaire(3, 5);
|
||||
|
||||
System.out.println("Sudoku :");
|
||||
System.out.println(sudoku.getGrille().toString());
|
||||
|
||||
sudoku.getGrille().creerBlocRectangulaire(3, 5);
|
||||
System.out.println("Blocs :");
|
||||
sudoku.getGrille().printBlocs();
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import java.util.Arrays;
|
||||
public class TestCreationGrille {
|
||||
@Test
|
||||
public void testLigne() {
|
||||
//CREATION D'UN SUDOKU SIMPLE 12*12
|
||||
// CREATION D'UN SUDOKU SIMPLE 12*12
|
||||
Sudoku sudoku = new Sudoku(12);
|
||||
sudoku.getGrille().setSymbolesPossibles(new ArrayList<>(Arrays.asList(
|
||||
Symbole.of(1),
|
||||
@@ -85,7 +85,13 @@ public class TestCreationGrille {
|
||||
sudoku.getGrille().setCase(11, 6, Symbole.of(11));
|
||||
sudoku.getGrille().setCase(11, 8, Symbole.of(6));
|
||||
sudoku.getGrille().setCase(11, 11, Symbole.of(3));
|
||||
// TODO
|
||||
|
||||
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 :");
|
||||
|
||||
@@ -7,10 +7,10 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class TestBloc {
|
||||
public class TestDoublonBloc {
|
||||
@Test
|
||||
public void testBloc() {
|
||||
System.out.println("TEST BLOC : ");
|
||||
System.out.println("TEST DOUBLON BLOC : ");
|
||||
System.out.println(new App().getGreeting());
|
||||
// Create a new Sudoku
|
||||
Sudoku sudoku = new Sudoku(9);
|
||||
@@ -51,6 +51,6 @@ public class TestBloc {
|
||||
|
||||
System.out.println("Symboles possibles :");
|
||||
sudoku.getGrille().printSymbolesPossibles();
|
||||
System.out.println("FIN TEST BLOC");
|
||||
System.out.println("FIN TEST DOUBLON BLOC");
|
||||
}
|
||||
}
|
||||
@@ -7,10 +7,10 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class TestColonne {
|
||||
public class TestDoublonColonne {
|
||||
@Test
|
||||
public void testColonne() {
|
||||
System.out.println("TEST COL : ");
|
||||
System.out.println("TEST DOUBLON COLONNE : ");
|
||||
System.out.println(new App().getGreeting());
|
||||
// Create a new Sudoku
|
||||
Sudoku sudoku = new Sudoku(9);
|
||||
@@ -50,6 +50,6 @@ public class TestColonne {
|
||||
|
||||
System.out.println("Symboles possibles :");
|
||||
sudoku.getGrille().printSymbolesPossibles();
|
||||
System.out.println("FIN TEST COL");
|
||||
System.out.println("FIN TEST DOUBLON COLONNE");
|
||||
}
|
||||
}
|
||||
@@ -5,10 +5,10 @@ import org.junit.jupiter.api.Test;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class TestLigne {
|
||||
public class TestDoublonLigne {
|
||||
@Test
|
||||
public void testLigne() {
|
||||
System.out.println("TEST LIGNE : ");
|
||||
public void testDoublonLigne() {
|
||||
System.out.println("TEST DOUBLON LIGNE : ");
|
||||
System.out.println(new App().getGreeting());
|
||||
// Create a new Sudoku
|
||||
Sudoku sudoku = new Sudoku(9);
|
||||
@@ -47,6 +47,6 @@ public class TestLigne {
|
||||
|
||||
System.out.println("Symboles possibles :");
|
||||
sudoku.getGrille().printSymbolesPossibles();
|
||||
System.out.println("FIN TEST LIGNE");
|
||||
System.out.println("FIN TEST DOUBLON LIGNE");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user