Ajout de la fonctionnalité d'import/export de grille #2
@ -1,6 +1,17 @@
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
/**
|
||||||
|
* Class to manage file import/export
|
||||||
|
* @version 1.0
|
||||||
|
* @author Amir Daouadi
|
||||||
|
* @author Lyanis Souidi
|
||||||
|
*/
|
||||||
public class FileManager {
|
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 {
|
public static Grid importGrid(File file) throws Exception {
|
||||||
Grid grid;
|
Grid grid;
|
||||||
try {
|
try {
|
||||||
@ -24,9 +35,7 @@ public class FileManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ds.close();
|
ds.close();
|
||||||
} catch (ArrayIndexOutOfBoundsException e) {
|
} catch (Exception e) {
|
||||||
throw new Exception("Le fichier est corrompu.");
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new Exception("Une erreur est survenue lors de la lecture du fichier.");
|
throw new Exception("Une erreur est survenue lors de la lecture du fichier.");
|
||||||
}
|
}
|
||||||
return grid;
|
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 {
|
public static void exportGrid(Grid grid, File file) throws Exception {
|
||||||
try (FileOutputStream fs = new FileOutputStream(file);
|
try {
|
||||||
DataOutputStream ds = new DataOutputStream(fs)) {
|
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
|
// Écriture de la position de Thésée
|
||||||
ds.writeByte(grid.getSize());
|
Square theseeSquare = grid.getThesee().getSquare();
|
||||||
|
ds.writeByte(theseeSquare.getRow());
|
||||||
|
ds.writeByte(theseeSquare.getColumn());
|
||||||
|
|
||||||
// Écriture de la position de Thésée
|
// Écriture de la position de la sortie
|
||||||
Square theseeSquare = grid.getThesee().getSquare();
|
for (int i = 0; i < grid.getSize(); i++) {
|
||||||
ds.writeByte(theseeSquare.getRow());
|
for (int j = 0; j < grid.getSize(); j++) {
|
||||||
ds.writeByte(theseeSquare.getColumn());
|
Square square = grid.getSquare(i, j);
|
||||||
|
if (square.isExit()) {
|
||||||
// Écriture de la position de la sortie
|
ds.writeByte(square.getRow());
|
||||||
for (int i = 0; i < grid.getSize(); i++) {
|
ds.writeByte(square.getColumn());
|
||||||
for (int j = 0; j < grid.getSize(); j++) {
|
break;
|
||||||
Square square = grid.getSquare(i,j);
|
}
|
||||||
if (square.isExit()) {
|
|
||||||
ds.writeByte(square.getRow());
|
|
||||||
ds.writeByte(square.getColumn());
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
// Écriture des murs
|
||||||
// Écriture des murs
|
int bit = 0;
|
||||||
int bit = 0;
|
byte value = 0;
|
||||||
byte value = 0;
|
for (int i = 0; i < grid.getSize(); i++) {
|
||||||
for (int i = 0; i < grid.getSize(); i++) {
|
for (int j = 0; j < grid.getSize(); j++) {
|
||||||
for (int j = 0; j < grid.getSize(); j++) {
|
Square square = grid.getSquare(i, j);
|
||||||
Square square = grid.getSquare(i, j);
|
if (square.isWall()) {
|
||||||
if (square.isWall()) {
|
value |= 1 << (7 - bit);
|
||||||
value |= 1 << (7 - bit);
|
}
|
||||||
}
|
bit++;
|
||||||
bit++;
|
if (bit == 8) {
|
||||||
if (bit == 8) {
|
ds.writeByte(value);
|
||||||
ds.writeByte(value);
|
value = 0;
|
||||||
value = 0;
|
bit = 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) {
|
} catch (FileNotFoundException e){
|
||||||
ds.writeByte(value);
|
throw new Exception("Fichier non trouvé.");
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new Exception("Une erreur est survenue lors de l'écriture du fichier.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user