fin de la méthode pour vérifier la conformité de la grille

This commit is contained in:
2024-05-01 19:15:58 +02:00
parent 040d8362c1
commit 461b7d488f
2 changed files with 70 additions and 47 deletions

View File

@@ -71,13 +71,13 @@ public class SudokuGrid extends JFrame {
VerifButton verifyer = new VerifButton(GRID_SIZE, grid);
verify.addActionListener(verifyer);
verify.addActionListener(verifyer);
bouton.add(load);
bouton.add(save);
bouton.add(verify);
bouton.add(verify);
// Ajout des panneaux à la fenetre
getContentPane().add(gridPanel, BorderLayout.CENTER);

View File

@@ -1,5 +1,6 @@
import javax.swing.*;
import java.awt.event.*;
import java.awt.Color;
public class VerifButton implements ActionListener {
private int GRID_SIZE;
@@ -10,63 +11,85 @@ public class VerifButton implements ActionListener {
this.grid = grid;
}
@Override
@Override
public void actionPerformed(ActionEvent e) {
if (grilleValide()) {
JOptionPane.showMessageDialog(null, "La grille est valide.", "Validation", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "La grille n'est pas valide.", "Erreur", JOptionPane.ERROR_MESSAGE);
}
}
}
private boolean grilleValide() {
for (int i = 0; i < this.GRID_SIZE; i++) {
for (int j = 0; j < this.GRID_SIZE; j++) {
if (!grid[i][j].getText().isEmpty()) {
int evaluant = Integer.parseInt(grid[i][j].getText());
for (int row = 0; row < this.GRID_SIZE; row++) {
for (int col = 0; col < this.GRID_SIZE; col++) {
if (!grid[row][col].getText().isEmpty()) {
int evaluant = Integer.parseInt(grid[row][col].getText());
// Test de validité sur les lignes
for (int x = i + 1; x < this.GRID_SIZE; x++) {
int comparateur = grid[x][j].getText().isEmpty() ? 0 : Integer.parseInt(grid[x][j].getText());
if (evaluant == comparateur) {
return false;
}
}
for (int x = i - 1; x >= 0; x--) {
int comparateur = grid[x][j].getText().isEmpty() ? 0 : Integer.parseInt(grid[x][j].getText());
if (evaluant == comparateur) {
return false;
}
}
for (int row2 = row + 1; row2 < this.GRID_SIZE; row2++) {
if (!grid[row2][col].getText().isEmpty()) {
int comparateur = Integer.parseInt(grid[row2][col].getText());
if (evaluant == comparateur) {
grid[row2][col].setBackground(Color.red);
grid[row][col].setBackground(Color.red);
return false;
}
}
}
for (int row2 = row - 1; row2 >= 0; row2--) {
if (!grid[row2][col].getText().isEmpty()) {
int comparateur = Integer.parseInt(grid[row2][col].getText());
if (evaluant == comparateur) {
grid[row2][col].setBackground(Color.red);
grid[row][col].setBackground(Color.red);
return false;
}
}
}
// Test de validité sur les colonnes
for (int x = j + 1; x < this.GRID_SIZE; x++) {
int comparateur = grid[i][x].getText().isEmpty() ? 0 : Integer.parseInt(grid[i][x].getText());
if (evaluant == comparateur) {
return false;
}
}
for (int x = j - 1; x >= 0; x--) {
int comparateur = grid[i][x].getText().isEmpty() ? 0 : Integer.parseInt(grid[i][x].getText());
if (evaluant == comparateur) {
return false;
}
}
for (int col2 = col + 1; col2 < this.GRID_SIZE; col2++) {
if (!grid[row][col2].getText().isEmpty()) {
int comparateur = Integer.parseInt(grid[row][col2].getText());
// Vérifier la validité dans les cases adjacentes
for (int row = i - 1; row <= i + 1; row++) {
for (int col = j - 1; col <= j + 1; col++) {
// Exclure la case actuelle
if (row == i && col == j) {
continue;
}
// Vérifier que la case est dans les limites de la grille
if (row >= 0 && row < GRID_SIZE && col >= 0 && col < GRID_SIZE) {
// Vérifier la validité des regions
if (!grid[row][col].getText().isEmpty()) {
int comp = Integer.parseInt(grid[row][col].getText());
if (evaluant == comp) {
return false;
if (evaluant == comparateur) {
grid[row][col2].setBackground(Color.red);
grid[row][col].setBackground(Color.red);
return false;
}
}
}
for (int col2 = col - 1; col2 >= 0; col2--) {
if (!grid[row][col2].getText().isEmpty()) {
int comparateur = Integer.parseInt(grid[row][col2].getText());
if (evaluant == comparateur) {
grid[row][col2].setBackground(Color.red);
grid[row][col].setBackground(Color.red);
return false;
}
}
}
// Vérifier la validité dans les régions
int rowregion = row/3*3;
int colregion = col/3*3;
for (int row2 = rowregion; row2 < rowregion + 3; row2++) {
for (int col2 = colregion; col2 < colregion + 3; col2++) {
if (row2 != row && col2 != col ) {
if (!grid[row2][col2].getText().isEmpty()) {
int comparateur = Integer.parseInt(grid[row2][col2].getText());
if (evaluant == comparateur) {
grid[row2][col2].setBackground(Color.red);
grid[row][col].setBackground(Color.red);
return false;
}
}
}
@@ -75,6 +98,6 @@ public class VerifButton implements ActionListener {
}
}
}
return true;
return true;
}
}