From 5b2bed487ca9fc440fc99690e3e68b0737616892 Mon Sep 17 00:00:00 2001 From: Lyanis Souidi Date: Fri, 28 Apr 2023 01:19:43 +0200 Subject: [PATCH 1/5] =?UTF-8?q?Cr=C3=A9ation=20du=20syst=C3=A8me=20de=20ge?= =?UTF-8?q?stion=20des=20fichiers=20.lab?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/FileManager.java | 56 ++++++++++++++++++++++++++++++++++++++++++++ src/Square.java | 4 ++++ 2 files changed, 60 insertions(+) create mode 100644 src/FileManager.java diff --git a/src/FileManager.java b/src/FileManager.java new file mode 100644 index 0000000..925feb2 --- /dev/null +++ b/src/FileManager.java @@ -0,0 +1,56 @@ +import java.io.*; + +public class FileManager { + public static Grid importGrid(File file) throws Exception { + Grid grid; + try { + FileInputStream fs = new FileInputStream(file); + DataInputStream ds = new DataInputStream(fs); + try { + grid = new Grid(ds.readByte()); + grid.getThesee().setSquare(grid.getSquare(ds.readByte(), ds.readByte())); + grid.getSquare(ds.readByte(), ds.readByte()).setExit(); + + int bit = 8; + byte value = 0; + for (int i = 0; i < grid.getSize(); i++) { + for (int j = 0; j < grid.getSize(); j++) { + if (bit == 8) { + value = ds.readByte(); + bit = 0; + } + if (((value >> (7 - bit)) & 1) == 1) grid.getSquare(i, j).setWall(); + bit++; + } + } + ds.close(); + } catch (ArrayIndexOutOfBoundsException e) { + throw new Exception("Le fichier est corrompu."); + } catch (IOException e) { + throw new Exception("Une erreur est survenue lors de la lecture du fichier."); + } + return grid; + } catch (FileNotFoundException e){ + throw new Exception("Fichier non trouvé."); + } + } + + public static void exportGrid(Grid grid, File file) throws Exception { + // TODO: Export de la grille + } + + // Test (à retirer) + public static void main(String[] args) throws Exception { + Grid grid = importGrid(new File("./src/petit.lab")); + + System.out.println("Grille " + grid.getSize() + "x" + grid.getSize() + " :"); + for (int i = 0; i < grid.getSize(); i++) { + for (int j = 0; j < grid.getSize(); j++) { + Square square = grid.getSquare(i, j); + System.out.println("\tCase " + i + "x" + j + " : isEmpty:" + square.isEmpty() + ", isWall:" + square.isWall() + ", isExit:" + square.isExit() + ", isThesee:" + square.isThesee()); + } + } + + exportGrid(grid, new File("./src/petit2.lab")); + } +} diff --git a/src/Square.java b/src/Square.java index 5e7de4c..3096ae3 100644 --- a/src/Square.java +++ b/src/Square.java @@ -34,6 +34,10 @@ public class Square { return this.type == 0; } + public boolean isThesee() { + return this.gridModel.getThesee().getSquare() == this; + } + /** * Sets the current square as a wall */ From e4ad5b180a014eb6f08e91079d29b32b4a4ce79d Mon Sep 17 00:00:00 2001 From: Amir Daouadi Date: Fri, 28 Apr 2023 02:41:09 +0200 Subject: [PATCH 2/5] Ajout de l'export --- src/FileManager.java | 48 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/FileManager.java b/src/FileManager.java index 925feb2..5afb28c 100644 --- a/src/FileManager.java +++ b/src/FileManager.java @@ -36,7 +36,53 @@ public class FileManager { } public static void exportGrid(Grid grid, File file) throws Exception { - // TODO: Export de la grille + try (FileOutputStream fs = new FileOutputStream(file); + DataOutputStream ds = new DataOutputStream(fs)) { + + // Écriture de la taille de la grille + ds.writeByte(grid.getSize()); + + // Écriture de la position de Thésée + Square theseeSquare = grid.getThesee().getSquare(); + ds.writeByte(theseeSquare.getRow()); + ds.writeByte(theseeSquare.getColumn()); + + // Écriture de la position de la sortie + for (int i = 0; i < grid.getSize(); i++) { + for (int j = 0; j < grid.getSize(); j++) { + Square square = grid.getSquare(i,j); + if (square.isExit()) { + ds.writeByte(square.getRow()); + ds.writeByte(square.getColumn()); + break; + } + } + } + + + // Écriture des murs + int bit = 0; + byte value = 0; + for (int i = 0; i < grid.getSize(); i++) { + for (int j = 0; j < grid.getSize(); j++) { + Square square = grid.getSquare(i, j); + if (square.isWall()) { + value |= 1 << (7 - bit); + } + bit++; + if (bit == 8) { + ds.writeByte(value); + value = 0; + bit = 0; + } + } + } + if (bit != 0) { + ds.writeByte(value); + } + } catch (IOException e) { + throw new Exception("Une erreur est survenue lors de l'écriture du fichier."); + } } // Test (à retirer) From 4446af333f34aeabc70c6e5154269ca5eade016e Mon Sep 17 00:00:00 2001 From: Lyanis Souidi Date: Fri, 28 Apr 2023 03:06:46 +0200 Subject: [PATCH 3/5] Modification de FileManager - Suppressions du code de debug - Modification de la gestion des exceptions - Ajout du JavaDoc --- src/FileManager.java | 117 ++++++++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 57 deletions(-) diff --git a/src/FileManager.java b/src/FileManager.java index 5afb28c..2089309 100644 --- a/src/FileManager.java +++ b/src/FileManager.java @@ -1,6 +1,17 @@ import java.io.*; - +/** + * Class to manage file import/export + * @version 1.0 + * @author Amir Daouadi + * @author Lyanis Souidi + */ public class FileManager { + /** + * Import a grid from a file + * @param file The file to import + * @return The imported grid + * @throws Exception If an error occurs during the import + */ public static Grid importGrid(File file) throws Exception { Grid grid; try { @@ -24,9 +35,7 @@ public class FileManager { } } ds.close(); - } catch (ArrayIndexOutOfBoundsException e) { - throw new Exception("Le fichier est corrompu."); - } catch (IOException e) { + } catch (Exception e) { throw new Exception("Une erreur est survenue lors de la lecture du fichier."); } return grid; @@ -35,68 +44,62 @@ public class FileManager { } } + /** + * Export a grid to a file + * @param grid The grid to export + * @param file The file to export to + * @throws Exception If an error occurs during the export + */ public static void exportGrid(Grid grid, File file) throws Exception { - try (FileOutputStream fs = new FileOutputStream(file); - DataOutputStream ds = new DataOutputStream(fs)) { + try { + FileOutputStream fs = new FileOutputStream(file); + DataOutputStream ds = new DataOutputStream(fs); + try { + // Écriture de la taille de la grille + ds.writeByte(grid.getSize()); - // Écriture de la taille de la grille - ds.writeByte(grid.getSize()); + // Écriture de la position de Thésée + Square theseeSquare = grid.getThesee().getSquare(); + ds.writeByte(theseeSquare.getRow()); + ds.writeByte(theseeSquare.getColumn()); - // Écriture de la position de Thésée - Square theseeSquare = grid.getThesee().getSquare(); - ds.writeByte(theseeSquare.getRow()); - ds.writeByte(theseeSquare.getColumn()); - - // Écriture de la position de la sortie - for (int i = 0; i < grid.getSize(); i++) { - for (int j = 0; j < grid.getSize(); j++) { - Square square = grid.getSquare(i,j); - if (square.isExit()) { - ds.writeByte(square.getRow()); - ds.writeByte(square.getColumn()); - break; + // Écriture de la position de la sortie + for (int i = 0; i < grid.getSize(); i++) { + for (int j = 0; j < grid.getSize(); j++) { + Square square = grid.getSquare(i, j); + if (square.isExit()) { + ds.writeByte(square.getRow()); + ds.writeByte(square.getColumn()); + break; + } } } - } - - // Écriture des murs - int bit = 0; - byte value = 0; - for (int i = 0; i < grid.getSize(); i++) { - for (int j = 0; j < grid.getSize(); j++) { - Square square = grid.getSquare(i, j); - if (square.isWall()) { - value |= 1 << (7 - bit); - } - bit++; - if (bit == 8) { - ds.writeByte(value); - value = 0; - bit = 0; + // Écriture des murs + int bit = 0; + byte value = 0; + for (int i = 0; i < grid.getSize(); i++) { + for (int j = 0; j < grid.getSize(); j++) { + Square square = grid.getSquare(i, j); + if (square.isWall()) { + value |= 1 << (7 - bit); + } + bit++; + if (bit == 8) { + ds.writeByte(value); + value = 0; + bit = 0; + } } } + if (bit != 0) { + ds.writeByte(value); + } + } catch (Exception e) { + throw new Exception("Une erreur est survenue lors de l'écriture du fichier."); } - if (bit != 0) { - ds.writeByte(value); - } - } catch (IOException e) { - throw new Exception("Une erreur est survenue lors de l'écriture du fichier."); + } catch (FileNotFoundException e){ + throw new Exception("Fichier non trouvé."); } } - - // Test (à retirer) - public static void main(String[] args) throws Exception { - Grid grid = importGrid(new File("./src/petit.lab")); - - System.out.println("Grille " + grid.getSize() + "x" + grid.getSize() + " :"); - for (int i = 0; i < grid.getSize(); i++) { - for (int j = 0; j < grid.getSize(); j++) { - Square square = grid.getSquare(i, j); - System.out.println("\tCase " + i + "x" + j + " : isEmpty:" + square.isEmpty() + ", isWall:" + square.isWall() + ", isExit:" + square.isExit() + ", isThesee:" + square.isThesee()); - } - } - - exportGrid(grid, new File("./src/petit2.lab")); - } } From 19349d89aecbbffb88a19c83fd55011f0e8969b4 Mon Sep 17 00:00:00 2001 From: Lyanis Souidi Date: Fri, 28 Apr 2023 03:16:05 +0200 Subject: [PATCH 4/5] =?UTF-8?q?Int=C3=A9gration=20de=20la=20fonctionnalit?= =?UTF-8?q?=C3=A9=20au=20menu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/HomeView.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/HomeView.java b/src/HomeView.java index 59f9fe4..83253e5 100644 --- a/src/HomeView.java +++ b/src/HomeView.java @@ -95,7 +95,7 @@ public class HomeView extends JPanel { return choisirGrille; } - private static JButton importerGrille(JFrame window) { + private static JButton importerGrille(Window window) { JPanel panel = new JPanel(); JButton importerGrille = new JButton("Importer une grille"); importerGrille.setPreferredSize(new Dimension(250, 50)); @@ -108,7 +108,14 @@ public class HomeView extends JPanel { int choix = fileChooser.showOpenDialog(panel); if (choix == JFileChooser.APPROVE_OPTION) { File fichier = fileChooser.getSelectedFile(); - // TODO: charger la grille depuis le fichier + try { + GridView gridView = new GridView(window); + new GridController(FileManager.importGrid(fichier), gridView); + window.setContentPane(gridView); + window.validate(); + } catch (Exception ex) { + JOptionPane.showMessageDialog(panel, ex.getMessage(), "Erreur", JOptionPane.ERROR_MESSAGE); + } } }); From fb2b518ee47f3177fb060550db4f4ae1f48348c4 Mon Sep 17 00:00:00 2001 From: Lyanis Souidi Date: Fri, 28 Apr 2023 03:21:10 +0200 Subject: [PATCH 5/5] FIX BUG: inversion des lignes et des colonnes --- src/FileManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/FileManager.java b/src/FileManager.java index 2089309..ac06b5f 100644 --- a/src/FileManager.java +++ b/src/FileManager.java @@ -30,7 +30,7 @@ public class FileManager { value = ds.readByte(); bit = 0; } - if (((value >> (7 - bit)) & 1) == 1) grid.getSquare(i, j).setWall(); + if (((value >> (7 - bit)) & 1) == 1) grid.getSquare(j, i).setWall(); bit++; } } @@ -80,7 +80,7 @@ public class FileManager { byte value = 0; for (int i = 0; i < grid.getSize(); i++) { for (int j = 0; j < grid.getSize(); j++) { - Square square = grid.getSquare(i, j); + Square square = grid.getSquare(j, i); if (square.isWall()) { value |= 1 << (7 - bit); }