-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLifeGamePanel.cs
88 lines (78 loc) · 2.59 KB
/
LifeGamePanel.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LifeGame
{
public partial class LifeGamePanel : Control
{
public LifeGamePanel()
{
InitializeComponent();
_rowCount = 32;
_columnCount = 32;
RenderPanels();
}
public LifeGamePanel(int h, int w)
{
InitializeComponent();
_rowCount = h;
_columnCount = w;
RenderPanels();
}
private int _rowCount = 16, _columnCount = 16;
public int RowCount
{
get => _rowCount;
set
{
if (_rowCount == value || value < 0 || 32 < value) return;
_rowCount = value;
RenderPanels();
}
}
public int ColumnCount
{
get => _columnCount;
set
{
if (_columnCount == value || value < 0 || 32 < value) return;
_columnCount = value;
RenderPanels();
}
}
private CellPanel[][] cells;
private void RenderPanels()
{
if(cells != null) foreach (var row in cells) foreach (var cell in row) if (!cell.IsDisposed) cell.Dispose();
this.Size = new Size(19 * RowCount + 1, 19 * ColumnCount + 1);
cells = new CellPanel[RowCount][];
for (int i = 0; i < RowCount; i++)
{
cells[i] = new CellPanel[ColumnCount];
for (int k = 0; k < ColumnCount; k++)
{
cells[i][k] = new CellPanel() { Parent = this, Location = new Point(19 * i, 19 * k) };
cells[i][k].PanelClicked += new EventHandler(OnPanelStateChanged);
}
}
}
public bool AllWhite { get => cells.All(_ => _.All(__ => !__.IsAlive)); }
public bool this[int rowIndex, int columnIndex]
{
get => cells[rowIndex][columnIndex].IsAlive;
set { cells[rowIndex][columnIndex].IsAlive = value; }
}
public event EventHandler PanelStateChanged;
public void OnPanelStateChanged(object sender, EventArgs e) => PanelStateChanged.Invoke(sender, e);
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
}
}