ajout de la detection de mauvais placement
This commit is contained in:
parent
461b7d488f
commit
d23988596d
@ -18,7 +18,7 @@ public class SudokuGrid extends JFrame {
|
||||
for (int j = 0; j < GRID_SIZE; j++) {
|
||||
grid[i][j] = new JTextField();
|
||||
|
||||
TextFilter filtre = new TextFilter(grid[i][j]);
|
||||
TextFilter filtre = new TextFilter(grid[i][j], GRID_SIZE, grid, i, j);
|
||||
|
||||
grid[i][j].addKeyListener(filtre);
|
||||
grid[i][j].setHorizontalAlignment(JTextField.CENTER);
|
||||
@ -53,7 +53,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);
|
||||
|
||||
@ -65,19 +65,12 @@ public class SudokuGrid extends JFrame {
|
||||
|
||||
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);
|
||||
|
137
TextFilter.java
137
TextFilter.java
@ -1,26 +1,49 @@
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
import java.awt.Color;
|
||||
|
||||
|
||||
public class TextFilter extends KeyAdapter {
|
||||
public class TextFilter extends KeyAdapter {
|
||||
|
||||
private JTextField Text;
|
||||
|
||||
public TextFilter (JTextField t) {
|
||||
private int GRID_SIZE;
|
||||
|
||||
private JTextField[][] grid;
|
||||
|
||||
private int row;
|
||||
|
||||
private int col;
|
||||
|
||||
private JTextField Text2;
|
||||
|
||||
|
||||
public TextFilter (JTextField t,int GRID_SIZE, JTextField[][] grid, int i, int j) {
|
||||
|
||||
this.Text = t;
|
||||
|
||||
this.GRID_SIZE = GRID_SIZE;
|
||||
|
||||
this.grid = grid;
|
||||
|
||||
this.row = i;
|
||||
|
||||
this.col = j;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
|
||||
public void keyTyped(KeyEvent e) {
|
||||
|
||||
char chaine = e.getKeyChar();
|
||||
char chiffre = e.getKeyChar();
|
||||
|
||||
int taille = this.Text.getText().length();
|
||||
|
||||
|
||||
if ( ((chaine < '0') || (chaine > '9')) && (chaine != KeyEvent.VK_BACK_SPACE)) {
|
||||
e.consume(); // ignorer l'événement
|
||||
if ( ((chiffre < '0') || (chiffre > '9')) && (chiffre != KeyEvent.VK_BACK_SPACE)) {
|
||||
|
||||
e.consume(); // ignorer l'événement
|
||||
}
|
||||
|
||||
if ( taille >= 1 ) {
|
||||
@ -30,4 +53,108 @@ public class TextFilter extends KeyAdapter {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public void keyPressed(KeyEvent e) {
|
||||
|
||||
char chiffre = e.getKeyChar();
|
||||
|
||||
if ( (chiffre > '0') || (chiffre < '9') && (chiffre != KeyEvent.VK_BACK_SPACE)) {
|
||||
|
||||
GrilleValide(chiffre);
|
||||
}
|
||||
|
||||
if ( chiffre == KeyEvent.VK_BACK_SPACE) {
|
||||
|
||||
if (!Text2.getText().isEmpty()) {
|
||||
|
||||
|
||||
Text.setBackground(Color.white);
|
||||
Text2.setBackground(Color.white);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void GrilleValide(char chiffre) {
|
||||
|
||||
int evaluant = Integer.parseInt(Character.toString(chiffre));
|
||||
|
||||
// Test de validité sur les lignes
|
||||
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) {
|
||||
this.grid[row2][col].setBackground(Color.red);
|
||||
this.grid[row][col].setBackground(Color.red);
|
||||
this.Text2 = grid[row2][col];
|
||||
}
|
||||
}
|
||||
}
|
||||
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) {
|
||||
this.grid[row2][col].setBackground(Color.red);
|
||||
this.grid[row][col].setBackground(Color.red);
|
||||
this.Text2 = grid[row2][col];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test de validité sur les colonnes
|
||||
for (int col2 = col + 1; col2 < this.GRID_SIZE; col2++) {
|
||||
if (!grid[row][col2].getText().isEmpty()) {
|
||||
int comparateur = Integer.parseInt(grid[row][col2].getText());
|
||||
|
||||
if (evaluant == comparateur) {
|
||||
this.grid[row][col2].setBackground(Color.red);
|
||||
this.grid[row][col].setBackground(Color.red);
|
||||
this.Text2 = grid[row][col];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
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) {
|
||||
this.grid[row][col2].setBackground(Color.red);
|
||||
this.grid[row][col].setBackground(Color.red);
|
||||
this.Text2 = grid[row][col2];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
this.grid[row2][col2].setBackground(Color.red);
|
||||
this.grid[row][col].setBackground(Color.red);
|
||||
this.Text2 = grid[row2][col2];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user