fin de la méthode pour vérifier la conformité de la grille
This commit is contained in:
@@ -71,13 +71,13 @@ public class SudokuGrid extends JFrame {
|
|||||||
|
|
||||||
VerifButton verifyer = new VerifButton(GRID_SIZE, grid);
|
VerifButton verifyer = new VerifButton(GRID_SIZE, grid);
|
||||||
|
|
||||||
verify.addActionListener(verifyer);
|
verify.addActionListener(verifyer);
|
||||||
|
|
||||||
|
|
||||||
bouton.add(load);
|
bouton.add(load);
|
||||||
bouton.add(save);
|
bouton.add(save);
|
||||||
|
|
||||||
bouton.add(verify);
|
bouton.add(verify);
|
||||||
|
|
||||||
// Ajout des panneaux à la fenetre
|
// Ajout des panneaux à la fenetre
|
||||||
getContentPane().add(gridPanel, BorderLayout.CENTER);
|
getContentPane().add(gridPanel, BorderLayout.CENTER);
|
||||||
|
113
VerifButton.java
113
VerifButton.java
@@ -1,5 +1,6 @@
|
|||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
|
import java.awt.Color;
|
||||||
|
|
||||||
public class VerifButton implements ActionListener {
|
public class VerifButton implements ActionListener {
|
||||||
private int GRID_SIZE;
|
private int GRID_SIZE;
|
||||||
@@ -10,63 +11,85 @@ public class VerifButton implements ActionListener {
|
|||||||
this.grid = grid;
|
this.grid = grid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
if (grilleValide()) {
|
if (grilleValide()) {
|
||||||
JOptionPane.showMessageDialog(null, "La grille est valide.", "Validation", JOptionPane.INFORMATION_MESSAGE);
|
JOptionPane.showMessageDialog(null, "La grille est valide.", "Validation", JOptionPane.INFORMATION_MESSAGE);
|
||||||
} else {
|
} else {
|
||||||
JOptionPane.showMessageDialog(null, "La grille n'est pas valide.", "Erreur", JOptionPane.ERROR_MESSAGE);
|
JOptionPane.showMessageDialog(null, "La grille n'est pas valide.", "Erreur", JOptionPane.ERROR_MESSAGE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean grilleValide() {
|
private boolean grilleValide() {
|
||||||
for (int i = 0; i < this.GRID_SIZE; i++) {
|
for (int row = 0; row < this.GRID_SIZE; row++) {
|
||||||
for (int j = 0; j < this.GRID_SIZE; j++) {
|
for (int col = 0; col < this.GRID_SIZE; col++) {
|
||||||
if (!grid[i][j].getText().isEmpty()) {
|
if (!grid[row][col].getText().isEmpty()) {
|
||||||
int evaluant = Integer.parseInt(grid[i][j].getText());
|
int evaluant = Integer.parseInt(grid[row][col].getText());
|
||||||
|
|
||||||
// Test de validité sur les lignes
|
// Test de validité sur les lignes
|
||||||
for (int x = i + 1; x < this.GRID_SIZE; x++) {
|
for (int row2 = row + 1; row2 < this.GRID_SIZE; row2++) {
|
||||||
int comparateur = grid[x][j].getText().isEmpty() ? 0 : Integer.parseInt(grid[x][j].getText());
|
if (!grid[row2][col].getText().isEmpty()) {
|
||||||
if (evaluant == comparateur) {
|
int comparateur = Integer.parseInt(grid[row2][col].getText());
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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
|
// Test de validité sur les colonnes
|
||||||
for (int x = j + 1; x < this.GRID_SIZE; x++) {
|
for (int col2 = col + 1; col2 < this.GRID_SIZE; col2++) {
|
||||||
int comparateur = grid[i][x].getText().isEmpty() ? 0 : Integer.parseInt(grid[i][x].getText());
|
if (!grid[row][col2].getText().isEmpty()) {
|
||||||
if (evaluant == comparateur) {
|
int comparateur = Integer.parseInt(grid[row][col2].getText());
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Vérifier la validité dans les cases adjacentes
|
if (evaluant == comparateur) {
|
||||||
for (int row = i - 1; row <= i + 1; row++) {
|
grid[row][col2].setBackground(Color.red);
|
||||||
for (int col = j - 1; col <= j + 1; col++) {
|
grid[row][col].setBackground(Color.red);
|
||||||
// Exclure la case actuelle
|
return false;
|
||||||
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) {
|
for (int col2 = col - 1; col2 >= 0; col2--) {
|
||||||
// Vérifier la validité des regions
|
if (!grid[row][col2].getText().isEmpty()) {
|
||||||
if (!grid[row][col].getText().isEmpty()) {
|
int comparateur = Integer.parseInt(grid[row][col2].getText());
|
||||||
int comp = Integer.parseInt(grid[row][col].getText());
|
if (evaluant == comparateur) {
|
||||||
if (evaluant == comp) {
|
grid[row][col2].setBackground(Color.red);
|
||||||
return false;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user