-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fenetre.java
386 lines (337 loc) · 14 KB
/
Fenetre.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
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import javax.swing.*;
//C:/Users/Jiloko/Documents/BatailleNavale/src/
public class Fenetre extends JFrame
{
private Grid gridtest;
private ImageIcon iconeau = new ImageIcon("src/eau.png");
private ImageIcon iconeautouch = new ImageIcon("src/eautouch.png");
private ImageIcon icondebris = new ImageIcon("src/debris.png");
private ImageIcon icondebristouch = new ImageIcon("src/debristouch.png");
private ImageIcon iconbattle1h = new ImageIcon("src/battleship1h.png");
private ImageIcon iconbattle2h = new ImageIcon("src/battleship2h.png");
private ImageIcon iconbattle3h = new ImageIcon("src/battleship3h.png");
private ImageIcon iconbattle4h = new ImageIcon("src/battleship4h.png");
private ImageIcon iconbattle1v = new ImageIcon("src/battleship1v.png");
private ImageIcon iconbattle2v = new ImageIcon("src/battleship2v.png");
private ImageIcon iconbattle3v = new ImageIcon("src/battleship3v.png");
private ImageIcon iconbattle4v = new ImageIcon("src/battleship4v.png");
private ImageIcon iconsub1h = new ImageIcon("src/submarin1h.png");
private ImageIcon iconsub2h = new ImageIcon("src/submarin2h.png");
private ImageIcon iconsub3h = new ImageIcon("src/submarin3h.png");
private ImageIcon iconsub1v = new ImageIcon("src/submarin1v.png");
private ImageIcon iconsub2v = new ImageIcon("src/submarin2v.png");
private ImageIcon iconsub3v = new ImageIcon("src/submarin3v.png");
private ImageIcon iconair1h = new ImageIcon("src/aircraft1h.png");
private ImageIcon iconair2h = new ImageIcon("src/aircraft2h.png");
private ImageIcon iconair3h = new ImageIcon("src/aircraft3h.png");
private ImageIcon iconair4h = new ImageIcon("src/aircraft4h.png");
private ImageIcon iconair5h = new ImageIcon("src/aircraft5h.png");
private ImageIcon iconair1v = new ImageIcon("src/aircraft1v.png");
private ImageIcon iconair2v = new ImageIcon("src/aircraft2v.png");
private ImageIcon iconair3v = new ImageIcon("src/aircraft3v.png");
private ImageIcon iconair4v = new ImageIcon("src/aircraft4v.png");
private ImageIcon iconair5v = new ImageIcon("src/aircraft5v.png");
private JPanel jp_droite;
private JLabel nom_J1;
private JPanel jp_grille1;
private JButton[][] tabbout1;
private JPanel jpg_bas;
private JPanel jp_gauche;
private JLabel nom_J2;
private JPanel jp_grille2;
private JButton[][] tabbout2;
public Player J1;
private JButton jb_avion;
private ActionListener al_avion;
private JButton jb_cuir;
private ActionListener al_cuir;
private JButton jb_sub;
private ActionListener al_sub;
private JButton b_new;
private ActionListener al_new;
private boolean nbavion;
private boolean nbcuir;
private boolean nbsub;
public Ship battleship = new Ship(ShipType.BATTLESHIP);
public Ship aircraft = new Ship(ShipType.AIRCRAFT_CARRIER);
public Ship submarin = new Ship(ShipType.SUBMARIN);
static Insets m = new Insets(-3,-3,-3,-3);
Partie jeu;
Game game;
Player P2; //toi
Player P1; //adv
Reseau resJeux;
String ipJoueur;
String ipAdv;
public Fenetre(String titre)
{
super(titre);
//Gauche de l'application
jp_gauche = new JPanel();
String nomJ1 = "Joueur 1";
// nomJ1 = JOptionPane.showInputDialog("Quel est votre nom de capitaine ?"); //(Chiant)
nom_J1 = new JLabel(nomJ1);
//grille J1
jp_grille1 = new JPanel(new GridLayout(10,10));
tabbout1 = new JButton[10][10];
for (int i = 0; i<10 ; i++) //ligne
{
for (int j = 0; j<10; j++) //colonne
{
JButton boutton = new JButton(new ImageIcon("src/eau.png"));
boutton.setMargin(m);
jp_grille1.add(boutton);
// boutton.addActionListener(ecouteur);
tabbout1[i][j]=boutton;
}
}
jp_gauche.add(nom_J1, BorderLayout.NORTH);
jp_gauche.add(jp_grille1, BorderLayout.CENTER);
//bas pour le J1
b_new = new JButton("Nouvelle partie");
jpg_bas = new JPanel();
jpg_bas.add(b_new);
jb_sub = new JButton("Sous-marin");
jb_avion = new JButton("Porte-avion");
jb_cuir = new JButton("Cuirrassé");
this.al_sub = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if(nbsub)
{
Partie.demande(submarin, J1);
majgrid1(J1);
nbsub=false;
}
else
{
JOptionPane.showMessageDialog(null, "Vous avez déjà placé votre sous-marin", "Erreur 005", JOptionPane.ERROR_MESSAGE);
}
}
};
this.jb_sub.addActionListener(al_sub);
this.al_cuir = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if(nbcuir)
{
Partie.demande(battleship, J1);
majgrid1(J1);
nbcuir = false;
}
else
{
JOptionPane.showMessageDialog(null, "Vous avez déjà placé votre cuirrassé", "Erreur 004", JOptionPane.ERROR_MESSAGE);
}
}
};
this.jb_cuir.addActionListener(al_cuir);
this.al_avion = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if(nbavion)
{
Partie.demande(aircraft, J1);
majgrid1(J1);
nbavion = false;
}
else
{
JOptionPane.showMessageDialog(null, "Vous avez déjà placé votre porte-avion", "Erreur 003", JOptionPane.ERROR_MESSAGE);
}
}
};
this.jb_avion.addActionListener(al_avion);
jb_sub.setVisible(false);
jb_cuir.setVisible(false);
jb_avion.setVisible(false);
jpg_bas.add(jb_sub);
jpg_bas.add(jb_cuir);
jpg_bas.add(jb_avion);
jp_gauche.add(jpg_bas, BorderLayout.SOUTH);
//Droite de l'application
jp_droite = new JPanel();
String nomJ2 = "Joueur 2";
nom_J2 = new JLabel(nomJ2); //Nom du joueur 2 à récupéré sur le serveur
jp_droite.add(nom_J2);
//grille J2
jp_grille2 = new JPanel(new GridLayout(10,10));
tabbout2 = new JButton[10][10];
for (int i = 0; i<10 ; i++) //ligne
{
for (int j = 0; j<10; j++) //colonne
{
JButton boutton = new JButton(new ImageIcon("src/brouillard.png"));
boutton.setMargin(m);
jp_grille2.add(boutton);
tabbout2[i][j]=boutton;
}
}
jp_droite.add(nom_J2, BorderLayout.NORTH);
jp_droite.add(jp_grille2, BorderLayout.SOUTH);
//Mise en page
this.add(jp_gauche, BorderLayout.WEST);
this.add(jp_droite, BorderLayout.EAST);
//bouton b_new commence une partie
this.al_new = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
jeu = new Partie();
J1 = new Player();
jb_sub.setVisible(true);
jb_cuir.setVisible(true);
jb_avion.setVisible(true);
b_new.setVisible(false);
nbavion = true;
nbcuir = true;
nbsub = true;
}
};
this.b_new.addActionListener(al_new);
//algo
// boolean test = true;
// while(test){
// if( nbavion == false && nbcuir == false && nbsub == false && b_new.isVisible() == false ) {
// System.out.print("OUIIIIIIIII");
// while(P1.isDestroyed() || P2.isDestroyed()){
// if(P1 != game.getCurrentEnnemy()){
// String x = JOptionPane.showInputDialog("Sur quel ligne voulez vous tirez ?");
// String y = JOptionPane.showInputDialog("Sur quel collonne voulez vous tirez ?");
// int res = game.fire(Integer.parseInt(x),Integer.parseInt(y));
// //majgrid2(res);
// game.nextTurn();
// }
// else{
// game.waitt();
// game.nextTurn();
// }
// }
//
// test = false;
// }
// else
// {
// System.out.print("CACA");
// }
//}
}
private void majgrid2(int row, int col, int etat)
{
if (etat == 0)
{
tabbout2[row][col].setIcon(iconeau);
}
else
{
tabbout2[row][col].setIcon(icondebris);
}
}
private void majgrid1(Player player)
{
gridtest = new Grid();
gridtest = player.getGridJoueur();
ShipType type = ShipType.AIRCRAFT_CARRIER;
Ship ship = new Ship(type);
Orientation orient = Orientation.HORIZONTAL;
for(int i=0; i<10 ; i++) //ligne
{
for (int j =0; j < 10; j++) //colonne
{
if (gridtest.getCell(i, j) instanceof OceanCell)
{
tabbout1[i][j].setIcon(iconeau);
}
// else if(gridtest.getCell(i, j) instanceof TouchOceanCell)
// {
// tabbout1[i][j].setIcon(iconeautouch);
// }
// else if(gridtest.getCell(i, j) instanceof TouchOceanCell)
// {
// tabbout1[i][j].setIcon(icondebristouch);
// }
else if (gridtest.getCell(i, j) instanceof ShipCell)
{
ship = ((ShipCell)gridtest.getCell(i, j)).getShip();
int size = ship.getSize();
orient = ((ShipCell)gridtest.getCell(i, j)).getOrient();
if (orient.isHorizontal())
{
if (ship.getSize() == 2)
{
tabbout1[i][j].setIcon(iconsub1h);
tabbout1[i][j+1].setIcon(iconsub2h);
tabbout1[i][j+2].setIcon(iconsub3h);
j=j+2;
}
else if (ship.getSize() == 3) //battleship
{
tabbout1[i][j].setIcon(iconbattle1h);
tabbout1[i][j+1].setIcon(iconbattle2h);
tabbout1[i][j+2].setIcon(iconbattle3h);
tabbout1[i][j+3].setIcon(iconbattle4h);
j=j+3;
}
else
{
tabbout1[i][j].setIcon(iconair1h);
tabbout1[i][j+1].setIcon(iconair2h);
tabbout1[i][j+2].setIcon(iconair3h);
tabbout1[i][j+3].setIcon(iconair4h);
tabbout1[i][j+4].setIcon(iconair5h);
j=j+4;
}
}
}
}
for(int i2=0; i2<10 ; i2++) //ligne
{
for (int j2 =0; j2 < 10; j2++) //colonne
{
if (gridtest.getCell(i2, j2) instanceof ShipCell)
{
ship = ((ShipCell)gridtest.getCell(i2, j2)).getShip();
orient = ((ShipCell)gridtest.getCell(i2, j2)).getOrient();
if (!orient.isHorizontal())
{
if (ship.getSize() == 2)
{
tabbout1[i2][j2].setIcon(iconsub1v);
tabbout1[i2+1][j2].setIcon(iconsub2v);
tabbout1[i2+2][j2].setIcon(iconsub3v);
i2=i2+2;
}
else if (ship.getSize() == 3) //battleship
{
tabbout1[i2][j2].setIcon(iconbattle1v);
tabbout1[i2+1][j2].setIcon(iconbattle2v);
tabbout1[i2+2][j2].setIcon(iconbattle3v);
tabbout1[i2+3][j2].setIcon(iconbattle4v);
i2=i2+3;
}
else
{
tabbout1[i2][j2].setIcon(iconair1v);
tabbout1[i2+1][j2].setIcon(iconair2v);
tabbout1[i2+2][j2].setIcon(iconair3v);
tabbout1[i2+3][j2].setIcon(iconair4v);
tabbout1[i2+4][j2].setIcon(iconair5v);
i2=i2+4;
}
}
}
}
}
}
}
}