-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlateforme.java
313 lines (266 loc) · 7.72 KB
/
Plateforme.java
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import java.util.Scanner;
import java.io.FileInputStream;
/**
* Classe Controleur pour l'exercice 5.
*
* @author Flem Anthony | Joigneau Arsène | Bassam Youssif youssif | Lemaitre
* Valentin | Si Ahmed Sara |
*
* Date: 20/12/2024 - 10h45
*/
// Classe Plateforme
public class Plateforme
{
/*------------------------*/
/* Attributs de la classe */
/*------------------------*/
private int posLig;
private int posCol;
private char[][] grille;
private int posLigSortie;
private int posColSortie;
private int niveau;
// Constructeur de la classe Plateforme
public Plateforme()
{
/*--------------*/
/* Instructions */
/*--------------*/
this.niveau = 1;
/*-------------------------------------------*/
/* Initialisation de la taille de la grille. */
/*-------------------------------------------*/
this.initialiserTableau();
/*--------------------------------------------*/
/* Lecture et écriture du fichier grille.data */
/*--------------------------------------------*/
this.remplirTableau();
}
// Accesseurs de la classe:
public int getNbLignes()
{
return this.grille.length;
}
public int getNbColonnes()
{
return this.grille[0].length;
}
public boolean estSortie()
{
return this.posLig == this.posLigSortie && this.posCol == this.posColSortie;
}
public char getCaseEnvironnement(int lig, int col)
{
return this.getCase(lig, col, 0);
}
public char getCase(int lig, int col, int couche)
{
if (couche == 1 && lig == this.posLig && col == this.posCol)
return '+';
return this.grille[lig][col];
}
// Méthode qui permet d'initialiser la taille de la grille en fonction des
// fichiers .data
public void initialiserTableau()
{
/*---------*/
/* Données */
/*---------*/
Scanner sc;
int ligne2;
int colonne;
ligne2 = 0;
colonne = 0;
/*-------------------------------------------*/
/* Initialisation de la taille de la grille. */
/*-------------------------------------------*/
try
{
sc = new Scanner(new FileInputStream("data/niveau_0" + this.niveau + ".data"), "UTF8");
while (sc.hasNextLine())
{
String currentLine = sc.nextLine();
ligne2++;
colonne = Math.max(colonne, currentLine.length());
}
sc.close();
} catch (Exception e)
{
e.printStackTrace();
}
this.grille = new char[ligne2][colonne];
}
// Méthode qui remplit le tableau avec les éléments des fichiers .data
public void remplirTableau()
{
/*---------*/
/* Données */
/*---------*/
Scanner sc;
int lig;
String ligne;
/*--------------------------------------------*/
/* Lecture et écriture du fichier grille.data */
/*--------------------------------------------*/
try
{
sc = new Scanner(new FileInputStream("data/niveau_0" + this.niveau + ".data"), "UTF8");
lig = 0;
while (sc.hasNextLine())
{
ligne = sc.nextLine();
for (int col = 0; col < ligne.length(); col++)
{
this.grille[lig][col] = ligne.charAt(col);
/*------------------------------*/
/* initialisation du personnage */
/*------------------------------*/
if (getCase(lig, col, 1) == '+')
{
this.grille[lig][col] = ' ';
this.posLig = lig;
this.posCol = col;
}
/*-----------------------------*/
/* initialisation de la sortie */
/*-----------------------------*/
if (getCaseEnvironnement(lig, col) == '@')
{
this.posLigSortie = lig;
this.posColSortie = col;
}
}
lig++;
}
sc.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
// Méthode qui permet d'accéder au niveau suivant.
public int niveauSuivant()
{
this.niveau++;
this.initialiserTableau();
this.remplirTableau();
return this.niveau;
}
// Méthode qui permet d'accéder au niveau précédent.
public int niveauPrecedent()
{
this.niveau--;
if (this.niveau == 0)
this.niveau++;
return this.niveau;
}
// Méthode qui gère le déplacement du personnage
public void deplacer(char direction)
{
/* ------------ */
/* Données */
/* ------------ */
int futureLig;
int futureCol;
boolean sauter;
/* ------------ */
/* Instructions */
/* ------------ */
futureLig = this.posLig;
futureCol = this.posCol;
sauter = false;
/*----------------------*/
/* Direction de l'objet */
/*----------------------*/
switch (direction)
{
case 'N' -> futureLig--;
case 'O' -> futureCol--;
case 'S' -> futureLig++;
case 'E' -> futureCol++;
case 'H' -> futureLig--;
}
/*-----------------------------------*/
/* Déplacement torique du personnage */
/*-----------------------------------*/
if (futureLig < 0)
futureLig = this.grille.length - 1;
if (futureCol < 0)
futureCol = this.grille[0].length - 1;
if (futureLig > this.grille.length - 1)
futureLig = 0;
if (futureCol > this.grille[0].length - 1)
futureCol = 0;
if (direction == 'H' && getCaseEnvironnement(futureLig, futureCol) != '='
&& (getCaseEnvironnement(this.posLig + 1, this.posCol) == '='
|| getCaseEnvironnement(this.posLig + 1, this.posCol) == '#'))
{
this.posLig = futureLig;
sauter = true;
System.out.println(!sauter);
}
/*---------------------------------------*/
/* Vérifie si il y a un mur ou une porte */
/*---------------------------------------*/
if (getCaseEnvironnement(futureLig, futureCol) != '=' && getCaseEnvironnement(futureLig, futureCol) != 'R'
&& getCaseEnvironnement(futureLig, futureCol) != 'B'
&& getCaseEnvironnement(futureLig, futureCol) != 'V')
{
this.posLig = futureLig;
this.posCol = futureCol;
}
/*------------------------*/
/* Gravité du personnage */
/*------------------------*/
while ((getCaseEnvironnement(this.posLig + 1, this.posCol) == ' '
|| getCaseEnvironnement(this.posLig + 1, this.posCol) == 'b'
|| getCaseEnvironnement(this.posLig + 1, this.posCol) == 'r'
|| getCaseEnvironnement(this.posLig + 1, this.posCol) == 'v'
|| getCaseEnvironnement(this.posLig + 1, this.posCol) == '@') && !sauter)
{
System.out.println("tombe");
futureLig = this.posLig + 1;
this.posLig = futureLig;
sauter = false;
}
/*------------------------------------------------------------*/
/* Vérifie si le joueur est sur l'echelle et donc peux monter */
/*------------------------------------------------------------*/
if (getCaseEnvironnement(this.posLig, this.posCol) == '#')
{
if (getCaseEnvironnement(futureLig, futureCol) != '=')
{
if (futureLig < 0)
futureLig = this.grille.length - 1;
if (futureLig > this.grille.length - 1)
futureLig = 0;
this.posLig = futureLig;
this.posCol = futureCol;
}
}
/*----------------------------------------------------*/
/* Permet d'enlever la porte pour accéder à la sortie */
/*----------------------------------------------------*/
if (getCaseEnvironnement(futureLig, futureCol) == 'r')
{
for (int cptLig = 0; cptLig < grille.length; cptLig++)
for (int cptCol = 0; cptCol < grille[0].length; cptCol++)
if (getCaseEnvironnement(cptLig, cptCol) == 'R' || getCaseEnvironnement(cptLig, cptCol) == 'r')
this.grille[cptLig][cptCol] = ' ';
}
if (getCaseEnvironnement(futureLig, futureCol) == 'v')
{
for (int cptLig = 0; cptLig < grille.length; cptLig++)
for (int cptCol = 0; cptCol < grille[0].length; cptCol++)
if (getCaseEnvironnement(cptLig, cptCol) == 'V' || getCaseEnvironnement(cptLig, cptCol) == 'v')
this.grille[cptLig][cptCol] = ' ';
}
if (getCaseEnvironnement(futureLig, futureCol) == 'b')
{
for (int cptLig = 0; cptLig < grille.length; cptLig++)
for (int cptCol = 0; cptCol < grille[0].length; cptCol++)
if (getCaseEnvironnement(cptLig, cptCol) == 'B' || getCaseEnvironnement(cptLig, cptCol) == 'b')
this.grille[cptLig][cptCol] = ' ';
}
}
}