From 85d535b4fd2fde55b3ced0a5b069a73c31886355 Mon Sep 17 00:00:00 2001 From: keraudre Date: Tue, 30 Apr 2024 12:08:10 +0200 Subject: [PATCH] =?UTF-8?q?cr=C3=A9ation=20de=20la=20fonctionnalit=C3=A9?= =?UTF-8?q?=20de=20validation=20d'une=20grille?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SudokuGrid.java | 13 +++++- VerifButton.java | 116 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 VerifButton.java diff --git a/SudokuGrid.java b/SudokuGrid.java index 5497a76..8603b69 100644 --- a/SudokuGrid.java +++ b/SudokuGrid.java @@ -34,7 +34,7 @@ public class SudokuGrid extends JFrame { // Bouton pour sauvegarder la grille JButton save = new JButton("Sauvegarder"); - SaveButton saver = new SaveButton(GRID_SIZE,grid); + SaveButton saver = new SaveButton(GRID_SIZE,grid); save.addActionListener(saver); @@ -42,12 +42,21 @@ public class SudokuGrid extends JFrame { // Bouton pour chargé la grille JButton load = new JButton("Charger"); - LoadButton loader = new LoadButton(GRID_SIZE, grid); + LoadButton loader = new LoadButton(GRID_SIZE, grid); load.addActionListener(loader); + // Bouton pour vérifier la grille + JButton verify = new JButton("Vérifier"); + + VerifButton verifyer = new VerifButton(GRID_SIZE, grid); + + verify.addActionListener(verifyer); + + bouton.add(load); bouton.add(save); + bouton.add(verify); // Ajout des panneaux à la fenetre getContentPane().add(gridPanel, BorderLayout.CENTER); diff --git a/VerifButton.java b/VerifButton.java new file mode 100644 index 0000000..822ac3b --- /dev/null +++ b/VerifButton.java @@ -0,0 +1,116 @@ +import javax.swing.*; +import java.awt.event.*; + + +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; + + } + + @Override + + public void actionPerformed(ActionEvent e) { + + GrilleValide(); + + } + + + public void GrilleValide() { + + int comparateur; + int evaluant; + boolean valide = true; + + for (int i = 0; i < this.GRID_SIZE; i++) { + for(int j = 0; j < this.GRID_SIZE; j++) { + + if (grid[i][j].getText().isEmpty()) { + + }else{ + + 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 + + } + } + } + } +} + +