From 5b2bed487ca9fc440fc99690e3e68b0737616892 Mon Sep 17 00:00:00 2001 From: Lyanis Souidi Date: Fri, 28 Apr 2023 01:19:43 +0200 Subject: [PATCH] =?UTF-8?q?Cr=C3=A9ation=20du=20syst=C3=A8me=20de=20gestio?= =?UTF-8?q?n=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 */