diff --git a/Enter$1.class b/Enter$1.class new file mode 100644 index 0000000..be8b132 Binary files /dev/null and b/Enter$1.class differ diff --git a/Enter.class b/Enter.class new file mode 100644 index 0000000..6eb710e Binary files /dev/null and b/Enter.class differ diff --git a/Enter.java b/Enter.java new file mode 100644 index 0000000..2e880d1 --- /dev/null +++ b/Enter.java @@ -0,0 +1,68 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +public class Enter extends JPanel { + private static final int GRID_SIZE = 3; + private static final int REGION_SIZE = 3; + private static final int CELL_SIZE = 50; + private int[][] grid; + private int[][] initialGrid; // Pour stocker la grille initiale + private int selectedRow = -1; + private int selectedCol = -1; + + public Enter(int[][] grid) { + this.grid = grid; + // Copie de la grille initiale + this.initialGrid = new int[grid.length][grid[0].length]; + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[i].length; j++) { + this.initialGrid[i][j] = grid[i][j]; + } + } + setPreferredSize(new Dimension(GRID_SIZE * REGION_SIZE * CELL_SIZE, GRID_SIZE * REGION_SIZE * CELL_SIZE)); + setFocusable(true); // Permet au JPanel de recevoir les événements de souris + + addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + int x = e.getX() / CELL_SIZE; + int y = e.getY() / CELL_SIZE; + if (x >= 0 && x < GRID_SIZE * REGION_SIZE && y >= 0 && y < GRID_SIZE * REGION_SIZE) { + selectedRow = y; + selectedCol = x; + System.out.println("Case sélectionnée : (" + selectedRow + ", " + selectedCol + ")"); + if (initialGrid[selectedRow][selectedCol] == 0) { // Ne permet la modification que pour les cellules vides + String valueStr = JOptionPane.showInputDialog(null, "Enter value for the selected cell:"); + try { + int value = Integer.parseInt(valueStr); + grid[selectedRow][selectedCol] = value; + repaint(); + } catch (NumberFormatException ex) { + JOptionPane.showMessageDialog(null, "Invalid input. Please enter a number."); + } + } else { + JOptionPane.showMessageDialog(null, "You can't edit the default value."); + } + } + } + }); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + + // Dessiner les cases de la grille + for (int i = 0; i < GRID_SIZE * REGION_SIZE; i++) { + for (int j = 0; j < GRID_SIZE * REGION_SIZE; j++) { + g.setColor(Color.BLACK); + g.drawRect(j * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE); + int value = grid[i][j]; + if (value != 0) { + g.drawString(String.valueOf(value), j * CELL_SIZE + CELL_SIZE / 2, i * CELL_SIZE + CELL_SIZE / 2); + } + } + } + } +} diff --git a/Grilles/Grille1 b/Grilles/Grille1 new file mode 100644 index 0000000..a0527ea --- /dev/null +++ b/Grilles/Grille1 @@ -0,0 +1,9 @@ +000095004 +530408702 +000700603 +900034080 +040010070 +020570006 +409002000 +607903021 +200650000 \ No newline at end of file diff --git a/Grilles/Grille2 b/Grilles/Grille2 new file mode 100644 index 0000000..a0077de --- /dev/null +++ b/Grilles/Grille2 @@ -0,0 +1,9 @@ +062500090 +100029000 +059008602 +047190300 +906302104 +001087920 +604800510 +000940007 +090001480 \ No newline at end of file diff --git a/Grilles/info.txt b/Grilles/info.txt new file mode 100644 index 0000000..01ec903 --- /dev/null +++ b/Grilles/info.txt @@ -0,0 +1 @@ +grille 2 : https://la-conjugaison.nouvelobs.com/sudoku/facile.php \ No newline at end of file diff --git a/Main.class b/Main.class index 585252d..42256b8 100644 Binary files a/Main.class and b/Main.class differ diff --git a/Main.java b/Main.java index 14399b0..197bfee 100644 --- a/Main.java +++ b/Main.java @@ -2,14 +2,19 @@ import javax.swing.*; public class Main { public static void main(String[] args) { - int[][] grid = SudokuGenerator.generateGrid(); + System.out.println("Le programme Main a démarré."); + + int[][] grid = SudokuGenerator.readGridFromFile(); // Demander à l'utilisateur de sélectionner un fichier de grille + System.out.println("Grille Sudoku générée à partir du fichier avec succès."); JFrame frame = new JFrame("Sudoku"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - SudokuDraw sudokuDraw = new SudokuDraw(grid); - frame.add(sudokuDraw); + Enter enterPanel = new Enter(grid); // Utilisation de la classe Enter pour permettre la saisie des valeurs + frame.add(enterPanel); frame.pack(); frame.setLocationRelativeTo(null); // Centrer la fenêtre sur l'écran frame.setVisible(true); + + System.out.println("La fenêtre Sudoku a été affichée."); } } diff --git a/SudokuGenerator.class b/SudokuGenerator.class index 2331083..d259d3e 100644 Binary files a/SudokuGenerator.class and b/SudokuGenerator.class differ diff --git a/SudokuGenerator.java b/SudokuGenerator.java index de7719d..c0811d1 100644 --- a/SudokuGenerator.java +++ b/SudokuGenerator.java @@ -1,5 +1,9 @@ +import javax.swing.*; +import java.io.*; + public class SudokuGenerator { public static int[][] generateGrid() { + // grille par défaut int[][] grid = { {0, 0, 0, 9, 0, 5, 0, 0, 4}, {5, 0, 3, 0, 0, 4, 0, 8, 7}, @@ -14,4 +18,34 @@ public class SudokuGenerator { return grid; } + + public static int[][] readGridFromFile() { + int[][] grid = new int[9][9]; + JFileChooser fileChooser = new JFileChooser(); + int result = fileChooser.showOpenDialog(null); + if (result == JFileChooser.APPROVE_OPTION) { + File selectedFile = fileChooser.getSelectedFile(); + try (BufferedReader reader = new BufferedReader(new FileReader(selectedFile))) { + for (int i = 0; i < 9; i++) { + String line = reader.readLine(); + // Vérifier la longueur de la ligne lue + if (line.length() != 9) { + JOptionPane.showMessageDialog(null, "Invalid file format. Please select a file with correct Sudoku grid format."); + return generateGrid(); // Charger la grille par défaut en cas d'erreur + } + for (int j = 0; j < 9; j++) { + char ch = line.charAt(j); + if (ch >= '1' && ch <= '9') { + grid[i][j] = Character.getNumericValue(ch); + } else { + grid[i][j] = 0; + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + return grid; + } }