-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameNamedLife.cpp
More file actions
213 lines (181 loc) · 4.95 KB
/
gameNamedLife.cpp
File metadata and controls
213 lines (181 loc) · 4.95 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
#include <iostream>
#include <ctime>
#define WORLD_HEIGHT 5
#define WORLD_WIDTH 5
//!!! заменить на delay
#ifdef _WIN32
#include <windows.h>
#define msleep(x) Sleep(x)
#else
#include <unistd.h>
#define msleep(x) usleep(x * 1000)
#endif
//!!!
using namespace std;
struct point {
bool isLive : 1;
};
// генерация первой жизни в мире
void setFirstLife(point world[][WORLD_HEIGHT])
{
for (int i = 0; i < WORLD_WIDTH; i++)
{
for (int j = 0; j < WORLD_HEIGHT; j++)
{
int num = rand() % 26;
if (num % 2 == 0)
{
world[i][j].isLive = 1;
}
else
{
world[i][j].isLive = 0;
}
}
}
}
// вывод на дисплей
void printWorld(point world[][WORLD_HEIGHT])
{
for (int i = 0; i < WORLD_WIDTH; i++)
{
for (int j = 0; j < WORLD_HEIGHT; j++)
{
if (world[i][j].isLive == 1)
{
cout << '*';
}
else
{
cout << ' ';
}
cout << ' ';
}
cout << endl;
}
cout << "поколение" << endl;
}
// количество живых клеток в мире
int countLiveCells(point world[][WORLD_HEIGHT])
{
int count = 0;
for (int i = 0; i < WORLD_WIDTH; i++)
{
for (int j = 0; j < WORLD_HEIGHT; j++)
{
if (world[i][j].isLive == 1)
{
count++;
}
}
}
return count;
}
// получение координат соседей клетки
void neighborsCoordinates(int neighbor[][2], int x, int y)
{
int k = 0;
for (int i = x - 1; i <= x + 1; i++)
{
for (int j = y - 1; j <= y + 1; j++)
{
if (i != x && j != y)
{
neighbor[k][0] = i;
neighbor[k][1] = j;
k++;
}
}
}
}
// количество живых соседей у клетки
int countLiveNeighbors(point world[][WORLD_HEIGHT], int x, int y)
{
int count = 0;
int neighbor[8][2];
int new_x, new_y;
neighborsCoordinates(neighbor, x, y);
for (int i = 0; i < 8; i++)
{
new_x = neighbor[i][0];
new_y = neighbor[i][1];
if ((new_x >= 0 && new_y >= 0) && (new_x < WORLD_WIDTH && new_y < WORLD_HEIGHT) && (world[new_x][new_y].isLive == 1))
{
count++;
}
}
return count;
}
// генерация следующего поколения
void nextGeneration(point world[][WORLD_HEIGHT], point previousWorld[][WORLD_HEIGHT])
{
int liveNeighbor;
for (int i = 0; i < WORLD_WIDTH; i++)
{
for (int j = 0; j < WORLD_HEIGHT; j++)
{
liveNeighbor = countLiveNeighbors(previousWorld, i, j);
if ((previousWorld[i][j].isLive == 0) && (liveNeighbor == 3))
{
world[i][j].isLive = 1;
}
else if (liveNeighbor < 2 || liveNeighbor > 3)
{
world[i][j].isLive = 0;
}
}
}
}
// копирование нынешнего поколения для сравнения со следующим
void copyingWorld(point thisWorld[][WORLD_HEIGHT], point copyWorld[][WORLD_HEIGHT])
{
for (int i = 0; i < WORLD_WIDTH; i++)
{
for (int j = 0; j < WORLD_HEIGHT; j++)
{
copyWorld[i][j] = thisWorld[i][j];
}
}
}
// сравнение текущего и предыдущего поколения
int comparingWorlds(point world[][WORLD_HEIGHT], point previousWorld[][WORLD_HEIGHT])
{
for (int i = 0; i < WORLD_WIDTH; i++)
{
for (int j = 0; j < WORLD_HEIGHT; j++)
{
if (world[i][j].isLive != previousWorld[i][j].isLive)
{
return -1;
}
}
}
return 0;
}
int main()
{
setlocale(LC_ALL, "Russian");
srand(time(NULL));
point world[WORLD_WIDTH][WORLD_HEIGHT];
point previousWorld[WORLD_WIDTH][WORLD_HEIGHT];
setFirstLife(world);
int liveCells = -1;
bool isOptimalConfig = 0;
do {
printWorld(world);
copyingWorld(world, previousWorld);
nextGeneration(world, previousWorld);
isOptimalConfig = comparingWorlds(world, previousWorld) == 0;
liveCells = countLiveCells(world);
if (isOptimalConfig)
{
cout << endl << "Сложилась стабильная конфигурация" << endl;
}
else if (liveCells == 0)
{
cout << endl << "Все клетки умерли" << endl;
}
msleep(1000);
} while (liveCells != 0 && !isOptimalConfig);
return 0;
}