Ajout generation grille, amélioration UI, commentaires
This commit is contained in:
@@ -4,45 +4,48 @@ import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class SudokuButtonListener implements ActionListener {
|
||||
private int row;
|
||||
private int ligne;
|
||||
private int col;
|
||||
private Grid grid;
|
||||
private JButton[][] buttons;
|
||||
private Sudoku sudoku;
|
||||
private JButton[][] boutons;
|
||||
|
||||
public SudokuButtonListener(int row, int col, Grid grid, JButton[][] buttons) {
|
||||
this.row = row;
|
||||
public SudokuButtonListener(int ligne, int col, Sudoku sudoku, JButton[][] boutons) {
|
||||
this.ligne = ligne;
|
||||
this.col = col;
|
||||
this.grid = grid;
|
||||
this.buttons = buttons;
|
||||
this.sudoku = sudoku;
|
||||
this.boutons = boutons;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
try {
|
||||
int num = Integer.parseInt(JOptionPane.showInputDialog("Enter a number:"));
|
||||
grid.getCell(row, col).setValue(num);
|
||||
if (num == 0) {
|
||||
buttons[row][col].setText(""); // Empty cell if the number is 0
|
||||
} else {
|
||||
buttons[row][col].setText(String.valueOf(num));
|
||||
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.");
|
||||
}
|
||||
if (!isValidMove(num, row, col)) {
|
||||
buttons[row][col].setForeground(Color.RED); // Set text color to red for invalid move
|
||||
} else {
|
||||
buttons[row][col].setForeground(Color.BLACK); // Reset text color
|
||||
}
|
||||
} catch (NumberFormatException ex) {
|
||||
JOptionPane.showMessageDialog(null, "Please enter a valid number.");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isValidMove(int num, int row, int col) {
|
||||
return isValidRow(num, row) && isValidCol(num, col) && isValidBox(num, row - row % 3, col - col % 3);
|
||||
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 isValidRow(int num, int row) {
|
||||
private boolean isValidRow(int num, int ligne) {
|
||||
for (int i = 0; i < 9; i++) {
|
||||
if (grid.getCell(row, i).getValue() == num && i != col) {
|
||||
if (sudoku.getGrid().getCell(ligne, i).getValue() == num && i != col) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -51,23 +54,23 @@ public class SudokuButtonListener implements ActionListener {
|
||||
|
||||
private boolean isValidCol(int num, int col) {
|
||||
for (int i = 0; i < 9; i++) {
|
||||
if (grid.getCell(i, col).getValue() == num && i != row) {
|
||||
if (sudoku.getGrid().getCell(i, col).getValue() == num && i != ligne) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isValidBox(int num, int boxStartRow, int boxStartCol) {
|
||||
private boolean isValidBox(int num, int Row, int Col) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int j = 0; j < 3; j++) {
|
||||
int row = i + boxStartRow;
|
||||
int col = j + boxStartCol;
|
||||
if (grid.getCell(row, col).getValue() == num && (row != this.row || col != this.col)) {
|
||||
int ligne = i + Row;
|
||||
int col = j + Col;
|
||||
if (sudoku.getGrid().getCell(ligne, col).getValue() == num && (ligne != this.ligne || col != this.col)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user