-
Notifications
You must be signed in to change notification settings - Fork 1
/
life.cpp
251 lines (228 loc) · 5.38 KB
/
life.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
/***********
Travis Sanders
Project 4: Conway's Game of Life
February 2014
Compile using: g++ -D_GLIBCXX_USE_NANOSLEEP -std=c++0x life.cpp -o life
************/
#include <iostream>
#include <cstdlib>
#include <thread>
#include <chrono>
#include <limits.h>
#include <string>
#include <sstream>
#include <time.h>
using std::cin;
using std::cout;
using std::endl;
using std::this_thread::sleep_for;
using std::chrono::milliseconds;
int width = 80;
int height = 22;
int gamesize = 1760;
int game[1760];
int game_plusplus[1760];
void rand_array();
void print_game();
void overpop();
void rando();
void grow();
void evolve();
void evolve(int seconds);
int neighbors(int cell);
int cell_check(int cell);
int verify_range(int cell);
/* Randomizes every cell in the array. Currently obsolete.
Use rando(). */
void rand_array() {
for (int i=0; i<gamesize; i++) {
game[i] = rand() % 2;
}
}
/* Clears environment. */
void clear() {
for (int i=0; i<gamesize; i++) {
game[i] = 0;
}
}
/* Floods environment. Test scenario. */
void overpop() {
for (int i=0; i<gamesize; i++) {
game[i] = 1;
}
}
/* Prints the current environment. */
void print_game() {
int i=0;
while (i<gamesize) {
for (int j=0; j<width; j++) {
if (game[i] == 0)
cout << " ";
else cout << "*";
i++;
}
cout << endl;
}
}
/* Debug output. Prints how many neighbors each cell has. */
void print_neighbors() {
int i=0;
while (i<gamesize) {
for (int j=0; j<width; j++) {
cout << neighbors(i);
i++;
}
cout << endl;
}
}
/* Calculates one step of environment growth. */
void grow() {
for (int i=0; i<gamesize; i++) {
game_plusplus[i] = cell_check(i);
}
for (int i=0; i<gamesize; i++) {
game[i] = game_plusplus[i];
}
}
/* Determines the next state of a single cell. */
int cell_check(int cell) {
int near = neighbors(cell);
if (game[cell] == 0) {
if (near == 3) {
return 1;
} else return 0;
}
if (game[cell] == 1) {
if (near < 2 || near > 3) {
return 0;
} else return 1;
}
return 0;
}
/* Returns number of neighbors for given cell. */
int neighbors(int cell) {
int neighbors = 0;
int length = width;
if (game[verify_range(cell - 1)] == 1) neighbors++;
if (game[verify_range(cell + 1)] == 1) neighbors++;
if (game[verify_range(cell - length)] == 1) neighbors++;
if (game[verify_range(cell - length - 1)] == 1) neighbors++;
if (game[verify_range(cell - length + 1)] == 1) neighbors++;
if (game[verify_range(cell + length + 1)] == 1) neighbors++;
if (game[verify_range(cell + length - 1)] == 1) neighbors++;
if (game[verify_range(cell + length)] == 1) neighbors++;
return neighbors;
}
/* Verifies that a cell is within the environment.
Used in wrap-around effect. */
int verify_range(int cell) {
if (cell < 0) {
return cell = gamesize + cell;
} else if (cell > gamesize-1) {
return cell = cell - gamesize;
} else return cell;
}
/* Simulates Game of Life indefinitely. */
void evolve() {
int seconds = INT_MAX;
cout << "EVOLVING INDEFINITELY!" << endl;
for (int i=0; i<seconds; i++) {
grow();
print_game();
std::this_thread::sleep_for (std::chrono::milliseconds(100));
}
}
/* Simulates Game of Life for given number of seconds. */
void evolve(int seconds) {
seconds = seconds * 10;
for (int i=0; i<seconds; i++) {
grow();
print_game();
std::this_thread::sleep_for (std::chrono::milliseconds(100));
}
}
/* Randomly fills a given percent of the environment. */
void rando(int percent) {
int stop_at = (gamesize * percent) / 100;
for (int i = 0; i<stop_at; i++) {
game[rand() % gamesize] = 1;
}
}
/* Creates a pulsar structure in the center of the environment. */
void pulsar(int mod2) {
game[758+mod2] = 1;
game[759+mod2] = 1;
game[761+mod2] = 1;
game[762+mod2] = 1;
game[837+mod2] = 1;
game[840+mod2] = 1;
game[843+mod2] = 1;
game[918+mod2] = 1;
game[919+mod2] = 1;
game[921+mod2] = 1;
game[922+mod2] = 1;
}
/* Creates a glider gun. */
void make_gliders() {
int glider_gun[36]={481,482,561,562,491,571,651,412,732,333,
813,334,814,575,416,736,497,577,657,578,341,342,421,422,
501,502,263,583,185,265,585,665,355,356,435,436};
for (int i=0; i<36; i++) {
game[glider_gun[i]] = 1;
}
}
/* Gets a user-input int. */
int get_int() {
std::stringstream ss;
std::string input;
std::getline(std::cin, input);
ss << input;
int return_me;
if (ss >> return_me) {
return return_me;
} else {
std::cout << "FAILURE. Invalid input. Please enter an int: ";
return get_int();
}
}
int main() {
srand(time(NULL));
bool run = true;
rando(50);
while (run) {
int selection;
cout << "Conway's Game of Life." << endl
<< "Enter 1 for pulsars, 2 for a glider gun, or 3 for randomization." << endl
<< "Enter any other number to continue the previous pattern."
<< endl << "Press 0 to exit. Input: ";
selection = get_int();
if (selection == 0) {
run = false;
}
if (selection == 1) {
clear();
pulsar(0);
}
if (selection == 2) {
cout << "WARNING: Glider gun will self destruct in approx. 10 seconds." << endl;
clear();
make_gliders();
}
if (selection == 3) {
clear();
cout << "What percent of the board should be covered?: ";
rando(get_int());
}
if (run) {
cout << "How many seconds should the program run for?" << endl
<< "(Game runs at 10 frames per second.)" << endl
<< "Enter 0 to run indefinitely (not recommended): ";
int seconds = get_int();
if (seconds == 0) {
evolve();
} else evolve(seconds);
print_neighbors();
}
}
return 0;
}