-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIA.java
More file actions
435 lines (377 loc) · 13.4 KB
/
IA.java
File metadata and controls
435 lines (377 loc) · 13.4 KB
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
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
/**
* L'IA du jeu, elle contient des methodes utilisées par le calcul
* du meilleure coup qu'elle peut jouer.
*
* @author Jean Guibert, Romain Bressan, Thomas Hennequin-Parey
*/
public class IA
{
/**
* La moteur du jeu. Contient un thread qui deplace les billes
* et des infos sur le coup précédent.
*/
Moteur m;
/**
* Table de jeu qui contient les Billes, c'est une copie de la table que l'utilisateur vois.
* C'est sur cette table que l'IA fait ses calcule pour trouver le meilleure coup.
*/
public Table tableIA;
/**
* Abscisse de la queue de l'IA.
*/
public float xSourisIA;
/**
* Ordonnée de la queue de l'IA.
*/
public float ySourisIA;
/**
* Force de l'IA.
*/
public int forceIA;
/**
* Nombre aléatoire qui est utilisé pour gérer la niveau de difficulté de l'IA.
*/
public Random rand = new Random();
/**
* Construit une <code>IA</code>.
*
* @param m
* Le moteur du jeu.
*/
public IA (Moteur m)
{
this.m = m;
this.tableIA = new Table(true, null);
this.tableIA.m.calIA = true;
}
/**
* Calcule toutes les possibilité de tire autour de la bille blanche
* et détermine laquelle sera la meilleure.
*/
public void calculMeilleureCoup()
{
int scoreMax = -2000,
scoreTMP,
force = Moteur.FORCE_MAX,
time,
lvl;
if (Moteur.OPT_lvlIA == 1 && !m.firstHit)
lvl = 5;
else if (Moteur.OPT_lvlIA == 2 && !m.firstHit)
lvl = 2;
else
lvl = 1;
this.tableIA.initBillesIA(m.t);
/*verifie que la bille blanche n'est pas dans un trou*/
verification ();
while(force > 150)
{
/*test toutes les trajectoires autour de la bille blanche*/
for (int i = ((int)m.t.blancheBille.x - 10), c = ((int)m.t.blancheBille.x + 10); i < c; ++i) /*tous les x posibles*/
{
for (int a = -Bille.RAYON, b = Bille.RAYON; a <= b; a += (Bille.RAYON * 2)) /*-10 pour le haut & +10 pour le bas*/
{
time = 0;
this.tableIA.initBillesIA(m.t);
/*trajectoire de la bille blanche*/
tableIA.m.bougerBilleBlanche(i,m.t.blancheBille.y + a, force);
/*Boucle du mouvement des billes*/
while(tableIA.m.bIsMoving() && time < 2000)
{
movingIA();
time++;
}
/*Evaluation du resultat obtenue*/
scoreTMP = evaluation();
/*si le resultat est mieux d'avant, ce resultat devient le meilleur coup*/
if (scoreMax < scoreTMP)
{
scoreMax = scoreTMP;
xSourisIA = i + rand.nextInt(lvl);
ySourisIA = m.t.blancheBille.y + a;
forceIA = force;
}
/*reviens a la position initiale càd remet les toutes les billes dans la position qu'elle avait avant le calcule*/
reset();
}
}
for (int i = ((int)m.t.blancheBille.y - 10), c = ((int)m.t.blancheBille.y + 10); i < c; ++i)/*tous les y posibles*/
{
for (int a = -Bille.RAYON, b = Bille.RAYON; a <= b; a += (Bille.RAYON * 2))/*-10 pour le gauche & +10 pour le droite*/
{
time = 0;
this.tableIA.initBillesIA(m.t);
/*trajectoire de la bille blanche*/
tableIA.m.bougerBilleBlanche(m.t.blancheBille.x + a, i, force);
/*Boucle du mouvement des billes*/
while(tableIA.m.bIsMoving() && time < 2000)
{
movingIA();
time++;
}
/*Evaluation du resultat obtenue*/
scoreTMP = evaluation();
/*si le resultat est mieux d'avant, ce resultat devient le meilleur coup*/
if (scoreMax < scoreTMP)
{
scoreMax = scoreTMP;
xSourisIA = m.t.blancheBille.x + a;
ySourisIA = i + rand.nextInt(lvl);
forceIA = force;
}
/*reviens a la position initiale càd remet les toutes les billes dans la position qu'elle avait avant le calcule*/
reset();
}
}
if (lvl == 1) force -= 70;
else force -= 100;
}
}
/**
* Calcul la valeur d'un coup.
*
* @return la valeur obtenue
*/
public int evaluation()
{
if (m.eq.c.equals(Color.RED))
return (evalCoupRouge() + rand.nextInt(10));
else if (m.eq.c.equals(Color.YELLOW))
return (evalCoupJaune() + rand.nextInt(10));
else if (m.eq.c.equals(Color.WHITE))
return (evalCoupBlanc() + rand.nextInt(10));
else if (m.eq.c.equals(Color.BLACK) && m.t.noireBille.dansPoche && Moteur.OPT_EmpocheBlancheApresNoire == 1)
return (evalCoupRentrerBlanche() + rand.nextInt(10));
else if (m.t.eqA.c.equals(Color.BLACK) && m.t.eqB.c.equals(Color.BLACK) && m.t.noireBille.dansPoche && Moteur.OPT_EmpocheBlancheApresNoire == 2)
return (evalCoupRentrerBlanche() + rand.nextInt(10));
else
return (evalCoupNoir() + rand.nextInt(10));
}
/**
* Calcul la valeur d'un coup si le joueur doit rentrer les billes rouges.
*
* @return la valeur obtenue.
*/
public int evalCoupRouge()
{
int score = 0;
if (tableIA.noireBille.dansPoche && !m.t.noireBille.dansPoche)
score -= 1000;
if (tableIA.blancheBille.dansPoche)
score -= 300;
for (int j = 0, e = tableIA.rougeBille.length; j < e; ++j)
if (tableIA.rougeBille[j].dansPoche && !m.t.rougeBille[j].dansPoche)
score += 30;
for (int j = 0, e = tableIA.jauneBille.length; j < e; ++j)
if (tableIA.jauneBille[j].dansPoche && !m.t.jauneBille[j].dansPoche)
score -= 40;
if (tableIA.m.firstBTouche == null || !tableIA.m.firstBTouche.couleur.equals(Color.RED))
score -= 50;
return score;
}
/**
* Calcul la valeur d'un coup si le joueur doit rentrer les billes jaunes.
*
* @return la valeur obtenue.
*/
public int evalCoupJaune()
{
int score = 0;
if (tableIA.noireBille.dansPoche && !m.t.noireBille.dansPoche)
score -= 1000;
if (tableIA.blancheBille.dansPoche)
score -= 300;
for (int j = 0, e = tableIA.rougeBille.length; j < e; ++j)
if (tableIA.rougeBille[j].dansPoche && !m.t.rougeBille[j].dansPoche)
score -= 40;
for (int j = 0, e = tableIA.jauneBille.length; j < e; ++j)
if (tableIA.jauneBille[j].dansPoche && !m.t.jauneBille[j].dansPoche)
score += 30;
if (tableIA.m.firstBTouche == null || !tableIA.m.firstBTouche.couleur.equals(Color.YELLOW))
score -= 50;
return score;
}
/**
* Calcul la valeur d'un coup si le joueur doit rentrer les billes rouges ou les billes jaunes.
*
* @return la valeur obtenue.
*/
public int evalCoupBlanc()
{
int score = 0;
Color c = getColFirstBDansPoch();
if (c != null)
{
if (tableIA.noireBille.dansPoche && !m.t.noireBille.dansPoche)
score -= 1000;
if (tableIA.blancheBille.dansPoche)
score -= 300;
for (int j = 0, e = tableIA.rougeBille.length; j < e; ++j)
if (tableIA.rougeBille[j].dansPoche && !m.t.rougeBille[j].dansPoche)
{
if (c.equals(Color.RED))
score += 20;
else
score -= 50;
}
for (int j = 0, e = tableIA.jauneBille.length; j < e; ++j)
if (tableIA.jauneBille[j].dansPoche && !m.t.jauneBille[j].dansPoche)
{
if (c.equals(Color.YELLOW))
score += 20;
else
score -= 50;
}
if (tableIA.m.firstBTouche == null)
score -= 100;
return score;
}
else
return score;
}
/**
* Calcul la valeur d'un coup si le joueur doit rentrer la bille noire
*
* @return la valeur obtenue.
*/
public int evalCoupNoir()
{
int score = 0;
if (tableIA.noireBille.dansPoche && !m.t.noireBille.dansPoche && m.nbRebond >= Moteur.OPT_nbRebBilleNoire)
score += 30;
if (tableIA.noireBille.dansPoche && !m.t.noireBille.dansPoche && m.nbRebond < Moteur.OPT_nbRebBilleNoire)
score -= 300;
if (tableIA.blancheBille.dansPoche)
score -= 300;
for (int j = 0, e = tableIA.rougeBille.length; j < e; ++j)
if (tableIA.rougeBille[j].dansPoche && !m.t.rougeBille[j].dansPoche)
score -= 300;
for (int j = 0, e = tableIA.jauneBille.length; j < e; ++j)
if (tableIA.jauneBille[j].dansPoche && !m.t.jauneBille[j].dansPoche)
score -= 300;
if (tableIA.m.firstBTouche == null || !tableIA.m.firstBTouche.couleur.equals(Color.BLACK))
score -= 300;
return score;
}
/**
* Calcul la valeur d'un coup si le joueur doit rentrer la bille blanche
*
* @return la valeur obtenue.
*/
public int evalCoupRentrerBlanche()
{
int score = 0;
if (tableIA.blancheBille.dansPoche && !m.t.blancheBille.dansPoche && m.nbRebond >= Moteur.OPT_nbRebBilleBlanche)
score += 30;
if (tableIA.blancheBille.dansPoche && !m.t.blancheBille.dansPoche && m.nbRebond < Moteur.OPT_nbRebBilleBlanche)
score -= 3000;
for (int j = 0, e = tableIA.rougeBille.length; j < e; ++j)
if (tableIA.rougeBille[j].dansPoche && !m.t.rougeBille[j].dansPoche)
score -= 3000;
for (int j = 0, e = tableIA.jauneBille.length; j < e; ++j)
if (tableIA.jauneBille[j].dansPoche && !m.t.jauneBille[j].dansPoche)
score -= 3000;
if (tableIA.m.firstBTouche == null)
score -= 300;
return score;
}
/**
* Pour avoir la couleur de la 1ere bille la liste
* des billes empochées.
* @return la couleur de la bille.
*/
public Color getColFirstBDansPoch ()
{
Bille b;
while (!tableIA.m.listBDansPoch.isEmpty())
{
b = tableIA.m.listBDansPoch.remove();
return b.couleur;
}
return null;
}
/**
* Verifi que la bille blanche n'est pas dans un trou.
* Si c'est le cas elle la place de manière aléatoire sur la table.
*/
public void verification ()
{
float xD, yD,
xG, yG;
if (m.bBlancDansPoch)
{
do
{
if (m.t.queue.coteTriangleCasse == 2)
{
xD = (float)( (int)(Math.random() * ((Table.ORI_X_T + Bille.RAYON) + 1
- (Table.WIDTH/4) ) )
+ (Table.WIDTH/4) );
yD = (float)( (int)(Math.random() * ((Table.ORI_Y_T + Bille.RAYON) + 1
- (Table.ORI_Y_T + Table.HEIGHT_T - Bille.RAYON) ) )
+ (Table.ORI_Y_T + Table.HEIGHT_T - Bille.RAYON) );
m.t.blancheBille.x = xD;
m.t.blancheBille.y = yD;
m.bBlancDansPoch = false;
m.t.blancheBille.dansPoche = false;
tableIA.blancheBille.x = xD;
tableIA.blancheBille.y = yD;
tableIA.m.bBlancDansPoch = false;
tableIA.blancheBille.dansPoche = false;
}
else
{
xG = (float)( (int)(Math.random() * (((Table.WIDTH/4)*3) + 1
- (Table.ORI_X_T+ Table.WIDTH_T - Bille.RAYON) ) )
+ (Table.ORI_X_T+ Table.WIDTH_T - Bille.RAYON) );
yG = (float)( (int)(Math.random() * ((Table.ORI_Y_T + Bille.RAYON) + 1
- (Table.ORI_Y_T + Table.HEIGHT_T - Bille.RAYON) ) )
+ (Table.ORI_Y_T + Table.HEIGHT_T - Bille.RAYON) );
m.t.blancheBille.x = xG;
m.t.blancheBille.y = yG;
m.bBlancDansPoch = false;
m.t.blancheBille.dansPoche = false;
tableIA.blancheBille.x = xG;
tableIA.blancheBille.y = yG;
tableIA.m.bBlancDansPoch = false;
tableIA.blancheBille.dansPoche = false;
}
}while(!m.t.verifCollision(m.t.blancheBille.x, m.t.blancheBille.y));
}
else
return;
}
/**
* Permet de réinitialiser toutes les billes
*/
public void reset ()
{
m.firstBTouche = null;
tableIA.blancheBille.initB(m.t.blancheBille);
tableIA.noireBille.initB(m.t.noireBille);
for (int j = 0, d = m.t.rougeBille.length; j < d; ++j)
tableIA.rougeBille[j].initB(m.t.rougeBille[j]);
for (int j = 0, d = m.t.jauneBille.length; j < d; ++j)
tableIA.jauneBille[j].initB(m.t.jauneBille[j]);
tableIA.m.listBDansPoch.clear();
}
/**
* Fait bouger les billes d'un "pixel"
*/
public void movingIA ()
{
for (int j = 0, d = tableIA.rougeBille.length; j < d; ++j)
if (tableIA.rougeBille[j].isMoving())
tableIA.rougeBille[j].bouleTraj.deplace();
for (int j = 0, d = tableIA.jauneBille.length; j < d; ++j)
if (tableIA.jauneBille[j].isMoving())
tableIA.jauneBille[j].bouleTraj.deplace();
if (tableIA.blancheBille.isMoving())
tableIA.blancheBille.bouleTraj.deplace();
if (tableIA.noireBille.isMoving())
tableIA.noireBille.bouleTraj.deplace();
}
}