diff --git a/Cell.class b/Cell.class new file mode 100644 index 0000000..c8b3b8c Binary files /dev/null and b/Cell.class differ diff --git a/Cell.java b/Cell.java index 9bad4e1..9847835 100644 --- a/Cell.java +++ b/Cell.java @@ -17,5 +17,4 @@ public class Cell { public void setValue(int value) { this.value = value; } -} - +} \ No newline at end of file diff --git a/Grid.class b/Grid.class new file mode 100644 index 0000000..06cddd8 Binary files /dev/null and b/Grid.class differ diff --git a/Grid.java b/Grid.java index 7875772..eea46a8 100644 --- a/Grid.java +++ b/Grid.java @@ -1,30 +1,60 @@ +import java.io.DataInputStream; +import java.io.FileInputStream; +import java.io.IOException; + public class Grid { private Cell[][] cells; public Grid() { cells = new Cell[9][9]; for (int i = 0; i < 9; i++) { - for (int j = 0; j < 9; j++) { - cells[i][j] = new Cell(); + for (int column = 0; column < 9; column++) { + cells[i][column] = new Cell(); } } } - public Grid(Grid grid) { - cells = new Cell[9][9]; + public void loadGridFromFile(String fileName) { + try (DataInputStream input = new DataInputStream(new FileInputStream(fileName))) { + for (int i = 0; i < 9; i++) { + String line = String.valueOf(input.readInt()); + for (int column = 0; column < 9; column++) { + int value; + if (column < line.length()) { + char digit = line.charAt(column); + value = Character.getNumericValue(digit); + } else { + value = 0; + } + cells[i][column].setValue(value); + } + } + System.out.println("Success"); + System.out.println(this); + } catch (IOException e) { + System.err.println("Error: " + e.getMessage()); + } + } + + public Cell getCell(int i, int column) { + return cells[i][column]; + } + + public void setCell(int i, int column, int value) { + cells[i][column].setValue(value); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); for (int i = 0; i < 9; i++) { - for (int j = 0; j < 9; j++) { - cells[i][j] = new Cell(grid.cells[i][j].getValue()); + for (int column = 0; column < 9; column++) { + int value = cells[i][column].getValue(); + sb.append(value).append(" "); } + sb.append("\n"); } + return sb.toString(); } - - - public Cell getCell(int ligne, int col) { - return cells[ligne][col]; - } - } - - diff --git a/Sudoku.class b/Sudoku.class new file mode 100644 index 0000000..0e1ad1d Binary files /dev/null and b/Sudoku.class differ diff --git a/Sudoku.java b/Sudoku.java index fc22ab5..63310b2 100644 --- a/Sudoku.java +++ b/Sudoku.java @@ -2,7 +2,7 @@ public class Sudoku { private Grid grid; public Sudoku() { - this.grid = new Grid(); //grille de base + this.grid = new Grid(); } public Grid getGrid() { @@ -10,19 +10,13 @@ public class Sudoku { } public static void main(String[] args) { - Sudoku sudoku = new Sudoku(); - sudoku.printGrid(); // Afficher la grille - new SudokuUI(sudoku); - } - - public void printGrid() { - for (int row = 0; row < 9; row++) { - for (int col = 0; col < 9; col++) { - int value = grid.getCell(row, col).getValue(); - System.out.print(value + " "); - } - System.out.println(); + if (args.length > 0) { + Sudoku sudoku = new Sudoku(); + sudoku.getGrid().loadGridFromFile(args[0]); + // Pas besoin d'afficher la grille ici, car loadGridFromFile() le fait déjà + new SudokuUI(sudoku.getGrid()); + } else { + System.err.println("Usage: java Sudoku <grid_file>"); } } -} - +} \ No newline at end of file diff --git a/SudokuButtonListener.class b/SudokuButtonListener.class new file mode 100644 index 0000000..7467af2 Binary files /dev/null and b/SudokuButtonListener.class differ diff --git a/SudokuButtonListener.java b/SudokuButtonListener.java index cf3064d..5563c3b 100644 --- a/SudokuButtonListener.java +++ b/SudokuButtonListener.java @@ -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; } -} - +} \ No newline at end of file diff --git a/SudokuUI.class b/SudokuUI.class new file mode 100644 index 0000000..8d4871a Binary files /dev/null and b/SudokuUI.class differ diff --git a/SudokuUI.java b/SudokuUI.java index ea418c5..db2f01c 100644 --- a/SudokuUI.java +++ b/SudokuUI.java @@ -2,11 +2,11 @@ import javax.swing.*; import java.awt.*; public class SudokuUI extends JFrame { - private Sudoku sudoku; + private Grid grid; private JButton[][] buttons; - public SudokuUI(Sudoku sudoku) { - this.sudoku = sudoku; + public SudokuUI(Grid grid) { + this.grid = grid; this.buttons = new JButton[9][9]; setTitle("Sudoku"); @@ -28,14 +28,13 @@ public class SudokuUI extends JFrame { } private void createGridButtons(JPanel gridPanel) { // Création sudoku - Grid grid = sudoku.getGrid(); for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { JButton button = new JButton(); button.setFont(new Font("Arial", Font.BOLD, 24)); button.setContentAreaFilled(false); button.setBorder(BorderFactory.createLineBorder(Color.BLACK)); - button.addActionListener(new SudokuButtonListener(row, col, sudoku, buttons)); + button.addActionListener(new SudokuButtonListener(row, col, grid, buttons)); gridPanel.add(button); buttons[row][col] = button; @@ -45,5 +44,4 @@ public class SudokuUI extends JFrame { } } } -} - +} \ No newline at end of file diff --git a/exemple.gri b/exemple.gri new file mode 100644 index 0000000..5637e4d Binary files /dev/null and b/exemple.gri differ