-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGridView.cs
502 lines (466 loc) · 16.6 KB
/
GridView.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Numerics;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BombPlane
{
public partial class GridView : UserControl
{
public GridView()
{
InitializeComponent();
foreach (Control control in tableLayoutPanel.Controls)
control.MouseClick += new MouseEventHandler(ButtonMouseClickForSelect);
}
private Plane[]? _planes;
public Plane[] Planes
{
get { return _planes; }
set
{
if (value != null)
{
if (value.Length != NumOfPlane)
throw new ArgumentException("Too many planes");
foreach (Plane plane in value)
if (!CheckPlaneBounded(plane))
throw new ArgumentException("Invalid plane");
}
_planes = value;
}
}
private int _selectedPlaneIndex = -1;
private Plane? SelectedPlane
{
get
{
if (_selectedPlaneIndex == -1)
return null;
else
return Planes[_selectedPlaneIndex];
}
}
private Button? _selectedButton;
public int? SelectedButtonX
{
get
{
if (_selectedButton == null)
return null;
else
return tableLayoutPanel.GetColumn(_selectedButton);
}
}
public int? SelectedButtonY
{
get
{
if (_selectedButton == null)
return null;
else
return tableLayoutPanel.GetRow(_selectedButton);
}
}
public Point? SelectedButtonPoint
{
get
{
if (_selectedButton == null)
return null;
else
return new Point((int)SelectedButtonX, (int)SelectedButtonY);
}
}
private bool _planeVisible;
public bool IsPlaneVisible
{
get
{
return _planeVisible;
}
set
{
if (Planes != null)
ClearPlanes();
if (value)
{
DrawPlanes();
foreach (Control control in this.tableLayoutPanel.Controls)
{
control.MouseClick += new MouseEventHandler(ButtonMouseClickForSelectPlane);
control.KeyPress += new KeyPressEventHandler(ButtonKeyPressForMove);
}
}
else
{
foreach (Control control in this.tableLayoutPanel.Controls)
{
control.MouseClick -= new MouseEventHandler(ButtonMouseClickForSelectPlane);
control.KeyPress -= new KeyPressEventHandler(ButtonKeyPressForMove);
}
}
_planeVisible = value;
}
}
public const int NumOfPlane = 3;
public const int RowCount = 10;
public const int ColumnCount = 10;
private static HashSet<Point> _points;
public static HashSet<Point> Points
{
get
{
if (_points == null)
_points = new HashSet<Point>();
for (int i = 0; i < ColumnCount; i++)
for (int j = 0; j < RowCount; j++)
_points.Add(new Point(i, j));
return _points;
}
}
public static IEnumerable<Plane> BoundedPlanes
{
get
{
for (int x = Plane.WingLength; x < ColumnCount - Plane.WingLength; x++)
for (int y = 0; y <= RowCount - Plane.BodyLength; y++)
yield return new(Direction.Up, x, y);
for (int x = Plane.WingLength; x < ColumnCount - Plane.WingLength; x++)
for (int y = Plane.BodyLength - 1; y < RowCount; y++)
yield return new(Direction.Down, x, y);
for (int x = 0; x <= ColumnCount - Plane.BodyLength; x++)
for (int y = Plane.WingLength; y < RowCount - Plane.WingLength; y++)
yield return new(Direction.Left, x, y);
for (int x = Plane.BodyLength - 1; x < ColumnCount; x++)
for (int y = Plane.WingLength; y < RowCount - Plane.WingLength; y++)
yield return new(Direction.Right, x, y);
}
}
public static Point ConvertStringToPoint(string str)
{
int Y = str[0] - 'A';
int X = str[1] - '0';
return new Point(X, Y);
}
public static string ConvertPointToString(int X, int Y)
{
return new string(new char[2] { (char)(Y + 'A'), (char)(X + '0') });
}
public static string ConvertPointToString(Point point)
{
return ConvertPointToString(point.Y, point.X);
}
public Control GetControlFromPosition(int X, int Y)
{
return tableLayoutPanel.GetControlFromPosition(X, Y);
}
public Control GetControlFromPosition(Point point)
{
return tableLayoutPanel.GetControlFromPosition(point.X, point.Y);
}
public Point GetControlPoint(Control control)
{
int X = tableLayoutPanel.GetColumn(control);
int Y = tableLayoutPanel.GetRow(control);
return new Point(X, Y);
}
public BombResult BombAndDraw(int X, int Y)
{
return BombAndDraw(new Point(X, Y));
}
public BombResult BombAndDraw(Point point)
{
Button selectedButton = (Button)GetControlFromPosition(point);
if (selectedButton.BackColor != Color.Yellow)
{
selectedButton.BackColor = Color.Yellow;
foreach (Plane plane in Planes)
{
if (point.X == plane.HeadX && point.Y == plane.HeadY)
{
DrawBombResult(point, BombResult.head);
return BombResult.head;
}
else
{
foreach (Point planePoint in plane.Points)
{
if (point.X == planePoint.X && point.Y == planePoint.Y)
{
DrawBombResult(point, BombResult.body);
return BombResult.body;
}
}
}
}
DrawBombResult(point, BombResult.none);
return BombResult.none;
}
return BombResult.error;
}
public void DrawBombResult(Point point, BombResult result)
{
Button selectedButton = (Button)GetControlFromPosition(point);
if (result != BombResult.error)
{
selectedButton.BackColor = Color.Yellow;
switch (result)
{
case BombResult.head:
selectedButton.Text = "头";
selectedButton.ForeColor = Color.OrangeRed;
break;
case BombResult.body:
selectedButton.Text = "身";
selectedButton.ForeColor = Color.LimeGreen;
break;
case BombResult.none:
selectedButton.Text = "空";
break;
}
}
}
private void ButtonMouseClickForSelectPlane(object sender, MouseEventArgs e)
{
int x = tableLayoutPanel.GetColumn(_selectedButton);
int y = tableLayoutPanel.GetRow(_selectedButton);
Point point = new Point(x, y);
for (int i = 0; i < NumOfPlane; i++)
{
if (Planes[i].Points.Contains(point))
{
_selectedPlaneIndex = i;
return;
}
}
_selectedPlaneIndex = -1;
}
private void ButtonMouseClickForSelect(object sender, MouseEventArgs e)
{
_selectedButton = (Button)sender;
}
private void ButtonKeyPressForMove(object sender, KeyPressEventArgs e)
{
if (SelectedPlane != null)
{
switch (e.KeyChar)
{
case 'w':
MovePlane(SelectedPlane, Direction.Up);
break;
case 's':
MovePlane(SelectedPlane, Direction.Down);
break;
case 'a':
MovePlane(SelectedPlane, Direction.Left);
break;
case 'd':
MovePlane(SelectedPlane, Direction.Right);
break;
case 'r':
RotatePlane(SelectedPlane);
break;
}
}
}
private bool MovePlane(Plane plane, Direction direction)
{
bool valid = true;
Point origin = plane.HeadPoint;
ClearPlanes();
switch (direction)
{
case Direction.Up:
plane.HeadY -= 1;
break;
case Direction.Down:
plane.HeadY += 1;
break;
case Direction.Left:
plane.HeadX -= 1;
break;
case Direction.Right:
plane.HeadX += 1;
break;
}
if (!CheckPlaneBounded(plane))
{
plane.HeadPoint = origin;
valid = false;
}
DrawPlanes();
return valid;
}
private bool RotatePlane(Plane plane)
{
bool valid = true;
Direction origin = plane.direction;
ClearPlanes();
switch (plane.direction)
{
case Direction.Up:
plane.direction = Direction.Right;
break;
case Direction.Down:
plane.direction = Direction.Left;
break;
case Direction.Left:
plane.direction = Direction.Up;
break;
case Direction.Right:
plane.direction = Direction.Down;
break;
}
if (!CheckPlaneBounded(plane))
{
plane.direction = origin;
valid = false;
}
DrawPlanes();
return valid;
}
public static bool CheckPlaneBounded(Plane plane)
{
foreach (var point in plane.Points)
if (point.X < 0 || point.Y < 0 || point.X >= ColumnCount || point.Y >= RowCount)
return false;
return true;
}
public static bool CheckPlaneNotConflicted(Plane plane, Plane[] planes)
{
foreach (Plane otherPlane in planes)
if (!ReferenceEquals(otherPlane, plane) && plane.Conflict(otherPlane))
return false;
return true;
}
private bool CheckPlaneNotConflicted(Plane plane)
{
return CheckPlaneNotConflicted(plane, Planes);
}
public static bool CheckPlanesValid(Plane[] planes)
{
foreach (Plane plane in planes)
{
if (!CheckPlaneBounded(plane))
return false;
if (!CheckPlaneNotConflicted(plane, planes))
return false;
}
return true;
}
public bool CheckPlanesValid()
{
return CheckPlanesValid(Planes);
}
public void ClearGrids()
{
foreach (Control control in tableLayoutPanel.Controls)
{
control.BackColor = SystemColors.ControlDark;
control.ForeColor = SystemColors.ControlText;
int X = tableLayoutPanel.GetColumn(control);
int Y = tableLayoutPanel.GetRow(control);
string column = X.ToString();
string row = ((char)('A' + Y)).ToString();
control.Text = row + column;
}
_selectedButton = null;
}
private void DrawPlanes()
{
foreach (Plane plane in Planes)
{
foreach (var point in plane.Points)
{
Control control = GetControlFromPosition(point.X, point.Y);
if (control.BackColor != Color.Yellow)
{
if (control.BackColor == Color.LimeGreen)
control.BackColor = Color.DarkGreen;
else
control.BackColor = Color.LimeGreen;
}
}
}
foreach (Plane plane in Planes)
{
Control control = GetControlFromPosition(plane.HeadPoint);
if (control.BackColor != Color.Yellow)
{
if (control.BackColor == Color.LimeGreen)
control.BackColor = Color.OrangeRed;
else
control.BackColor = Color.DarkRed;
}
}
}
public void ClearPlanes()
{
foreach (Plane plane in Planes)
{
foreach (var point in plane.Points)
{
Control control = GetControlFromPosition(point.X, point.Y);
Color color = control.BackColor;
if (color == Color.LimeGreen || color == Color.DarkGreen || color == Color.OrangeRed)
control.BackColor = SystemColors.ControlDark;
}
}
}
public Point? GetBombPoint()
{
if (_selectedButton != null && _selectedButton.BackColor == Color.Yellow)
throw new DuplicatedSelectionException("The button have been selected.");
else
return SelectedButtonPoint;
}
public List<(Point, BombResult)> GetCurrentGridStates()
{
List<(Point, BombResult)> results = new();
foreach (Control control in tableLayoutPanel.Controls)
{
if (control.BackColor == Color.Yellow)
{
if (control.Text == "身")
{
Point point = GetControlPoint(control);
results.Add((point, BombResult.body));
}
else if (control.Text == "头")
{
Point point = GetControlPoint(control);
results.Add((point, BombResult.head));
}
}
}
return results;
}
public void InitializePlanes()
{
Random random = new();
_planes = new Plane[NumOfPlane] {
new Plane((Direction)random.Next(0, 3), random.Next(0, ColumnCount), random.Next(0, RowCount)),
new Plane((Direction)random.Next(0, 3), random.Next(0, ColumnCount), random.Next(0, RowCount)),
new Plane((Direction)random.Next(0, 3), random.Next(0, ColumnCount), random.Next(0, RowCount)),
};
int index = 0;
while (!CheckPlanesValid(_planes))
_planes[index++ % NumOfPlane] = new Plane((Direction)random.Next(0, 3), random.Next(0, ColumnCount), random.Next(0, RowCount));
if (IsPlaneVisible)
IsPlaneVisible = true;
}
}
public class DuplicatedSelectionException : Exception
{
public DuplicatedSelectionException(string? message) : base(message) { }
}
}