2024-04-28 01:40:14 +02:00
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
|
|
|
public class SudokuButtonListener implements ActionListener {
|
2024-05-04 00:13:44 +02:00
|
|
|
private int ligne;
|
2024-04-28 01:40:14 +02:00
|
|
|
private int col;
|
2024-05-04 00:13:44 +02:00
|
|
|
private Sudoku sudoku;
|
|
|
|
private JButton[][] boutons;
|
2024-04-28 01:40:14 +02:00
|
|
|
|
2024-05-04 00:13:44 +02:00
|
|
|
public SudokuButtonListener(int ligne, int col, Sudoku sudoku, JButton[][] boutons) {
|
|
|
|
this.ligne = ligne;
|
2024-04-28 01:40:14 +02:00
|
|
|
this.col = col;
|
2024-05-04 00:13:44 +02:00
|
|
|
this.sudoku = sudoku;
|
|
|
|
this.boutons = boutons;
|
2024-04-28 01:40:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
2024-05-04 00:13:44 +02:00
|
|
|
String input = JOptionPane.showInputDialog("Entrez un nombre :");
|
|
|
|
if (input != null && input.length() > 0) {
|
|
|
|
try {
|
|
|
|
int num = Integer.parseInt(input);
|
|
|
|
sudoku.getGrid().getCell(ligne, col).setValue(num);
|
|
|
|
if (num == 0) {
|
|
|
|
boutons[ligne][col].setText(""); // Case vide si le nombre est 0
|
|
|
|
} else {
|
|
|
|
boutons[ligne][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
|
|
|
|
} else {
|
|
|
|
boutons[ligne][col].setForeground(Color.BLACK); // Réinitialise la couleur du texte
|
|
|
|
}
|
|
|
|
} catch (NumberFormatException ex) {
|
|
|
|
JOptionPane.showMessageDialog(null, "Veuillez entrer un nombre valide.");
|
2024-04-30 12:20:16 +02:00
|
|
|
}
|
2024-04-28 01:40:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-04 00:13:44 +02:00
|
|
|
private boolean isValidMove(int num, int ligne, int col) {
|
|
|
|
return isValidRow(num, ligne) && isValidCol(num, col) && isValidBox(num, ligne - ligne % 3, col - col % 3);
|
2024-04-28 01:40:14 +02:00
|
|
|
}
|
|
|
|
|
2024-05-04 00:13:44 +02:00
|
|
|
private boolean isValidRow(int num, int ligne) {
|
2024-04-28 01:40:14 +02:00
|
|
|
for (int i = 0; i < 9; i++) {
|
2024-05-04 00:13:44 +02:00
|
|
|
if (sudoku.getGrid().getCell(ligne, i).getValue() == num && i != col) {
|
2024-04-28 01:40:14 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isValidCol(int num, int col) {
|
|
|
|
for (int i = 0; i < 9; i++) {
|
2024-05-04 00:13:44 +02:00
|
|
|
if (sudoku.getGrid().getCell(i, col).getValue() == num && i != ligne) {
|
2024-04-28 01:40:14 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-05-04 00:13:44 +02:00
|
|
|
private boolean isValidBox(int num, int Row, int Col) {
|
2024-04-28 01:40:14 +02:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
for (int j = 0; j < 3; j++) {
|
2024-05-04 00:13:44 +02:00
|
|
|
int ligne = i + Row;
|
|
|
|
int col = j + Col;
|
|
|
|
if (sudoku.getGrid().getCell(ligne, col).getValue() == num && (ligne != this.ligne || col != this.col)) {
|
2024-04-28 01:40:14 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2024-05-04 00:13:44 +02:00
|
|
|
}
|