-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
202 lines (179 loc) · 6.16 KB
/
MainWindow.xaml.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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
namespace GameOfLife
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ObservableCollection<Cell> allCells = new ObservableCollection<Cell>();
const int HEIGHT = 15;
const int WIDTH = 15;
const int VISIBLE_CELL_Y = 50;
const int VISIBLE_CELL_X = 50;
const int CANON_PLANE_WIDTH = 36;
const int CANON_PLANE_HEIGHT = 9;
int fromX = 0;
int fromY = 0;
List<int> neighborToBorn = new List<int>() { 3 };
List<int> neighborToStayAlive = new List<int>() { 2, 3 };
Timer timer;
public MainWindow()
{
InitializeComponent();
itemsControl.ItemsSource = allCells;
//itemsControl.Height = HEIGHT * VISIBLE_CELL_Y;
//itemsControl.Width = WIDTH * VISIBLE_CELL_X;
// Initialize the list with the default cells
for (int y = 0; y < VISIBLE_CELL_Y; y++)
{
for (int x = 0; x < VISIBLE_CELL_X; x++)
{
allCells.Add(new Cell()
{
X = x,
Y = y,
IsAlive = false,
IsEffectiveNow = true
});
}
}
DataContext = new { Rows = VISIBLE_CELL_X, Columns = VISIBLE_CELL_Y };
}
private Cell GetCell(double ix, double iy)
{
int x = (int)Math.Floor(ix);
int y = (int)Math.Floor(iy);
Cell cell = allCells.FirstOrDefault(c => c.X == x && c.Y == y);
return cell ?? new Cell() { X = x, Y = y, IsAlive = false, IsEffectiveNow = true };
}
private void ToggleAlive(object sender, MouseButtonEventArgs e)
{
if (timer == default)
{
if (sender is Rectangle clickedRect)
{
if (clickedRect.DataContext is Cell cell)
{
cell.IsAlive = !cell.IsAlive;
}
}
}
}
private void CheckAlive(object state)
{
foreach (Cell cell in allCells)
cell.IsEffectiveNow = true;
foreach (Cell cell in allCells)
{
bool alive = IsAlive(cell.X, cell.Y);
if (alive != cell.IsAlive)
{
cell.IsEffectiveNow = false;
cell.IsAlive = alive;
}
}
}
private bool IsAlive(int ix, int iy)
{
int nbAliveCells = 0;
for (int dy = -1; dy <= 1; dy++)
{
for (int dx = -1; dx <= 1; dx++)
{
if (dx == 0 && dy == 0) continue;
Cell neighbor = GetCell(ix + dx, iy + dy);
nbAliveCells += neighbor.IsEffectiveNow ? neighbor.IsAlive ? 1 : 0 : !neighbor.IsAlive ? 1 : 0;
}
}
return GetCell(ix, iy).IsAlive ? neighborToStayAlive.Contains(nbAliveCells) : neighborToBorn.Contains(nbAliveCells);
}
private void BtnStart_Click(object sender, RoutedEventArgs e)
{
if (timer != default)
{
timer.Dispose();
timer = default;
btnStart.Content = "Start";
}
else
{
timer = new Timer(CheckAlive, default, 0, 500);
btnStart.Content = "Stop";
}
}
private void BtnCanonPlane_Click(object sender, RoutedEventArgs e)
{
if (timer != default) return;
foreach (Cell cell in allCells)
cell.IsAlive = false;
for (int y = fromY + 1; y <= fromY + CANON_PLANE_HEIGHT; y++)
{
for (int x = fromX + 1; x <= fromX + CANON_PLANE_WIDTH; x++)
{
Cell cell = GetCell(x, y);
cell.IsAlive = IsCanonPlaneCellAlive(x, y);
}
}
}
private bool IsCanonPlaneCellAlive(int x, int y)
{
if (y == 1 && x == 23)
{
return true;
}
else if (y == 2 && (x == 22 || x == 24))
{
return true;
}
else if (y == 3 && (x == 12 || x == 13 || x == 20 || x == 21 || x == 25 || x == 35 || x == 36))
{
return true;
}
else if (y == 4 && (x == 11 || x == 13 || x == 18 || x == 19 || x == 21 || x == 25 || x == 35 || x == 36))
{
return true;
}
else if (y == 5 && (x == 1 || x == 2 || x == 10 || x == 11 || x == 12 || x == 17 || x == 18 || x == 19 || x == 21 || x == 25))
{
return true;
}
else if (y == 6 && (x == 1 || x == 2 || x == 9 || x == 10 || x == 11 || x == 16 || x == 19 || x == 20 || x == 22 || x == 24))
{
return true;
}
else if (y == 7 && (x == 10 || x == 11 || x == 12 || x == 17 || x == 18 || x == 23))
{
return true;
}
else if (y == 8 && (x == 11 || x == 13))
{
return true;
}
else if (y == 9 && (x == 12 || x == 13))
{
return true;
}
return false;
}
private void DisplayCoordinate(object sender, MouseEventArgs e)
{
if (sender is Rectangle clickedRect)
{
if (clickedRect.DataContext is Cell cell)
{
string state = cell.IsAlive ? "alive" : "dead";
tblCoordinates.Text = $"[{cell.X}, {cell.Y}] is {state}";
}
}
}
}
}