-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLoader.java
More file actions
executable file
·135 lines (122 loc) · 5.96 KB
/
Loader.java
File metadata and controls
executable file
·135 lines (122 loc) · 5.96 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
129
130
131
132
133
134
135
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
* Classe Loader. Permet le chargement d'une partie.
*
* @author (Emeric de Bernis)
*/
public class Loader {
private Backgammon world;
private BufferedReader reader;
public Loader(Backgammon world) {
this.world = world;
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("SAV Extension", "sav");
chooser.setFileFilter(filter);
chooser.showOpenDialog(new JFrame());
try {
this.reader = new BufferedReader(new FileReader(chooser.getSelectedFile().getPath()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void loadGame() {
this.world.removeAllPions();
try {
// Chargement du tour de jeu
String turn = this.reader.readLine();
if (turn.contains("blanc")) this.world.getManager().setTurn("blanc");
else this.world.getManager().setTurn("noir");
this.world.getText().ecrire(turn+"_turn_text");
// Chargement des entiers du manager
String line_int = this.reader.readLine();
String[] mots_int = line_int.split(";");
this.world.getManager().setNumberOfBlackOut(Integer.parseInt(mots_int[0]));
this.world.getManager().setNumberOfWhiteOut(Integer.parseInt(mots_int[1]));
// Chargement des booléens du manager
String line_bool = this.reader.readLine();
String[] mots_bool = line_bool.split(";");
this.world.getManager().setBlackCanLeave(Boolean.parseBoolean(mots_bool[0]));
this.world.getManager().setWhiteCanLeave(Boolean.parseBoolean(mots_bool[1]));
} catch (IOException e) {
e.printStackTrace();
}
// Chargement des données concernant les 15 pions blancs
for (int i=0; i<15; i++) {
try {
String line = this.reader.readLine();
String[] mots = line.split(";");
// Si le pion était en prison
if (Boolean.parseBoolean(mots[3])) {
PionBlanc pion = new PionBlanc(this.world, this.world.getPrisons().get("blanc"));
this.world.getPionsBlancs().add(pion);
pion.setMovable(Boolean.parseBoolean(mots[2]));
pion.setInPrison(Boolean.parseBoolean(mots[3]));
pion.setReadyToLeave(Boolean.parseBoolean(mots[4]));
this.world.addObject(pion, this.world.getPrisons().get("blanc").getXcoordinate(), this.world.getPrisons().get("blanc").getYcoordinates().get(pion.getGraphic_col_rank()));
}
else if (Integer.parseInt(mots[0]) == 0 || Integer.parseInt(mots[0]) == 25) {
PionBlanc pion = new PionBlanc(this.world, this.world.getSorties().get("blanc"));
this.world.getPionsBlancs().add(pion);
pion.setMovable(Boolean.parseBoolean(mots[2]));
pion.setInPrison(Boolean.parseBoolean(mots[3]));
pion.setReadyToLeave(Boolean.parseBoolean(mots[4]));
this.world.addObject(pion, this.world.getSorties().get("blanc").getXcoordinate(), this.world.getSorties().get("blanc").getYcoordinates().get(this.world.getManager().getNumberOut(pion.getColor())));
}
else {
PionBlanc pion = new PionBlanc(this.world, this.world.getColumns().get(Integer.parseInt(mots[0])));
this.world.getPionsBlancs().add(pion);
pion.setOld_col(this.world.getColumns().get(Integer.parseInt(mots[1])));
pion.setMovable(Boolean.parseBoolean(mots[2]));
pion.setInPrison(Boolean.parseBoolean(mots[3]));
pion.setReadyToLeave(Boolean.parseBoolean(mots[4]));
this.world.addObject(pion, this.world.getColumns().get(Integer.parseInt(mots[0])).getXcoordinate(), this.world.getColumns().get(Integer.parseInt(mots[0])).getYcoordinates().get(pion.getGraphic_col_rank()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
// Chargement des données concernant les 15 pions noirs
for (int i=0; i<15; i++) {
try {
String line = this.reader.readLine();
String[] mots = line.split(";");
// Si le pion était en prison
if (Boolean.parseBoolean(mots[3])) {
PionNoir pion = new PionNoir(this.world, this.world.getPrisons().get("noir"));
this.world.getPionsNoirs().add(pion);
pion.setMovable(Boolean.parseBoolean(mots[2]));
pion.setInPrison(Boolean.parseBoolean(mots[3]));
pion.setReadyToLeave(Boolean.parseBoolean(mots[4]));
this.world.addObject(pion, this.world.getPrisons().get("noir").getXcoordinate(), this.world.getPrisons().get("noir").getYcoordinates().get(pion.getGraphic_col_rank()));
}
else if (Integer.parseInt(mots[0]) == 0 || Integer.parseInt(mots[0]) == 25) {
PionNoir pion = new PionNoir(this.world, this.world.getSorties().get("noir"));
this.world.getPionsNoirs().add(pion);
pion.setMovable(Boolean.parseBoolean(mots[2]));
pion.setInPrison(Boolean.parseBoolean(mots[3]));
pion.setReadyToLeave(Boolean.parseBoolean(mots[4]));
this.world.addObject(pion, this.world.getSorties().get("noir").getXcoordinate(), this.world.getSorties().get("noir").getYcoordinates().get(this.world.getManager().getNumberOut(pion.getColor())));
}
else {
PionNoir pion = new PionNoir(this.world, this.world.getColumns().get(Integer.parseInt(mots[0])));
this.world.getPionsNoirs().add(pion);
pion.setOld_col(this.world.getColumns().get(Integer.parseInt(mots[1])));
pion.setMovable(Boolean.parseBoolean(mots[2]));
pion.setInPrison(Boolean.parseBoolean(mots[3]));
pion.setReadyToLeave(Boolean.parseBoolean(mots[4]));
this.world.addObject(pion, this.world.getColumns().get(Integer.parseInt(mots[0])).getXcoordinate(), this.world.getColumns().get(Integer.parseInt(mots[0])).getYcoordinates().get(pion.getGraphic_col_rank()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
JOptionPane.showMessageDialog(null, "La partie a été chargée", "Chargement", JOptionPane.INFORMATION_MESSAGE);
}
}