feat: add bloc particuliers
This commit is contained in:
@@ -116,7 +116,56 @@ public class Grille {
|
||||
* @param positions
|
||||
* @return
|
||||
*/
|
||||
// TODO : Refactor to use BlocBuilder
|
||||
public void creerBlocPersonnalise(List<int[]> positions) {
|
||||
try {
|
||||
// Validation des positions
|
||||
for (int[] pos : positions) {
|
||||
if (pos[0] < 0 || pos[0] >= taille || pos[1] < 0 || pos[1] >= taille) {
|
||||
throw new IllegalArgumentException("Position invalide : (" + pos[0] + ", " + pos[1] + ")");
|
||||
}
|
||||
}
|
||||
|
||||
// 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
Console.errorln(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void creerBlocCarre() {
|
||||
try {
|
||||
@@ -364,28 +413,29 @@ public class Grille {
|
||||
|
||||
@Override
|
||||
// public String toString() {
|
||||
// StringBuilder sb = new StringBuilder();
|
||||
// int plusLongSymbole = this.getLongueurSymboleLePlusLong();
|
||||
// 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);
|
||||
// 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");
|
||||
// }
|
||||
// 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();
|
||||
// return sb.toString();
|
||||
// }
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
Reference in New Issue
Block a user