-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerMouse.java
More file actions
337 lines (312 loc) · 8.14 KB
/
ControllerMouse.java
File metadata and controls
337 lines (312 loc) · 8.14 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
* La classe <code>ControllerMouse</code> est le controller de la fenetre
* principale du jeu. Cette classe gere les clics et la disposition des objets
* dans la grille
* @version 0.1
* @author Morgan Jully et Killian Mocret
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import java.lang.*;
import java.io.*;
public class ControllerMouse implements MouseListener {
/**
* Attribut pour récuperer la fenetre du jeu
*/
private Frame fenetre;
/**
* Tableau d'objet qui correspond aux objets de la grille
* quand le joueur choisit de jouer avec une
* grille aletoire
*/
private Couleur[][] tab;
/**
* Tableau qui correspond au tablaux d'objet mais avec des
* entiers (1 = vert, 2 = bleu, 3 = rouge)
*/
private int[][] tabi;
/**
* Tableau d entier qui stocke le groupe survolé
* par l utilisateur
* -2 signifie que la case est vide
*/
private int[][] groupe;
/**
* Attribut qui stocke la taille des groupes
* -1 signifie que la case d'appartient pas au groupe
*/
private int taille_groupe = 0;
/**
* Attribut qui stocke le score actuel
*/
private int score=0;
/**
* Objet de la classe Score qui affiche un label
* avec le score
*/
private Score scoreL = new Score();
/**
* Attribut qui permet de savoir le nombre de clics après
* la fin du jeu
*/
private int click = 0;
private JLabel test = new JLabel("");
/**
* Constructeur qui ajoute un ecouteur de la classe ControllerMouse
* sur tout les objets du tableaux d objets tab
* @param f qui permet de recuperer les 2 tableaux
*/
public ControllerMouse(Frame f) {
groupe = new int[10][15];
this.fenetre = f;
this.tab = f.getTab();
this.tabi = f.getTabi();
for (int i=0; i<10; i++) {
for (int j=0; j<15; j++) {
this.tab[i][j].addMouseListener(this);
this.tab[i][j].setBackground(Color.WHITE);
groupe[i][j] = -1;
}
}
JPanel t = new JPanel();
Font font = new Font("TYPE1_FONT", Font.BOLD, 33);
this.test.setFont(font);
this.fenetre.add(t, BorderLayout.NORTH);
t.setBackground(Color.WHITE);
t.add(this.scoreL);
t.add(test);
}
@Override
public void mouseClicked(MouseEvent e) {
int fin=1;
if((Couleur.getFin())) {
this.click++;
if(this.click>=2) {
this.fenetre.dispose();
for (int i=0; i<10; i++) {
for (int j=0; j<15; j++) {
if(tabi[i][j] != -2) {
fin = 0;
}
}
}
if(fin==0) {
Couleur.setFin(false);
MenuFin menu = new MenuFin("/img/lose.png", this.score);
} else {
MenuFin menu = new MenuFin("/img/win.png", this.score);
Couleur.setFin(false);
}
}
}
}
@Override
public void mouseEntered(MouseEvent e) {
for (int i=0; i<10; i++) {
for (int j=0; j<15; j++) {
if((e.getSource() == this.tab[i][j]) && (this.tabi[i][j]!=-2)) {
this.tab[i][j].setBackground(Color.YELLOW);
checkGroupe(i,j);
System.out.println("Taille groupe : " + this.taille_groupe);
for (i=0; i<10; i++) {
for (j=0; j<15; j++) {
if((groupe[i][j]!=-1) && (this.tabi[i][j] != -2)) {
this.tab[i][j].setBackground(Color.YELLOW);
}
}
}
}
}
}
}
/**
* Methode qui permet reprer le groupe de bloc en
* fonction du bloc survole
* @param x : indice x du bloc survole
* @param y : indice y du bloc survole
*/
public void checkGroupe(int x, int y) {
if(y>0) {
if((this.tabi[x][y] == this.tabi[x][y-1]) && (this.tabi[x][y] != -2)) {
if((isInGroup(x,y-1))) {
this.groupe[x][y-1] = this.tabi[x][y-1];
this.taille_groupe++;
checkGroupe(x, y-1);
}
}
}
if(y+1<15) {
if((this.tabi[x][y] == this.tabi[x][y+1]) && (this.tabi[x][y] != -2)) {
if((isInGroup(x,y+1))) {
this.groupe[x][y+1] = this.tabi[x][y+1];
this.taille_groupe++;
checkGroupe(x, y+1);
}
}
}
if(x>0) {
if((this.tabi[x][y] == this.tabi[x-1][y]) && (this.tabi[x][y] != -2)) {
if((isInGroup(x-1, y))) {
this.groupe[x-1][y] = this.tabi[x-1][y];
this.taille_groupe++;
checkGroupe(x-1, y);
}
}
}
if(x+1<10) {
if((this.tabi[x][y] == this.tabi[x+1][y]) && (this.tabi[x][y] != -2)) {
if((isInGroup(x+1, y))) {
this.groupe[x+1][y] = this.tabi[x+1][y];
this.taille_groupe++;
checkGroupe(x+1, y);
}
}
}
}
@Override
public void mouseExited(MouseEvent e) {
for (int i=0; i<10; i++) {
for (int j=0; j<15; j++) {
this.tab[i][j].setBackground(Color.WHITE);
this.groupe[i][j] = -1;
}
}
this.taille_groupe = 0;
}
@Override
public void mousePressed(MouseEvent e) {
}
/**
* Methode qui vérifie si le bloc n a pas
* deja ete ajouté dans le tableau
* groupe
* @param x : indice x du bloc
* @param y : indice y du bloc
*/
public boolean isInGroup(int x, int y) {
for (int i=0; i<10; i++) {
for (int j=0; j<15; j++) {
if(groupe[x][y] == tabi[i][j])
return false;
}
}
return true;
}
@Override
public void mouseReleased(MouseEvent e) {
int tgroupe=0;
for(int i=0;i<10;i++) {
System.out.println("");
for (int j=0; j<15; j++) {
if(this.groupe[i][j]!=-1) {
this.tab[i][j].setBackground(Color.WHITE);
this.tab[i][j].resetCouleur();
this.tab[i][j].setCouleur(0);
this.tabi[i][j] = -2;
this.groupe[i][j] = -1;
tgroupe++;
}
reorganiseColonne(j);
System.out.print(tabi[i][j] + " ");
}
}
if(tgroupe >2){
this.score += (tgroupe-2)*(tgroupe-2);
this.scoreL.setScore(this.score);
System.out.println();
System.out.println("Score : " + this.score);
}
for (int i=0; i<15; i++) {
checkTouteColonneVide();
}
System.out.println("");
if(checkJeuFin() == 1){
Couleur.setFin(true);
this.fenetre.repaint();
this.test.setText(" | Cliquez pour continuer");
this.fenetre.addMouseListener(this);
}
}
/**
* Methode qui reorganise la colonne en bouchant les blocs libres
* @param j : la colonne a reorganiser
*/
public void reorganiseColonne(int j) {
for (int i=9; i>0; i--) {
if(this.tabi[i][j] == -2) {
int couleur = this.tabi[i-1][j];
this.tab[i-1][j].resetCouleur();
this.tab[i-1][j].setCouleur(0);
this.tabi[i-1][j] = -2;
this.tab[i][j].resetCouleur();
this.tab[i][j].setCouleur(couleur);
this.tabi[i][j] = couleur;
}
}
}
/**
* Methode qui verifie si chaque colonne est vide
* et si oui copie les colonnes de droite vers la gauche
*/
public void checkTouteColonneVide() {
int result = 0;
for (int i=0; i<15; i++) {
result = checkSiUneColonnesVide(i);
if((result == 1) && (i!=14)) {
copieColonne(i);
result = 0;
}
}
}
/**
*Methode qui verifie si une colonne est vide
* @param j : la colonne a verifie
*/
public int checkSiUneColonnesVide(int j) {
int var = 1;
for (int i=0; i<10; i++) {
if(this.tabi[i][j] !=-2)
var = 0;
}
if(j==14)
var=0;
return var;
}
/**
* Methode qui deplace une colonne vers la gauche
* @param j : la colonne a copie
*/
public void copieColonne(int j) {
for (int i=0; i<10; i++) {
int couleur = this.tabi[i][j+1];
this.tabi[i][j+1] = -2;
this.tab[i][j+1].resetCouleur();
this.tab[i][j+1].setCouleur(0);
this.tabi[i][j] = couleur;
this.tab[i][j].resetCouleur();
this.tab[i][j].setCouleur(couleur);
}
}
/**
* Methode qui veirife si la partie est fini
*/
public int checkJeuFin() {
this.taille_groupe = 0;
int fin = 1;
for (int i=0; i<10; i++) {
for (int j=0; j<15; j++) {
checkGroupe(i,j);
if(this.taille_groupe != 0)
fin = 0;
}
}
for (int i=0; i<10; i++) {
for (int j=0; j<15; j++) {
groupe[i][j] = -1;
}
}
return fin;
}
}