-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
228 lines (220 loc) · 6.5 KB
/
Source.cpp
File metadata and controls
228 lines (220 loc) · 6.5 KB
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
#include <vector>
#include <Windows.h>
#include <list>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <random>
#include <bitset>
using namespace std;
enum TypeCell
{
env,
alive
};
struct Cell
{
TypeCell type;
friend ostream& operator<<(ostream& out, const Cell& cell)
{
if (cell.type == env) out << '.';
else if (cell.type == alive) out << '#';
return out;
}
};
struct Field1D
{
int n = 0;
vector<Cell> cells;
Field1D(int n) :n(n), cells(vector<Cell>(n)) {}
int getNum(int pos, TypeCell type = alive, int radius = 1) const
{
int count = 0;
for (int i = pos - radius; i <= pos + radius; i++)
if (cells[(i + n) % n].type == type)
count++;
return count;
}
Cell& operator[](int i) { return cells[i]; }
Cell operator[](int i) const { return cells[i]; } // const âàðèàíò äëÿ cout
friend ostream& operator<<(ostream& out, const Field1D& field)
{
for (int i = 0; i < field.n; i++)
out << field[i];
return out;
}
};
struct Field2D
{
int n = 0;
int m = 0;
vector<Field1D> cells;
Field2D() {}
Field2D(int n, int m) : n(n), m(m), cells(vector<Field1D>(n, Field1D(m))) {}
int getNum(int posX, int posY, TypeCell type = alive, int radius = 1) const
{
int count = 0;
for (int i = posX - radius; i <= posX + radius; i++)
count += cells[(i + n) % n].getNum(posY, type, radius);
return count;
}
Field1D& operator[](int i) { return cells[i]; }
Field1D operator[](int i) const { return cells[i]; }
friend ostream& operator<<(ostream& out, const Field2D& field)
{
for (int i = 0; i < field.n; i++)
out << field[i] << "\n";
return out;
}
};
struct Field3D
{
int n = 0;
int m = 0;
int k = 0;
vector<Field2D> cells;
Field3D() {}
Field3D(int n, int m, int k) : n(n), m(m), k(k), cells(vector<Field2D>(k, Field2D(n, m))) {}
int getNum(int posZ, int posX, int posY, TypeCell type = alive, int radius = 1) const
{
int count = 0;
for (int i = posZ - radius; i <= posZ + radius; i++)
{
count += cells[(i + k) % k].getNum(posX, posY, type, radius);
}
return count;
}
Field2D& operator[](int i) { return cells[i]; }
Field2D operator[](int i) const { return cells[i]; }
friend ostream& operator<<(ostream& out, const Field3D& field)
{
for (int i = 0; i < field.k; i++)
out << i << ":\n" << field[i] << "\n";
return out;
}
};
struct iGame
{
int n = 0;
int m = 0;
int k = 0;
int seed = 0; // ñëó÷àéíàÿ âåëè÷èíà äëÿ ãåíåðàòîðà
double probability = 0.0; // âåðîÿòíîñòü òîãî, ÷òî êëåòêà æèâàÿ
int dimension = 1; // ðàçìåðíîñòü
int radius = 1; // ðàäèóñ ïðîâåðêè, ãðàíèöà âêëþ÷åíà
int loneliness = 2; // ñ ýòîãî ÷èñëà è ìåíüøå êëåòêè óìèðàþò îò îäèíî÷åñòâà
int birth_start = 3; // ñ ýòîãî ÷èñëà è äî birth_end ïîÿâëÿåòñÿ æèâàÿ êëåòêà
int birth_end = 3;
int overpopulation = 5; // ñ ýòîãî ÷èñëà è äàëüøå êëåòêè ïîãèáàþò îò ïåðåíàñåëåíèÿ
virtual void runGame(int numIt) = 0;
};
struct Game2D : public iGame
{
Field2D field;
Field2D fieldNext;
Game2D() { dimension = 2; }
Game2D(int n, int m) {
this->n = n;
this->m = m;
dimension = 2;
field = fieldNext = Field2D(n, m);
}
void setGame(double p, int s = 0)
{
probability = p;
seed = s;
field = Field2D(n, m);
vector<int> tmp(n * m);
iota(tmp.begin(), tmp.end(), 0);
shuffle(tmp.begin(), tmp.end(), std::mt19937(seed));
for (int i = 0; i < (int)(p * n * m + 0.5); i++)
{
int x = tmp[i] / m;
int y = tmp[i] % m;
field[x][y].type = TypeCell::alive;
}
}
void runGame(int numIt) override
{
for (int it = 0; it < numIt; it++)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
int count = field.getNum(i, j);
fieldNext[i][j].type = field[i][j].type;
if (count <= loneliness || count >= overpopulation) fieldNext[i][j].type = TypeCell::env;
else if (count >= birth_start && count <= birth_end) fieldNext[i][j].type = TypeCell::alive;
}
}
field = fieldNext;
}
}
void startGame()
{
}
};
struct Game3D :public iGame
{
Field3D field;
Field3D fieldNext;
Game3D() { dimension = 3; }
Game3D(int n, int m, int k) {
this->n = n;
this->m = m;
this->k = k;
dimension = 3;
field = fieldNext = Field3D(n, m, k);
}
void setGame(double p, int s = 0)
{
probability = p;
seed = s;
for (int j = 0; j < k; j++)
{
vector<int> tmp(n * m);
iota(tmp.begin(), tmp.end(), 0);
shuffle(tmp.begin(), tmp.end(), std::mt19937(seed));
for (int i = 0; i < (int)(p * n * m + 0.5); i++)
{
int x = tmp[i] / m;
int y = tmp[i] % m;
field[j][x][y].type = TypeCell::alive;
}
seed++;
}
}
void runGame(int numIt) override
{
for (int it = 0; it < numIt; it++)
{
for (int i = 0; i < k; i++)
{
for (int j = 0; j < n; j++)
{
for (int t = 0; t < m; t++)
{
int count = field.getNum(i, j, t);
fieldNext[i][j][t].type = field[i][j][t].type;
if (count <= loneliness || count >= overpopulation) fieldNext[i][j][t].type = TypeCell::env;
else if (count >= birth_start && count <= birth_end) fieldNext[i][j][t].type = TypeCell::alive;
}
}
}
field = fieldNext;
}
}
void startGame()
{
}
};
int main()
{
Game3D game = Game3D(3, 3, 4);
game.setGame(0.35);
cout << "test probability. Start: \n" << game.field << "\n";
game.runGame(4);
cout << "test probability. End 4 it: \n" << game.field << "\n";
return 0;
}