forked from mitchgre/life
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameOfLife-noAnim.cpp
372 lines (304 loc) · 7.65 KB
/
gameOfLife-noAnim.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
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
/*
Author: Gregory Mitchell
Date Created: 11/20/14
Last Modified: 11/23/14
Filename: gameOfLife-week7.cpp
Overview: this is an static (not animated) implementation of Conway's
game of life.
-------------------------------------------------------*/
#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#define ROWS 22
#define COLS 80
using namespace std;
struct cell {
int x;
int y;
bool occupied;
};
// walk clockwise around the current cell to get 8 neighbors
struct neighbors {
cell topLeft;
cell topCenter;
cell topRight;
cell centerRight;
cell bottomRight;
cell bottomCenter;
cell bottomLeft;
cell centerLeft;
cell all[8];
};
int mod(int a, int b)
{
int r = a % b;
return r < 0 ? r + b : r;
}
cell getTopLeft(cell current)
{
cell block; // create a new cell
block.x = mod(current.x - 1, COLS);
block.y = mod(current.y - 1, ROWS);
return block;
}
cell getTopCenter(cell current)
{
cell block; // create a new cell
block.x = mod(current.x, COLS);
block.y = mod(current.y - 1, ROWS);
return block;
}
cell getTopRight(cell current)
{
cell block; // create a new cell
block.x = mod(current.x + 1, COLS);
block.y = mod(current.y - 1, ROWS);
return block;
}
cell getCenterRight(cell current)
{
cell block; // create a new cell
block.x = mod(current.x + 1, COLS);
block.y = mod(current.y, ROWS);
return block;
}
cell getBottomRight(cell current)
{
cell block; // create a new cell
block.x = mod(current.x + 1, COLS);
block.y = mod(current.y + 1, ROWS);
return block;
}
cell getBottomCenter(cell current)
{
cell block; // create a new cell
block.x = mod(current.x, COLS);
block.y = mod(current.y + 1, ROWS);
return block;
}
cell getBottomLeft(cell current)
{
cell block; // create a new cell
block.x = mod(current.x - 1, COLS);
block.y = mod(current.y + 1, ROWS);
return block;
}
cell getCenterLeft(cell current)
{
cell block; // create a new cell
block.x = mod(current.x - 1, COLS);
block.y = mod(current.y, ROWS);
return block;
}
// purpose: return a structure containing all neighboring cells
neighbors getNeighbors (cell current)
{
neighbors hood;
hood.topLeft = getTopLeft(current);
hood.topCenter = getTopCenter(current);
hood.topRight = getTopRight(current);
hood.centerRight = getCenterRight(current);
hood.bottomRight = getBottomRight(current);
hood.bottomCenter = getBottomCenter(current);
hood.bottomLeft = getBottomLeft(current);
hood.centerLeft = getCenterLeft(current);
// store everything in array for easy iteration.
hood.all[0] = hood.topLeft;
hood.all[1] = hood.topCenter;
hood.all[2] = hood.topRight;
hood.all[3] = hood.centerRight;
hood.all[4] = hood.bottomRight;
hood.all[5] = hood.bottomCenter;
hood.all[6] = hood.bottomLeft;
hood.all[7] = hood.centerLeft;
return hood;
}
// purpose: search the world array for current cell to determine if it is empty
bool isVacant(cell current, char (&world)[COLS][ROWS])
{
char occupied = world[current.x][current.y];
if (occupied == ' ')
return true;
else
return false;
}
// purpose: get number of occupied/vacant neighbors
int numberOfVacantNeighbors(cell current, char (&world)[COLS][ROWS])
{
int vacancies = 0;
neighbors hood = getNeighbors(current);
// determine vacancy for each neighbor
for (int i = 0; i < 8; i++)
{
if (isVacant(hood.all[i], world))
vacancies++;
}
return vacancies;
}
// purpose: see if occupied cell can survive loneliness
// "If an occupied cell has zero or one neighbor, it dies of loneliness."
bool loneliness (cell current, char (&world)[COLS][ROWS])
{
int vacancies = numberOfVacantNeighbors(current, world);
int occupancies = 8 - vacancies;
if (occupancies <= 1)
return true;
else
return false;
}
// purpose: see if occupied cell can survive overcrowding
// "If an occupied cell has more than three neighbors, it dies of overcrowding."
bool overcrowding (cell current, char (&world)[COLS][ROWS])
{
int vacancies = numberOfVacantNeighbors(current, world);
int occupancies = 8 - vacancies;
if (occupancies > 3)
return true;
else
return false;
}
// purpose: see if vacant cell can regenerate
// "If an empty cell has exactly three occupied neighbor cells,
// there is a birth of a new cell to replace the empty cell."
bool rebirth (cell current, char (&world)[COLS][ROWS])
{
int vacancies = numberOfVacantNeighbors(current, world);
int occupancies = 8 - vacancies;
if (occupancies == 3)
return true;
else
return false;
}
void processWorlds (char (&startGen)[COLS][ROWS], char (&nextGen)[COLS][ROWS])
{
// generate cells for the next world
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLS; j++)
{
cell me;
me.x = j;
me.y = i;
me.occupied = !isVacant(me, startGen);
if (me.occupied)
{
if (loneliness(me, startGen))
{
nextGen[j][i] = ' ';
// printf("killed nextGen[%d][%d] from loneliness\n",j,i);
}
else if (overcrowding(me, startGen))
{
nextGen[j][i] = ' ';
// printf("killed nextGen[%d][%d] from overcrowding\n",j,i);
}
else // not lonely or overcrowded so me live on
nextGen[j][i] = '.';
}
else // me am vacant
{
if (rebirth(me, startGen) )
{
nextGen[j][i] = '.';
// printf("birthed nextGen[%d][%d] from vacancy\n",j,i);
}
else
nextGen[j][i] = ' ';
}
}
}
void displayGen(char (&world)[COLS][ROWS])
{
// top border
for (int i = 0; i < COLS; i++)
printf("-");
printf("\n");
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
printf("%c",world[j][i]);
printf("\n");
}
// bottom border
for (int i = 0; i < COLS; i++)
printf("-");
printf("\n");
}
int main()
{
int generation = 0;
int limit;
char startGen[COLS][ROWS]; // current generation
char nextGen[COLS][ROWS]; // next generation
char* sgPtr = &(startGen[0][0]);
char* ngPtr = &(nextGen[0][0]);
cout << "Enter a generation limit: " << endl;
cin >> limit;
// define initial conditions somehow.
// wipe the startGen clean
for (int i = 0; i < ROWS*COLS; i++)
{
sgPtr[i] = ' ';
}
// simple blinker: (test left-right wrapping)
startGen[79][5] = '.';
startGen[0][5] = '.';
startGen[1][5] = '.';
// simple blinker (blows up block):
/*
startGen[78][10] = '.';
startGen[79][10] = '.';
startGen[0][10] = '.';
*/
// simple blinker (test top-bottom wrapping)
startGen[40][20] = '.';
startGen[40][21] = '.';
startGen[40][0] = '.';
// simple block:
startGen[1][10] = '.';
startGen[2][10] = '.';
startGen[1][11] = '.';
startGen[2][11] = '.';
printf("\ngeneration: %d\n",generation);
// display generation
displayGen(startGen);
generation++;
while (generation <= limit)
{
printf("\ngeneration: %d\n",generation);
// wipe the next generation clean
/*
for (int i = 0; i < ROWS*COLS; i++)
{
ngPtr[i] = ' ';
}
*/
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLS; j++)
nextGen[j][i] = ' ';
// repopulate the next generation
processWorlds(startGen, nextGen);
/*
// clear screen
for (int i = 0; i < ROWS+1; i++)
cout << endl;
*/
// display next generation cout << string(nextGen);
printf("\n");
displayGen(nextGen);
printf("\n");
// startGen = nextGen on the next pass
for (int i = 0; i < ROWS*COLS; i++)
{
sgPtr[i] = ngPtr[i];
}
/*
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLS; j++)
startGen[j][i] = nextGen[j][i];
*/
generation++;
}
printf("\n");
return 0;
}