fix: better padding for console rendering

This commit is contained in:
2025-02-07 17:56:27 +01:00
parent a36960c57a
commit dd21b9a13a
2 changed files with 46 additions and 61 deletions

View File

@@ -24,68 +24,25 @@ public class Multidoku {
placements.add(new SudokuPlacement(sudoku, offsetLigne, offsetColonne));
}
// // Méthode daffichage combiné
// public String toStringCombined() {
// // 1. Déterminer la taille globale de laffichage
// 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 laffichage 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 daffichage 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() {
// 1. Déterminer la taille globale de la grille combinée
// 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] = " "; // case vide affichée par défaut
global[i][j] = " ".repeat(cellWidth); // case vide affichée par défaut
}
}
@@ -94,7 +51,6 @@ public class Multidoku {
for (SudokuPlacement sp : placements) {
Grille grille = sp.getSudoku().getGrille();
int taille = grille.getTaille();
int maxLen = grille.getLongueurSymboleLePlusLong(); // pour le padding
for (int i = 0; i < taille; i++) {
for (int j = 0; j < taille; j++) {
// Coordonnées globales calculées à partir de l'offset
@@ -103,22 +59,25 @@ public class Multidoku {
Case currentCase = grille.getCase(i, j);
String cellStr = currentCase.toString();
// Calcul du padding nécessaire pour un alignement uniforme
int pad = maxLen - cellStr.length();
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) {
// 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 + " ";
cellDisplay = cellStr + padding;
}
// Insertion dans la matrice globale
global[globalLigne][globalColonne] = cellDisplay;
}
// Insertion dans la matrice globale (ajout d'un espace pour séparer les
// colonnes)
global[globalLigne][globalColonne] = cellDisplay + " ";
}
}
}
@@ -134,6 +93,20 @@ public class Multidoku {
return sb.toString();
}
/**
* Méthode utilitaire qui vérifie si une case est partagée.
* On parcourt la liste des groupes de cases partagées et si la case y figure,
* on renvoie true.
*/
private boolean isSharedCase(Case c) {
for (List<Case> groupe : casesPartagees) {
if (groupe.contains(c)) {
return true;
}
}
return false;
}
public void ajouterCasesPartagees(List<Case> cases) {
casesPartagees.add(new ArrayList<>(cases));
ajouterContraintePartagee(new ContrainteCasePartagee(cases));
@@ -179,4 +152,14 @@ public class Multidoku {
public List<List<Case>> getCasesPartagees() {
return casesPartagees;
}
public boolean estValide(Case c) {
for (Contrainte contraintePartagee : contraintesPartagees) {
if (!contraintePartagee.estRespectee(null, c)) {
return false;
}
}
return true;
}
}

View File

@@ -26,8 +26,10 @@ public class TestMultidoku {
s2.getGrille().getCase(0, 0));
multidoku.ajouterCasesPartagees(casesPartagees);
multidoku.ajouterContraintePartagee(new ContrainteCasePartagee(casesPartagees));
ArrayList<Symbole> symboles = new ArrayList<>();
for (int i = 1; i <= 9; i++) {
for (int i = 10; i <= 19; i++) {
symboles.add(Symbole.of(i));
}