lecture de fichier +affichage debug
This commit is contained in:
@@ -6,36 +6,33 @@ import java.awt.event.ActionListener;
|
||||
public class SudokuButtonListener implements ActionListener {
|
||||
private int row;
|
||||
private int col;
|
||||
private Sudoku sudoku;
|
||||
private Grid grid;
|
||||
private JButton[][] buttons;
|
||||
|
||||
public SudokuButtonListener(int row, int col, Sudoku sudoku, JButton[][] buttons) {
|
||||
public SudokuButtonListener(int row, int col, Grid grid, JButton[][] buttons) {
|
||||
this.row = row;
|
||||
this.col = col;
|
||||
this.sudoku = sudoku;
|
||||
this.grid = grid;
|
||||
this.buttons = buttons;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String input = JOptionPane.showInputDialog("Enter a number:");
|
||||
if (input != null && input.length() > 0) {
|
||||
try {
|
||||
int num = Integer.parseInt(input);
|
||||
sudoku.getGrid().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));
|
||||
}
|
||||
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.");
|
||||
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));
|
||||
}
|
||||
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.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +42,7 @@ public class SudokuButtonListener implements ActionListener {
|
||||
|
||||
private boolean isValidRow(int num, int row) {
|
||||
for (int i = 0; i < 9; i++) {
|
||||
if (sudoku.getGrid().getCell(row, i).getValue() == num && i != col) {
|
||||
if (grid.getCell(row, i).getValue() == num && i != col) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -54,7 +51,7 @@ 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 != row) {
|
||||
if (grid.getCell(i, col).getValue() == num && i != row) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -66,12 +63,11 @@ public class SudokuButtonListener implements ActionListener {
|
||||
for (int j = 0; j < 3; j++) {
|
||||
int row = i + boxStartRow;
|
||||
int col = j + boxStartCol;
|
||||
if (sudoku.getGrid().getCell(row, col).getValue() == num && (row != this.row || col != this.col)) {
|
||||
if (grid.getCell(row, col).getValue() == num && (row != this.row || col != this.col)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user