-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChessBoard.cs
402 lines (356 loc) · 10.7 KB
/
ChessBoard.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CaroLAN_v2
{
public class ChessBoard
{
public Panel CHESSBOARD;
public Form2 form2;
public List<List<Button>> matrix;
public bool isHostGame;
public bool isTurnForMe;
public SocketManager SOCKET;
public const int ChieuNgangBanCo = 28;
public const int ChieuCaoBanCo = 25;
public const int SoQuanDeThang = 5;
private event EventHandler boardChecked;
public event EventHandler BoardChecked
{
add
{
boardChecked += value;
}
remove
{
boardChecked -= value;
}
}
private event EventHandler game_over;
public event EventHandler Game_over
{
add
{
game_over += value;
}
remove
{
game_over -= value;
}
}
public ChessBoard(Panel chessboard, Form2 thisform2, bool isHostGame)
{
this.CHESSBOARD = chessboard;
this.form2 = thisform2;
this.isHostGame = isHostGame;
this.isTurnForMe = isHostGame;
this.SOCKET = form2.SOCKET;
}
#region Method
public void DrawChessBoard()
{
CHESSBOARD.Controls.Clear();
CHESSBOARD.Enabled = true;
matrix = new List<List<Button>>();
for (int i = 0; i < ChieuNgangBanCo; i++) //28 hàng ngang
{
matrix.Add(new List<Button>());
for (int j = 0; j < ChieuCaoBanCo; j++)
{
Button btn = new Button()
{
Height = 25,
Width = 25,
Location = new Point(i * 25, j * 25),
Tag = i.ToString()
};
CHESSBOARD.Controls.Add(btn);
matrix[i].Add(btn);
btn.Click += Btn_Click;
}
}
//if (!isTurnForMe)
//{
// CHESSBOARD.Enabled = false;
//}
}
private void Btn_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (btn.BackgroundImage == null && isTurnForMe)
{
Point point = GetPotion(btn);
if (isHostGame)
{
btn.BackgroundImage = Resource1.X;
btn.ImageKey = "X";
}
else
{
btn.BackgroundImage = Resource1.O;
btn.ImageKey = "O";
}
ChangeTurn();
SocketData Mark_data = new SocketData((int)SocketCommand.SEND_POINT, point);
SOCKET.Send(Mark_data);
boardChecked?.Invoke(this, new EventArgs());
if (isEndGame(btn))
{
EndGame();
}
}
else
{
return;
}
}
#endregion
void ChangeTurn()
{
isTurnForMe = !isTurnForMe;
//CHESSBOARD.Enabled = isTurnForMe;
if(isTurnForMe)
{
form2.pictureBox_MainTurn.Image = Resource1.haveTurn;
form2.pictureBox_GuestTurn.Image = null;
form2.label_TurnFor.Text = "Your turn !";
}
else
{
form2.pictureBox_GuestTurn.Image = Resource1.haveTurn;
form2.pictureBox_MainTurn.Image = null;
form2.label_TurnFor.Text = "Opponent turn !";
}
}
public Point GetPotion(Button button)
{
Point point = new Point();
point.X = Convert.ToInt32(button.Tag.ToString());
point.Y = matrix[point.X].IndexOf(button);
return point;
}
public void GuestMark(Point point)
{
if (isHostGame) //kiểm tra xem mình là gì? Host là X; => Gúet O
{
matrix[point.X][point.Y].BackgroundImage = Resource1.O;
matrix[point.X][point.Y].ImageKey = "O";
}
else
{
matrix[point.X][point.Y].BackgroundImage = Resource1.X;
matrix[point.X][point.Y].ImageKey = "X";
}
form2.ActiveControl = matrix[point.X][point.Y];
ChangeTurn();
boardChecked?.Invoke(this, new EventArgs());
if (isEndGame(matrix[point.X][point.Y]))
{
EndGame();
}
}
public void EndGame()
{
//if(isTurnForMe)
//{
// if(MessageBox.Show("You looseeeeeeeee !!!","",MessageBoxButtons.OK) == DialogResult.OK)
// {
// //NewGame();
// }
//}
//else
//{
// if(MessageBox.Show("You Winnnnnnnnnnn !!!","",MessageBoxButtons.OK) == DialogResult.OK)
// {
// //NewGame();
// }
//}
game_over?.Invoke(this, new EventArgs());
}
public void NewGame()
{
form2.Reset_ProgressBar();
DrawChessBoard();
}
#region CheckWinLose
bool isEndGame(Button btn)
{
return isEndHorizontal(btn) || isEndPrimaryDiagonal(btn) ||
isEndVertical(btn) || isEndSubDiagonal(btn);
}
bool isEndHorizontal(Button btn)
{
Point point = GetPotion(btn);
int countL = 0;
int countR = 0;
//[0,X]
int temp;
if (point.X > 0)
{
temp = point.X - 1;
while (matrix[temp][point.Y].ImageKey == btn.ImageKey)
{
temp--;
countL++;
if (temp < 0)
break;
}
}
if (point.X < ChieuNgangBanCo - 1)
{
temp = point.X + 1;
while (matrix[temp][point.Y].ImageKey == btn.ImageKey)
{
temp++;
countR++;
if (temp >=ChieuNgangBanCo)
break;
}
}
if(countL + countR >= SoQuanDeThang -1)
{
return true;
}
else
{
return false;
}
}
bool isEndVertical(Button btn)
{
Point point = GetPotion(btn);
int countU = 0;
int countD = 0;
//[0,Y]
int temp;
if (point.Y > 0)
{
temp = point.Y - 1;
while (matrix[point.X][temp].ImageKey == btn.ImageKey)
{
temp--;
countU++;
if (temp < 0)
break;
}
}
//down:
if (point.Y < ChieuCaoBanCo - 1)
{
temp = point.Y + 1;
while (matrix[point.X][temp].ImageKey == btn.ImageKey)
{
temp++;
countD++;
if (temp >= ChieuCaoBanCo)
break;
}
}
if (countU + countD >= (SoQuanDeThang - 1)) //Chua tinh cai point
{
return true;
}
else
{
return false;
}
}
bool isEndPrimaryDiagonal(Button btn)
{
Point point = GetPotion(btn);
int toadoX = point.X;
int toadoY = point.Y;
//Len:
int countLen = 0;
int tempX = toadoX - 1;
int tempY = toadoY - 1;
if (toadoX > 0 && toadoY > 0)
{
while (matrix[tempX][tempY].ImageKey == btn.ImageKey)
{
countLen++;
tempX--;
tempY--;
if (tempX < 0 || tempY < 0)
{
break;
}
}
}
//Xuong:
int countXuong = 0;
tempX = toadoX + 1;
tempY = toadoY + 1;
if (toadoX <ChieuNgangBanCo - 1 && toadoY < ChieuCaoBanCo - 1)
{
while (matrix[tempX][tempY].ImageKey == btn.ImageKey)
{
countXuong++;
tempX++;
tempY++;
if (tempX >= ChieuNgangBanCo || tempY >= ChieuCaoBanCo)
{
break;
}
}
}
if (countXuong + countLen >= SoQuanDeThang - 1)
{
return true;
}
else
return false;
}
bool isEndSubDiagonal(Button btn)
{
Point point = GetPotion(btn);
int toadoX = point.X;
int toadoY = point.Y;
//Len:
int tempX = toadoX + 1;
int tempY = toadoY - 1;
int countLen = 0;
if (toadoX < ChieuNgangBanCo - 1 && toadoY > 0)
{
while (matrix[tempX][tempY].ImageKey == btn.ImageKey)
{
countLen++;
tempX++;
tempY--;
if (tempX >= ChieuNgangBanCo || tempY < 0)
{
break;
}
}
}
//Xuong:
tempX = toadoX - 1;
tempY = toadoY + 1;
int countXuong = 0;
if (toadoX > 0 && toadoY < ChieuCaoBanCo - 1)
{
while (matrix[tempX][tempY].ImageKey == btn.ImageKey)
{
countXuong++;
tempX--;
tempY++;
if (tempX < 0 || tempY >= ChieuCaoBanCo)
{
break;
}
}
}
if (countLen + countXuong >= SoQuanDeThang - 1)
{
return true;
}
else
return false;
}
#endregion
}
}