-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
224 lines (218 loc) · 7.8 KB
/
Form1.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
using System;
using System.Windows.Forms;
namespace Demineur
{
public partial class frmDemineur : Form
{
Cell[,] Grille;
frmSettings frmSettings;
int DefSize = 8;
bool IsFirstClicked = true;
int nbBomb;
public frmDemineur(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 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);
}
}
}
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);
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);
}
}
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 void btnCell_Click(object sender, MouseEventArgs e)
{
// create X,Y point (0,0) explicitly with System.Drawing
// System.Drawing.Point leftTop = new System.Drawing.Point(Location.X+20, Location.Y+50);
// this.Location;
// set mouse position
// Cursor.Position = leftTop;
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 (IsFirstClicked)
{
IsFirstClicked = 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;
}
else
{
cellClicked.Btn.Image = ilImages.Images[0];
}
}
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;
InitialisationGrille(DefSize);
IsFirstClicked = true;
}
}
else if(cellClicked.Btn.Image == null)
{
cellClicked.Show(ilImages.Images[1]);
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;
InitialisationGrille(DefSize);
IsFirstClicked = true;
}
return;
}
}
private void frmDemineur_FormClosed(object sender, FormClosedEventArgs e)
{
frmSettings.Show();
}
}
}