Bonjour,
J'essaye d'implémenter un programme qui permette de jouer à un jeu de Robot Turtle, je suis en train de développer son interface graphique, mais j'éprouve des difficultés.
Je n'en suis qu'au début de l'implémentation d'interface graphique, le code est pour l'instant simplement censé afficher le plateau en fonction du nombre de joueurs choisi par un clic de bouton.
Selon le code que j'essaie de faire fonctionner, au moment de choisir le nombre de joueurs souhaitant participer au jeu en cliquant sur le bouton correspondant, je crée un nouvel objet Plateau, puis j'appelle la méthode lancerJeu2, s'il y a 2 joueurs, la méthode lancerJeu3, s'il y a 3 joueurs, et la méthode lancerJeu4, s'il y a 4 joueurs.
Cependant, lorsque je clique sur le bouton approprié, le bouton reste enfoncé et le programme marque un temps d'arrêt, je ne sais pas d'où vient ce problème.
Voici mon code pour vous aider à comprendre quelle est l'erreur qui est présente dans mon code :
classe Principale :
classe Plateau :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package fctPrincipaleMain; import java.awt.Color; /* * Les règles du jeu sont : A chaque tour, les joueurs ont 3 choix : 1)Compléter * le programme avec des cartes de leur main, puis après, ils peuvent défausser * le reste de leurs cartes et doivent piocher des cartes dans la pile * principale jusqu'à en avoir 5 dans leur main, ils mettent les cartes * défaussées dans la pile de défausse, qui deviendra la pile Principale * lorsqu'il n'y aura plus de carte à utiliser 2) Executer le programme, puis * défausse et pioche, après l'éxécution du programme, ses cartes sont ajoutées * à la pile de défausse 3) Construire un mur pour bloquer des joueurs adverses */ public class Principale { public static String[][] plateau = new String[8][8]; static Joueur joueur1 = new Joueur("bleue", "1"); static Joueur joueur2 = new Joueur("rouge", "2"); static Joueur joueur3 = new Joueur("verte", "3"); static Joueur joueur4 = new Joueur("rose", "4"); public static PositionImages positionImages = new PositionImages(); public Plateau plateauDeJeu; public Principale() { for(int k = 0; k < 8; k ++) { for(int j = 0; j < 8; j ++) { plateau[k][j]=" "; } } } public static void main(String[] args) throws InterruptedException { positionImages.nbreJoueursAffichage(); } }
classe Joueur :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package fctPrincipaleMain; import java.util.Scanner; // Cette classe va gérer les interactions entre les tortues public class Plateau extends Principale{ Scanner scanner = new Scanner(System.in); private int cpt = 0; static Partie partie = new Partie(); public Plateau() { super(); } public void lancerJeu2 () throws InterruptedException { positionImages.afficherFenetre(); partie.setJoyauxPlateau2(); partie.setMursPierrePlateau(); joueur1.setNombreDeJoueurs(2); joueur1.setNumeroJoueur("1"); joueur1.setCouleurJoueur("bleue"); joueur1.initialisation(); joueur2.setNombreDeJoueurs(2); joueur2.setNumeroJoueur("2"); joueur2.setCouleurJoueur("rouge"); joueur2.initialisation(); while (joueur1.estPartieGagnee() == false && joueur2.estPartieGagnee() == false) { if (cpt % 2 == 1) { joueur2.play(); cpt++; } else { joueur1.play(); cpt++; } } if (joueur1.estPartieGagnee() == true) { System.out.println("Le joueur bleu a gagné"); } else if (joueur2.estPartieGagnee() == true) { System.out.println("Le joueur rouge a gagné"); } } public void lancerJeu3 () throws InterruptedException { positionImages.afficherFenetre(); partie.setJoyauxPlateau3(); partie.setMursPierrePlateau(); joueur1.setNombreDeJoueurs(3); joueur1.setNumeroJoueur("1"); joueur1.setCouleurJoueur("bleue"); joueur1.initialisation(); joueur2.setNombreDeJoueurs(3); joueur2.setNumeroJoueur("2"); joueur2.setCouleurJoueur("rouge"); joueur2.initialisation(); joueur3.setNombreDeJoueurs(3); joueur3.setNumeroJoueur("3"); joueur3.setCouleurJoueur("verte"); joueur3.initialisation(); while ((joueur1.estPartieGagnee() == false && joueur2.estPartieGagnee() == false) || (joueur1.estPartieGagnee() == false && joueur3.estPartieGagnee() == false) || (joueur2.estPartieGagnee() == false && joueur3.estPartieGagnee() == false) || (joueur1.estPartieGagnee() == false && joueur2.estPartieGagnee() == false && joueur3.estPartieGagnee() == false)) { if (joueur1.estPartieGagnee() == false) { joueur1.play(); } else if (joueur1.estPartieGagnee() == true) { System.out.println("Le joueur bleu a gagné. Il ne joue plus."); } if (joueur2.estPartieGagnee() == false) { joueur2.play(); } else if (joueur2.estPartieGagnee() == true) { System.out.println("Le joueur rouge a gagné. Il ne joue plus."); } if (joueur3.estPartieGagnee() == false) { joueur3.play(); } else if (joueur3.estPartieGagnee() == true) { System.out.println("Le joueur vert a gagné. Il ne joue plus."); } } if (joueur1.estPartieGagnee() == false) { System.out.println("Les joueurs rouge et vert ont gagné la partie. Le joueur bleu a perdu."); } if (joueur2.estPartieGagnee() == false) { System.out.println("Les joueurs bleu et vert ont gagné la partie. Le joueur rouge a perdu."); } if (joueur3.estPartieGagnee() == false) { System.out.println("Les joueurs bleu et rouge ont gagné la partie. Le joueur vert a perdu."); } } public void lancerJeu4 () throws InterruptedException { positionImages.afficherFenetre(); partie.setJoyauxPlateau4(); joueur1.setNombreDeJoueurs(4); joueur1.setNumeroJoueur("1"); joueur1.setCouleurJoueur("bleue"); joueur1.initialisation(); joueur2.setNombreDeJoueurs(4); joueur2.setNumeroJoueur("2"); joueur2.setCouleurJoueur("rouge"); joueur2.initialisation(); joueur3.setNombreDeJoueurs(4); joueur3.setNumeroJoueur("3"); joueur3.setCouleurJoueur("verte"); joueur3.initialisation(); joueur4.setNombreDeJoueurs(4); joueur4.setNumeroJoueur("4"); joueur4.setCouleurJoueur("rose"); joueur4.initialisation(); while ((joueur1.estPartieGagnee() == false && joueur2.estPartieGagnee() == false) || (joueur1.estPartieGagnee() == false && joueur3.estPartieGagnee() == false) || (joueur1.estPartieGagnee() == false && joueur4.estPartieGagnee() == false) || (joueur2.estPartieGagnee() == false && joueur3.estPartieGagnee() == false) || (joueur2.estPartieGagnee() == false && joueur4.estPartieGagnee() == false) || (joueur3.estPartieGagnee() == false && joueur4.estPartieGagnee() == false) || (joueur1.estPartieGagnee() == false && joueur2.estPartieGagnee() == false && joueur3.estPartieGagnee() == false) || (joueur1.estPartieGagnee() == false && joueur2.estPartieGagnee() == false && joueur4.estPartieGagnee() == false) || (joueur1.estPartieGagnee() == false && joueur3.estPartieGagnee() == false && joueur4.estPartieGagnee() == false) || (joueur2.estPartieGagnee() == false && joueur3.estPartieGagnee() == false && joueur4.estPartieGagnee() == false) || (joueur1.estPartieGagnee() == false && joueur2.estPartieGagnee() == false && joueur3.estPartieGagnee() == false && joueur4.estPartieGagnee() == false)) { if (joueur1.estPartieGagnee() == false) { joueur1.play(); } else if (joueur1.estPartieGagnee() == true) { System.out.println("Le joueur bleu a gagné. Il ne joue plus."); } if (joueur2.estPartieGagnee() == false) { joueur2.play(); } else if (joueur2.estPartieGagnee() == true) { System.out.println("Le joueur rouge a gagné. Il ne joue plus."); } if (joueur3.estPartieGagnee() == false) { joueur3.play(); } else if (joueur3.estPartieGagnee() == true) { System.out.println("Le joueur vert a gagné. Il ne joue plus."); } if (joueur4.estPartieGagnee() == false) { joueur4.play(); } else if (joueur4.estPartieGagnee() == true) { System.out.println("Le joueur rose a gagné. Il ne joue plus."); } } if (joueur1.estPartieGagnee() == false) { System.out.println("Les joueurs bleu, rouge et vert ont gagné la partie. Le joueur rose a perdu."); } if (joueur2.estPartieGagnee() == false) { System.out.println("Les joueurs bleu, vert et rose ont gagné la partie. Le joueur rouge a perdu."); } if (joueur3.estPartieGagnee() == false) { System.out.println("Les joueurs bleu, rouge et rose ont gagné la partie. Le joueur vert a perdu."); } if (joueur4.estPartieGagnee() == false) { System.out.println("Les joueurs bleu, rouge et vert ont gagné la partie. Le joueur rose a perdu."); } } public static void rafraichirPlateau() { positionImages.rafraichirAffichage(); } }
classe Carte :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697 package fctPrincipaleMain; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Deque; import java.util.List; import java.util.Scanner; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Joueur extends Plateau { public ArrayList<Carte> programme = new ArrayList<Carte>(); ArrayList<Carte> main = new ArrayList<Carte>(); ArrayList<Carte> pileDefausse = new ArrayList<Carte>(); ArrayList<Carte> pilePrincipale = new ArrayList<Carte>(); ArrayList<Mur> mursGlace = new ArrayList<Mur>(); ArrayList<Mur> mursPierre = new ArrayList<Mur>(); Scanner scanner = new Scanner(System.in); public String couleurTortue = " "; ArrayList<Carte> cartes; public char direction; private int[] moveY = new int[] { -1, 0, 1, 0 }; private int[] moveX = new int[] { 0, 1, 0, -1 }; private Character[] directions = new Character[] { 'N', 'E', 'S', 'O' }; public int[] position = new int[2]; public ArrayDeque<String> instructions = new ArrayDeque<String>(); public boolean partieGagnee = false; public String numeroJoueur; private int nbreJoueurs; public int cpt1; public int cpt2; public int[] cpt = new int[4]; public Joueur(String couleurTortue, String numeroJoueur) { super(); couleurTortue = this.couleurTortue; numeroJoueur = this.numeroJoueur; } public void play() throws InterruptedException { System.out.println("Tour du joueur numéro " + getNumeroJoueur() + " de couleur " + getCouleur()); System.out.println("Quelle action voulez-vous jouer ?\n" + " 1 - Compléter le programme\n" + " 2 - Placer un mur\n" + " 3 - Exécuter le programme"); while (scanner.hasNextInt() == false) { scanner.nextLine(); System.out.println("Veuillez taper un nombre compris entre 1 et 3"); scanner.hasNextInt(); } int nombreChoisi = scanner.nextInt(); while (nombreChoisi != 1 && nombreChoisi != 2 && nombreChoisi != 3) { System.out.println("Quel action voulez-vous jouer " + "(nombre compris entre 1 et 3) ?"); nombreChoisi = scanner.nextInt(); scanner.nextLine(); } if (nombreChoisi == 1) { completerProgramme(); } else if (nombreChoisi == 2) { placerMur(); } else { executerProgramme(); } defaussePioche(); if (pilePrincipale.size() <= 5) { int length = pileDefausse.size(); for (int k = 0; k < length; k++) { pilePrincipale.add(pileDefausse.get(length - k - 1)); pileDefausse.remove(length - k - 1); } Collections.shuffle(pilePrincipale); } } public void initialisation() { for (int k = 0; k < 18; k++) { pilePrincipale.add(new Carte("bleu", "avancer")); } for (int k = 0; k < 8; k++) { pilePrincipale.add(new Carte("jaune", "tourner à gauche")); } for (int k = 0; k < 8; k++) { pilePrincipale.add(new Carte("violette", "tourner à droite")); } for (int k = 0; k < 3; k++) { pilePrincipale.add(new Carte("laser", "bloquer")); } Collections.shuffle(pilePrincipale); for (int k = 0; k < 5; k++) { main.add(pilePrincipale.get(pilePrincipale.size() - 1)); pilePrincipale.remove(pilePrincipale.get(pilePrincipale.size() - 1)); } for (int k = 0; k < 3; k++) { mursPierre.add(new Mur("P")); } for (int k = 0; k < 2; k++) { mursGlace.add(new Mur("G")); } setPositionDeDepart(); } /* GETTER */ public String getCouleur() { return couleurTortue; } public String getNumeroJoueur() { return numeroJoueur; } public ArrayList<Carte> getProgramme() { return programme; } public ArrayList<Carte> getMain() { return main; } public ArrayList<Carte> getPileDefausse() { return pileDefausse; } public boolean estPartieGagnee() { return partieGagnee; } /* SETTER */ public void setNumeroJoueur(String pNumero) { numeroJoueur = pNumero; } public void setCouleurJoueur(String pCouleur) { couleurTortue = pCouleur; } public void setPositionDeDepart() { if (nbreJoueurs == 2) { if (couleurTortue.equals("bleue")) { position[0] = 0; position[1] = 1; updatePlateauWithPosition("1"); } if (couleurTortue.equals("rouge")) { position[0] = 0; position[1] = 5; updatePlateauWithPosition("2"); } } else if (nbreJoueurs == 3) { if (couleurTortue.equals("bleue")) { position[0] = 0; position[1] = 0; updatePlateauWithPosition("1"); } if (couleurTortue.equals("rouge")) { position[0] = 0; position[1] = 3; updatePlateauWithPosition("2"); } if (couleurTortue.equals("verte")) { position[0] = 0; position[1] = 6; updatePlateauWithPosition("3"); } } else { if (couleurTortue.equals("bleue")) { position[0] = 0; position[1] = 0; updatePlateauWithPosition("1"); } if (couleurTortue.equals("rouge")) { position[0] = 0; position[1] = 2; updatePlateauWithPosition("2"); } if (couleurTortue.equals("verte")) { position[0] = 0; position[1] = 5; updatePlateauWithPosition("3"); } if (couleurTortue.equals("rose")) { position[0] = 0; position[1] = 7; updatePlateauWithPosition("4"); } } direction = 'S'; } public void setNombreDeJoueurs(int nbreJoueurs) { this.nbreJoueurs = nbreJoueurs; } public void completerProgramme() { if (main.size() == 0) { System.out.println("Vous ne pouvez pas compléter le programme car votre main est vide."); } else { int lengthMain = main.size(); int cptMain = 0; System.out.println("Votre main est : "); for (int k = 0; k < lengthMain; k++) { System.out.println("Carte de couleur : " + main.get(k).getCouleur() + " et d'instruction : " + main.get(k).getInstruction()); } for (int k = 0; k < lengthMain; k++) { System.out.println("Voulez-vous ajouter la " + k + "ème carte" + " à votre programme ?"); String reponse = scanner.nextLine(); while (reponse.equals("OUI") == false && reponse.equals("oui") == false && reponse.equals("Oui") == false && reponse.equals("NON") == false && reponse.equals("non") == false && reponse.equals("Non") == false) { System.out.println("Voulez-vous l'ajouter à votre programme ?"); reponse = scanner.nextLine(); } if (reponse.equals("OUI") || reponse.equals("oui") || reponse.equals("Oui")) { programme.add(main.get(k - cptMain)); instructions.add(programme.get(programme.size() - 1).getInstruction()); main.remove(k - cptMain); cptMain++; } } } } private void executerProgramme() throws InterruptedException { while (instructions.size() > 0) { programme.remove(programme.get(instructions.size() - 1)); String instruction = instructions.pollFirst(); int indexDirection = Arrays.asList(directions).indexOf(direction); System.out.println("Playing Move " + instruction); Thread.sleep(500); if (instruction.equals("tourner à gauche")) { updateDirection(indexDirection - 1); } else if (instruction.equals("tourner à droite")) { updateDirection(indexDirection + 1); } else if (instruction.equals("avancer")) { int[] newPosition = new int[2]; newPosition[0] = position[0] + moveY[indexDirection]; newPosition[1] = position[1] + moveX[indexDirection]; if (isValidPosition(newPosition)) { if (plateau[newPosition[0]][newPosition[1]].equals("G") || plateau[newPosition[0]][newPosition[1]].equals("P")) { updateDirection(indexDirection + 2); } else if (plateau[newPosition[0]][newPosition[1]].equals("1")) { plateau[newPosition[0]][newPosition[1]] = " "; joueur1.setPositionDeDepart(); plateau[position[0]][position[1]] = " "; setPositionDeDepart(); } else if (plateau[newPosition[0]][newPosition[1]].equals("2")) { plateau[newPosition[0]][newPosition[1]] = " "; joueur2.setPositionDeDepart(); plateau[position[0]][position[1]] = " "; setPositionDeDepart(); } else if (plateau[newPosition[0]][newPosition[1]].equals("3")) { plateau[newPosition[0]][newPosition[1]] = " "; joueur3.setPositionDeDepart(); plateau[position[0]][position[1]] = " "; setPositionDeDepart(); } else if (plateau[newPosition[0]][newPosition[1]].equals("4")) { plateau[newPosition[0]][newPosition[1]] = " "; joueur4.setPositionDeDepart(); plateau[position[0]][position[1]] = " "; setPositionDeDepart(); } else if (plateau[newPosition[0]][newPosition[1]].equals("JBleu") || plateau[newPosition[0]][newPosition[1]].equals("JRouge") || plateau[newPosition[0]][newPosition[1]].equals("JVert") || plateau[newPosition[0]][newPosition[1]].equals("JRose")) { partieGagnee = true; } else { updatePosition(indexDirection); } } else { updateDirection(indexDirection + 2); } } // CARTE LASER : else { if (indexDirection == 0) { for (int k = position[0]; k > 8; k--) { if (plateau[k][position[1]].equals("G")) { plateau[k][position[1]] = " "; } else if (plateau[k][position[1]].equals("1") && nbreJoueurs == 2) { joueur1.updateDirection(indexDirection + 2); } else if (plateau[k][position[1]].equals("2") && nbreJoueurs == 2) { joueur2.updateDirection(indexDirection + 2); } else if (plateau[k][position[1]].equals("1") && nbreJoueurs != 2) { plateau[k][position[1]] = " "; joueur1.setPositionDeDepart(); } else if (plateau[k][position[1]].equals("2") && nbreJoueurs != 2) { plateau[k][position[1]] = " "; joueur2.setPositionDeDepart(); } else if (plateau[k][position[1]].equals("3")) { plateau[k][position[1]] = " "; joueur3.setPositionDeDepart(); } else if (plateau[k][position[1]].equals("4")) { plateau[k][position[1]] = " "; joueur4.setPositionDeDepart(); } if (plateau[k][position[1]].equals("JBleu") || plateau[k][position[1]].equals("JRouge") || plateau[k][position[1]].equals("JVert") || plateau[k][position[1]].equals("JRose")) { if (nbreJoueurs == 2) { updateDirection(indexDirection + 2); } else { plateau[k][position[1]] = " "; setPositionDeDepart(); } } } } else if (indexDirection == 1) { for (int k = position[1]; k < 8; k++) { if (plateau[position[0]][k] == "G") { plateau[position[0]][k] = " "; } else if (plateau[position[0]][k] == "1" && nbreJoueurs == 2) { joueur1.updateDirection(indexDirection + 2); } else if (plateau[k][position[1]] == "2" && nbreJoueurs == 2) { joueur2.updateDirection(indexDirection + 2); } else if (plateau[position[0]][k] == "1" && nbreJoueurs != 2) { joueur1.setPositionDeDepart(); plateau[position[0]][k] = " "; } else if (plateau[position[0]][k] == "2" && nbreJoueurs != 2) { plateau[position[0]][k] = " "; joueur2.setPositionDeDepart(); } else if (plateau[position[0]][k] == "3") { plateau[position[0]][k] = " "; joueur3.setPositionDeDepart(); } else if (plateau[position[0]][k] == "4") { plateau[position[0]][k] = " "; joueur4.setPositionDeDepart(); } if (plateau[position[0]][k].equals("JBleu") || plateau[position[0]][k].equals("JVert") || plateau[position[0]][k].equals("JRouge") || plateau[position[0]][k].equals("JRose")) { if (nbreJoueurs == 2) { updateDirection(indexDirection + 2); } else { plateau[position[0]][k] = " "; setPositionDeDepart(); } } } } else if (indexDirection == 2) { for (int k = position[0]; k < 8; k++) { if (plateau[k][position[1]] == "G") { plateau[k][position[1]] = " "; } else if (plateau[k][position[1]].equals("1") && nbreJoueurs == 2) { joueur1.updateDirection(indexDirection + 2); } else if (plateau[k][position[1]].equals("2") && nbreJoueurs == 2) { joueur2.updateDirection(indexDirection + 2); } else if (plateau[k][position[1]].equals("1") && nbreJoueurs != 2) { plateau[k][position[1]] = " "; joueur1.setPositionDeDepart(); } else if (plateau[k][position[1]].equals("2") && nbreJoueurs != 2) { plateau[k][position[1]] = " "; joueur2.setPositionDeDepart(); } else if (plateau[k][position[1]].equals("3")) { plateau[k][position[1]] = " "; joueur3.setPositionDeDepart(); } else if (plateau[k][position[1]].equals("4")) { plateau[k][position[1]] = " "; joueur4.setPositionDeDepart(); } if (plateau[k][position[1]] == "JBleu" || plateau[k][position[1]] == "JVert" || plateau[k][position[1]] == "JRouge" || plateau[k][position[1]] == "JRose") { if (nbreJoueurs == 2) { updateDirection(indexDirection + 2); } else { setPositionDeDepart(); plateau[k][position[1]] = " "; } } } } else { for (int k = position[1]; k > 0; k--) { if (plateau[position[0]][k] == "G") { plateau[position[0]][k] = " "; } else if (plateau[position[0]][k] == "1" && nbreJoueurs == 2) { joueur1.updateDirection(indexDirection + 2); } else if (plateau[k][position[1]] == "2" && nbreJoueurs == 2) { joueur2.updateDirection(indexDirection + 2); } else if (plateau[position[0]][k] == "1" && nbreJoueurs == 2) { joueur1.setPositionDeDepart(); plateau[position[0]][k] = " "; } else if (plateau[position[0]][k] == "2" && nbreJoueurs == 2) { joueur2.setPositionDeDepart(); plateau[position[0]][k] = " "; } else if (plateau[position[0]][k] == "3") { joueur3.setPositionDeDepart(); plateau[position[0]][k] = " "; } else if (plateau[position[0]][k] == "4") { joueur4.setPositionDeDepart(); plateau[position[0]][k] = " "; } if (plateau[position[0]][k] == "JBleu" || plateau[position[0]][k] == "JVert" || plateau[position[0]][k] == "JRouge" || plateau[position[0]][k] == "JRose") { if (nbreJoueurs == 2) { updateDirection(indexDirection + 2); } else { setPositionDeDepart(); plateau[position[0]][k] = " "; } } } } } // FIN DE LA CARTE LASER updatePlateauWithPosition(numeroJoueur); displayTableau(); } } private void updateDirection(int newIndexDirection) { int validIndex = (newIndexDirection + 4) % 4; direction = directions[validIndex]; Plateau.rafraichirPlateau(); } private void updatePosition(int indexDirection) { int[] newPosition = new int[2]; plateau[position[0]][position[1]] = " "; newPosition[0] = position[0] + moveY[indexDirection]; newPosition[1] = position[1] + moveX[indexDirection]; if (isValidPosition(newPosition)) { position = newPosition; } else { System.out.println("/!\\ This is an Invalid Move !"); } } private boolean isValidPosition(int[] newPosition) { return newPosition[0] >= 0 && newPosition[0] < plateau.length && newPosition[1] >= 0 && newPosition[1] < plateau[0].length; } private void updatePlateauWithPosition(String numeroJoueur) { viderTableau(); plateau[position[0]][position[1]] = numeroJoueur; } private static void viderTableau() { for (int i = 0; i < plateau.length; i++) { for (int j = 0; j < plateau[0].length; j++) { if (plateau[i][j] == null) { plateau[i][j] = " "; } } } Plateau.rafraichirPlateau(); } public void displayTableau() { // TODO Auto-generated method stub for (int i = 0; i < plateau.length; i++) { System.out.print("|"); for (int j = 0; j < plateau[i].length; j++) { System.out.print(plateau[i][j]); } System.out.println("|"); } System.out.println("Position x=" + position[1] + " y =" + position[0] + "\n direction :" + direction); } public void placerMur() { if (mursPierre.size() > 0 && mursGlace.size() > 0) { System.out.println( "Quel type de mur souhaitez-vous ajouter ?\n" + "G - mur de glace\n" + "P - mur de pierre"); String typeMur = scanner.nextLine(); while (typeMur.equals("G") == false && typeMur.equals("P") == false) { System.out.println("Quel type de mur souhaitez-vous ajouter(P/G) ?"); typeMur = scanner.nextLine(); } System.out.println("Quelle est l'ordonnée où vous souhaitez placer " + "votre mur ?"); System.out.println("Ordonnée(=num de ligne) : "); while (scanner.hasNextInt() == false) { scanner.nextLine(); System.out.println("Veuillez choisir un nombre compris entre 0 et 7"); scanner.hasNextInt(); } int ordMur = scanner.nextInt(); scanner.nextLine(); System.out.println("Quelle est l'abscisse où vous souhaitez placer " + "votre mur ?"); System.out.println("Abscisse(=num de colonne) : "); while (scanner.hasNextInt() == false) { scanner.nextLine(); System.out.println("Veuillez choisir un nombre compris entre 0 et 7"); scanner.hasNextInt(); } int absMur = scanner.nextInt(); scanner.nextLine(); int[] nPosition = new int[4]; nPosition[0] = ordMur; nPosition[1] = absMur; while (isValidPosition(nPosition) == false || plateau[ordMur][absMur] != " " || vaBloquer(ordMur, absMur)) { cpt1 = 0; cpt2 = 0; for (int k = 0; k < 4; k++) { cpt[k] = 0; } System.out.println("Veuillez donnez une ordonnée comprise entre 0 et 7"); System.out.println( "La case que vous souhaitez occuper ne doit pas être déjà utilisée.\\nVeillez aussi à ne pas bloquer un joyau ou un joueur"); ordMur = scanner.nextInt(); scanner.nextLine(); System.out.println("Veuillez donnez une abscisse comprise entre 0 et 7"); System.out.println( "La case que vous souhaitez occuper ne doit pas être déjà utilisée.\\nVeillez aussi à ne pas bloquer un joyau ou un joueur"); absMur = scanner.nextInt(); scanner.nextLine(); } if (typeMur.equals("G")) { // On place le mur plateau[ordMur][absMur] = "G"; mursGlace.remove(mursGlace.size() - 1); } if (typeMur.equals("P")) { plateau[ordMur][absMur] = "P"; mursPierre.remove(mursPierre.size() - 1); } } else if (mursGlace.size() == 0 && mursPierre.size() > 0) { System.out.println("Il ne reste plus de murs de glace, donc vous ne pouvez placer que des murs de pierre."); System.out.println("Quelle est l'ordonnée où vous souhaitez placer " + "votre mur de pierre?"); System.out.println("Ordonnée(=num de ligne) : "); while (scanner.hasNextInt() == false) { scanner.nextLine(); System.out.println("Veuillez choisir un nombre compris entre 0 et 7"); scanner.hasNextInt(); } int ordMur = scanner.nextInt(); scanner.nextLine(); System.out.println("Quelle est l'abscisse où vous souhaitez placer " + "votre mur de pierre?"); System.out.println("Abscisse(=num de colonne) : "); while (scanner.hasNextInt() == false) { scanner.nextLine(); System.out.println("Veuillez choisir un nombre compris entre 0 et 7"); scanner.hasNextInt(); } int absMur = scanner.nextInt(); scanner.nextLine(); int[] nPosition = new int[4]; nPosition[0] = ordMur; nPosition[1] = absMur; while (isValidPosition(nPosition) == false || plateau[ordMur][absMur] != " " || vaBloquer(ordMur, absMur)) { cpt1 = 0; cpt2 = 0; for (int k = 0; k < 4; k++) { cpt[k] = 0; } System.out.println("Veuillez donnez une ordonnée comprise entre 0 et 7"); System.out.println( "La case que vous souhaitez occuper ne doit pas être déjà utilisée.\nVeillez aussi à ne pas bloquer un joyau ou un joueur"); ordMur = scanner.nextInt(); scanner.nextLine(); System.out.println("Veuillez donnez une abscisse comprise entre 0 et 7"); System.out.println( "La case que vous souhaitez occuper ne doit pas être déjà utilisée.\nVeillez aussi à ne pas bloquer un joyau ou un joueur"); absMur = scanner.nextInt(); scanner.nextLine(); } plateau[ordMur][absMur] = "P"; mursPierre.remove(mursPierre.size() - 1); } else if (mursGlace.size() > 0 && mursPierre.size() == 0) { System.out.println("Il ne reste plus de murs de pierre, donc vous ne pouvez placer que des murs de glace."); System.out.println("Quelle est l'ordonnée où vous souhaitez placer " + "votre mur de glace ?"); System.out.println("Ordonnée(=num de ligne) : "); while (scanner.hasNextInt() == false) { scanner.nextLine(); System.out.println("Veuillez choisir un nombre compris entre 0 et 7"); scanner.hasNextInt(); } int ordMur = scanner.nextInt(); scanner.nextLine(); System.out.println("Quelle est l'abscisse où vous souhaitez placer " + "votre mur de glace?"); System.out.println("Abscisse(=num de colonne) : "); while (scanner.hasNextInt() == false) { scanner.nextLine(); System.out.println("Veuillez choisir un nombre compris entre 0 et 7"); scanner.hasNextInt(); } int absMur = scanner.nextInt(); scanner.nextLine(); int[] nPosition = new int[4]; nPosition[0] = ordMur; nPosition[1] = absMur; while (isValidPosition(nPosition) == false || plateau[ordMur][absMur] != " " || vaBloquer(ordMur, absMur)) { cpt1 = 0; cpt2 = 0; for (int k = 0; k < 4; k++) { cpt[k] = 0; } System.out.println("Veuillez donnez une ordonnée comprise entre 0 et 7"); System.out.println( "La case que vous souhaitez occuper ne doit pas être déjà utilisée./nVeillez aussi à ne pas bloquer un joyau ou un joueur"); ordMur = scanner.nextInt(); scanner.nextLine(); System.out.println("Veuillez donnez une abscisse comprise entre 0 et 7"); System.out.println( "La case que vous souhaitez occuper ne doit pas être déjà utilisée.\nVeillez aussi à ne pas bloquer un joyau ou un joueur"); absMur = scanner.nextInt(); scanner.nextLine(); } plateau[ordMur][absMur] = "G"; mursGlace.remove(mursGlace.size() - 1); } else { System.out.println("Il ne reste plus de murs de glace ni de pierre."); } } public boolean vaBloquer(int abs, int ord) { for (int k = 0; k < 4; k++) { int[] newPosition = new int[2]; newPosition[0] = abs + moveY[k]; newPosition[1] = ord + moveX[k]; if (isValidPosition(newPosition)) { if (plateau[newPosition[0]][newPosition[1]].equals("1") || plateau[newPosition[0]][newPosition[1]].equals("2") || plateau[newPosition[0]][newPosition[1]].equals("3") || plateau[newPosition[0]][newPosition[1]].equals("4") || plateau[newPosition[0]][newPosition[1]].equals("JBleu") || plateau[newPosition[0]][newPosition[1]].equals("JVert") || plateau[newPosition[0]][newPosition[1]].equals("JRose") || plateau[newPosition[0]][newPosition[1]].equals("JRouge")) { cpt1++; for (int j = 0; j < 4; j++) { int[] newPosition1 = new int[2]; newPosition1[0] = newPosition[0] + moveY[j]; newPosition1[1] = newPosition[1] + moveX[j]; if (isValidPosition(newPosition1)) { if (plateau[newPosition1[0]][newPosition1[1]].equals(" ")) { cpt[k]++; } } } } } } for (int k = 0; k < 4; k++) { if (cpt[k] != 0) { cpt2++; } } if (cpt1 == cpt2) { return false; } else { return true; } } public void defaussePioche() { System.out.println("Voulez-vous défausser votre main et piocher des cartes ?"); String reponse = scanner.nextLine(); while (reponse.equals("OUI") == false && reponse.equals("oui") == false && reponse.equals("Oui") == false && reponse.equals("NON") == false && reponse.equals("non") == false && reponse.equals("Non") == false) { System.out.println("Voulez-vous défausser votre main et piocher des cartes(oui/non) ?"); reponse = scanner.nextLine(); } if (reponse.equals("OUI") || reponse.equals("oui") || reponse.equals("Oui")) { int lengthMain = main.size(); if (lengthMain > 0) { for (int k = 0; k < lengthMain; k++) { pileDefausse.add(main.get(lengthMain - k - 1)); main.remove(lengthMain - k - 1); } } for (int k = 0; k < 5; k++) { main.add(pilePrincipale.get(pilePrincipale.size() - 1)); pilePrincipale.remove(pilePrincipale.get(pilePrincipale.size() - 1)); } } else { } } }
classe Mur :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package fctPrincipaleMain; import java.util.Scanner; //Enlever l'instruction qui ne sert à rien public class Carte { public String couleur; public String instruction; Scanner scanner = new Scanner(System.in); public Carte(String couleur, String instruction) { this.couleur = couleur; this.instruction = instruction; } public String getInstruction() { return instruction; } public String getCouleur() { return couleur; } }
classe Partie :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package fctPrincipaleMain; public class Mur { String typeMur; public Mur(String typeMur) { typeMur = this.typeMur; } public String getTypeDeMur() { return typeMur; } }
classe PositionImages :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package fctPrincipaleMain; public class Partie extends Plateau { private int nbreJoueurs; public Partie() { super(); // this.nbreJoueurs = nbreJoueurs; } public void setJoyauxPlateau2() { int ordJoyau2 = 7; int absJoyau2 = 3; plateau[ordJoyau2][absJoyau2] = "JVert"; } public void setJoyauxPlateau3() { int ordJoyau3R = 7; int absJoyau3R = 0; plateau[ordJoyau3R][absJoyau3R] = "JRose"; int ordJoyau3V = 7; int absJoyau3V = 3; plateau[ordJoyau3V][absJoyau3V] = "JVert"; int ordJoyau3B = 7; int absJoyau3B = 6; plateau[ordJoyau3B][absJoyau3B] = "JBleu"; } public void setJoyauxPlateau4() { int ordJoyau3R = 7; int absJoyau3R = 1; plateau[ordJoyau3R][absJoyau3R] = "JRose"; int ordJoyau3B = 7; int absJoyau3B = 6; plateau[ordJoyau3B][absJoyau3B] = "JBleu"; } public void setMursPierrePlateau() { for (int k = 0; k < 8; k++) { plateau[k][7] = "P"; } } }
En espérant avoir été clair dans la présentation de mon problème,
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package fctPrincipaleMain; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.AffineTransform; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class PositionImages extends Principale implements ActionListener { public Image imageDeFond = new ImageIcon("C:/Users/enriq/eclipse-workspace/projetJava/imageDébut/background.jpeg") .getImage(); public Image murGlace = new ImageIcon("C:/Users/enriq/eclipse-workspace/projetJava/imageDébut/ICE.png").getImage(); public Image murPierre = new ImageIcon("C:/Users/enriq/eclipse-workspace/projetJava/imageDébut/WALL.png").getImage() .getScaledInstance(50, 50, Image.SCALE_DEFAULT); public Image robot1 = new ImageIcon("C:/Users/enriq/eclipse-workspace/projetJava/imageDébut/TortueBleue.jpg") .getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT); public Image robot2 = new ImageIcon("C:/Users/enriq/eclipse-workspace/projetJava/imageDébut/TortueRouge.png") .getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT); public Image robot3 = new ImageIcon("C:/Users/enriq/eclipse-workspace/projetJava/imageDébut/TortueVerte.png") .getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT); public Image robot4 = new ImageIcon("C:/Users/enriq/eclipse-workspace/projetJava/imageDébut/TortueRose.png") .getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT); public Image rubis = new ImageIcon("C:/Users/enriq/eclipse-workspace/projetJava/imageDébut/RUBY.png").getImage() .getScaledInstance(50, 50, Image.SCALE_DEFAULT); private JFrame fenetre; private Panneau paneau; private JPanel panelNbreJoueurs; private JLabel choixNbreJoueurs; private JButton deuxJoueurs; private JButton troisJoueurs; private JButton quatreJoueurs; public JButton completerProgramme; public JButton executerProgramme; public JButton placerMur; public PositionImages() { // super(); fenetre = new JFrame(); fenetre.setSize(400, 400); fenetre.setTitle("ROBOT TURTLE"); fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); fenetre.setVisible(true); } public void nbreJoueursAffichage() { panelNbreJoueurs = new JPanel(); choixNbreJoueurs = new JLabel("Combien de joueurs veulent jouer ?"); panelNbreJoueurs.add(choixNbreJoueurs); deuxJoueurs = new JButton("2"); panelNbreJoueurs.add(deuxJoueurs); deuxJoueurs.addActionListener(this); troisJoueurs = new JButton("3"); panelNbreJoueurs.add(troisJoueurs); troisJoueurs.addActionListener(this); quatreJoueurs = new JButton("4"); panelNbreJoueurs.add(quatreJoueurs); quatreJoueurs.addActionListener(this); fenetre.add(panelNbreJoueurs); } public void affichagePlateau() { paneau = new Panneau(); fenetre.add(paneau); paneau.setBackground(Color.WHITE); fenetre.setVisible(true); } public void rafraichirAffichage() { paneau.repaint(); } public void afficherFenetre() { fenetre.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub Object source = e.getSource(); if (source == deuxJoueurs) { fenetre.removeAll(); Plateau plateau = new Plateau(); try { affichagePlateau(); plateau.lancerJeu2(); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } if (source == troisJoueurs) { fenetre.removeAll(); Plateau plateau = new Plateau(); try { affichagePlateau(); plateau.lancerJeu3(); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } if (source == quatreJoueurs) { fenetre.removeAll(); Plateau plateau = new Plateau(); try { affichagePlateau(); plateau.lancerJeu4(); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } class Panneau extends JPanel { public static final int x = 30, y = 20, h = 50; /*** A savoir, en java, les rotations se font selon le sens horaire ***/ public void paintComponent(Graphics g) { super.paintComponent(g); g.drawLine(x, y, x + 8 * h, y); g.drawLine(x + 8 * h, y, x + 8 * h, y + 8 * h); g.drawLine(x + 8 * h, y + 8 * h, x, y + 8 * h); g.drawLine(x, y + 8 * h, x, y); // cases for (int k = 1; k < 8; k++) { g.drawLine(x + k * h, y, x + k * h, y + 8 * h); g.drawLine(x, y + k * h, x + 8 * h, y + k * h); } for (int k = 0; k < 8; k++) { for (int i = 0; i < 8; i++) { if (plateau[k][i].equals("P")) { g.drawImage(murPierre, x + i * h, y + k * h, 50, 50, this); } if (plateau[k][i].equals("G")) { g.drawImage(murGlace, x + i * h, y + k * h, 50, 50, this); } if (plateau[k][i].equals("1")) { if (joueur1.direction == 'S') { rotateImage(g, robot1, x + i * h, y + k * h, -90); g.drawRect(x + i * h, y + k * h, robot1.getWidth(this), robot1.getHeight(this)); } else if (joueur1.direction == 'E') { rotateImage(g, robot1, x + i * h, y + k * h, 180); g.drawRect(x + i * h, y + k * h, robot1.getWidth(this), robot1.getHeight(this)); } else if (joueur1.direction == 'O') { rotateImage(g, robot1, x + i * h, y + k * h, 0); g.drawRect(x + i * h, y + k * h, robot1.getWidth(this), robot1.getHeight(this)); } else if (joueur1.direction == 'N') { rotateImage(g, robot1, x + i * h, y + k * h, 90); g.drawRect(x + i * h, y + k * h, robot1.getWidth(this), robot1.getHeight(this)); } } else if (plateau[k][i].equals("2")) { if (joueur2.direction == 'S') { rotateImage(g, robot2, x + i * h, y + k * h, -90); g.drawRect(x + i * h, y + k * h, robot2.getWidth(this), robot2.getHeight(this)); } else if (joueur2.direction == 'E') { rotateImage(g, robot2, x + i * h, y + k * h, 180); g.drawRect(x + i * h, y + k * h, robot2.getWidth(this), robot2.getHeight(this)); } else if (joueur2.direction == 'O') { rotateImage(g, robot2, x + i * h, y + k * h, 0); g.drawRect(x + i * h, y + k * h, robot2.getWidth(this), robot2.getHeight(this)); } else if (joueur2.direction == 'N') { rotateImage(g, robot2, x + i * h, y + k * h, 90); g.drawRect(x + i * h, y + k * h, robot2.getWidth(this), robot2.getHeight(this)); } } else if (plateau[k][i].equals("3")) { if (joueur3.direction == 'S') { rotateImage(g, robot3, x + i * h, y + k * h, -90); g.drawRect(x + i * h, y + k * h, robot3.getWidth(this), robot3.getHeight(this)); } else if (joueur3.direction == 'E') { rotateImage(g, robot3, x + i * h, y + k * h, 180); g.drawRect(x + i * h, y + k * h, robot3.getWidth(this), robot3.getHeight(this)); } else if (joueur3.direction == 'O') { rotateImage(g, robot3, x + i * h, y + k * h, 0); g.drawRect(x + i * h, y + k * h, robot3.getWidth(this), robot3.getHeight(this)); } else if (joueur3.direction == 'N') { rotateImage(g, robot3, x + i * h, y + k * h, 90); g.drawRect(x + i * h, y + k * h, robot3.getWidth(this), robot3.getHeight(this)); } } else if (plateau[k][i].equals("4")) { if (joueur4.direction == 'S') { rotateImage(g, robot4, x + i * h, y + k * h, -90); g.drawRect(x + i * h, y + k * h, robot4.getWidth(this), robot4.getHeight(this)); } else if (joueur4.direction == 'E') { rotateImage(g, robot4, x + i * h, y + k * h, 180); g.drawRect(x + i * h, y + k * h, robot4.getWidth(this), robot4.getHeight(this)); } else if (joueur4.direction == 'O') { rotateImage(g, robot4, x + i * h, y + k * h, 0); g.drawRect(x + i * h, y + k * h, robot4.getWidth(this), robot4.getHeight(this)); } else if (joueur4.direction == 'N') { rotateImage(g, robot4, x + i * h, y + k * h, 90); g.drawRect(x + i * h, y + k * h, robot4.getWidth(this), robot4.getHeight(this)); } } if (plateau[k][i].equals("JRose") || plateau[k][i].equals("JBleu") || plateau[k][i].equals("JRouge") || plateau[k][i].equals("JVert")) { g.drawImage(rubis, x + i * h, y + k * h, 50, 50, this); } } } } private void rotateImage(Graphics g, Image image, int x, int y, double angle) { Graphics2D g2d = (Graphics2D) g.create(); g2d.transform(AffineTransform.getRotateInstance(Math.toRadians(angle), x + image.getWidth(this) / 2, y + image.getHeight(this) / 2)); g2d.drawImage(image, x, y, this); g2d.dispose(); } }
En vous remerciant d'avance pour votre réponse,
Bien cordialement.
Partager