Bonjour,
Je suis en train de conclure un jeu d'Othello qui tourne sans bug si je ne m'abuse (inch allah ).
Me reste le problème suivant, plus didactique que fonctionnel.
Quand l'IA joue, j'aimerais que le système freine et affiche un artificiel "l'IA réfléchit" pendant deux secondes. Ma fonction freine()...
Or, j'ai beau chercher, et j'ai à peu près tout testé, je ne vois pas où ni quand la placer cette fonction, pour produire l'effet que je désire. Cela ne "freine" jamais au moment où je le désire... Je dois rater un truc important.
Voici l'intégralité de ma classe Fenetre, ne pas se moquer de mon code de débutante svp.
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 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * Fenetre.java * * Created on 28 août 2010, 17:47:48 */ package othello; import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Vector; import javax.swing.AbstractButton; /** * * @author sylvau */ public class Fenetre extends javax.swing.JFrame implements ActionListener,Constantes{ private Jeu monJeu; /** Creates new form Fenetre */ public Fenetre() { initComponents(); this.monJeu=new Jeu(); this.setVisible(true); //tous les boutons se branchent sur procédure clic this.brancheActionListener(); //Affiche plateau selon damier this.affichePlateau(AIDE); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { plateau = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); jButton5 = new javax.swing.JButton(); jButton6 = new javax.swing.JButton(); jButton7 = new javax.swing.JButton(); jButton8 = new javax.swing.JButton(); jButton9 = new javax.swing.JButton(); jButton10 = new javax.swing.JButton(); jButton11 = new javax.swing.JButton(); jButton12 = new javax.swing.JButton(); jButton13 = new javax.swing.JButton(); jButton14 = new javax.swing.JButton(); jButton15 = new javax.swing.JButton(); jButton16 = new javax.swing.JButton(); jButton17 = new javax.swing.JButton(); jButton18 = new javax.swing.JButton(); jButton19 = new javax.swing.JButton(); jButton20 = new javax.swing.JButton(); jButton21 = new javax.swing.JButton(); jButton22 = new javax.swing.JButton(); jButton23 = new javax.swing.JButton(); jButton24 = new javax.swing.JButton(); jButton25 = new javax.swing.JButton(); jButton26 = new javax.swing.JButton(); jButton27 = new javax.swing.JButton(); jButton28 = new javax.swing.JButton(); jButton29 = new javax.swing.JButton(); jButton30 = new javax.swing.JButton(); jButton31 = new javax.swing.JButton(); jButton32 = new javax.swing.JButton(); jButton33 = new javax.swing.JButton(); jButton34 = new javax.swing.JButton(); jButton35 = new javax.swing.JButton(); jButton36 = new javax.swing.JButton(); jButton37 = new javax.swing.JButton(); jButton38 = new javax.swing.JButton(); jButton39 = new javax.swing.JButton(); jButton40 = new javax.swing.JButton(); jButton41 = new javax.swing.JButton(); jButton42 = new javax.swing.JButton(); jButton43 = new javax.swing.JButton(); jButton44 = new javax.swing.JButton(); jButton45 = new javax.swing.JButton(); jButton46 = new javax.swing.JButton(); jButton47 = new javax.swing.JButton(); jButton48 = new javax.swing.JButton(); jButton49 = new javax.swing.JButton(); jButton50 = new javax.swing.JButton(); jButton51 = new javax.swing.JButton(); jButton52 = new javax.swing.JButton(); jButton53 = new javax.swing.JButton(); jButton54 = new javax.swing.JButton(); jButton55 = new javax.swing.JButton(); jButton56 = new javax.swing.JButton(); jButton57 = new javax.swing.JButton(); jButton58 = new javax.swing.JButton(); jButton59 = new javax.swing.JButton(); jButton60 = new javax.swing.JButton(); jButton61 = new javax.swing.JButton(); jButton62 = new javax.swing.JButton(); jButton63 = new javax.swing.JButton(); jButton64 = new javax.swing.JButton(); afficheTour = new javax.swing.JLabel(); afficheScore = new javax.swing.JLabel(); restart = new javax.swing.JButton(); private void restartActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.monJeu=new Jeu(); this.affichePlateau(AIDE); } /** * @param args the command line arguments */ // Variables declaration - do not modify private javax.swing.JLabel afficheScore; private javax.swing.JLabel afficheTour; private javax.swing.JButton jButton1; private javax.swing.JButton jButton10; private javax.swing.JButton jButton11; private javax.swing.JButton jButton12; private javax.swing.JButton jButton13; private javax.swing.JButton jButton14; private javax.swing.JButton jButton15; private javax.swing.JButton jButton16; private javax.swing.JButton jButton17; private javax.swing.JButton jButton18; private javax.swing.JButton jButton19; private javax.swing.JButton jButton2; private javax.swing.JButton jButton20; private javax.swing.JButton jButton21; private javax.swing.JButton jButton22; private javax.swing.JButton jButton23; private javax.swing.JButton jButton24; private javax.swing.JButton jButton25; private javax.swing.JButton jButton26; private javax.swing.JButton jButton27; private javax.swing.JButton jButton28; private javax.swing.JButton jButton29; private javax.swing.JButton jButton3; private javax.swing.JButton jButton30; private javax.swing.JButton jButton31; private javax.swing.JButton jButton32; private javax.swing.JButton jButton33; private javax.swing.JButton jButton34; private javax.swing.JButton jButton35; private javax.swing.JButton jButton36; private javax.swing.JButton jButton37; private javax.swing.JButton jButton38; private javax.swing.JButton jButton39; private javax.swing.JButton jButton4; private javax.swing.JButton jButton40; private javax.swing.JButton jButton41; private javax.swing.JButton jButton42; private javax.swing.JButton jButton43; private javax.swing.JButton jButton44; private javax.swing.JButton jButton45; private javax.swing.JButton jButton46; private javax.swing.JButton jButton47; private javax.swing.JButton jButton48; private javax.swing.JButton jButton49; private javax.swing.JButton jButton5; private javax.swing.JButton jButton50; private javax.swing.JButton jButton51; private javax.swing.JButton jButton52; private javax.swing.JButton jButton53; private javax.swing.JButton jButton54; private javax.swing.JButton jButton55; private javax.swing.JButton jButton56; private javax.swing.JButton jButton57; private javax.swing.JButton jButton58; private javax.swing.JButton jButton59; private javax.swing.JButton jButton6; private javax.swing.JButton jButton60; private javax.swing.JButton jButton61; private javax.swing.JButton jButton62; private javax.swing.JButton jButton63; private javax.swing.JButton jButton64; private javax.swing.JButton jButton7; private javax.swing.JButton jButton8; private javax.swing.JButton jButton9; private javax.swing.JPanel plateau; private javax.swing.JButton restart; // End of variables declaration //ce qui se passe quand bot joue public void lanceBot(){ if (monJeu.majCaseJouable()){ //bot cherche et joue monJeu.botJoue(); monJeu.tourSuivant(); monJeu.majMsgTour(); monJeu.majMsgScore(); } } public void freine(){ try {Thread.sleep(2000); } catch(InterruptedException e) {} } public void razPlateau(){ //On efface toutes les images du dernier affichage for (Component monBouton : plateau.getComponents()) { if (monBouton instanceof AbstractButton) { ((AbstractButton)monBouton).setIcon(new javax.swing.ImageIcon(getClass().getResource(""))); } } } public void plateau(int typeAffichage){ int i=-1; razPlateau(); Vector <Case> monDamier=this.monJeu.getDamier(); //affichage bot if(typeAffichage==SIMPLE){ for (Component monBouton : plateau.getComponents()) { if (monBouton instanceof AbstractButton) { i++; if(monDamier.get(i).valeur==NOIR){ ((AbstractButton)monBouton).setIcon(new javax.swing.ImageIcon(getClass().getResource("/othello/img/noir.gif"))); } else if(monDamier.get(i).valeur==BLANC){ ((AbstractButton)monBouton).setIcon(new javax.swing.ImageIcon(getClass().getResource("/othello/img/blanc.gif"))); } } } } //affichage humain else if(typeAffichage==AIDE){ for (Component monBouton : plateau.getComponents()) { if (monBouton instanceof AbstractButton) { i++; if(monDamier.get(i).valeur==NOIR){ ((AbstractButton)monBouton).setIcon(new javax.swing.ImageIcon(getClass().getResource("/othello/img/noir.gif"))); } else if(monDamier.get(i).valeur==BLANC){ ((AbstractButton)monBouton).setIcon(new javax.swing.ImageIcon(getClass().getResource("/othello/img/blanc.gif"))); } else if(monDamier.get(i).valeur==VIDE){ monJeu.majVerdict(i); if(monJeu.verdict[8]==1){ ((AbstractButton)monBouton).setIcon(new javax.swing.ImageIcon(getClass().getResource("/othello/img/greenDot.gif"))); } } } } } afficheTour.setText(monJeu.getMsgTour()); afficheScore.setText(monJeu.getMsgScore()); } public void affichePlateau(int typeAffichage){ restart.setVisible(false); //Si jeu finit façon normale, modifie les variables d'affichage if(monJeu.checkFin()){ restart.setVisible(true); plateau(typeAffichage); } //Sinon,vérifier que jeu n'est pas bloqué pour joueur courant else if(monJeu.checkBloque()){ System.out.println("******************JOUEUR "+monJeu.getTour()+"BLOQUE*********************"); //si c le cas, on passe la main au suivant et maj les affichages monJeu.majCurJoueurBloque(); System.out.println("********************JOUEUR "+monJeu.getTour()+"PREND LA MAIN*********************"); plateau(typeAffichage); //la valeur de tour a changé, il faut la récupérer //monJeu.tour=monJeu.getTour(); // On teste si second joueur pas bloqué if(monJeu.checkBloque()){ System.out.println("*******************JOUEUR "+monJeu.getTour()+"BLOQUE AUSSI !***********************"); //Si deux joueurs bloqués, fin bloquée, on attribue les cases vides au gagnant et maj variables affichage monJeu.majBloque(); restart.setVisible(true); plateau(typeAffichage); } //Si second joueur joue et est blanc else if(monJeu.getTour()==BLANC){ System.out.println("JOUEUR BLANC JOUE"); lanceBot(); //si c'est fini if(monJeu.checkFin()){ restart.setVisible(true); plateau(typeAffichage); } //sinon blanc joue else{ System.out.println("JOUEUR BLANC JOUE"); plateau(typeAffichage); //si noir est bloqué à ce stade if(monJeu.checkBloque()){ System.out.println("JOUEUR NOIR EST BLOQUE"); monJeu.majBloque(); restart.setVisible(true); plateau(typeAffichage); } } } //si second joueur joue et est noir else{ System.out.println("JOUEUR NOIR JOUE"); monJeu.majMsgTour(); monJeu.majMsgScore(); plateau(typeAffichage); } } //si tour normal blanc else if (monJeu.getTour()==BLANC){ lanceBot(); System.out.println("JOUEUR BLANC A JOUE"); //si c'est fini if(monJeu.checkFin()){ restart.setVisible(true); plateau(typeAffichage); } //sinon else{ System.out.println("JOUEUR NOIR JOUE"); plateau(typeAffichage); } } //si tour normal noir else{ monJeu.majMsgTour(); monJeu.majMsgScore(); plateau(typeAffichage); } } public void brancheActionListener(){ for (Component monBouton : plateau.getComponents()) { if (monBouton instanceof AbstractButton) { ((AbstractButton)monBouton).addActionListener(this); } } } //Ce qui se passe à chaque clic (ie humain joue) public void actionPerformed (ActionEvent evt){ Object monTruc=evt.getSource(); int monIndex=-1; for (Component monBouton : plateau.getComponents()) { monIndex++; if (monBouton instanceof AbstractButton) { if (monTruc.equals(monBouton)){ //ne permettre le clic que sur case vide if(monJeu.damier.get(monIndex).valeur==VIDE){ //1) affecter le tableau verdict monJeu.majVerdict(monIndex); //2A) Si pose interdite => bip if (monJeu.verdict[8]==-1){ java.awt.Toolkit.getDefaultToolkit().beep(); //et on affiche l'aide (toutes les cases accessibles) this.affichePlateau(AIDE); } //2B) Si pose permise, mise à jour else{ monJeu.majDamier(monIndex); monJeu.tourSuivant(); //rebouger après vers simple this.affichePlateau(AIDE); } } //si clic sur case possédant déjà un pion => bip else{ java.awt.Toolkit.getDefaultToolkit().beep(); } } } } } }
Partager