-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSaver.java
More file actions
executable file
·128 lines (116 loc) · 3.03 KB
/
Saver.java
File metadata and controls
executable file
·128 lines (116 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;
/**
* Classe Saver. Permet de sauvegarder une partie.
*
* @author (Emeric de Bernis)
*/
public class Saver {
private Backgammon world;
private BufferedWriter writer;
public Saver(Backgammon world) {
try {
String filename = JOptionPane.showInputDialog("Veuillez rentrer le nom du fichier de sauvegarde : ");
FileWriter fstream = new FileWriter(filename+".sav");
this.writer = new BufferedWriter(fstream);
this.world = world;
} catch (IOException e) {
e.printStackTrace();
}
}
public void saveGame() {
try {
this.writer.write(this.world.getManager().getTurn());
this.writer.newLine();
} catch (IOException e1) {
e1.printStackTrace();
}
// Enregistrement des entiers du manager
for (int i : this.world.getManager().getInts()) {
try {
this.writer.write(String.valueOf(i));
this.writer.write(';');
} catch (IOException e) {
e.printStackTrace();
}
}
try {
this.writer.newLine();
} catch (IOException e) {
e.printStackTrace();
}
// Enregistrement des booléens du manager
for (boolean bool : this.world.getManager().getBools()) {
try {
if (bool)this.writer.write("true");
else this.writer.write("false");
this.writer.write(';');
} catch (IOException e) {
e.printStackTrace();
}
}
try {
this.writer.newLine();
} catch (IOException e) {
e.printStackTrace();
}
// Enregistrement des données concernant les pions blancs
for (Pion pion : this.world.getPionsBlancs()) {
for (int i : pion.getInts()) {
try {
this.writer.write(String.valueOf(i));
this.writer.write(';');
} catch (IOException e) {
e.printStackTrace();
}
}
for (boolean bool : pion.getBools()) {
try {
if (bool)this.writer.write("true");
else this.writer.write("false");
this.writer.write(';');
} catch (IOException e) {
e.printStackTrace();
}
}
try {
this.writer.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
// Enregistrement des données concernant les pions noirs
for (Pion pion : this.world.getPionsNoirs()) {
for (int i : pion.getInts()) {
try {
this.writer.write(String.valueOf(i));
this.writer.write(';');
} catch (IOException e) {
e.printStackTrace();
}
}
for (boolean bool : pion.getBools()) {
try {
if (bool)this.writer.write("true");
else this.writer.write("false");
this.writer.write(';');
} catch (IOException e) {
e.printStackTrace();
}
}
try {
this.writer.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
this.writer.close();
} catch (IOException e) {
e.printStackTrace();
}
JOptionPane.showMessageDialog(null, "La partie a été sauvegardée", "Sauvegarde", JOptionPane.INFORMATION_MESSAGE);
}
}