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();
|
||||
|
||||
69
app/src/test/java/sudoku/TestBlocParticuliers.java
Normal file
69
app/src/test/java/sudoku/TestBlocParticuliers.java
Normal file
@@ -0,0 +1,69 @@
|
||||
package sudoku;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestBlocParticuliers {
|
||||
|
||||
@Test
|
||||
public void blocParticuliers() {
|
||||
System.out.println("TEST BLOC PARTICULIERS : ");
|
||||
System.out.println(new App().getGreeting());
|
||||
// Create a new Sudoku
|
||||
Sudoku sudoku = new Sudoku(4);
|
||||
|
||||
sudoku.getGrille().setSymbolesPossibles(new ArrayList<>(Arrays.asList(
|
||||
Symbole.of(1),
|
||||
Symbole.of(2),
|
||||
Symbole.of(3),
|
||||
Symbole.of(4))));
|
||||
|
||||
sudoku.ajouterContrainte(new ContrainteBloc());
|
||||
sudoku.ajouterContrainte(new ContrainteLigne());
|
||||
sudoku.ajouterContrainte(new ContrainteColonne());
|
||||
|
||||
sudoku.getGrille().setCase(0, 0, Symbole.of(1));
|
||||
sudoku.getGrille().setCase(3, 1, Symbole.of(2));
|
||||
sudoku.getGrille().setCase(2, 2, Symbole.of(3));
|
||||
|
||||
sudoku.getGrille().creerBlocPersonnalise(Arrays.asList(
|
||||
new int[] { 0, 0 },
|
||||
new int[] { 3, 3 },
|
||||
new int[] { 0, 1 },
|
||||
new int[] { 2, 3 }));
|
||||
sudoku.getGrille().creerBlocPersonnalise(Arrays.asList(
|
||||
new int[] { 0, 2 },
|
||||
new int[] { 1, 3 },
|
||||
new int[] { 1, 2 },
|
||||
new int[] { 3, 1 }));
|
||||
sudoku.getGrille().creerBlocPersonnalise(Arrays.asList(
|
||||
new int[] { 1, 1 },
|
||||
new int[] { 1, 0 },
|
||||
new int[] { 0, 3 },
|
||||
new int[] { 3, 0 }));
|
||||
sudoku.getGrille().creerBlocPersonnalise(Arrays.asList(
|
||||
new int[] { 2, 0 },
|
||||
new int[] { 2, 1 },
|
||||
new int[] { 2, 2 },
|
||||
new int[] { 3, 2 }));
|
||||
|
||||
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();
|
||||
|
||||
ResolveurBacktraceSimple resolveur = new ResolveurBacktraceSimple(sudoku);
|
||||
resolveur.resoudre(sudoku, true);
|
||||
|
||||
System.out.println("Sudoku résolu :");
|
||||
System.out.println(sudoku.getGrille().toString());
|
||||
|
||||
System.out.println("FIN TEST BLOC PARTICULIERS");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user