ajout de petit correctifs

This commit is contained in:
Yann KERAUDREN 2024-05-05 14:16:20 +02:00
parent 812d9d7a26
commit b31d0f0dbb
2 changed files with 165 additions and 144 deletions

@ -99,8 +99,6 @@ public class Resolver implements ActionListener {
JOptionPane.showMessageDialog(null, "Grille terminé en " + df.format(durée) + " secondes", "Résolution automatique", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Grille terminé en " + df.format(durée) + " secondes", "Résolution automatique", JOptionPane.INFORMATION_MESSAGE);
} }
} }

@ -17,8 +17,8 @@ public class TextFilter extends KeyAdapter {
private int row; private int row;
private int col; private int col;
private ArrayList<Integer> bad_numbers = new ArrayList<Integer>(); private ArrayList<Integer> bad_numbers = new ArrayList<Integer>();
private boolean errorDetected = true; private boolean errorDetected = false;
private int status; private int status;
public TextFilter (JTextField t,int GRID_SIZE, JTextField[][] grid, int i, int j, int status) { public TextFilter (JTextField t,int GRID_SIZE, JTextField[][] grid, int i, int j, int status) {
@ -37,6 +37,7 @@ public class TextFilter extends KeyAdapter {
char chiffre = e.getKeyChar(); char chiffre = e.getKeyChar();
int taille = this.Text.getText().length(); int taille = this.Text.getText().length();
boolean end_game = true;
// Si la longueur du texte est égale à 3 après l'ajout du chiffre, définir la taille de la police à 20 // Si la longueur du texte est égale à 3 après l'ajout du chiffre, définir la taille de la police à 20
if (taille > 1) { if (taille > 1) {
@ -52,57 +53,79 @@ public class TextFilter extends KeyAdapter {
// Vérifier le statut (1 == concepteur,2 == joueur) // 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')) {
GrilleValide(chiffre); GrilleValide(chiffre);
}
} else {
e.consume(); // empêcher la saisie de chiffres lorsque qu'une erreur est détectée
} }
} else {
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 // permet de bloquer la saisie de doublon dans la même case
if (!Text.getText().isEmpty() && (chiffre != KeyEvent.VK_BACK_SPACE)) { if (!Text.getText().isEmpty() && (chiffre != KeyEvent.VK_BACK_SPACE)) {
String existingText = Text.getText(); String existingText = Text.getText();
char[] existingChars = existingText.toCharArray(); char[] existingChars = existingText.toCharArray();
for (char c : existingChars) { for (char c : existingChars) {
if (chiffre == c) { if (chiffre == c) {
e.consume(); // empêcher la réécriture du même chiffre e.consume(); // empêcher la réécriture du même chiffre
return; return;
} }
}
} }
}
// Réinitialiser les couleurs si la touche de retour arrière est pressée lors d'une erreur // 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);
}
errorDetected = true; // Réinitialiser errorDetected après avoir corrigé les erreurs
} }
errorDetected = false; // Réinitialiser errorDetected après avoir corrigé les erreurs
}
// Défini la taille maiximum par case // Défini la taille maiximum par case
if ( taille >= 4) { if ( taille >= 4) {
e.consume(); e.consume();
}
//Test si toutes les cases sont remplies et affiche un message de fin
if (!errorDetected) {
for (int row = 0; row < GRID_SIZE; row++) {
for (int col = 0; col < GRID_SIZE; col++) {
String value = grid[row][col].getText();
// Vérifier si la case contient plus d'un chiffre
if (value.length() > 1) {
end_game = false;
}
if (grid[row][col].getText().isEmpty()) {
end_game = false;
}
}
} }
if (end_game) {
JOptionPane.showMessageDialog(null, "Bravo vous avez terminé la grille !", "Fin du jeu", JOptionPane.INFORMATION_MESSAGE);
}
}
} else if(status == 1){ } else if(status == 1){
// Traite la saisie pour savoir si le chiffer peut être posé // 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 // 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 // Taille maximum par case
if ( taille >= 1 ) { if ( taille >= 1 ) {
e.consume(); e.consume();
} }
} }
// colorie la case si elle est bonne ou pas // colorie la case si elle est bonne ou pas
@ -111,108 +134,108 @@ public class TextFilter extends KeyAdapter {
} }
} }
// permet de validé les règles du sudoku pour un chiffre posé. // 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 // Test de validité sur les lignes
for (int row2 = row + 1; row2 < this.GRID_SIZE; row2++) { for (int row2 = row + 1; row2 < this.GRID_SIZE; row2++) {
if (!grid[row2][col].getText().isEmpty()) { if (!grid[row2][col].getText().isEmpty()) {
int comparateur = Integer.parseInt(grid[row2][col].getText()); int comparateur = Integer.parseInt(grid[row2][col].getText());
if (evaluant == comparateur) { if (evaluant == comparateur) {
this.grid[row2][col].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(col); this.bad_numbers.add(col);
errorDetected = false; errorDetected = true;
}
}
} }
for (int row2 = row - 1; row2 >= 0; row2--) { }
if (!grid[row2][col].getText().isEmpty()) { }
int comparateur = Integer.parseInt(grid[row2][col].getText()); for (int row2 = row - 1; row2 >= 0; row2--) {
if (evaluant == comparateur) { if (!grid[row2][col].getText().isEmpty()) {
this.grid[row2][col].setBackground(Color.red); int comparateur = Integer.parseInt(grid[row2][col].getText());
this.grid[row][col].setBackground(Color.red); if (evaluant == comparateur) {
this.bad_numbers.add(row2); this.grid[row2][col].setBackground(Color.red);
this.bad_numbers.add(col); this.grid[row][col].setBackground(Color.red);
errorDetected = false; this.bad_numbers.add(row2);
} this.bad_numbers.add(col);
} errorDetected = true;
} }
}
}
// Test de validité sur les colonnes // Test de validité sur les colonnes
for (int col2 = col + 1; col2 < this.GRID_SIZE; col2++) { for (int col2 = col + 1; col2 < this.GRID_SIZE; col2++) {
if (!grid[row][col2].getText().isEmpty()) { if (!grid[row][col2].getText().isEmpty()) {
int comparateur = Integer.parseInt(grid[row][col2].getText()); int comparateur = Integer.parseInt(grid[row][col2].getText());
if (evaluant == comparateur) { if (evaluant == comparateur) {
this.grid[row][col2].setBackground(Color.red); this.grid[row][col2].setBackground(Color.red);
this.grid[row][col].setBackground(Color.red); this.grid[row][col].setBackground(Color.red);
this.bad_numbers.add(row); this.bad_numbers.add(row);
this.bad_numbers.add(col2); this.bad_numbers.add(col2);
errorDetected = false; errorDetected = true;
}
}
} }
for (int col2 = col - 1; col2 >= 0; col2--) { }
if (!grid[row][col2].getText().isEmpty()) { }
int comparateur = Integer.parseInt(grid[row][col2].getText()); for (int col2 = col - 1; col2 >= 0; col2--) {
if (evaluant == comparateur) { if (!grid[row][col2].getText().isEmpty()) {
this.grid[row][col2].setBackground(Color.red); int comparateur = Integer.parseInt(grid[row][col2].getText());
this.grid[row][col].setBackground(Color.red); if (evaluant == comparateur) {
this.bad_numbers.add(row); this.grid[row][col2].setBackground(Color.red);
this.bad_numbers.add(col2); this.grid[row][col].setBackground(Color.red);
errorDetected = false; this.bad_numbers.add(row);
} this.bad_numbers.add(col2);
} errorDetected = true;
} }
}
}
// Vérifier la validité dans les régions // Vérifier la validité dans les régions
int rowregion = this.row/3*3; int rowregion = this.row/3*3;
int colregion = this.col/3*3; int colregion = this.col/3*3;
for (int row2 = rowregion; row2 < rowregion + 3; row2++) { for (int row2 = rowregion; row2 < rowregion + 3; row2++) {
for (int col2 = colregion; col2 < colregion + 3; col2++) { for (int col2 = colregion; col2 < colregion + 3; col2++) {
if (row2 != row && col2 != col ) { if (row2 != row && col2 != col ) {
if (!grid[row2][col2].getText().isEmpty()) { if (!grid[row2][col2].getText().isEmpty()) {
int comparateur = Integer.parseInt(grid[row2][col2].getText()); int comparateur = Integer.parseInt(grid[row2][col2].getText());
if (evaluant == comparateur) { if (evaluant == comparateur) {
this.grid[row2][col2].setBackground(Color.red); this.grid[row2][col2].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(col2);
errorDetected = false; errorDetected = true;
}
}
}
} }
}
} }
} }
}
}
// permet de colorier en blanc si l'erreur n'est plus présente // 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) { if (this.grid[row2][this.col].getBackground() == Color.red) {
this.grid[row2][this.col].setBackground(Color.white); this.grid[row2][this.col].setBackground(Color.white);
} }
} }
//coloriage sur les colonnes //coloriage sur les colonnes
for (int col2 = 0; col2 < this.GRID_SIZE; col2++) { for (int col2 = 0; col2 < this.GRID_SIZE; col2++) {
if (this.grid[this.row][col2].getBackground() == Color.red) { if (this.grid[this.row][col2].getBackground() == Color.red) {
this.grid[this.row][col2].setBackground(Color.white); this.grid[this.row][col2].setBackground(Color.white);
} }
} }
//coloriage des régions //coloriage des régions
int rowregion = this.row/3*3; int rowregion = this.row/3*3;
int colregion = this.col/3*3; int colregion = this.col/3*3;
for (int row2 = rowregion; row2 < rowregion + 3; row2++) { for (int row2 = rowregion; row2 < rowregion + 3; row2++) {
for (int col2 = colregion; col2 < colregion + 3; col2++) { for (int col2 = colregion; col2 < colregion + 3; col2++) {
if (this.grid[row2][col2].getBackground() == Color.red) { if (this.grid[row2][col2].getBackground() == Color.red) {
this.grid[row2][col2].setBackground(Color.white); this.grid[row2][col2].setBackground(Color.white);
}
}
} }
}
}
} }
} }