-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatterns.cpp
More file actions
471 lines (461 loc) · 12.8 KB
/
patterns.cpp
File metadata and controls
471 lines (461 loc) · 12.8 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
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
#include <vector>
#include <Windows.h>
#include <list>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <random>
#include <bitset>
#include <locale.h>
#include <conio.h>
using namespace std;
enum GameEvent
{
FieldEmpty,
FullField
};
struct Observer
{
virtual void newEvent(GameEvent event) = 0;
};
struct Subject
{
vector<Observer*> obs;
void subscribe(Observer* tmp)
{
obs.push_back(tmp);
}
void update(GameEvent event)
{
for (auto sub : obs)
sub->newEvent(event);
}
};
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;
}
int getLive()
{
int count = 0;
for (int i = 0; i < n; i++)
{
if (cells[i].type == alive)
{
count++;
}
}
return count;
}
Cell& operator[](int i) { return cells[i]; }
Cell operator[](int i) const { return cells[i]; }
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;
}
int getLive()
{
int count = 0;
for (int i = 0; i < n; i++)
{
count += cells[i].getLive();
}
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;
}
int getLive()
{
int count = 0;
for (int i = 0; i < k; i++)
{
count += cells[i].getLive();
}
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:public Subject
{
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;
int birth_end = 3;
int overpopulation = 5;
virtual void runGame(int numIt) = 0;
virtual void setGame(double p, int s = 0) = 0;
virtual void print(ostream& out) const = 0;
friend ostream& operator<<(ostream& out, iGame& g)
{
g.print(out);
return out;
}
};
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;
int alliveCount = field.getLive();
if (alliveCount == 0)
update(FieldEmpty);
if (alliveCount == n*m)
update(FullField);
}
}
void print(ostream& out)const
{
out << field;
}
};
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;
loneliness = 5;
birth_start = 6;
birth_end = 9;
overpopulation = 10;
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 = (8253729 * seed + 2396403) % 32768;
}
}
void runGame(int numIt) override
{
for (int i = 0; i < k; i++)
{
for (int j = 0; j < n; j++)
{
for (int l = 0; l < m; l++)
{
fieldNext[i][j][l].type = TypeCell::env;
}
}
}
while (numIt)
{
for (int i = 0; i < k; i++)
{
for (int j = 0; j < n; j++)
{
for (int l = 0; l < m; l++)
{
int count = field.getNum(i, j, l);
if (count <= loneliness || count >= overpopulation) { fieldNext[i][j][l].type = TypeCell::env; }
else if (count >= birth_start && count <= birth_end) { fieldNext[i][j][l].type = TypeCell::alive; }
else fieldNext[i][j][l].type = field[i][j][l].type;
}
}
}
field = fieldNext;
int alliveCount = field.getLive();
if (alliveCount == 0)
update(FieldEmpty);
if (alliveCount == n * m * k)
update(FullField);
numIt--;
}
}
void print(ostream& out)const
{
out << field;
}
};
class view : public Observer
{
public:
void newEvent(GameEvent event)
{
if (event == FieldEmpty || event == FullField)
{
status = gameOver;
}
}
iGame* game = 0;
int game_dim = 0;
int button = 0;
enum statusgame
{
gameSetDim,
gameSetSize,
gameSetP,
gameReady,
gameRun,
gamePause,
gameOver,
};
statusgame status = gameSetDim;
void gotocoord(int x, int y = 0) {
COORD position = { y, x };
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsole, position);
}
view()
{
setlocale(LC_ALL, "Russian");
}
void start()
{
while (1)
{
if (status == gameSetDim)
{
system("cls");
cout << "âûáåðèòå ðàçìåðíîñòü èãðû, ïîæàëóéñòà\n" << "1\n" << "2\n" << "3\n";
int pos = 0;
gotocoord(pos + 1);
while (1)
{
if (_kbhit())
{
button = _getch();
if (button == 'w')
{
pos = (pos - 1 + 3) % 3;
gotocoord(pos + 1);
}
else if (button == 's')
{
pos = (pos + 1) % 3;
gotocoord(pos + 1);
}
else if (button == 13 && pos != 0)
{
game_dim = pos + 1;
gotocoord(4);
system("cls");
status = gameSetSize;
int size;
int count;
double p;
if (status == gameSetSize)
{
if (game_dim == 3)
{
cout << "âûáåðèòå êîëè÷åñòâî ïîëåé, ïîæàëóéñòà\n";
cin >> count;
cout << "âûáåðèòå ðàçìåð èãðû, ïîæàëóéñòà\n";
cin >> size;
}
else {
cout << "âûáåðèòå ðàçìåð èãðû, ïîæàëóéñòà\n";
cin >> size;
}
}
status = gameSetP;
if (status == gameSetP)
{
cout << "âûáåðèòå probability èãðû, ïîæàëóéñòà\n";
cin >> p;
}
if (game_dim == 2)
{
game = new Game2D(size, size);
game->setGame(0.34, p);
game->subscribe(this);
}
if (game_dim == 3)
{
game = new Game3D(size, size, count);
game->setGame(0.34, p);
game->subscribe(this);
}
break;
}
}
}
system("cls");
cout << *game;
status = gameReady;
}
else if (status == gamePause)
{
cout << "âû ïîñòàâèëè èãðó íà ïàóçó,âûáåðèòå l, ÷òîáû ïðîäîëæèòü èëè j, ÷òîáû íà÷àòü çàíîâî\n";
int c = _getch();
if (c == 'l')
status = gameRun;
if (c == 'j')
status = gameSetDim;
}
else if (status == gameReady)
{
status = gameRun;
}
else if (status == gameRun)
{
if (_kbhit() && (status != gamePause))
{
int c = _getch();
if (c == 'p')
{
system("cls");
status = gamePause;
}
}
system("cls");
game->runGame(1);
cout << *game;
Sleep(200);
}
else if (status == gameOver)
{
system("cls");
cout << "Íàæìèòå r ÷òîáû íà÷àòü èãðó çàíîâî èëè f ÷òîáû âûéòè\n";
int c = _getch();
if (c == 'r')
status = gameSetDim;
if (c == 'f')
break;
}
else
{
cout << "Unknown\n";
break;
}
}
}
};
int main()
{
view b;
b.start();
}