diff --git a/Makefile b/Makefile index ec0fa6f..cc41866 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ J = java Main.class : Main.java SudokuGrid.class ${JC} ${JCFLAGS} Main.java -SudokuGrid.class : SudokuGrid.java TextFilter.class SaveButton.class LoadButton.class +SudokuGrid.class : SudokuGrid.java TextFilter.class SaveButton.class LoadButton.class VerifButton.class ${JC} ${JCFLAGS} SudokuGrid.java TextFilter.class : TextFilter.java @@ -22,6 +22,9 @@ SaveButton.class : SaveButton.java LoadButton.class : LoadButton.java ${JC} ${JCFLAGS} LoadButton.java +VerifButton.class : VerifButton.java + ${JC} ${JCFLAGS} VerifButton.java + ### REGLES OPTIONNELLES ### diff --git a/SudokuGrid.java b/SudokuGrid.java index 804087f..36472df 100644 --- a/SudokuGrid.java +++ b/SudokuGrid.java @@ -7,7 +7,7 @@ public class SudokuGrid extends JFrame { private JTextField[][] grid; public SudokuGrid() { - // Panneau pour la grille Sudoku + // Panneau pour la grille Sudoku. JPanel gridPanel = new JPanel(); gridPanel.setLayout(new GridLayout(GRID_SIZE, GRID_SIZE)); // Utiliser GridLayout gridPanel.setBackground(new Color(88, 169, 191)); // Fond vert @@ -65,7 +65,7 @@ public class SudokuGrid extends JFrame { load.addActionListener(loader); - /* + // Bouton pour vérifier la grille JButton verify = new JButton("Vérifier"); @@ -73,11 +73,11 @@ public class SudokuGrid extends JFrame { 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); diff --git a/VerifButton.java b/VerifButton.java index dd4c672..02d69bd 100644 --- a/VerifButton.java +++ b/VerifButton.java @@ -1,139 +1,80 @@ -/* - import javax.swing.*; import java.awt.event.*; - -public class VerifButton implements ActionListener { - +public class VerifButton implements ActionListener { private int GRID_SIZE; private JTextField[][] grid; public VerifButton(int GRID_SIZE, JTextField[][] grid) { - - this.GRID_SIZE = GRID_SIZE; - - this.grid = grid; - + this.GRID_SIZE = GRID_SIZE; + this.grid = grid; } @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); + } + } - GrilleValide(); + 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()); + // 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; + } + } + + // 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; + } + } + + // 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; + } + } + } + } + } + } + } + } + return true; } } - - - public void GrilleValide() { - - int comparateur; - int evaluant; - boolean valide = true; - int[] list_region; - - for (int i = 0; i < this.GRID_SIZE; i++) { - for(int j = 0; j < this.GRID_SIZE; j++) { - - if (!grid[i][j].getText().isEmpty()) { - - evaluant = Integer.parseInt(grid[i][j].getText()); - - - // test de validité sur les lignes - for (int x = i+1; x < this.GRID_SIZE; x++) { - - if (grid[x][j].getText().isEmpty()) { - - comparateur = 0; - }else{ - - comparateur = Integer.parseInt(grid[x][j].getText()); - } - - if (evaluant == comparateur) { - - System.out.println("grille invalide2"); - - } - } - for (int x = i-1; x >= 0; x--) { - - if (grid[x][j].getText().isEmpty()) { - - comparateur = 0; - }else{ - - comparateur = Integer.parseInt(grid[x][j].getText()); - } - - if (evaluant == comparateur) { - - System.out.println("grille invalide1"); - } - } - - //test de validité sur les colonnes - for (int x = j+1; x < this.GRID_SIZE; x++) { - - if (grid[i][x].getText().isEmpty()) { - - comparateur = 0; - }else{ - - comparateur = Integer.parseInt(grid[i][x].getText()); - } - - if (evaluant == comparateur) { - - System.out.println("grille invalide"); - } - } - for (int x = j-1; x >= 0; x--) { - - if (grid[i][x].getText().isEmpty()) { - - comparateur = 0; - }else{ - - comparateur = Integer.parseInt(grid[i][x].getText()); - } - - if (evaluant == comparateur) { - - System.out.println("grille invalide"); - } - } - - //test de validité sur la région - - if ( i < 3) { - - if ( j < 3) { - - for (int v = 0; v < 3; v++) { - for (int w = 0; w < 3; w++) { - - if (!grid[v][w].getText().isEmpty()) { - - list_region.add(grid[v][w].getText().isEmpty()); - - } - } - } - - - - - - - } - } - } - } -} - } - - */