Methodes en trop supprimée + nettoyage git

This commit is contained in:
2024-05-04 14:59:38 +02:00
parent 8adb8b4547
commit 12ae377ec0
5 changed files with 81 additions and 121 deletions

View File

@@ -4,16 +4,16 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SudokuButtonListener implements ActionListener {
private int ligne;
private int row;
private int col;
private Sudoku sudoku;
private JButton[][] boutons;
private JButton[][] buttons;
public SudokuButtonListener(int ligne, int col, Sudoku sudoku, JButton[][] boutons) {
this.ligne = ligne;
public SudokuButtonListener(int row, int col, Sudoku sudoku, JButton[][] buttons) {
this.row = row;
this.col = col;
this.sudoku = sudoku;
this.boutons = boutons;
this.buttons = buttons;
}
@Override
@@ -22,16 +22,16 @@ public class SudokuButtonListener implements ActionListener {
if (input != null && input.length() > 0) {
try {
int num = Integer.parseInt(input);
sudoku.getGrid().getCell(ligne, col).setValue(num);
sudoku.getGrid().getCell(row, col).setValue(num);
if (num == 0) {
boutons[ligne][col].setText(""); // Case vide si le nombre est 0
buttons[row][col].setText(""); // Case vide si le nombre est 0
} else {
boutons[ligne][col].setText(String.valueOf(num));
buttons[row][col].setText(String.valueOf(num));
}
if (!isValidMove(num, ligne, col)) {
boutons[ligne][col].setForeground(Color.RED); // Met le texte en rouge en cas de mouvement invalide
if (!isValid(num, row, col)) {
buttons[row][col].setForeground(Color.RED); // Met le texte en rouge en cas de mouvement invalide
} else {
boutons[ligne][col].setForeground(Color.BLACK); // Réinitialise la couleur du texte
buttons[row][col].setForeground(Color.BLACK); // Réinitialise la couleur du texte
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Veuillez entrer un nombre valide.");
@@ -39,13 +39,13 @@ public class SudokuButtonListener implements ActionListener {
}
}
private boolean isValidMove(int num, int ligne, int col) {
return isValidRow(num, ligne) && isValidCol(num, col) && isValidBox(num, ligne - ligne % 3, col - col % 3);
private boolean isValid(int num, int row, int col) {
return isValidRow(num, row) && isValidCol(num, col) && isValidBox(num, row - row % 3, col - col % 3);
}
private boolean isValidRow(int num, int ligne) {
private boolean isValidRow(int num, int row) {
for (int i = 0; i < 9; i++) {
if (sudoku.getGrid().getCell(ligne, i).getValue() == num && i != col) {
if (sudoku.getGrid().getCell(row, i).getValue() == num && i != col) {
return false;
}
}
@@ -54,19 +54,19 @@ public class SudokuButtonListener implements ActionListener {
private boolean isValidCol(int num, int col) {
for (int i = 0; i < 9; i++) {
if (sudoku.getGrid().getCell(i, col).getValue() == num && i != ligne) {
if (sudoku.getGrid().getCell(i, col).getValue() == num && i != row) {
return false;
}
}
return true;
}
private boolean isValidBox(int num, int Row, int Col) {
private boolean isValidBox(int num, int boxStartRow, int boxStartCol) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int ligne = i + Row;
int col = j + Col;
if (sudoku.getGrid().getCell(ligne, col).getValue() == num && (ligne != this.ligne || col != this.col)) {
int row = i + boxStartRow;
int col = j + boxStartCol;
if (sudoku.getGrid().getCell(row, col).getValue() == num && (row != this.row || col != this.col)) {
return false;
}
}