Squashed commit of the following:
commit862ff6e08dAuthor: Morph01 <thibaut6969delastreet@gmail.com> Date: Mon Jan 20 12:49:38 2025 +0100 feat: affichage de la grille colorée et vérification de tt les contraintes commitb1dde68ec1Author: Morph01 <thibaut6969delastreet@gmail.com> Date: Sun Jan 5 11:16:34 2025 +0100 feat: add constraint management and validation to Sudoku class commitd8486a3bd7Author: Morph01 <thibaut6969delastreet@gmail.com> Date: Sun Jan 5 11:13:19 2025 +0100 feat: implement constraint classes for Sudoku (row, column, block) commit8945072074Author: Morph01 <thibaut6969delastreet@gmail.com> Date: Sun Jan 5 11:12:59 2025 +0100 refactor: rename methods to French for consistency in the Sudoku application commite35123e9feAuthor: Morph01 <thibaut6969delastreet@gmail.com> Date: Sun Jan 5 11:11:40 2025 +0100 fix: update build.gradle to use testRuntimeOnly for JUnit Jupiter engine
This commit is contained in:
@@ -42,11 +42,53 @@ public class Grille {
|
||||
return cases[ligne][colonne];
|
||||
}
|
||||
|
||||
private static final String[] QUATRE_COULEURS = {
|
||||
"\u001B[31m", // Rouge
|
||||
"\u001B[32m", // Vert
|
||||
"\u001B[34m", // Bleu
|
||||
"\u001B[33m" // Jaune
|
||||
};
|
||||
|
||||
private int getCouleurDisponible(int blocRow, int blocCol) {
|
||||
List<Integer> couleursUtilisees = new ArrayList<>();
|
||||
int blocSize = (int) Math.sqrt(taille);
|
||||
|
||||
// Parcourir les voisins (haut, bas, gauche, droite, et diagonaux)
|
||||
for (int dRow = -1; dRow <= 1; dRow++) {
|
||||
for (int dCol = -1; dCol <= 1; dCol++) {
|
||||
if (dRow == 0 && dCol == 0)
|
||||
continue; // Ignorer le bloc courant
|
||||
|
||||
int voisinRow = blocRow + dRow;
|
||||
int voisinCol = blocCol + dCol;
|
||||
|
||||
// Vérifier si le voisin est dans les limites
|
||||
if (voisinRow >= 0 && voisinRow < taille / blocSize &&
|
||||
voisinCol >= 0 && voisinCol < taille / blocSize) {
|
||||
int blockIndex = voisinRow * (taille / blocSize) + voisinCol;
|
||||
if (blockIndex < blocs.size()) {
|
||||
couleursUtilisees.add(blocs.get(blockIndex).getCouleurIndex());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Trouver une couleur non utilisée
|
||||
for (int c = 0; c < QUATRE_COULEURS.length; c++) {
|
||||
if (!couleursUtilisees.contains(c)) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
// Retourner une couleur par défaut (ne devrait pas arriver avec 4 couleurs)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Crée un bloc à partir des positions spécifiées
|
||||
*
|
||||
* Exemple :
|
||||
* sudoku.getGrille().createBloc(Arrays.asList(
|
||||
* sudoku.getGrille().creerBloc(Arrays.asList(
|
||||
* new int[] { 0, 0 },
|
||||
* new int[] { 0, 1 },
|
||||
* new int[] { 0, 2 },
|
||||
@@ -60,30 +102,49 @@ public class Grille {
|
||||
* @param positions
|
||||
* @return
|
||||
*/
|
||||
public Bloc createBloc(List<int[]> positions) {
|
||||
Bloc bloc = new Bloc();
|
||||
public Bloc creerBloc(List<int[]> positions) {
|
||||
int i = positions.get(0)[0];
|
||||
int j = positions.get(0)[1];
|
||||
int blocSize = (int) Math.sqrt(taille);
|
||||
int couleurIndex = getCouleurDisponible(i / blocSize, j / blocSize);
|
||||
|
||||
Bloc bloc = new Bloc(QUATRE_COULEURS[couleurIndex], couleurIndex);
|
||||
for (int[] pos : positions) {
|
||||
bloc.addCase(cases[pos[0]][pos[1]]);
|
||||
bloc.ajouterCase(cases[pos[0]][pos[1]]);
|
||||
}
|
||||
blocs.add(bloc);
|
||||
return bloc;
|
||||
}
|
||||
|
||||
public void createSquareBlocs() {
|
||||
public void creerBlocCarre() {
|
||||
try {
|
||||
int blocSize = (int) Math.sqrt(taille);
|
||||
if (blocSize * blocSize != taille) {
|
||||
throw new IllegalArgumentException("La taille de la grille doit être un carré parfait.");
|
||||
}
|
||||
for (int i = 0; i < taille; i += blocSize) {
|
||||
for (int j = 0; j < taille; j += blocSize) {
|
||||
|
||||
// Create blocks in 3x3 pattern
|
||||
for (int blocRow = 0; blocRow < blocSize; blocRow++) {
|
||||
for (int blocCol = 0; blocCol < blocSize; blocCol++) {
|
||||
List<int[]> positions = new ArrayList<>();
|
||||
for (int k = 0; k < blocSize; k++) {
|
||||
for (int l = 0; l < blocSize; l++) {
|
||||
positions.add(new int[] { i + k, j + l });
|
||||
|
||||
// Add all positions for current block
|
||||
for (int i = 0; i < blocSize; i++) {
|
||||
for (int j = 0; j < blocSize; j++) {
|
||||
positions.add(new int[] {
|
||||
blocRow * blocSize + i,
|
||||
blocCol * blocSize + j
|
||||
});
|
||||
}
|
||||
}
|
||||
createBloc(positions);
|
||||
|
||||
int couleurIndex = getCouleurDisponible(blocRow, blocCol);
|
||||
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) {
|
||||
@@ -91,16 +152,6 @@ public class Grille {
|
||||
}
|
||||
}
|
||||
|
||||
// TO MOVE TO FUTUR CONSTRAINTS
|
||||
// public boolean isValid() {
|
||||
// for (Bloc bloc : blocs) {
|
||||
// if (!bloc.isValid()) {
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
|
||||
public void printBlocs() {
|
||||
for (Bloc bloc : blocs) {
|
||||
System.out.println(bloc.toString());
|
||||
@@ -199,15 +250,66 @@ public class Grille {
|
||||
System.out.println(sb.toString());
|
||||
}
|
||||
|
||||
public int getTaille() {
|
||||
return taille;
|
||||
}
|
||||
|
||||
private Bloc findBlocForCase(Case target) {
|
||||
for (Bloc bloc : blocs) {
|
||||
if (bloc.getCases().contains(target)) {
|
||||
return bloc;
|
||||
}
|
||||
}
|
||||
return null; // Ne devrait jamais arriver si la grille est bien construite
|
||||
}
|
||||
|
||||
/**
|
||||
* Vérifie si toutes les contraintes sont respectées
|
||||
* S'arrête dès qu'une contrainte n'est pas respectée
|
||||
* @param contraintes
|
||||
* @return
|
||||
*/
|
||||
public boolean verifierToutesContraintes(List<Contrainte> contraintes) {
|
||||
// Vérifier chaque case de la grille
|
||||
for (int i = 0; i < taille; i++) {
|
||||
for (int j = 0; j < taille; j++) {
|
||||
Case currentCase = cases[i][j];
|
||||
// Ne vérifier que les cases qui ont un symbole
|
||||
if (currentCase.getSymbole() != null) {
|
||||
// Vérifier toutes les contraintes pour cette case
|
||||
for (Contrainte contrainte : contraintes) {
|
||||
if (!contrainte.estRespectee(this, currentCase)) {
|
||||
Console.errorln("Contrainte non respectée à la position : ligne=" + i + ", colonne=" + j);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < taille; i++) {
|
||||
for (int j = 0; j < taille; j++) {
|
||||
sb.append(cases[i][j].toString()).append(" ");
|
||||
Case currentCase = cases[i][j];
|
||||
Bloc bloc = findBlocForCase(currentCase);
|
||||
|
||||
if (bloc != null) {
|
||||
sb.append(bloc.getCouleur()) // Couleur du bloc
|
||||
.append(currentCase.toString()) // Contenu de la case
|
||||
.append("\u001B[0m ") // Réinitialiser la couleur
|
||||
.append(" ");
|
||||
} else {
|
||||
sb.append(currentCase.toString()).append(" ");
|
||||
}
|
||||
}
|
||||
sb.append("\n");
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user