-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameGrid.java
244 lines (222 loc) · 7.7 KB
/
GameGrid.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
import javax.swing.*;
import java.awt.*;
/**
* Classe <code>GameGrid</code>
* @author Yvan
* @version 1.0
*/
public class GameGrid extends JPanel{
/**
* Parent de la classe, utilsié pour pouvoir faire des appels plus tard dans le code en cas de besoins
* Comme pour signaler un changement du nombre de bombes restantes
*/
private Game parent;
/**
* Paramètres de la partie
*/
/**
* Hauteur de la grille
*/
protected int hauteur;
/**
* Largeur de la grille
*/
protected int largeur;
/**
* Nombre de bombes sur la grille
*/
protected int nbBombes;
/**
* Status de la partie
* Indique si la partie est finie ou non (perte ou victoire)
*/
protected boolean ended = false;
/**
* Permet de stocker les différentes cases.
* Tableau multidimentionnel
*/
protected Case tab[][] = null;
/**
* Initialisation de la classe <code>Grille</code>
* Créer la fenêtre et vient placer les bombes
* @param h hauteur de la grille
* @param l largeur de la grille
* @param b nombres de bombes
* @param pF fenêtre parent (jeu)
* @see Game
*/
public GameGrid(int h, int l, int b, Game pF){
/* Initialisation des variables */
this.parent = pF;
this.tab = new Case[h][l];
this.hauteur = h;
this.largeur = l;
this.nbBombes = b;
GridLayout gestionnaire = new GridLayout(h,l);
this.setLayout(gestionnaire);
for(int x=0; x < this.hauteur; x++){ /* hauteur */
for(int y = 0; y < this.largeur; y++){ /* largeur */
Case caseToAdd = new Case(x,y, this);
this.tab[x][y] = caseToAdd;
/* On force la dimension de la case */
caseToAdd.setSize(30, 30);
caseToAdd.setPreferredSize(new Dimension(30,30));
this.add(caseToAdd);
}
}
}
/**
* Générer aléatoirement la grille
*/
public void generateGrid(){
/* Placement des bombes aléatoirement */
int nbToPlace = nbBombes;
while(0 < nbToPlace){
for(int x=0; x < this.hauteur; x++){
for(int y = 0; y < this.largeur; y++){
if(Math.random() > 0.99){
if(!this.tab[x][y].isBomb){
this.tab[x][y].setBomb();
this.processNeighboorCase(x, y);
nbToPlace--;
}
}
if(nbToPlace == 0) break;
}
if(nbToPlace == 0) break;
}
}
}
/*
* S'occupe de prévenir les cases voisines qu'il y a une nouvelle bombe à côté d'elles.
* @param x coordonées x de la bombe
* @param y cordonnées y de la bombe
* @see Case
*/
private void processNeighboorCase(int x, int y){
/* Insérer les trucs à faire lol */
for (int _x = - 1; _x <= 1 ; _x++){
for(int _y = - 1; _y <= 1; _y++){
/* On vérifie que la case calculée ne soit pas en dehors du tableau pour éviter les erreurs outofbond*/
if(x + _x >= 0 && y + _y >= 0 && x + _x < this.hauteur && y + _y < this.largeur){
this.tab[x + _x ][y + _y].incrementerNbBombesAlentours();
}
}
}
}
/**
* Afficher les autres cases aux alentours d'une case (en l'occurence des cases qui ne sont pas des bombes)
* @param x coordonées x de la case
* @param y cordonnées y de la case
* @see Case
*/
public void revealNonBombsNeighboors(int x, int y){
/* Insérer les trucs à faire lol */
for (int _x = - 1; _x <= 1 ; _x++){
for(int _y = - 1; _y <= 1; _y++){
/* On vérifie que la case calculée ne soit pas en dehors du tableau pour éviter les erreurs outofbond*/
if(x + _x >= 0 && y + _y >= 0 && x + _x < this.hauteur && y + _y < this.largeur){
if(!this.tab[x + _x ][y + _y].isBomb && !this.tab[x + _x ][y + _y].revealed){
this.tab[x + _x ][y + _y].leftClick(false);
}
}
}
}
}
/**
* Afficher les autres cases aux alentours d'une case
* @param x coordonées x de la case
* @param y cordonnées y de la case
* @see Case
*/
public void revealNeighboors(int x, int y){
/* Insérer les trucs à faire lol */
for (int _x = - 1; _x <= 1 ; _x++){
for(int _y = - 1; _y <= 1; _y++){
/* On vérifie que la case calculée ne soit pas en dehors du tableau pour éviter les erreurs outofbond*/
if(x + _x >= 0 && y + _y >= 0 && x + _x < this.hauteur && y + _y < this.largeur){
if(!this.tab[x + _x ][y + _y].revealed){
this.tab[x + _x ][y + _y].leftClick(false);
}
}
}
}
}
/**
* Compte le nombre de drapeaux à côté d'une case
* @param x coordonées x de la case
* @param y cordonnées y de la case
* @return le nombre de drapeaux à côté d'une case
* @see Case
*/
public int countFlagsNear(int x, int y){
/* Insérer les trucs à faire lol */
int count = 0;
for (int _x = - 1; _x <= 1 ; _x++){
for(int _y = - 1; _y <= 1; _y++){
/* On vérifie que la case calculée ne soit pas en dehors du tableau pour éviter les erreurs outofbond*/
if(x + _x >= 0 && y + _y >= 0 && x + _x < this.hauteur && y + _y < this.largeur){
if(this.tab[x + _x ][y + _y].state != 0){
count++;
}
}
}
}
return count;
}
/**
* Méthode appellée lorsqu'un bombe a été révélée par un joueur
* @see Case
*/
public void gameLost(){
for(int x=0; x < this.hauteur; x++){ /* hauteur */
for(int y = 0; y < this.largeur; y++){ /* largeur */
this.tab[x][y].reveal();
}
}
this.ended = true;
new EndScreen("Perdu !", this.parent);
}
/**
* Vérifie si le joueur a gagné la partie ou pas encore
* Méthode appellée à chaque fois que le joueur signale une bombe
* @see Case
*/
public void checkWin(){
boolean win = true;
for(int x=0; x < this.hauteur; x++){ /* hauteur */
for(int y = 0; y < this.largeur; y++){ /* largeur */
if(!this.tab[x][y].revealed & !this.tab[x][y].isBomb) win = false;
if(!this.tab[x][y].isBomb && this.tab[x][y].state != 0) win = false;
if(this.tab[x][y].isBomb && this.tab[x][y].state != 1) win = false;
if(!win){
break;
}
}
if(!win) break;
}
if(win){
this.ended = true;
new EndScreen("Vous avez gagné !!!", this.parent);
}
}
/**
* Méthode pour connaître le nombre de flags sur le plateau
* @return le nombre de flags sur le plateau
*/
public int countFlags(){
int count = 0;
for(int x=0; x < this.hauteur; x++){ /* hauteur */
for(int y = 0; y < this.largeur; y++){ /* largeur */
if(this.tab[x][y].state == 1) count++;
}
}
return count;
}
/**
* Méthode pour mettre à jour le nombre de flags sur le plateau
*/
public void updateFlagsCount(){
this.parent.updateFlagsCount(this.countFlags());
}
}