maj
This commit is contained in:
240
TextFilter.java
240
TextFilter.java
@@ -1,3 +1,8 @@
|
|||||||
|
/*
|
||||||
|
La classe <code>TextFilter</code> est utilisée pour gerer toutes les fonctions lié à la saisie des touches
|
||||||
|
@version 1.1
|
||||||
|
@author Thomas Follea, Yann Keraudren
|
||||||
|
*/
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
@@ -37,16 +42,15 @@ public class TextFilter extends KeyAdapter {
|
|||||||
if (taille > 1) {
|
if (taille > 1) {
|
||||||
Text.setFont(new Font("Verdana", Font.BOLD, 20));
|
Text.setFont(new Font("Verdana", Font.BOLD, 20));
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
Text.setFont(new Font("Verdana", Font.BOLD, 40));
|
Text.setFont(new Font("Verdana", Font.BOLD, 40));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ignorer l'événement si le caractère entré n'est pas un chiffre entre 1 et 9 ou la touche de retour arrière
|
||||||
if ( ((chiffre < '1') || (chiffre > '9')) && (chiffre != KeyEvent.VK_BACK_SPACE)) {
|
if ( ((chiffre < '1') || (chiffre > '9')) && (chiffre != KeyEvent.VK_BACK_SPACE)) {
|
||||||
|
|
||||||
e.consume(); // ignorer l'événement
|
e.consume(); // ignorer l'événement
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Vérifier le statut (1 == concepteur,2 == joueur)
|
||||||
if (status == 2){
|
if (status == 2){
|
||||||
if (errorDetected) {
|
if (errorDetected) {
|
||||||
if ((chiffre >= '1') && (chiffre <= '9')) {
|
if ((chiffre >= '1') && (chiffre <= '9')) {
|
||||||
@@ -56,6 +60,19 @@ public class TextFilter extends KeyAdapter {
|
|||||||
e.consume(); // empêcher la saisie de chiffres lorsque qu'une erreur est détectée
|
e.consume(); // empêcher la saisie de chiffres lorsque qu'une erreur est détectée
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// permet de bloquer la saisie de doublon dans la même case
|
||||||
|
if (!Text.getText().isEmpty() && (chiffre != KeyEvent.VK_BACK_SPACE)) {
|
||||||
|
String existingText = Text.getText();
|
||||||
|
char[] existingChars = existingText.toCharArray();
|
||||||
|
for (char c : existingChars) {
|
||||||
|
if (chiffre == c) {
|
||||||
|
e.consume(); // empêcher la réécriture du même chiffre
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Réinitialiser les couleurs si la touche de retour arrière est pressée lors d'une erreur
|
||||||
if ((!bad_numbers.isEmpty()) && (chiffre == KeyEvent.VK_BACK_SPACE)) {
|
if ((!bad_numbers.isEmpty()) && (chiffre == KeyEvent.VK_BACK_SPACE)) {
|
||||||
Text.setBackground(Color.white);
|
Text.setBackground(Color.white);
|
||||||
while (!bad_numbers.isEmpty()) {
|
while (!bad_numbers.isEmpty()) {
|
||||||
@@ -64,157 +81,138 @@ public class TextFilter extends KeyAdapter {
|
|||||||
errorDetected = true; // Réinitialiser errorDetected après avoir corrigé les erreurs
|
errorDetected = true; // Réinitialiser errorDetected après avoir corrigé les erreurs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Défini la taille maiximum par case
|
||||||
if ( taille >= 4) {
|
if ( taille >= 4) {
|
||||||
|
|
||||||
e.consume();
|
e.consume();
|
||||||
}
|
}
|
||||||
} else if(status == 1){
|
} else if(status == 1){
|
||||||
|
// Traite la saisie pour savoir si le chiffer peut être posé
|
||||||
if ( taille > 1 && ((chiffre >= '1') && (chiffre <= '9')) && (chiffre != KeyEvent.VK_ENTER)) {
|
if ( taille > 1 && ((chiffre >= '1') && (chiffre <= '9')) && (chiffre != KeyEvent.VK_ENTER)) {
|
||||||
|
|
||||||
GrilleValide(chiffre);
|
GrilleValide(chiffre);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Réinitialiser les couleurs si la touche de retour arrière est pressée lors d'une erreur
|
||||||
if ((!bad_numbers.isEmpty()) && (chiffre == KeyEvent.VK_BACK_SPACE)) {
|
if ((!bad_numbers.isEmpty()) && (chiffre == KeyEvent.VK_BACK_SPACE)) {
|
||||||
|
|
||||||
Text.setBackground(Color.white);
|
Text.setBackground(Color.white);
|
||||||
|
|
||||||
while(!bad_numbers.isEmpty()) {
|
while(!bad_numbers.isEmpty()) {
|
||||||
|
|
||||||
grid[bad_numbers.removeFirst()][bad_numbers.removeFirst()].setBackground(Color.white);
|
grid[bad_numbers.removeFirst()][bad_numbers.removeFirst()].setBackground(Color.white);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Taille maximum par case
|
||||||
if ( taille >= 1 ) {
|
if ( taille >= 1 ) {
|
||||||
|
|
||||||
e.consume();
|
e.consume();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// colorie la case si elle est bonne ou pas
|
||||||
if ( (bad_numbers.isEmpty()) && (chiffre == KeyEvent.VK_BACK_SPACE)) {
|
if ( (bad_numbers.isEmpty()) && (chiffre == KeyEvent.VK_BACK_SPACE)) {
|
||||||
|
|
||||||
Coloriage();
|
Coloriage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// permet de validé les règles du sudoku pour un chiffre posé.
|
||||||
public void GrilleValide(char chiffre) {
|
public void GrilleValide(char chiffre) {
|
||||||
|
int evaluant = Integer.parseInt(Character.toString(chiffre));
|
||||||
int evaluant = Integer.parseInt(Character.toString(chiffre));
|
// Test de validité sur les lignes
|
||||||
|
for (int row2 = row + 1; row2 < this.GRID_SIZE; row2++) {
|
||||||
// Test de validité sur les lignes
|
if (!grid[row2][col].getText().isEmpty()) {
|
||||||
for (int row2 = row + 1; row2 < this.GRID_SIZE; row2++) {
|
int comparateur = Integer.parseInt(grid[row2][col].getText());
|
||||||
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.bad_numbers.add(row2);
|
|
||||||
this.bad_numbers.add(col);
|
|
||||||
errorDetected = 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) {
|
|
||||||
this.grid[row2][col].setBackground(Color.red);
|
|
||||||
this.grid[row][col].setBackground(Color.red);
|
|
||||||
this.bad_numbers.add(row2);
|
|
||||||
this.bad_numbers.add(col);
|
|
||||||
errorDetected = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.bad_numbers.add(row);
|
|
||||||
this.bad_numbers.add(col2);
|
|
||||||
errorDetected = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
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.bad_numbers.add(row);
|
|
||||||
this.bad_numbers.add(col2);
|
|
||||||
errorDetected = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Vérifier la validité dans les régions
|
|
||||||
int rowregion = this.row/3*3;
|
|
||||||
int colregion = this.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) {
|
if (evaluant == comparateur) {
|
||||||
this.grid[row2][col2].setBackground(Color.red);
|
this.grid[row2][col].setBackground(Color.red);
|
||||||
this.grid[row][col].setBackground(Color.red);
|
this.grid[row][col].setBackground(Color.red);
|
||||||
this.bad_numbers.add(row2);
|
this.bad_numbers.add(row2);
|
||||||
this.bad_numbers.add(col2);
|
this.bad_numbers.add(col);
|
||||||
errorDetected = false;
|
errorDetected = 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) {
|
||||||
|
this.grid[row2][col].setBackground(Color.red);
|
||||||
|
this.grid[row][col].setBackground(Color.red);
|
||||||
|
this.bad_numbers.add(row2);
|
||||||
|
this.bad_numbers.add(col);
|
||||||
|
errorDetected = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// 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.bad_numbers.add(row);
|
||||||
|
this.bad_numbers.add(col2);
|
||||||
|
errorDetected = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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.bad_numbers.add(row);
|
||||||
|
this.bad_numbers.add(col2);
|
||||||
|
errorDetected = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vérifier la validité dans les régions
|
||||||
|
int rowregion = this.row/3*3;
|
||||||
|
int colregion = this.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.bad_numbers.add(row2);
|
||||||
|
this.bad_numbers.add(col2);
|
||||||
|
errorDetected = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// permet de colorier en blanc si l'erreur n'est plus présente
|
||||||
public void Coloriage() {
|
public void Coloriage() {
|
||||||
|
//coloriage sur les lignes
|
||||||
//coloriage sur les lignes
|
for (int row2 = 0; row2 < this.GRID_SIZE; row2++) {
|
||||||
for (int row2 = 0; row2 < this.GRID_SIZE; row2++) {
|
if (this.grid[row2][this.col].getBackground() == Color.red) {
|
||||||
|
this.grid[row2][this.col].setBackground(Color.white);
|
||||||
if (this.grid[row2][this.col].getBackground() == Color.red) {
|
}
|
||||||
|
}
|
||||||
this.grid[row2][this.col].setBackground(Color.white);
|
|
||||||
}
|
//coloriage sur les colonnes
|
||||||
}
|
for (int col2 = 0; col2 < this.GRID_SIZE; col2++) {
|
||||||
|
if (this.grid[this.row][col2].getBackground() == Color.red) {
|
||||||
//coloriage sur les colonnes
|
this.grid[this.row][col2].setBackground(Color.white);
|
||||||
for (int col2 = 0; col2 < this.GRID_SIZE; col2++) {
|
}
|
||||||
|
}
|
||||||
if (this.grid[this.row][col2].getBackground() == Color.red) {
|
|
||||||
|
//coloriage des régions
|
||||||
this.grid[this.row][col2].setBackground(Color.white);
|
int rowregion = this.row/3*3;
|
||||||
}
|
int colregion = this.col/3*3;
|
||||||
}
|
for (int row2 = rowregion; row2 < rowregion + 3; row2++) {
|
||||||
|
for (int col2 = colregion; col2 < colregion + 3; col2++) {
|
||||||
//coloriage des régions
|
if (this.grid[row2][col2].getBackground() == Color.red) {
|
||||||
int rowregion = this.row/3*3;
|
this.grid[row2][col2].setBackground(Color.white);
|
||||||
int colregion = this.col/3*3;
|
}
|
||||||
for (int row2 = rowregion; row2 < rowregion + 3; row2++) {
|
}
|
||||||
for (int col2 = colregion; col2 < colregion + 3; col2++) {
|
|
||||||
|
|
||||||
if (this.grid[row2][col2].getBackground() == Color.red) {
|
|
||||||
|
|
||||||
this.grid[row2][col2].setBackground(Color.white);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user