fix: better padding for console rendering
This commit is contained in:
@@ -24,68 +24,25 @@ public class Multidoku {
|
|||||||
placements.add(new SudokuPlacement(sudoku, offsetLigne, offsetColonne));
|
placements.add(new SudokuPlacement(sudoku, offsetLigne, offsetColonne));
|
||||||
}
|
}
|
||||||
|
|
||||||
// // 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 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 maxLigne = 0, maxColonne = 0;
|
||||||
|
int globalMaxLen = 0;
|
||||||
for (SudokuPlacement sp : placements) {
|
for (SudokuPlacement sp : placements) {
|
||||||
int taille = sp.getSudoku().getGrille().getTaille();
|
int taille = sp.getSudoku().getGrille().getTaille();
|
||||||
maxLigne = Math.max(maxLigne, sp.getOffsetLigne() + taille);
|
maxLigne = Math.max(maxLigne, sp.getOffsetLigne() + taille);
|
||||||
maxColonne = Math.max(maxColonne, sp.getOffsetColonne() + 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
|
// 2. Création et initialisation de la matrice globale
|
||||||
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] = " "; // 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) {
|
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++) {
|
||||||
// Coordonnées globales calculées à partir de l'offset
|
// Coordonnées globales calculées à partir de l'offset
|
||||||
@@ -103,22 +59,25 @@ public class Multidoku {
|
|||||||
|
|
||||||
Case currentCase = grille.getCase(i, j);
|
Case currentCase = grille.getCase(i, j);
|
||||||
String cellStr = currentCase.toString();
|
String cellStr = currentCase.toString();
|
||||||
// Calcul du padding nécessaire pour un alignement uniforme
|
int pad = globalMaxLen - cellStr.length();
|
||||||
int pad = maxLen - cellStr.length();
|
|
||||||
String padding = " ".repeat(pad);
|
String padding = " ".repeat(pad);
|
||||||
|
|
||||||
// Récupérer le bloc associé à la case afin d'obtenir sa couleur
|
// Récupérer le bloc associé à la case afin d'obtenir sa couleur
|
||||||
Bloc bloc = grille.findBlocForCase(currentCase);
|
Bloc bloc = grille.findBlocForCase(currentCase);
|
||||||
String cellDisplay;
|
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) {
|
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";
|
cellDisplay = bloc.getCouleur() + cellStr + padding + "\u001B[0m";
|
||||||
} else {
|
} 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();
|
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) {
|
public void ajouterCasesPartagees(List<Case> cases) {
|
||||||
casesPartagees.add(new ArrayList<>(cases));
|
casesPartagees.add(new ArrayList<>(cases));
|
||||||
ajouterContraintePartagee(new ContrainteCasePartagee(cases));
|
ajouterContraintePartagee(new ContrainteCasePartagee(cases));
|
||||||
@@ -179,4 +152,14 @@ public class Multidoku {
|
|||||||
public List<List<Case>> getCasesPartagees() {
|
public List<List<Case>> getCasesPartagees() {
|
||||||
return casesPartagees;
|
return casesPartagees;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean estValide(Case c) {
|
||||||
|
for (Contrainte contraintePartagee : contraintesPartagees) {
|
||||||
|
if (!contraintePartagee.estRespectee(null, c)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,8 +26,10 @@ public class TestMultidoku {
|
|||||||
s2.getGrille().getCase(0, 0));
|
s2.getGrille().getCase(0, 0));
|
||||||
multidoku.ajouterCasesPartagees(casesPartagees);
|
multidoku.ajouterCasesPartagees(casesPartagees);
|
||||||
|
|
||||||
|
multidoku.ajouterContraintePartagee(new ContrainteCasePartagee(casesPartagees));
|
||||||
|
|
||||||
ArrayList<Symbole> symboles = new ArrayList<>();
|
ArrayList<Symbole> symboles = new ArrayList<>();
|
||||||
for (int i = 1; i <= 9; i++) {
|
for (int i = 10; i <= 19; i++) {
|
||||||
symboles.add(Symbole.of(i));
|
symboles.add(Symbole.of(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user