2023-04-28 01:19:43 +02:00
|
|
|
import java.io.*;
|
2023-04-28 03:06:46 +02:00
|
|
|
/**
|
|
|
|
* Class to manage file import/export
|
2023-04-28 15:44:56 +02:00
|
|
|
* @version 1.1
|
2023-04-28 03:06:46 +02:00
|
|
|
* @author Amir Daouadi
|
|
|
|
* @author Lyanis Souidi
|
|
|
|
*/
|
2023-04-28 01:19:43 +02:00
|
|
|
public class FileManager {
|
2023-04-28 03:06:46 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2023-04-28 01:19:43 +02:00
|
|
|
public static Grid importGrid(File file) throws Exception {
|
|
|
|
Grid grid;
|
|
|
|
try {
|
|
|
|
FileInputStream fs = new FileInputStream(file);
|
|
|
|
DataInputStream ds = new DataInputStream(fs);
|
|
|
|
try {
|
2023-04-28 15:44:56 +02:00
|
|
|
grid = new Grid(ds.read());
|
|
|
|
grid.getThesee().setSquare(grid.getSquare(ds.read(), ds.read()));
|
|
|
|
grid.getSquare(ds.read(), ds.read()).setExit();
|
2023-04-28 01:19:43 +02:00
|
|
|
|
|
|
|
int bit = 8;
|
2023-04-28 15:44:56 +02:00
|
|
|
int value = 0;
|
2023-04-28 01:19:43 +02:00
|
|
|
for (int i = 0; i < grid.getSize(); i++) {
|
|
|
|
for (int j = 0; j < grid.getSize(); j++) {
|
|
|
|
if (bit == 8) {
|
2023-04-28 15:44:56 +02:00
|
|
|
value = ds.read();
|
2023-04-28 01:19:43 +02:00
|
|
|
bit = 0;
|
|
|
|
}
|
2023-04-28 03:21:10 +02:00
|
|
|
if (((value >> (7 - bit)) & 1) == 1) grid.getSquare(j, i).setWall();
|
2023-04-28 01:19:43 +02:00
|
|
|
bit++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ds.close();
|
2023-04-28 03:06:46 +02:00
|
|
|
} catch (Exception e) {
|
2023-04-28 01:19:43 +02:00
|
|
|
throw new Exception("Une erreur est survenue lors de la lecture du fichier.");
|
|
|
|
}
|
|
|
|
return grid;
|
|
|
|
} catch (FileNotFoundException e){
|
|
|
|
throw new Exception("Fichier non trouvé.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-28 03:06:46 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2023-04-28 01:19:43 +02:00
|
|
|
public static void exportGrid(Grid grid, File file) throws Exception {
|
2023-04-28 03:06:46 +02:00
|
|
|
try {
|
|
|
|
FileOutputStream fs = new FileOutputStream(file);
|
|
|
|
DataOutputStream ds = new DataOutputStream(fs);
|
|
|
|
try {
|
|
|
|
// Écriture de la taille de la grille
|
|
|
|
ds.writeByte(grid.getSize());
|
2023-04-28 02:41:09 +02:00
|
|
|
|
2023-04-28 03:06:46 +02:00
|
|
|
// Écriture de la position de Thésée
|
|
|
|
Square theseeSquare = grid.getThesee().getSquare();
|
|
|
|
ds.writeByte(theseeSquare.getRow());
|
|
|
|
ds.writeByte(theseeSquare.getColumn());
|
2023-04-28 02:41:09 +02:00
|
|
|
|
2023-04-28 03:06:46 +02:00
|
|
|
// É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;
|
|
|
|
}
|
2023-04-28 02:41:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-28 03:06:46 +02:00
|
|
|
// É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++) {
|
2023-04-28 03:21:10 +02:00
|
|
|
Square square = grid.getSquare(j, i);
|
2023-04-28 03:06:46 +02:00
|
|
|
if (square.isWall()) {
|
|
|
|
value |= 1 << (7 - bit);
|
|
|
|
}
|
|
|
|
bit++;
|
|
|
|
if (bit == 8) {
|
|
|
|
ds.writeByte(value);
|
|
|
|
value = 0;
|
|
|
|
bit = 0;
|
|
|
|
}
|
2023-04-28 02:41:09 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-28 03:06:46 +02:00
|
|
|
if (bit != 0) {
|
|
|
|
ds.writeByte(value);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new Exception("Une erreur est survenue lors de l'écriture du fichier.");
|
2023-04-28 02:41:09 +02:00
|
|
|
}
|
2023-04-28 03:06:46 +02:00
|
|
|
} catch (FileNotFoundException e){
|
|
|
|
throw new Exception("Fichier non trouvé.");
|
2023-04-28 01:19:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|