feat: colors in multidokus !
This commit is contained in:
@@ -313,7 +313,7 @@ public class Grille {
|
|||||||
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;
|
||||||
|
|||||||
@@ -24,9 +24,56 @@ public class Multidoku {
|
|||||||
placements.add(new SudokuPlacement(sudoku, offsetLigne, offsetColonne));
|
placements.add(new SudokuPlacement(sudoku, offsetLigne, offsetColonne));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Méthode d’affichage combiné
|
// // Méthode d’affichage combiné
|
||||||
|
// public String toStringCombined() {
|
||||||
|
// // 1. Déterminer la taille globale de l’affichage
|
||||||
|
// int maxLigne = 0, maxColonne = 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);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // 2. Créer une matrice globale et l'initialiser (ici avec "-" pour une case
|
||||||
|
// // vide)
|
||||||
|
// String[][] global = new String[maxLigne][maxColonne];
|
||||||
|
// for (int i = 0; i < maxLigne; i++) {
|
||||||
|
// for (int j = 0; j < maxColonne; j++) {
|
||||||
|
// global[i][j] = " "; // ou " " selon votre choix
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // 3. Pour chaque sudoku, placer l’affichage 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++) {
|
||||||
|
// // Calcul des coordonnées globales
|
||||||
|
// int globalLigne = sp.getOffsetLigne() + i;
|
||||||
|
// int globalColonne = sp.getOffsetColonne() + j;
|
||||||
|
// // Ici, on récupère la représentation de la case via toString (possiblement
|
||||||
|
// avec
|
||||||
|
// // la couleur)
|
||||||
|
// global[globalLigne][globalColonne] = grille.getCase(i, j).toString();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // 4. Construire la chaîne finale d’affichage ligne par ligne
|
||||||
|
// StringBuilder sb = new StringBuilder();
|
||||||
|
// for (int i = 0; i < maxLigne; i++) {
|
||||||
|
// for (int j = 0; j < maxColonne; j++) {
|
||||||
|
// sb.append(global[i][j]).append(" ");
|
||||||
|
// }
|
||||||
|
// sb.append("\n");
|
||||||
|
// }
|
||||||
|
// return sb.toString();
|
||||||
|
// }
|
||||||
|
|
||||||
public String toStringCombined() {
|
public String toStringCombined() {
|
||||||
// 1. Déterminer la taille globale de l’affichage
|
// 1. Déterminer la taille globale de la grille combinée
|
||||||
int maxLigne = 0, maxColonne = 0;
|
int maxLigne = 0, maxColonne = 0;
|
||||||
for (SudokuPlacement sp : placements) {
|
for (SudokuPlacement sp : placements) {
|
||||||
int taille = sp.getSudoku().getGrille().getTaille();
|
int taille = sp.getSudoku().getGrille().getTaille();
|
||||||
@@ -34,36 +81,53 @@ public class Multidoku {
|
|||||||
maxColonne = Math.max(maxColonne, sp.getOffsetColonne() + taille);
|
maxColonne = Math.max(maxColonne, sp.getOffsetColonne() + taille);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Créer une matrice globale et l'initialiser (ici avec "-" pour une case
|
// 2. Création et initialisation de la matrice globale
|
||||||
// vide)
|
|
||||||
String[][] global = new String[maxLigne][maxColonne];
|
String[][] global = new String[maxLigne][maxColonne];
|
||||||
for (int i = 0; i < maxLigne; i++) {
|
for (int i = 0; i < maxLigne; i++) {
|
||||||
for (int j = 0; j < maxColonne; j++) {
|
for (int j = 0; j < maxColonne; j++) {
|
||||||
global[i][j] = " "; // ou " " selon votre choix
|
global[i][j] = " "; // case vide affichée par défaut
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Pour chaque sudoku, placer l’affichage de ses cases dans la grille globale
|
// 3. Pour chaque sudoku, placer l'affichage de chacune de ses cases dans la
|
||||||
|
// grille globale
|
||||||
for (SudokuPlacement sp : placements) {
|
for (SudokuPlacement sp : placements) {
|
||||||
Grille grille = sp.getSudoku().getGrille();
|
Grille grille = sp.getSudoku().getGrille();
|
||||||
int taille = grille.getTaille();
|
int taille = grille.getTaille();
|
||||||
|
int maxLen = grille.getLongueurSymboleLePlusLong(); // pour le padding
|
||||||
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++) {
|
||||||
// Calcul des coordonnées globales
|
// Coordonnées globales calculées à partir de l'offset
|
||||||
int globalLigne = sp.getOffsetLigne() + i;
|
int globalLigne = sp.getOffsetLigne() + i;
|
||||||
int globalColonne = sp.getOffsetColonne() + j;
|
int globalColonne = sp.getOffsetColonne() + j;
|
||||||
// Ici, on récupère la représentation de la case via toString (possiblement avec
|
|
||||||
// la couleur)
|
Case currentCase = grille.getCase(i, j);
|
||||||
global[globalLigne][globalColonne] = grille.getCase(i, j).toString();
|
String cellStr = currentCase.toString();
|
||||||
|
// Calcul du padding nécessaire pour un alignement uniforme
|
||||||
|
int pad = maxLen - 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;
|
||||||
|
if (bloc != null) {
|
||||||
|
// Concaténation de la couleur, du symbole, du padding, et du code de
|
||||||
|
// réinitialisation
|
||||||
|
cellDisplay = bloc.getCouleur() + cellStr + padding + "\u001B[0m ";
|
||||||
|
} else {
|
||||||
|
cellDisplay = cellStr + padding + " ";
|
||||||
|
}
|
||||||
|
// Insertion dans la matrice globale
|
||||||
|
global[globalLigne][globalColonne] = cellDisplay;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Construire la chaîne finale d’affichage ligne par ligne
|
// 4. Construction de la chaîne d'affichage finale
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (int i = 0; i < maxLigne; i++) {
|
for (int i = 0; i < maxLigne; i++) {
|
||||||
for (int j = 0; j < maxColonne; j++) {
|
for (int j = 0; j < maxColonne; j++) {
|
||||||
sb.append(global[i][j]).append(" ");
|
sb.append(global[i][j]);
|
||||||
}
|
}
|
||||||
sb.append("\n");
|
sb.append("\n");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,10 @@ public class TestMultidoku {
|
|||||||
sudoku.ajouterContrainte(new ContrainteColonne());
|
sudoku.ajouterContrainte(new ContrainteColonne());
|
||||||
sudoku.ajouterContrainte(new ContrainteBloc());
|
sudoku.ajouterContrainte(new ContrainteBloc());
|
||||||
sudoku.getGrille().creerBlocCarre();
|
sudoku.getGrille().creerBlocCarre();
|
||||||
|
|
||||||
|
for (int i = 0; i < sudoku.getGrille().getTaille(); i++) {
|
||||||
|
sudoku.getGrille().setCase(i, i, symboles.get(i));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("Sudoku 1 :");
|
System.out.println("Sudoku 1 :");
|
||||||
|
|||||||
Reference in New Issue
Block a user