Ajout de la fonctionnalité d'import/export de grille #2

Merged
Lyanis SOUIDI merged 6 commits from feature_filemanager into master 2023-04-28 03:23:44 +02:00
3 changed files with 118 additions and 2 deletions

105
src/FileManager.java Normal file
View File

@ -0,0 +1,105 @@
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 {
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(j, i).setWall();
bit++;
}
}
ds.close();
} catch (Exception 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é.");
}
}
/**
* 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 {
// É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(j, i);
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.");
}
} catch (FileNotFoundException e){
throw new Exception("Fichier non trouvé.");
}
}
}

View File

@ -95,7 +95,7 @@ public class HomeView extends JPanel {
return choisirGrille; return choisirGrille;
} }
private static JButton importerGrille(JFrame window) { private static JButton importerGrille(Window window) {
JPanel panel = new JPanel(); JPanel panel = new JPanel();
JButton importerGrille = new JButton("Importer une grille"); JButton importerGrille = new JButton("Importer une grille");
importerGrille.setPreferredSize(new Dimension(250, 50)); importerGrille.setPreferredSize(new Dimension(250, 50));
@ -108,7 +108,14 @@ public class HomeView extends JPanel {
int choix = fileChooser.showOpenDialog(panel); int choix = fileChooser.showOpenDialog(panel);
if (choix == JFileChooser.APPROVE_OPTION) { if (choix == JFileChooser.APPROVE_OPTION) {
File fichier = fileChooser.getSelectedFile(); 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);
}
} }
}); });

View File

@ -34,6 +34,10 @@ public class Square {
return this.type == 0; return this.type == 0;
} }
public boolean isThesee() {
return this.gridModel.getThesee().getSquare() == this;
}
/** /**
* Sets the current square as a wall * Sets the current square as a wall
*/ */