forked from WMystral/Marrakech
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid.java
610 lines (566 loc) · 15.1 KB
/
Grid.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/**
* Cette classe <code>Grid</code> gère la/les grilles de jeu de l'application
*
* @version 0.1
*/
import java.awt.Color;
import static java.lang.Math.abs;
public class Grid extends javax.swing.JPanel
{
/**
* Composante tableau de tableau comprenant des cellules.
*/
private Cell[][] cells;
/**
* Composante tableau pour le nombre de joueurs (de base : 2)
*/
private int[] player = new int[2]; //ligne/colonne
/**
* Un tableau de boolean pour compter la dime.
*/
private boolean[][] dime;
/**
* Composante pour la rotation du personnage
* 0, 90, 180 (vers le haut) ou 270
*/
private int rotation;
/**
* Le tour
*/
private int k;
/**
* La classe parent.
*/
private Game game;
/**
* Constante pour le nombre de carrés sur une ligne
*/
private final int SIDE_LENTGH = 7; //nombre de carrés sur ligne
/**
* Constante pour la taille des carrés
*/
private final int CELL_SIZE = 80; //taille des carrés
/**
* Constante pour l'écart entre les cellules
*/
private final int CELL_OFFSET = CELL_SIZE + 1; //écart entre les cellules
/**
* La position x avec l'offet horizontal pour l'affichage de la grille.
*/
private int xoff;
/**
* La position y avec l'offet vertical pour l'affichage de la grille.
*/
private int yoff;
/**
* Un couple de valuer x, y.
*/
private java.awt.Point point;
/**
* #define une valeur pour une case contenant un joueur.
*/
private final int PLAYER = -1;
/*
* #define une valeur pour une case vide.
*/
private final int EMPTY = 0;
/**
* La liste des voisins et de leurs voisins.
*/
private java.util.List<java.util.List<Pair>> super_list = new java.util.ArrayList<>();
/**
* Constructeur <code>Grid</code>
*
* @param f une fenêtre
* @param g la grille
*/
public Grid(Frame f, Game g)
{
super();
this.game = g;
javax.swing.RepaintManager.currentManager(this).markCompletelyClean(this);
this.init(f);
this.gen();
}
/**
* Initie les composantes graphiques de la grille
*
* @param f une fenêtre
*/
private void init(Frame f)
{
this.setBounds(0, 0, f.getWidth(), f.getHeight());
this.setLayout(null);
}
/**
* Génère une grille de jeu
*/
private void gen()
{
int xoff = this.getWidth() - (SIDE_LENTGH * (CELL_SIZE + 3));
xoff = (int) (xoff / 4.0);
this.xoff = xoff;
int yoff = this.getHeight() - (SIDE_LENTGH * (CELL_SIZE + 3));
yoff = (int) (yoff / 2.0);
this.yoff = yoff;
Cell[][] tmp = new Cell[SIDE_LENTGH][SIDE_LENTGH];
this.dime = new boolean[SIDE_LENTGH][SIDE_LENTGH];
for (int i = 0 ; i < SIDE_LENTGH ; i++)
{
for (int j = 0 ; j < SIDE_LENTGH ; j++)
{
if (i == SIDE_LENTGH / 2 && j == i)
{
this.player[0] = 3;
this.player[1] = 3;
}
Cell elem = new Cell(this, EMPTY, i, j);
int x = j * CELL_OFFSET + xoff;
int y = i * CELL_OFFSET + yoff;
elem.setBounds(x, y, CELL_SIZE, CELL_SIZE);
this.add(elem);
tmp[i][j] = elem;
}
}
this.cells = tmp;
this.rotation = 180;
}
/**
* Envoi la grille à l'instant t au terminal
*/
private void gridShow()
{
int range = SIDE_LENTGH;
for (int i = 0 ; i < range ; i++)
{
for (int j = 0 ; j < range ; j++)
{
Cell elem = this.cells[i][j];
System.out.print(elem.getValue()+" ");
}
System.out.println();
}
System.out.println();
}
/**
* Affiche à l'écran les éléments graphiques
*
* @param g un élément de type java.awt.Graphics
*/
@Override
public void paintComponent(java.awt.Graphics g)
{
int width = (int) this.getSize().width;
int height = (int) this.getSize().height;
java.awt.Graphics2D g2 = (java.awt.Graphics2D) g.create();
g2.setColor(Color.BLACK);
g2.fillRect(0, 0, width, height);
try
{
java.io.File path = new java.io.File("./assets/background/menu_bg.jpg");
java.awt.Image img = javax.imageio.ImageIO.read(path);
g2.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER, 0.2f));
g2.drawImage(img, 0, 0, width, height, this);
}
catch (java.io.IOException e)
{
System.err.println(e.getMessage());
}
try
{
java.io.File path = new java.io.File("./assets/background/game_bg.png");
java.awt.Image img = javax.imageio.ImageIO.read(path);
g2.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER, 1.0f));
g2.drawImage(img, this.xoff-79, this.yoff-82, 724, 725, this);
}
catch (java.io.IOException e)
{
System.err.println(e.getMessage());
}
}
/**
* Change l'apparence et la position de Assam
*/
public void render(int k)
{
this.k = k;
int row = this.player[0];
int col = this.player[1];
this.cells[row][col].setIcon(this.load("P"+k));
}
/**
* Charge une image et la rotatione
*
* @param file le nom du fichier
*/
private javax.swing.Icon load(String file) {
javax.swing.Icon img = null;
try
{
javax.swing.ImageIcon icon = new javax.swing.ImageIcon("./assets/sprites/"+file+".png");
icon = new javax.swing.ImageIcon(icon.getImage().getScaledInstance(CELL_SIZE, CELL_SIZE, java.awt.Image.SCALE_DEFAULT));
img = icon;
if (this.rotation % 360 != 0)
img = rotate(icon);
}
catch (Exception e)
{
System.err.println(e.getMessage());
}
return img;
}
/**
* Tourne une image
*
* @param icon l'image à tourner
*/
private javax.swing.ImageIcon rotate(javax.swing.ImageIcon icon)
{
int width = icon.getIconWidth();
int height = icon.getIconHeight();
int type = java.awt.image.BufferedImage.TYPE_INT_ARGB_PRE;
java.awt.image.BufferedImage img = new java.awt.image.BufferedImage(height, width, type);
java.awt.Graphics2D g2d = (java.awt.Graphics2D) img.createGraphics();
double midx = width / 2.0;
double midy = height / 2.0;
g2d.rotate((double) Math.toRadians((double) this.rotation), midx, midy);
g2d.drawImage(icon.getImage(), 0, 0, width, height, null);
g2d.dispose();
return new javax.swing.ImageIcon(img);
}
/**
* Applique le déplacement de Assam
*
* @param direction la direction
* @param movement le résultat du dé
*/
public void move(String direction, int movement)
{
int angle = 0;
int pos=0;
if (direction.equals("GAUCHE"))
angle = -90;
else if (direction.equals("DROITE"))
angle = 90;
if (this.rotation % 360 == 0 && direction.equals("GAUCHE"))
pos = 270;
else
pos = abs(this.rotation + angle);
int row = this.player[0];
int col = this.player[1];
this.cells[row][col].setIcon(null);
int s = SIDE_LENTGH*CELL_SIZE+(SIDE_LENTGH-1)* 3;
this.repaint(this.xoff, this.yoff, s, s);
if (pos % 360 == 0) //en bas
{
if (row + movement > SIDE_LENTGH - 1)
{
this.player[0] = abs(SIDE_LENTGH - movement + SIDE_LENTGH - row - 1);
pos = 180;
if (col % 2 == 0)
{
this.player[1] = col - 1;
if (col == 0)
{
this.player[0] = SIDE_LENTGH - 1;
this.player[1] = abs(SIDE_LENTGH - movement - row);
pos = 270;
}
}
else
this.player[1] = col + 1;
}
else
{
pos = 0;
this.player[0] = row + movement;
}
}
else if (pos % 360 == 90) //gauche
{
if (col - movement < 0)
{
this.player[1] = abs(movement - col - 1);
pos = 270;
if (row % 2 == 0)
{
this.player[0] = row + 1;
if (row == SIDE_LENTGH - 1)
{
this.player[0] = abs(SIDE_LENTGH - movement + col);
this.player[1] = 0;
pos = 180;
}
}
else
this.player[0] = row - 1;
}
else
{
pos = 90;
this.player[1] = col - movement;
}
}
else if (pos % 360 == 180) //en haut
{
if (row - movement < 0)
{
pos = 0;
this.player[0] = abs(movement - row - 1);
if (col % 2 == 0)
{
this.player[1] = col + 1;
if (col == SIDE_LENTGH - 1)
{
this.player[0] = 0;
this.player[1] = abs(SIDE_LENTGH - movement + row);
pos = 90;
}
}
else
this.player[1] = col - 1;
}
else
{
pos = 180;
this.player[0] = row - movement;
}
}
else if (pos % 360 == 270) //à droite
{
if (col + movement > SIDE_LENTGH - 1)
{
pos = 90;
this.player[1] = SIDE_LENTGH - movement + SIDE_LENTGH - col - 1;
if (row % 2 == 0)
{
this.player[0] = row - 1;
if (row == 0)
{
this.player[0] = abs(SIDE_LENTGH - movement - col);
this.player[1] = SIDE_LENTGH - 1;
pos = 0;
}
}
else
this.player[0] = row + 1;
}
else
{
pos = 270;
this.player[1] = col + movement;
}
}
this.rotation = pos;
this.render(this.k);
this.super_list.clear();
this.locateCarpet(this.player[0], this.player[1]);
}
/**
* Place dans la super_list les "bons" voisins de la case i,j
*
* @param i position sur l'axe x
* @param j position sur l'axe y
*/
private void locateCarpet(int i, int j)
{
for (int xoff = -1 ; xoff <= 1 ; xoff++)
{
for (int yoff = -1 ; yoff <= 1 ; yoff++)
{
int ioff = i + xoff;
int joff = j + yoff;
if (ioff > -1 && ioff < SIDE_LENTGH && joff > -1 && joff < SIDE_LENTGH
&& (ioff == i || joff == j) && !(ioff == i && joff == j))
{
Cell elem = this.cells[ioff][joff];
java.util.List<Pair> sub_list = new java.util.ArrayList<>();
if (joff > 0 && (j != joff - 1 || i != ioff) && this.isPossible(elem, this.cells[ioff][joff-1]))
{
this.cells[ioff][joff].setHighlight(false);
sub_list.add(new Pair(elem, this.cells[ioff][joff-1]));
}
if (joff + 1 < SIDE_LENTGH && (j != joff + 1 || i != ioff) && this.isPossible(elem, this.cells[ioff][joff+1]))
{
this.cells[ioff][joff].setHighlight(false);
sub_list.add(new Pair(elem, this.cells[ioff][joff+1]));
}
if (ioff > 0 && (j != joff || i != ioff - 1) && this.isPossible(elem, this.cells[ioff-1][joff]))
{
this.cells[ioff][joff].setHighlight(false);
sub_list.add(new Pair(elem, this.cells[ioff-1][joff]));
}
if (ioff + 1 < SIDE_LENTGH && (j != joff || i != ioff + 1) && this.isPossible(elem, this.cells[ioff+1][joff]))
{
this.cells[ioff][joff].setHighlight(false);
sub_list.add(new Pair(elem, this.cells[ioff+1][joff]));
}
this.super_list.add(sub_list);
}
}
}
}
/**
* On compare la cellule a et b pour vérifier la condition.
*
* @param a une cellule à comparer
* @param b une autre cellule à comparer
*/
private boolean isPossible(Cell a, Cell b)
{
int val_a = a.getValue();
int val_b = b.getValue();
Color color_a = new Color(a.getBackground().getRGB());
Color color_b = new Color(b.getBackground().getRGB());
return (!color_a.equals(color_b) || val_a != val_b || val_b == EMPTY);
}
/**
* Affiche la surbrillance.
*
* @param x position sur l'axe x
* @param y position sur l'axe y
*/
public void clean(int x, int y)
{
int posX = this.player[0];
int posY = this.player[1];
this.cl(x, y, posX, posY);
this.point = new java.awt.Point(x, y);
this.iterator(this.cells[x][y]);
}
/**
* Affiche la surbrillance.
*
* @param elem la cellule qui a le focus
*/
public void iterator(Cell elem)
{
for (int i = 0 ; i < this.super_list.size() ; i++)
{
for (int j = 0 ; j < this.super_list.get(i).size() ; j++)
{
Pair sub_item = this.super_list.get(i).get(j);
if (sub_item.node().equals(elem))
sub_item.neighbour().setHighlight(true);
}
}
}
/**
* permet de repaint seulement une partie.
*
* @param i position i dans la grille
* @param j position j dans la grille
*/
public java.awt.Rectangle getRectangle(int i, int j)
{
return new java.awt.Rectangle(this.xoff + CELL_SIZE * j, this.yoff + CELL_SIZE * i, CELL_SIZE, CELL_SIZE);
}
public void clean2(int x, int y)
{
this.cl(x, y, (int) this.point.getX(), (int) this.point.getY());
int i = this.player[0];
int j = this.player[1];
Cell elem = this.cells[i][j];
if (elem.getValue() != EMPTY && !elem.getBackground().equals(Player.colors[this.k]))
{
this.parse(i, j, elem.getBackground());
int k = getDim();
int tmp = this.game.getPlayer().getCoins() - k;
this.game.getPlayer().setCoins(-k);
this.game.getPlayer(elem.getBackground()).setCoins(k);
if (tmp < 0)
{
this.game.getPlayer(elem.getBackground()).setCoins(-tmp);
this.game.scoring();
}
}
this.game.loop();
}
/**
* Retire la surbrillance de toutes les casses autout de la casse
*
* @param x position i dans la grille de la première cellule
* @param y position j dans la grille de la première cellule
* @param xx position i dans la grille de la deuxième cellume
* @param yy position j dans la grille de la deuxième cellume
*/
public void cl(int x, int y, int xx, int yy)
{
for (int xoff = -1 ; xoff <= 1 ; xoff++)
{
for (int yoff = -1 ; yoff <= 1 ; yoff++)
{
int ioff = xx + xoff;
int joff = yy + yoff;
if (ioff > -1 && ioff < SIDE_LENTGH && joff > -1 && joff < SIDE_LENTGH
&& (ioff == xx || joff == yy) && !(ioff == xx && joff == yy))
{
this.cells[ioff][joff].removeHighlight();
}
}
}
Cell tmp = this.cells[x][y];
tmp.setValue(this.game.getTurn());
Color c = Player.colors[this.k];
Color color = new Color(c.getRed(), c.getGreen(), c.getBlue(), 180);
tmp.setBackground(color);
tmp.setColor(color);
}
/**
* Compte la dime.
* Cherhe une casse voisine dont la couleur correspond.
*
* @param posX la position X dans la grille
* @param posY la position y dans la grille
* @param color la couleur de la cellule
*/
private void parse(int posX, int posY, Color color)
{
for (int xoff = -1 ; xoff <= 1 ; xoff++)
{
for (int yoff = -1 ; yoff <= 1 ; yoff++)
{
int ioff = posX + xoff;
int joff = posY + yoff;
if (ioff > -1 && ioff < SIDE_LENTGH && joff > -1 && joff < SIDE_LENTGH
&& (ioff == posX || joff == posY) && !(ioff == posX && joff == posY)
&& !this.dime[ioff][joff] && this.cells[ioff][joff].getBackground().equals(color))
{
this.dime[ioff][joff] = true;
this.parse(ioff, joff, color);
}
}
}
}
/**
* Retourne le nombre à payer.
*/
private int getDim()
{
int res = 0;
for (int i = 0 ; i < SIDE_LENTGH ; i++)
{
for (int j = 0 ; j < SIDE_LENTGH ; j++)
{
if (this.dime[i][j])
res++;
this.dime[i][j] = false;
}
}
return res == 0 ? 1 : res;
}
/**
* Compte le nombre de tapis posés que possède un joueur.
*
* @param color la couleur du joueur
*/
public int count(Color color)
{
int cmp = 0;
for (Cell[] item : this.cells)
for (Cell sub_item : item)
if (sub_item.getBackground().equals(color))
cmp++;
return cmp;
}
}