import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.EOFException; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Grid { private Cell[][] cells; public Grid() { cells = new Cell[9][9]; for (int ligne = 0; ligne < 9; ligne++) { for (int column = 0; column < 9; column++) { cells[ligne][column] = new Cell(); } } } public void loadGridFromFile(String fileName) { try (DataInputStream input = new DataInputStream(new FileInputStream(fileName))) { for (int ligne = 0; ligne < 9; ligne++) { String line = String.valueOf(input.readInt()); int length = line.length(); if (length < 9) { line = "000000000".substring(length) + line; // Ajoute les zéros au début si nécessaire } for (int column = 0; column < 9; column++) { char number = line.charAt(column); int value = Character.getNumericValue(number); cells[ligne][column].setValue(value); } } System.out.println("Success"); System.out.println(this); } catch (FileNotFoundException e) { System.err.println("Erreur : Fichier non trouvé: " + e.getMessage()); } catch (EOFException e) { System.err.println("Erreur : Fin de fichier atteinte prématurément: " + e.getMessage()); } catch (IOException e) { System.err.println("Erreur d'entrée/sortie: " + e.getMessage()); } } public void saveGridToFile(String fileName) { try (DataOutputStream output = new DataOutputStream(new FileOutputStream(fileName))) { for (int ligne = 0; ligne < 9; ligne++) { StringBuilder line = new StringBuilder(); for (int column = 0; column < 9; column++) { int value = cells[ligne][column].getValue(); line.append(value); } output.writeInt(Integer.parseInt(line.toString())); } System.out.println("Grille sauvegardée avec succès dans " + fileName); } catch (FileNotFoundException e) { System.err.println("Erreur : Fichier non trouvé: " + e.getMessage()); } catch (IOException e) { System.err.println("Erreur d'entrée/sortie: " + e.getMessage()); } } public Cell getCell(int ligne, int col) { return cells[ligne][col]; } public void copyFrom(Grid second_grid) { for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { this.cells[row][col].setValue(second_grid.cells[row][col].getValue()); } } } public void printGrid() { for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { System.out.print(cells[row][col].getValue() + " "); } System.out.println(); } } }