création de la fonctionnalité de validation d'une grille
This commit is contained in:
@@ -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);
|
||||
|
116
VerifButton.java
Normal file
116
VerifButton.java
Normal file
@@ -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
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user