-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLife_Game.cpp
257 lines (185 loc) · 4.82 KB
/
Life_Game.cpp
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
#include <random>
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <array>
#include <chrono>
#include <thread>
using namespace std;
class universe
{
private:
universe() = delete;
public:
enum class seed
{
random,
ten_cell_row,
small_explorer,
explorer
};
public:
universe(size_t const width, size_t const height) :
rows(height), cols(width), grid(width* height), dist(0, 4)
{
auto seed_data = array<int, std::mt19937::state_size>{};
std::random_device rd;
generate(seed_data.begin(), seed_data.end(), std::ref(rd));
std::seed_seq seq(seed_data.begin(), seed_data.end());
mt.seed(seq);
}
void run(seed const s, int const generations, std::chrono::milliseconds const ms = std::chrono::milliseconds(100))
{
reset();
initialize(s);
display();
int i = 0;
do
{
next_generation();
display();
using namespace std::chrono_literals;
std::this_thread::sleep_for(ms);
} while (i++ < generations || generations == 0);
}
private:
int count_alive() { return 0; }
template<typename T1, typename... T>
auto count_alive(T1 s, T... st) { return s + count_alive(st...); }
void next_generation()
{
vector<unsigned char> newgrid(grid.size());
for (size_t r = 0; r < rows; ++r)
{
for (size_t c = 0; c < cols; ++c)
{
auto count = count_neighbors(r, c);
if (cell(c, r) == alive)
{
newgrid[r * cols + c] = (count == 2 || count == 3) ? alive:dead;
}
else
{
newgrid[r * cols + c] = (count == 3) ? alive : dead;
}
}
}
grid.swap(newgrid);
}
void reset_display()
{
#ifdef _WIN32
system("cls");
#endif
}
void display()
{
reset_display();
for (size_t r = 0; r < rows; ++r)
{
for (size_t c = 0; c < cols; ++c)
{
cout << (cell(c, r) ? '*' : ' ');
}
cout << endl;
}
}
void initialize(seed const s)
{
if (s == seed::small_explorer)
{
auto mr = rows / 2;
auto mc = cols / 2;
cell(mc, mr) = alive;
cell(mc - 1, mr + 1) = alive;
cell(mc, mr + 1) = alive;
cell(mc + 1, mr + 1) = alive;
cell(mc - 1, mr + 2) = alive;
cell(mc + 1, mr + 2) = alive;
cell(mc, mr + 3) = alive;
}
else if (s == seed::explorer)
{
auto mr = rows / 2;
auto mc = cols / 2;
cell(mc - 2, mr - 2) = alive;
cell(mc, mr - 2) = alive;
cell(mc + 2, mr - 2) = alive;
cell(mc - 2, mr - 1) = alive;
cell(mc + 2, mr - 1) = alive;
cell(mc - 2, mr) = alive;
cell(mc + 2, mr) = alive;
cell(mc - 2, mr + 1) = alive;
cell(mc + 2, mr + 1) = alive;
cell(mc - 2, mr + 2) = alive;
cell(mc, mr - 2) = alive;
cell(mc + 2, mr + 2) = alive;
}
else if (s == seed::ten_cell_row)
{
for (size_t c = cols / 2 - 5; c < cols / 2 + 5; c++)
{
cell(c, rows / 2) = alive;
}
}
else
{
for (size_t r = 0; r < rows; ++r)
{
for (size_t c = 0; c < cols; ++c)
{
cell(c, r) = dist(mt) == 0 ? alive : dead;
}
}
}
}
void reset()
{
for (size_t r = 0; r < rows; ++r)
{
for (size_t c = 0; c < cols; ++c)
{
cell(c, r) = dead;
}
}
}
int count_neighbors(size_t const row, size_t const col)
{
if (row == 0 && col == 0)
return count_alive(cell(1, 0), cell(1, 1), cell(0, 1));
if (row == 0 && col == cols - 1)
return count_alive(cell(cols - 2, 0), cell(cols - 2, 1), cell(cols - 1, 1));
if (row == rows - 1 && col == 0)
return count_alive(cell(0, rows - 2), cell(1, rows - 2), cell(1, rows - 1));
if (row == rows - 1 && col == cols - 1)
return count_alive(cell(cols - 1, rows - 2), cell(cols - 2, rows - 2), cell(cols - 2, rows - 1));
if (row == 0 && col > 0 && col < cols - 1)
return count_alive(cell(col - 1, 0), cell(col - 1, 1), cell(col, 1), cell(col + 1, 1), cell(col + 1, 0));
if (row == rows - 1 && col > 0 && col < cols - 1)
return count_alive(cell(col - 1, row), cell(col - 1, row - 1), cell(col, row - 1), cell(col + 1, row - 1), cell(col + 1, row));
if (col == 0 && row > 0 && row < rows - 1)
return count_alive(cell(0, row - 1), cell(1, row - 1), cell(1, row), cell(1, row + 1), cell(0, row + 1));
if (col == cols - 1 && row > 0 && row < rows - 1)
return count_alive(cell(col, row - 1), cell(col - 1, row - 1), cell(col - 1, row), cell(col - 1, row + 1), cell(col, row + 1));
return count_alive(cell(col - 1, row - 1), cell(col, row - 1), cell(col + 1, row - 1), cell(col + 1, row), cell(col + 1, row + 1), cell(col, row + 1), cell(col - 1, row + 1), cell(col - 1, row));
}
unsigned char& cell(size_t const col, size_t const row)
{
return grid[row * cols + col];
}
private:
size_t rows;
size_t cols;
vector<unsigned char> grid;
const unsigned char alive = 1;
const unsigned char dead = 0;
std::uniform_int_distribution<> dist;
std::mt19937 mt;
};
int main()
{
using namespace std::chrono_literals;
universe u(50, 20);
u.run(universe::seed::random, 100, 100ms);
}