-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrmDemineurAI.cs
425 lines (401 loc) · 16.2 KB
/
frmDemineurAI.cs
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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Demineur
{
public partial class frmDemineurAI : Form
{
Cell[,] Grille;
int nbRecursion = 0;
List<Cell> lstCellToCheck = new List<Cell>();
frmSettings frmSettings;
bool IsFirstClick = true;
int DefSize = 8;
int nbBomb;
public frmDemineurAI(int size, frmSettings frm)
{
InitializeComponent();
frmSettings = frm;
DefSize = size;
nbBomb = DefSize * DefSize / 8;
InitialisationGrille(DefSize);
}
public void RemoveAllButtons(int size)
{
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
// Supprime le bouton du formulaire
this.Controls.Remove(Controls.Find(Grille[x, y].Btn.Name, true)[0]);
}
}
}
public async void InitialisationGrille(int size)
{
Grille = new Cell[DefSize, DefSize];
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
Grille[x, y] = new Cell(x, y, 30, this);
Grille[x, y].Btn.MouseDown += new MouseEventHandler(btnCell_Click);
}
}
// SetBomb(DefSize, nbBomb);
// SetNbBombAround(DefSize);
await Task.Delay(TimeSpan.FromMilliseconds(1000));
RandomMove();
}
public void SetBomb(int size, int nb, int xClicked, int yClicked)
{
Random rand = new Random();
int x, y;
do
{
x = rand.Next(0, size - 1);
y = rand.Next(0, size - 1);
// Si la case n'est pas déjà une bombe
if (!Grille[x, y].IsBomb)
{
// Si la case n'est pas la case cliquée
if (x != xClicked && y != yClicked)
{
// Si la case n'est pas une case autour de la case cliquée
if ((x != xClicked-1 && y != yClicked-1) &&
(x != xClicked && y != yClicked - 1) &&
(x != xClicked+1 && y != yClicked-1) &&
(x != xClicked-1 && y != yClicked) &&
(x != xClicked+1 && y != yClicked) &&
(x != xClicked-1 && y != yClicked+1) &&
(x != xClicked && y != yClicked+1) &&
(x != xClicked+1 && y != yClicked+1)) {
Grille[x, y].IsBomb = true;
nb--;
}
}
}
} while (nb != 0);
}
public void SetNbBombAround(int size)
{
int count = 0;
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
count = 0;
for (int i = -1; i < 2; i++)
{
for (int j = -1; j < 2; j++)
{
int xoff = x + i;
int yoff = y + j;
if (xoff > -1 && xoff < size && yoff > -1 && yoff < size)
{
if (Grille[xoff, yoff].IsBomb) count++;
}
Grille[x, y].NbBombsAroud = count;
}
}
}
}
}
public void ShowAllCells(int size)
{
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
Grille[x, y].Show(ilImages.Images[1]);
}
}
}
public void ShowPropagation(int x, int y, int size)
{
if (Grille[x, y].NbBombsAroud == 0)
{
Grille[x, y].Show(null);
for (int i = -1; i < 2; i++)
{
int xoff = x + i;
if (xoff < 0 || xoff >= size) continue;
for (int j = -1; j < 2; j++)
{
int yoff = y + j;
if (yoff < 0 || yoff >= size) continue;
if (!Grille[xoff, yoff].IsShowed)
{
ShowPropagation(xoff, yoff, size);
}
}
}
}
else
{
Grille[x, y].Show(null);
// Ajouter cette case dans la liste des cases à contrôler si celle-ci n'y est pas déjà
if (!lstCellToCheck.Contains(Grille[x, y])) lstCellToCheck.Add(Grille[x, y]);
}
}
public bool CheckWin(int size)
{
int countCellRevealed = 0;
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
if (Grille[x, y].IsShowed) countCellRevealed++;
}
}
if (countCellRevealed == (size * size) - nbBomb) return true;
return false;
}
public async void btnCell_Click(object sender, MouseEventArgs e)
{
nbRecursion++;
Button btn = (Button)sender;
MouseEventArgs me = (MouseEventArgs)e;
Cell cellClicked;
cellClicked = Grille[Convert.ToInt32(btn.Name.Split('.')[0]),
Convert.ToInt32(btn.Name.Split('.')[1])];
// Si c'est le premier coup
if (IsFirstClick)
{
IsFirstClick = false;
SetBomb(DefSize, nbBomb, cellClicked.X, cellClicked.Y);
SetNbBombAround(DefSize);
}
if (me.Button == MouseButtons.Right && !cellClicked.IsShowed)
{
if (cellClicked.Btn.Image != null)
{
cellClicked.Btn.Image = null;
cellClicked.IsFlagged = false;
}
else
{
cellClicked.Btn.Image = ilImages.Images[0];
cellClicked.IsFlagged = true;
}
}
else
{
if (cellClicked.IsBomb && cellClicked.Btn.Image == null)
{
DialogResult dr = new DialogResult();
ShowAllCells(DefSize);
dr = MessageBox.Show("Voulez-vous recommencer une partie ?", "Partie perdue", MessageBoxButtons.YesNo);
if (dr == DialogResult.No) Environment.Exit(0);
else if (dr == DialogResult.Yes)
{
// RemoveAllButtons(DefSize);
Grille = null;
this.Close();
frmDemineurAI f = new frmDemineurAI(DefSize, frmSettings);
f.Show();
await Task.Delay(TimeSpan.FromMilliseconds(100));
frmSettings.Hide();
return;
}
}
else if (cellClicked.Btn.Image == null)
{
cellClicked.Show(ilImages.Images[1]);
// Ajouter la case dans la liste de case à contrôler
lstCellToCheck.Add(cellClicked);
ShowPropagation(cellClicked.X, cellClicked.Y, DefSize);
}
}
if (CheckWin(DefSize))
{
DialogResult dr = new DialogResult();
ShowAllCells(DefSize);
dr = MessageBox.Show("Voulez-vous recommencer une partie ?", "Partie gagnée", MessageBoxButtons.YesNo);
if (dr == DialogResult.No) Environment.Exit(0);
else if (dr == DialogResult.Yes)
{
// RemoveAllButtons(DefSize);
Grille = null;
this.Close();
frmDemineurAI f = new frmDemineurAI(DefSize, frmSettings);
f.Show();
await Task.Delay(TimeSpan.FromMilliseconds(100));
frmSettings.Hide();
return;
}
}
// Intelligence artificielle
// Supprimer de la liste les cases à contrôler
// qui ont toutes leurs cases autour flaggées ou découvertes
bool isIaBlocked = true;
// Contrôler les 3 règles pour toutes les cases à contrôler
foreach (Cell c in lstCellToCheck)
{
// await Task.Delay(TimeSpan.FromMilliseconds(1000));
// Règle 1
// Si toutes les cases cachées (drapeaux non compris) sont des bombes
if (Regle1(c.X, c.Y))
{
isIaBlocked = false;
// Mettre un drapeau sur toutes ces cases
for (int i = -1; i < 2; i++)
{
int xoff = c.X + i;
if (xoff < 0 || xoff >= DefSize) continue;
for (int j = -1; j < 2; j++)
{
int yoff = c.Y + j;
if (yoff < 0 || yoff >= DefSize) continue;
// Si la case n'est pas découverte et n'est pas déjà un drapeau
if(!Grille[xoff, yoff].IsShowed && !Grille[xoff, yoff].IsFlagged)
{
System.Drawing.Point cellLocation = new System.Drawing.Point(Location.X + 20 + 30 * xoff, Location.Y + 50 + 30 * yoff);
await Task.Delay(TimeSpan.FromMilliseconds(50));
// Placer la souris sur le bouton
Cursor.Position = cellLocation;
Grille[xoff, yoff].Btn.Image = ilImages.Images[0];
Grille[xoff, yoff].IsFlagged = true;
}
}
}
}
}
foreach (Cell c in lstCellToCheck)
{
// Règle 2
// Si toutes les cases cachées autour ne sont pas des bombes
if (Regle2(c.X, c.Y))
{
// Découvrir toutes ces cases
for (int i = -1; i < 2; i++)
{
int xoff = c.X + i;
if (xoff < 0 || xoff >= DefSize) continue;
for (int j = -1; j < 2; j++)
{
int yoff = c.Y + j;
if (yoff < 0 || yoff >= DefSize) continue;
// Si la case n'est pas découverte et n'est pas déjà un drapeau
if (!Grille[xoff, yoff].IsShowed && !Grille[xoff, yoff].IsFlagged)
{
System.Drawing.Point cellLocation = new System.Drawing.Point(Location.X + 20 + 30 * xoff, Location.Y + 50 + 30 * yoff);
await Task.Delay(TimeSpan.FromMilliseconds(50));
// Placer la souris sur le bouton
Cursor.Position = cellLocation;
MouseEventArgs me1 = new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0);
btnCell_Click(Grille[xoff, yoff].Btn, me1);
nbRecursion--;
return;
}
}
}
}
}
nbRecursion--;
// MessageBox.Show(nbRecursion.ToString());
// Si l'IA ne peut plus rien faire, cliquer sur une case au hasard
//if (isIaBlocked) MessageBox.Show("IA bloquée");// RandomMove();
//isIaBlocked = false;
// Si le programme arrive ici et que c'est la racine des appels, c'est que l'IA est bloquée, il faut donc cliquer sur une case au hasard
//if (nbRecursion == 0) {
RandomMove();
// MessageBox.Show("IA bloquée");
//}
}
private void frmDemineur_FormClosed(object sender, FormClosedEventArgs e)
{
frmSettings.Show();
}
public async Task<Cell> RandomMove()
{
await Task.Delay(TimeSpan.FromMilliseconds(100));
Random rand = new Random();
int x, y;
do
{
// Générer une case aléatoire
x = rand.Next(0, DefSize - 1);
y = rand.Next(0, DefSize - 1);
} while (Grille[x, y].IsShowed || Grille[x, y].IsFlagged);
// Créer un point au coordonnées de la case
System.Drawing.Point cellLocation = new System.Drawing.Point(Location.X + 20 + 30 * x, Location.Y + 50 + 30 * y);
await Task.Delay(TimeSpan.FromMilliseconds(100));
// Placer la souris sur le bouton
Cursor.Position = cellLocation;
// Ajouter la case dans la liste de case à contrôler si celle-ci n'y est pas déjà
if (!lstCellToCheck.Contains(Grille[x, y])) lstCellToCheck.Add(Grille[x, y]);
// Simuler un clic de souris
MouseEventArgs me = new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0);
btnCell_Click(Grille[x, y].Btn, me);
if (Grille[x, y].IsBomb) return null;
return Grille[x,y]; // Coordonnées de la case
}
private void frmDemineurAI_FormClosed(object sender, FormClosedEventArgs e)
{
frmSettings.Show();
}
// 1 : Si une case a le même montant de case cachées autour d'elle
// que de bombes qui ne sont pas des drapeaux,
// alors toutes les cases cachées sont des bombes
public bool Regle1(int x, int y)
{
int nbHiddenCells = 0;
int nbBombToFindAround = Grille[x, y].NbBombsAroud;
for (int i = -1; i < 2; i++)
{
int xoff = x + i;
if (xoff < 0 || xoff >= DefSize) continue;
for (int j = -1; j < 2; j++)
{
int yoff = y + j;
if (yoff < 0 || yoff >= DefSize) continue;
if(!Grille[xoff, yoff].IsShowed && !Grille[xoff, yoff].IsFlagged)
{
nbHiddenCells++;
} else if(Grille[xoff, yoff].IsFlagged)
{
nbBombToFindAround--;
}
}
}
if(nbHiddenCells == nbBombToFindAround)
{
return true;
} else return false;
}
// 2 : Si une case a le même montant de drapeaux autour d'elle
// que son nombre de bombes,
// alors toutes les cases restantes autour d'elle ne sont pas des bombes
public bool Regle2(int x, int y)
{
int nbFlagsAround= 0;
int nbBomAround = Grille[x, y].NbBombsAroud;
for (int i = -1; i < 2; i++)
{
int xoff = x + i;
if (xoff < 0 || xoff >= DefSize) continue;
for (int j = -1; j < 2; j++)
{
int yoff = y + j;
if (yoff < 0 || yoff >= DefSize) continue;
if (Grille[xoff, yoff].IsFlagged)
{
nbFlagsAround++;
}
}
}
if (nbFlagsAround == Grille[x, y].NbBombsAroud)
{
return true;
}
else return false;
}
// 3 :
public bool Regle3()
{
return false;
}
}
}