-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFenetreJeu.java
408 lines (363 loc) · 12.2 KB
/
FenetreJeu.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JButton;
import java.awt.Color;
public class FenetreJeu extends JFrame implements ActionListener {
private LinkedList<Carte> pioche; //paquet de cartes
private Joueur[] joueur= new Joueur[4]; //tableau dejoueurs
private LinkedList <Carte> cartesEnCours; //liste qui stocke les cartes à afficher
private JButton jouer; //pour jouer la carte
private JButton boutonPioche; //pour piocher
private JButton joueurSuivant;
private int positionCarte; //numéro choisi par le joueur
private JTextField num; //JtextField pour récupérer le numéro de carte
private LinkedList <Carte> defausse;
private boolean sensJeu; //true si sens est changé
public int joueurEnCours; //indice du joueur courant
public Carte carteSupDefausse;
public JTextField tourJoueur;
private LinkedList<Carte> liste2ou4; //liste de cartes qui stocke les cartes à piocher si +2 ou +4
public JTextField couleur;
public JPanel choixCouleur;
public String couleurChoisie;
public JButton jSuivant;
public JFrame frame; //Fenetre entre chaque joueur
public boolean joSuivant; //true si on passe au joueur suivant
public FenetreJeu (Joueur[] j,LinkedList<Carte> pioche){
cartesEnCours=new LinkedList<Carte>();
joueur=j;
defausse=new LinkedList<Carte>();
defausse.add(piocher(pioche));
carteSupDefausse=new Carte();
carteSupDefausse=defausse.get(defausse.size()-1);
liste2ou4=new LinkedList<Carte> ();
joueurEnCours=0;
sensJeu=false;
joSuivant=false;
cartesEnCours=joueur[0].cartes;
this.pioche=pioche;
this.setTitle("UNO");
this.setSize(1920,1080);
this.setLocation(0,0);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Bouton Piocher
boutonPioche= new JButton(new ImageIcon("uno_dos.jpg"));
boutonPioche.setBounds(300,65,136,205);
boutonPioche.addActionListener(this);
//Bouton Jouer
jouer=new JButton();
jouer.setBounds(100,0,200,150);
jouer.setText("Jouer");
jouer.setBackground(new Color(255,215,0));
jouer.setFont(new java.awt.Font("Tahoma", 1, 36));
jouer.addActionListener(this);
//Texte du numéro saisi
num=new JTextField();
num.setBounds(100,170,200,150);
num.setFont(new java.awt.Font("Tahoma", 1, 30));
//Bouton passer au Joueur Suivant
joueurSuivant=new JButton();
joueurSuivant.setBounds(100,340,200,150);
joueurSuivant.setText("Joueur suivant");
joueurSuivant.setBackground(new Color(255,215,0));
joueurSuivant.setFont(new java.awt.Font("Tahoma", 1, 20));
joueurSuivant.addActionListener(this);
//Texte affichant le joueur en cours
tourJoueur=new JTextField();
tourJoueur.setBounds(20,510,400,50);
tourJoueur.setOpaque(false);
tourJoueur.setBorder(null);
tourJoueur.setEditable(false);
tourJoueur.setForeground(Color.white);
tourJoueur.setBackground(Color.blue);
tourJoueur.setText("Le joueur en cours est 1 "+joueur[0].nom);
tourJoueur.setFont(new Font ("",Font.PLAIN,30));
//photo fond
JLabel photo=new JLabel(new ImageIcon("fondFenetreJeu1.png"));
photo.setBounds(0,0,1920,1080);
//Texte couleur saisie
couleur=new JTextField();
couleur.setBounds(50,50,200,100);
couleur.setFont(new java.awt.Font("Tahoma", 1, 30));
couleur.setForeground(Color.white);
couleur.setBackground(Color.black);
// JPanel choixCouleur
choixCouleur=new JPanel();
choixCouleur.setBounds(600,400,300,200);
choixCouleur.setLayout(null);
choixCouleur.setBackground(Color.white);
couleurVisible(false);
// JPanel principal
JPanel conteneurPrincipal = new JPanel();
conteneurPrincipal.setBounds(0,0,1920,1080);
conteneurPrincipal.setLayout(null);
// JPanel pioche
JPanel zonePioche = new JPanel();
zonePioche.setLayout(null);
zonePioche.setBounds(1000,100,500,600);
zonePioche.setOpaque(false);
choixCouleur.add(couleur);
zonePioche.add(joueurSuivant);
zonePioche.add(tourJoueur);
zonePioche.add(jouer);
zonePioche.add(num);
conteneurPrincipal.add(choixCouleur);
conteneurPrincipal.add(boutonPioche);
conteneurPrincipal.add(zonePioche);
conteneurPrincipal.add(photo);
this.add(conteneurPrincipal);
this.setVisible(true);
partie();
//Fenetre entre chaque joueur
jSuivant = new JButton();
jSuivant.setText("Passer au joueur suivant");
jSuivant.setFont(new java.awt.Font("Tahoma", 1, 50));
jSuivant.setBounds(760,412,350,250);
jSuivant.addActionListener(this);
frame = new JFrame();
frame.setContentPane(jSuivant);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(1920,1080));
frame.pack();
frame.setVisible(false);
}
//méthode pour piocher des cartes
public Carte piocher(LinkedList<Carte> pioche){
//on teste si la pioche est vide si oui on remet les cartes de la defausse en gardant la dernière carte jouée
if(pioche.isEmpty()){
for(int i=0;i<defausse.size()-1;i++){
pioche.add(defausse.get(i));
defausse.remove(i);
}
}
int i= (int)(pioche.size()*Math.random());
Carte c = pioche.get(i);
pioche.remove(i);
return c;
}
//Méthode qui lance la partie
public void partie(){
cartesEnCours=joueur[0].cartes;
Carte c= defausse.get(defausse.size()-1);
//La partie ne commence pas si la première carte est +2/+4/ChoixCouleur
if((c.type.equals("+2")) || (c.type.equals("+4")|| c.type.equals("choixCouleur"))){
defausse.add(piocher(pioche));
partie();
}
//Si la partie commence avec la carte inverser ou passer
jouerPasserouInverser();
}
//permet d'ajouter des cartes dans liste2ou4
public void plusDeCartes ( Carte dCarte,LinkedList <Carte> pioche){
if( dCarte.type == "+2" || dCarte.type == "+4"){
liste2ou4.add(piocher(pioche));
liste2ou4.add(piocher(pioche));
if(dCarte.type == "+4"){
liste2ou4.add(piocher(pioche));
liste2ou4.add(piocher(pioche));
}
}
}
//actualise la carte sup et l'enlève de la main du joueur
public void carteSuperieure(Joueur j){
Carte p=cartesEnCours.get(positionCarte);
defausse.add(p);
carteSupDefausse=defausse.get(defausse.size()-1);
j.cartes.remove(positionCarte);
repaint();
}
//Méthode qui dessine la liste des cartes du joueur en cours
public void paint(Graphics g){
super.paint(g);
for(int i=0; i<cartesEnCours.size();i++){
cartesEnCours.get(i).dessine(g,i*(1920)/cartesEnCours.size(),800);
//affichage numéros en dessus des cartes
Font fonte = new Font("TimesRoman ",Font.BOLD,20);
g.setFont(fonte);
g.setColor(Color.white);
g.drawString(Integer.toString(i+1) , i*(1920)/cartesEnCours.size() +10,780);
}
defausse.get(defausse.size()-1).dessine(g,100,100);
}
//Méthode qui met à jour cartesEnCours
public void majCartesEnCours(int i){
cartesEnCours=joueur[i].cartes;
repaint();
}
//Test si une carte de pouvoir void est jouable
public boolean jouableVoid(Carte carteSup, Carte c){
boolean a=(c.getCouleur().equals(carteSup.getCouleur()) || c.valeur==carteSup.valeur || c.couleur=="NOIR");
if((carteSup.type=="+2" || carteSup.type=="+4" || carteSup.type=="inverser" || carteSup.type=="passer") && carteSup.pouvoir=="void"){
a= a || (c.type==carteSup.type);
}
return a;
}
//Méthode qui incrémente le tour du joueur
public void avancer(){
if(sensJeu==false){
joueurEnCours++;
if(joueurEnCours>=4){
joueurEnCours=0;
}
}
if(sensJeu){
joueurEnCours--;
if(joueurEnCours<0){
joueurEnCours=joueur.length-1;
}
}
tourJoueur.setText("le joueur en cours est "+ Integer.toString(joueurEnCours+1)+" "+ joueur[joueurEnCours].nom);
}
//Méthode pour jouer inverser et passer
public void jouerPasserouInverser(){
//si carte passer
if(carteSupDefausse.pouvoir=="passer"){
visible(false);
JOptionPane.showMessageDialog(this,"Vous passez votre tour");
carteSupDefausse.pouvoir="void";
}
//Si carte inverser
if(carteSupDefausse.pouvoir=="inverser"){
visible(false);
sensJeu=!sensJeu;
JOptionPane.showMessageDialog(this,"Changement du sens du jeu");
carteSupDefausse.pouvoir="void";
}
}
//visibilité boutons
public void visible(boolean b){
jouer.setVisible(b);
boutonPioche.setVisible(b);
}
//Visibilité Panel choix couleur
public void couleurVisible(boolean b){
choixCouleur.setVisible(b);
}
//test si la partie est finie
public void winOrLose(){ //Si un joueur n'a plus de cartes, il remporte la manche et gagne des points (somme des valeurs des cartes des autres)
String s;
if(joueur[joueurEnCours].cartes.isEmpty()){
for(int i=0;i<joueurEnCours;i++){
joueur[joueurEnCours].calculScore(joueur[i]);
}
for(int i=joueurEnCours+1;i<joueur.length;i++){
joueur[joueurEnCours].calculScore(joueur[i]);
}
FenetreFin fin = new FenetreFin(joueur[joueurEnCours]);
}
}
//déroulement du jeu
public void actionPerformed(ActionEvent e){
if(e.getSource()==jouer){
if(Integer.parseInt(num.getText())>=1){
positionCarte=Integer.parseInt(num.getText())-1;
//si la carte est une carte chiffre ou n'a pas de pouvoir
if(carteSupDefausse.pouvoir.equals("void")){
if(jouableVoid(carteSupDefausse,cartesEnCours.get(positionCarte))){
carteSuperieure(joueur[joueurEnCours]);
majCartesEnCours(joueurEnCours);
visible(false);
}
else{
JOptionPane.showMessageDialog(this,"Veuillez piocher");
}
}
//si la carte est un +2
if(carteSupDefausse.pouvoir=="+2"){
if(joSuivant){
if(cartesEnCours.get(positionCarte).pouvoir=="+2"){
carteSuperieure(joueur[joueurEnCours]);
plusDeCartes(carteSupDefausse,pioche);
}
else{
plusDeCartes(carteSupDefausse,pioche);
for(Carte c : liste2ou4){
joueur[joueurEnCours].cartes.add(c);
carteSupDefausse.pouvoir="void";
}
liste2ou4.clear();
}
}
majCartesEnCours(joueurEnCours);
joSuivant=false;
}
//Si la carte est un +4
if(carteSupDefausse.pouvoir=="+4"){
couleurVisible(true);
if(joSuivant){
if(cartesEnCours.get(positionCarte).pouvoir=="+4"){
carteSuperieure(joueur[joueurEnCours]);
plusDeCartes(carteSupDefausse,pioche);
}
else{
plusDeCartes(carteSupDefausse,pioche);
for(Carte c : liste2ou4){
joueur[joueurEnCours].cartes.add(c);
carteSupDefausse.pouvoir="void";
}
liste2ou4.clear();
}
}
majCartesEnCours(joueurEnCours);
joSuivant=false;
}
//si carte choix couleur
if(carteSupDefausse.pouvoir=="choixCouleur"){
JOptionPane.showMessageDialog(this,"Choisissez une couleur");
couleurVisible(true);
carteSupDefausse.pouvoir = "void";
}
//si carte passer
if(carteSupDefausse.pouvoir=="passer" ){
visible(false);
}
//Si carte inverser
if(carteSupDefausse.pouvoir=="inverser"){
visible(false);
sensJeu=!sensJeu;
JOptionPane.showMessageDialog(this,"Changement du sens du jeu");
carteSupDefausse.pouvoir="void";
}
}
else{
JOptionPane.showMessageDialog(this,"Entrez un numéro de carte supérieur ou égal à 1 ou piochez");
}
}
if (e.getSource()== boutonPioche){
joueur[joueurEnCours].cartes.add(piocher(pioche));
majCartesEnCours(joueurEnCours);
visible(false);
}
if(e.getSource()==joueurSuivant){
visible(true);
winOrLose();
frame.setVisible(true);
avancer();
majCartesEnCours(joueurEnCours);
}
if(e.getSource()==jSuivant){
if(carteSupDefausse.pouvoir=="passer"){
JOptionPane.showMessageDialog(this,"Vous passez votre tour");
visible(false);
carteSupDefausse.pouvoir="void";
}
if((carteSupDefausse.type == "choixCouleur")||(carteSupDefausse.type == "+4")){
couleurChoisie = couleur.getText();
couleurVisible(false);
if(couleurChoisie.equals("VERT") || couleurChoisie.equals("ROUGE") || couleurChoisie.equals("BLEU") ||couleurChoisie.equals("JAUNE")){
carteSupDefausse.couleur = couleurChoisie;
JOptionPane.showMessageDialog(this,"Jouer une carte de couleur "+couleurChoisie);
}else{
JOptionPane.showMessageDialog(this,"Veuillez choisir une autre couleur");
}
}
if(carteSupDefausse.pouvoir=="+2"|| carteSupDefausse.pouvoir=="+4"){
joSuivant=true;
}
frame.setVisible(false);
}
}
}