IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

Prise en compte des clics sur les boutons


Sujet :

AWT/Swing Java

  1. #1
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2019
    Messages
    61
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 25
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2019
    Messages : 61
    Points : 44
    Points
    44
    Par défaut Prise en compte des clics sur les boutons
    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 :

    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 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
    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 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
    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 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
    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 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
    package fctPrincipaleMain;
     
    public class Mur {
    	String typeMur;
     
    	public Mur(String typeMur)  {
    		typeMur = this.typeMur;
    	}
     
    	public String getTypeDeMur() {
    		return typeMur;
    	}
     
    }
    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
    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";
    		}
    	}
     
    }
    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
    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 espérant avoir été clair dans la présentation de mon problème,

    En vous remerciant d'avance pour votre réponse,

    Bien cordialement.

  2. #2
    Modérateur
    Avatar de joel.drigo
    Homme Profil pro
    Ingénieur R&D - Développeur Java
    Inscrit en
    Septembre 2009
    Messages
    12 430
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 55
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur R&D - Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2009
    Messages : 12 430
    Points : 29 131
    Points
    29 131
    Billets dans le blog
    2
    Par défaut
    Salut,

    Swing utilise un thread unique (appelé Event Dispatch Thread) qui effectue tous les traitements d'affichage et d’interaction (traitement des événements, entre autre des ActionListener). Un thread ne pouvant faire qu'une chose à la fois, tout ce que tu y fais s'exécute et le résultat n'est visible qu'à la fin. Comme tu lances des boucles lors du clic sur le bouton dans les méthodes "lancerJeu", toute l'UI est gelée jusqu'à la fin de ces boucles.

    Tu pourrais lancer les boucles dans un thread et à la fin mettre à jour l'UI avec SwingUtilities.invokeLater(). Mais tu ne pourras pas voir de résultats intermédiaires (l'avancement des tortues par exemple).

    Pour résoudre le problème de manière général, il faut lancer ces boucles via un SwingWorker. Dans sa méthode doInBackground, tu fais les boucles. Elles produisent des résultats que tu publies via méthode publish(). Dans la méthode process() tu traites ces résultats pour mettre à jour l'affichage. Attention aux accès concurrentiels, en particulier sur les listes. Les méthodes doInBackground() et process() s'exécutent dans des threads différents : il faut éviter de modifier et de lire une même liste dans les deux threads en même temps. On peut utiliser synchronized pour empêcher ces accès concurrentiels, mais on risque d'avoir des gels temporaires de l'UI : il faut donc limiter le temps d'exécution de ces traitements. On peut aussi procéder par copie, de manière à ce que ce qu'il se passe dans process ne manipule jamais ce qui est manipulé dans doInBackground.
    A priori, globalement, tu devrais pouvoir t'en sortir en tentant d'abord de simplement lancer tes boucles actuelles dans un thread. Je n'ai pas regardé tous le détail du programme, mais la partie affichage est la seule à priori devant être faite dans l'EDT. Les repaint() (donc les appels à rafraîchirPlateau() ) devraient suffire à traiter ça. Tu risques en revanche que tes boucles prennent tout le cpu et n'en laissent pas pour l'EDT. Essaye et regarde ce que ça donne pour commencer. Une solution simple peut être d'introduire des petites pauses dans les boucles (Thread.sleep()). Si ça ne fonctionne pas, utilise un SwingWorker.

    Mais ça c'est l'optique de faire l'interface de saisie toujours en console, ce qui n'est pas très adapté à une interface graphique en fenêtre. Du coup tu vas peut-être vouloir utiliser aussi des boutons et des champs de saisie à la place. Dans ce cas, toute la structure du programme est à repenser. Il ne faut plus penser en boucle, mais en événements successifs, en changement d'un joueur au suivant.

  3. #3
    Modérateur
    Avatar de joel.drigo
    Homme Profil pro
    Ingénieur R&D - Développeur Java
    Inscrit en
    Septembre 2009
    Messages
    12 430
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 55
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur R&D - Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2009
    Messages : 12 430
    Points : 29 131
    Points
    29 131
    Billets dans le blog
    2
    Par défaut
    Voici un petit exemple vite fait de comment gérer la succession de tour de chaque 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
    package jeu;
     
    import java.awt.CardLayout;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
     
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
     
    public class ExempleJeu extends JFrame {
     
    	private static final String CHOIX_JOUEUR = "choixjoueur";
    	private static final String PLATEAU = "plateau";
    	private static final String FINJEU = "finjeu";
     
    	private JPanel mainPanel;
    	private CardLayout cardLayout;
    	private Game game;
    	private JeuPanel jeupanel;
    	private FinJeuPanel finjeupanel;
     
    	public ExempleJeu() {
    		super("Exemple Jeu");
     
    		game=new Game();
    		setSize(400,400);
    		setResizable(false);
    		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    		setLocationRelativeTo(null);
    		addWindowListener(new WindowAdapter() {
    			@Override
    			public void windowClosing(WindowEvent e) {
    				if ( JOptionPane.showConfirmDialog(ExempleJeu.this, "Voulez-vous quitter le jeu ?", "Exemple jeu", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION ) {
    					dispose();
    				}
    			}
    		});
     
    		mainPanel = new JPanel();
    		cardLayout = new CardLayout();
    		mainPanel.setLayout(cardLayout);
     
    		mainPanel.add(new ChoixJoueurPanel(this), CHOIX_JOUEUR);
    		jeupanel = new JeuPanel(this,game);
    		mainPanel.add(jeupanel, PLATEAU);
    		finjeupanel = new FinJeuPanel(this);
    		mainPanel.add(finjeupanel, FINJEU);
     
    		add(mainPanel);
     
    		init();
     
    	}
     
    	public void init() {
    		game.reset();
    		cardLayout.show(mainPanel, CHOIX_JOUEUR);
    	}
     
    	public Game getGame() {
    		return game;
    	}
     
    	public void lancerJeu(int nbjoueurs) {
    		game.setNombreJoueurs(nbjoueurs);
    		jeupanel.update();
    		cardLayout.show(mainPanel, PLATEAU);
    	}
     
    	public void finJeu() {
    		game.setArriveeDernierJoueur();
    		finjeupanel.update();
    		cardLayout.show(mainPanel, FINJEU);
    	}
     
    	public void rafraichir() {
    		jeupanel.repaint();
    	}
     
    	public static void main(String[] args) {
     
    		setSystemLookAndFeel();
    		new ExempleJeu().setVisible(true);
     
    	}
     
    	public static void setSystemLookAndFeel() {
    		try {
    			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
    				| UnsupportedLookAndFeelException e) {
    		}
    	}
     
    }
    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
    package jeu;
     
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
     
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
    public class ChoixJoueurPanel extends JPanel {
     
    	public ChoixJoueurPanel(ExempleJeu exempleJeu) {
    		super(new GridBagLayout());
     
    		GridBagConstraints gbc = new GridBagConstraints();
     
    		JLabel label = new JLabel("Choisissez le nombre de joueurs", JLabel.CENTER);
    		label.setFont(getFont().deriveFont(18f));
     
    		gbc.gridx = 0;
    		gbc.gridy = 0;
    		gbc.gridwidth = 3;
    		gbc.anchor = GridBagConstraints.CENTER;
    		gbc.fill = GridBagConstraints.HORIZONTAL;
    		add(label, gbc);
     
    		gbc.gridx = GridBagConstraints.RELATIVE;
    		gbc.gridy = 1;
    		gbc.gridwidth = 1;
    		gbc.weightx = 1;
    		gbc.insets = new Insets(10, 10, 10, 10);
    		for(int i=Game.NOMBRE_JOUEUR_MIN; i<=Game.NOMBRE_JOUEUR_MAX; i++) {
    			JButton button = new JButton(String.valueOf(i));
    			final int nbjoueurs=i;
    			button.addActionListener(e-> exempleJeu.lancerJeu(nbjoueurs));
    			add(button, gbc);
    		}
     
    	}
     
    }
    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
    package jeu;
     
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.util.Arrays;
    import java.util.Comparator;
     
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
    public class FinJeuPanel extends JPanel {
     
    	private ExempleJeu exempleJeu;
     
    	public FinJeuPanel(ExempleJeu exempleJeu) {
    		super(new GridBagLayout());
    		this.exempleJeu = exempleJeu;
    	}
     
    	public void update() {
    		removeAll();
    		Joueur[] joueurs = exempleJeu.getGame().getJoueurs();
    		Arrays.sort(joueurs, Comparator.comparing(Joueur::getArrivee));
     
    		GridBagConstraints gbc = new GridBagConstraints();
    		gbc.gridx = 0;
    		gbc.gridy = GridBagConstraints.RELATIVE;
    		gbc.anchor = GridBagConstraints.CENTER;
    		gbc.insets = new Insets(5,0,0,0);
     
    		add( new JLabel("Arrivées:"), gbc);
     
    		for(int i=0; i<joueurs.length; i++) {
    			String arrivee = getPosition( joueurs[i].getArrivee(), joueurs.length ) + " - " + joueurs[i].getNom();
    			JLabel joueurLabel = new JLabel(arrivee);
    			joueurLabel.setIcon(Joueur.getIcon(joueurs[i].getCouleur()));
    			joueurLabel.setPreferredSize(new Dimension(150, joueurLabel.getPreferredSize().height));
    			add(joueurLabel, gbc);
    		}
     
    		JButton button = new JButton("Rejouer");
    		add(button, gbc);
    		button.addActionListener(e-> exempleJeu.init());
     
     
    	}
     
    	private String getPosition(int arrivee, int nbjoueurs) {
    		switch( arrivee ) {
    		case 1:
    			return "Premier";
    		case 2:
    			if ( nbjoueurs==2 ) {
    				return "Second";
    			}
    			else {
    				return "Deuxième";
    			}
    		case 3:
    			return "Troisième";
    		default:
    			return "Quatriè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
    package jeu;
     
    import java.util.concurrent.ThreadLocalRandom;
     
    public class Game {
     
    	public static final int NOMBRE_JOUEUR_MIN = 2;
    	public static final int NOMBRE_JOUEUR_MAX = 4;
    	public static final int FIN = 10;
     
    	private Object[][] plateau;
    	private Joueur[] joueurs;
    	private int joueur;
    	private int position;
     
    	public Game() {
    	}
     
    	public void reset() {
    		plateau=null;
    	}
     
    	public void setNombreJoueurs(int nbjoueurs) {
    		joueurs = new Joueur[nbjoueurs];
    		for(int i=0; i<joueurs.length; i++) {
    			joueurs[i]=new Joueur(i);
    		}
    		plateau = new Object[nbjoueurs*2-1][FIN];
    		joueur = 0;
    		position = 1;
    	}
     
    	public int getNbColonnes() {
    		return plateau==null?0:plateau[0].length;
    	}
     
    	public int getNbLignes() {
    		return plateau==null?0:plateau.length;
    	}
     
    	public Joueur getJoueur() {
    		return joueurs[joueur];
    	}
     
    	public Joueur[] getJoueurs() {
    		return joueurs;
    	}
     
    	public void avancerJoueur() {
    		int random = ThreadLocalRandom.current().nextInt(1, 3);
    		Joueur joueur = getJoueur();
    		joueur.avancer(random);
    		if ( joueur.fin() ) {
    			joueur.setArrivee(position++);
    		}
    	}
     
    	public boolean fin() {
    		int nbjoueurs = joueurs.length;
    		for(Joueur joueur : joueurs) {
    			if ( joueur.fin() ) {
    				nbjoueurs--;
    			}
    		}
    		return nbjoueurs<=1;
    	}
     
    	public void prochainJoueur() {
    		do {
    			joueur++;
    			if  ( joueur>=joueurs.length ) {
    				joueur=0;
    			}
    		}
    		while( joueurs[joueur].fin() );
    	}
     
    	public void setArriveeDernierJoueur() {
    		for(Joueur joueur : joueurs) {
    			if ( !joueur.fin() ) {
    				joueur.setArrivee(joueurs.length);
    				break;
    			}
    		}
    	}
     
    }
    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
    package jeu;
     
    import java.awt.FlowLayout;
     
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
    public class InteractionPanel extends JPanel {
     
    	private JLabel label;
    	private ExempleJeu exempleJeu;
     
    	public InteractionPanel(ExempleJeu exempleJeu) {
    		super(new FlowLayout(FlowLayout.CENTER));
    		this.exempleJeu = exempleJeu;
    		label = new JLabel();
    		add(label);
    		JButton button = new JButton("Avancer");
    		button.addActionListener(e-> jouer());
    		add(button);
    	}
     
    	private void jouer() {
    	   exempleJeu.getGame().avancerJoueur();
    	   if ( exempleJeu.getGame().fin( ) ) {
    		   exempleJeu.finJeu();
    	   }
    	   else {
    		   exempleJeu.rafraichir();
    		   exempleJeu.getGame().prochainJoueur();
    		   setJoueur(exempleJeu.getGame().getJoueur());
    	   }
    	}
     
    	public void setJoueur(Joueur joueur) {
    		label.setText(joueur.getNom()+" à vous de jouer : ");
    	}
     
    }
    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
    package jeu;
     
    import java.awt.BorderLayout;
     
    import javax.swing.BorderFactory;
    import javax.swing.JPanel;
     
    public class JeuPanel extends JPanel {
     
    	private Game game;
     
    	private PlateauPanel plateau;
     
    	private InteractionPanel interaction;
     
    	public JeuPanel(ExempleJeu exempleJeu, Game game) {
    		super(new BorderLayout());
    		this.game=game;
    		setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    		this.game=game;
    		plateau = new PlateauPanel(game);
    		add(plateau);
    		interaction = new InteractionPanel(exempleJeu);
    		add(interaction, BorderLayout.SOUTH);
    	}
     
    	public void update() {
    		interaction.setJoueur(game.getJoueur());
    	}
     
    }
    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
    package jeu;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.Map;
     
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
     
    public class Joueur {
     
    	private static final Color[] COULEURS = {Color.GREEN.darker(), Color.ORANGE, Color.MAGENTA, Color.CYAN.darker()};
    	private static final Map<Color, Icon> ICONS = createIcons(COULEURS);
    	private String nom;
    	private Color couleur;
    	private int position;
    	private int joueur;
    	private int arrivee;
     
    	public Joueur(int joueur) {
    		this.joueur = joueur;
    		this.nom = "Joueur " + (joueur+1);
    		this.couleur = COULEURS[joueur];
    		this.arrivee = Integer.MAX_VALUE;
    	}
     
    	private static Map<Color, Icon> createIcons(Color[] couleurs) {
    		Map<Color, Icon> map = new HashMap<>();
    		for(Color color : couleurs) {
    			map.put(color, createIcon32x32(color));
    		}
    		return Collections.unmodifiableMap(map);
    	}
     
    	public String getNom() {
    		return nom;
    	}
     
    	public Color getCouleur() {
    		return couleur;
    	}
     
    	public int getJoueur() {
    		return joueur;
    	}
     
    	public int getPosition() {
    		return position;
    	}
     
    	public void avancer(int n) {
    		position+=n;
    		if ( position >= Game.FIN  ) {
    			position = Game.FIN;
    		}
    	}
     
    	public boolean fin() {
    		return position == Game.FIN;
    	}
     
    	public void setArrivee(int arrivee) {
    		this.arrivee=arrivee;
    	}
     
    	public int getArrivee() {
    		return arrivee;
    	}
     
    	public static Icon getIcon(Color couleur) {
    		return ICONS.get(couleur);
    	}
     
    	public static Icon createIcon32x32(Color color) {
    		BufferedImage image = new BufferedImage(24, 24, BufferedImage.TYPE_INT_RGB);
    		Graphics g = image.createGraphics();
    		g.setColor(color);
    		g.fillRect(0, 0, 23, 23);
    		if ( color==Color.BLACK ) {
    			g.setColor(Color.WHITE);
    			g.drawRect(1, 1, 21, 21);
    		}
    		else {
    			g.setColor(Color.BLACK);
    			g.drawRect(0, 0, 23, 23);
    		}
    		g.dispose();
     
    		return new ImageIcon(image);
    	}
     
    }
    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
    package jeu;
     
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
     
    import javax.swing.JPanel;
     
    public class PlateauPanel extends JPanel {
     
    	private static final int TAILLE = 32;
    	private Game game;
     
    	public PlateauPanel(Game game) {
    		this.game=game;
    		setBackground(Color.BLACK);
    	}
     
    	@Override
    	public void paint(Graphics g) {
    		super.paint(g);
    		int width = game.getNbColonnes()*TAILLE;
    		int heigth = game.getNbLignes()*TAILLE;
    		int offsetx =  (getWidth()-width)/2;
    		int offsety =  (getHeight()-heigth)/2;
    		drawGrid(g, offsetx, offsety, width, heigth);
    		drawJoueurs(g, offsetx, offsety);
    	}
     
    	private void drawJoueurs(Graphics g, int offsetx, int offsety) {
    		for(Joueur joueur : game.getJoueurs()) {
    			drawJoueur(g, joueur, offsetx, offsety);
    		}
    	}
     
    	private void drawJoueur(Graphics g, Joueur joueur, int offsetx, int offsety) {
    		int positionx = offsetx + joueur.getPosition()*TAILLE;
    		int positiony = offsety + joueur.getJoueur()*2 * TAILLE;
    		positionx+=5;
    		positiony+=5;
    		int taille = TAILLE-10;
    		g.setColor(joueur.getCouleur());
    		g.fillOval(positionx, positiony, taille, taille);
    		if ( joueur==game.getJoueur() ) {
    			((Graphics2D)g).setStroke(new BasicStroke(3));
    			g.setColor(Color.WHITE);
    			g.drawOval(positionx, positiony, taille, taille);
    		}
    	}
     
    	private void drawGrid(Graphics g, int offsetx, int offsety, int width, int heigth) {
     
    		g.setColor(Color.WHITE);
    		for(int i=0; i<=game.getNbLignes(); i++) {
    			g.drawLine(offsetx, offsety+i*TAILLE, offsetx+width, offsety+i*TAILLE);
    		}
    		for(int i=0; i<=game.getNbColonnes(); i++) {
    			g.drawLine(offsetx+i*TAILLE, offsety, offsetx+i*TAILLE, offsety+heigth);
    		}
    	}
     
    	@Override
    	public Dimension getPreferredSize() {
     
    		return new Dimension(game.getNbColonnes()* TAILLE, game.getNbLignes()* TAILLE);
    	}
     
    }

  4. #4
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2019
    Messages
    61
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 25
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2019
    Messages : 61
    Points : 44
    Points
    44
    Par défaut
    D'accord, merci beaucoup pour ta réponse et tes explications.

    Je vais essayer de me servir de ce que tu m'as appris pour implémenter mon interface graphique.

    Bien cordialement.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Comment gérer des clics sur les touches du clavier:
    Par choko83 dans le forum Débuter avec Java
    Réponses: 4
    Dernier message: 11/03/2008, 11h15
  2. Gérer les clics sur les boutons
    Par cyberlewis dans le forum Windows
    Réponses: 4
    Dernier message: 08/02/2004, 15h34

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo