SAE21_2021/SaveManager.java

78 lines
2.1 KiB
Java
Raw Normal View History

2022-05-24 13:49:09 +02:00
import java.awt.Dimension;
import java.io.*;
public class SaveManager {
public void save(Grille grille, String nomFichier) {
Dimension taille = grille.getDimensionGrille();
2022-05-25 08:47:55 +02:00
String metaData= (int) taille.getWidth()+"x"+ (int) taille.getHeight()+"x"+ grille.getMines()+"\n";
2022-05-24 13:49:09 +02:00
try {
BufferedWriter fichier = new BufferedWriter(new FileWriter(nomFichier));
Case[] toSave = grille.getPlateau();
fichier.write(metaData);
for(Case c : toSave) {
fichier.write(c.toString());
}
fichier.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public SaveData load(String nomFichier) {
String toReturn="";
String ligne;
2022-05-25 08:47:55 +02:00
int mines=0;
2022-05-24 13:49:09 +02:00
Dimension dimension=new Dimension(0,0);
2022-05-25 08:47:55 +02:00
SaveData toRet=new SaveData(null, null, 0);
2022-05-24 13:49:09 +02:00
try {
BufferedReader fichier = new BufferedReader(new FileReader(nomFichier));
ligne=fichier.readLine();
2022-05-25 08:47:55 +02:00
String[] infos = ligne.split("x");
2022-05-24 13:49:09 +02:00
2022-05-25 08:47:55 +02:00
int largeur = Integer.parseInt(infos[0]);
int hauteur = Integer.parseInt(infos[1]);
mines = Integer.parseInt(infos[2]);
2022-05-24 13:49:09 +02:00
dimension = new Dimension(largeur, hauteur);
while((ligne = fichier.readLine()) != null) {
toReturn += ligne;
}
fichier.close();
} catch (IOException e) {
e.printStackTrace();
}
2022-05-25 08:47:55 +02:00
toRet.cases=toReturn;
toRet.taille=dimension;
toRet.mines=mines;
2022-05-24 13:49:09 +02:00
2022-05-24 23:41:34 +02:00
File save = new File(nomFichier);
save.delete();
2022-05-24 13:49:09 +02:00
return toRet;
}
public class SaveData{
public Dimension taille;
public String cases;
2022-05-25 08:47:55 +02:00
public int mines;
2022-05-24 13:49:09 +02:00
2022-05-25 08:47:55 +02:00
public SaveData(Dimension taille, String cases, int mines) {
2022-05-24 13:49:09 +02:00
this.taille = taille;
this.cases = cases;
}
public String toString() {
2022-05-25 08:47:55 +02:00
return (int) taille.getWidth()+"x"+(int) taille.getHeight()+"x"+mines+"\n"+cases;
2022-05-24 13:49:09 +02:00
}
}
}