-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameoflife.cpp
More file actions
376 lines (320 loc) · 10.5 KB
/
Gameoflife.cpp
File metadata and controls
376 lines (320 loc) · 10.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
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
#include "Gameoflife.h"
#include <iostream>
#include <chrono> //chrono, thread and limits for pause
#include <thread>
#include <limits>
#include <cstdlib> //to use rand function
#include <ctime> //to initialize srand function to time(NULL)
using namespace std;
Originalgrid::Originalgrid ()
{
n = 0;
countofx = 1;
}
char Originalgrid::setOriginalgrid (string filename, char mode, int output, char type, int rows, int columns)
{
if (type == 'R' || type == 'r') { //randomly initializing grid
r = rows;
c = columns;
TotalArea = r*c;
srand (time(NULL));
density = rand()/(RAND_MAX+1.0);
NoofX = density*TotalArea;
a = r;
b = c;
ptrr = new int; //pointer of rows to be used in subsequent functions
*ptrr = r;
ptrc = new int; //pointer of columns to be used isubsequent functions
*ptrc = c;
pdimensions = new char*[a+2]; //pointer to create the original array
for (int i = 0; i < a+2; i++) {
for (int j = 0; j < b+2; j++) {
pdimensions[i] = new char[b+2];
}
}
r = 1;
c = 1;
for (int i = 0; i < a+2; i++) { //initializing the entire array with ''-'
for (int j = 0; j < b+2; j++) {
*(*(pdimensions + i) + j) = '-';
}
}
for (int i = 1; i < a+1; i++) { //inputting the no. of X
for (int j = 1; j < b+1; j++) {
if (NoofX != 0) {
*(*(pdimensions + i) + j) = 'X';
--NoofX;
}
}
}
} else if (type == 'F' || type == 'f') { //initializing grid using input from a file
Gameoflife.open (filename);
if (!Gameoflife) { //Exception if error in opening file
cout << "Unable to open the file. Please check the following and run the program again -" << endl;
cout << "1 - File is not open already" << endl;
cout << "2 - Check some other program is not using it" << endl;
cout << "3 - File name/path is correct" << endl;
exit(1);
}
getline (Gameoflife,s);
istringstream firstrow (s);
firstrow >> r;
getline (Gameoflife,s);
istringstream secondrow (s);
secondrow >> c;
a = r;
b = c;
ptrr = new int;
*ptrr = r;
ptrc = new int;
*ptrc = c;
pdimensions = new char*[a+2];
for (int i = 0; i < a+2; i++) {
for (int j = 0; j < b+2; j++) {
pdimensions[i] = new char[b+2];
}
}
r = 1;
c = 1;
for (int i = 0; i < a+2; i++) {
for (int j = 0; j < b+2; j++) {
*(*(pdimensions + i) + j) = '-';
}
}
while (Gameoflife >> cell) {
if (c == b) {
*(*(pdimensions + r) + c) = cell;
r++;
c = 1;
}
else {
*(*(pdimensions + r) + c) = cell;
c++;
}
}
}
if (mode == 'M' || mode == 'm') { //generating right neighbours for mirror mode
for (int i = 0; i < 1; i++) {
for (int j = 1; j < b+2 ; j++) {
*(*(pdimensions + i) + j) = *(*(pdimensions + i+1) + j);
}
}
for (int i = a+1; i < a+2 ; i++) {
for (int j = 1; j < b+2; j++) {
*(*(pdimensions + i) + j) = *(*(pdimensions + i-1) + j);
}
}
for (int i = 1; i < a+2; i++) {
for (int j = 0; j < 1; j++) {
*(*(pdimensions + i) + j) = *(*(pdimensions + i) + j+1);
}
}
for (int i = 1; i < a+2; i++) {
for (int j = b+1; j < b+2; j++) {
*(*(pdimensions + i) + j) = *(*(pdimensions + i) + j-1);
}
}
*(*(pdimensions + 0) + 0) = *(*(pdimensions + 1) + 1);
*(*(pdimensions + 0) + b+1) = *(*(pdimensions + 1) + b);
*(*(pdimensions + a+1) + 0) = *(*(pdimensions + a) + 1);
*(*(pdimensions + a+1) + b+1) = *(*(pdimensions + a) + b);
} else if (mode == 'D' or mode == 'd') {
for (int i = 0; i < 1; i++) {
for (int j = 1; j < b+2 ; j++) {
*(*(pdimensions + i) + j) = *(*(pdimensions + a) + j);
}
}
for (int i = a+1; i < a+2 ; i++) {
for (int j = 1; j < b+2; j++) {
*(*(pdimensions + i) + j) = *(*(pdimensions + 1) + j);
}
}
for (int i = 1; i < a+2; i++) {
for (int j = 0; j < 1; j++) {
*(*(pdimensions + i) + j) = *(*(pdimensions + i) + b);
}
}
for (int i = 1; i < a+2; i++) {
for (int j = b+1; j < b+2; j++) {
*(*(pdimensions + i) + j) = *(*(pdimensions + i) + 1);
}
}
*(*(pdimensions + 0) + 0) = *(*(pdimensions + a) + b);
*(*(pdimensions + 0) + b+1) = *(*(pdimensions + a) + 1);
*(*(pdimensions + a+1) + 0) = *(*(pdimensions + 1) + b);
*(*(pdimensions + a+1) + b+1) = *(*(pdimensions + 1) + 1);
}
if (output == 3) { //to output to a file
freopen ("Rejoice.out", "w", stdout);
};
cout << "Generation number: 0" << endl;
for (int i = 1; i < a+1; i++) {
for (int j = 1; j < b+1; j++) {
cout << *(*(pdimensions + i) + j);
}
cout << endl;
}
int xcount = 0;
for (int i = 1; i < *ptrr+1; i++) {
for (int j = 1; j < *ptrc+1; j++) {
if (*(*(pdimensions + i) + j) == 'X') {
++xcount;
}
}
}
if (xcount == 0) {
cout << "The current generation is nice and stable. No more generations possible. Tada!" << endl;
exit(1);
}
}
char Originalgrid::copyOriginalgrid ()
{
copypdimensions = new char*[*ptrr]; //ponter to the grid that is copied
for (int i = 0; i < *ptrr+2; i++) {
for (int j = 0; j < *ptrc+2; j++) {
copypdimensions[i] = new char [*ptrc+2];
}
}
count = new int*[*ptrr];
for (int i = 0; i < *ptrr+2; i++) {
for (int j = 0; j < *ptrc+2; j++) {
count[i] = new int [*ptrc+2];
}
}
for (int i = 0; i < *ptrr+2; i++) { //copying the current generation
for (int j = 0; j < *ptrc+2; j++) {
*(*(copypdimensions + i) + j) = *(*(pdimensions + i) + j);
}
}
}
char Originalgrid::computeOriginalgrid (char mode, int output)
{
while (countofx != 0) {
if (output == 2) { //pressing enter to progress
cout << "Press ENTER to contine..." << endl;
cin.ignore (numeric_limits <streamsize> :: max(), '\n');
} else if (output == 1) { //pausing for specified time before outputting generation
chrono::seconds dura (2);
this_thread::sleep_for (dura);
}
int check = 0;
for (int i = 1; i < *ptrr+1; i++) {
for (int j = 1; j < *ptrc+1; j++) {
ncount = 0;
for (int k = i-1; k < i+2; k++) {
for (int l = j-1; l < j+2; l++) {
if (*(*(copypdimensions + k) + l) == 'X') {
++ncount;
}
}
}
if (*(*(copypdimensions + i) + j) == 'X') {
--ncount;
}
*(*(count + i) + j) = ncount;
}
}
for (int i = 1; i < *ptrr+1; i++) {
for (int j = 1; j < *ptrc+1; j++) {
if (*(*(count + i) +j) == 3) {
if (*(*(pdimensions + i) + j) == '-') {
*(*(pdimensions + i) + j) = 'X';
}
} else if (*(*(count + i) + j) != 2 && *(*(count + i) + j) != 3) {
if (*(*(pdimensions + i) + j) == 'X') {
*(*(pdimensions + i) + j) = '-';
}
} else continue;
}
}
for (int i = 1; i < *ptrr+1; i++) {
for (int j = 1; j < *ptrc+1; j++) {
if (*(*(copypdimensions + i) + j) != *(*(pdimensions + i) +j)) {
++check;
}
}
}
if (check == 0) {
cout << "The current generation is nice and stable. No more generations possible. Tada!" << endl;
exit(1);
}
++n;
cout << "Generation number: " << n << endl;
for (int i = 1; i < *ptrr+1; i++) {
for (int j = 1; j < *ptrc+1; j++) {
cout << *(*(pdimensions + i) + j);
}
cout << endl;
}
if (mode == 'M' || mode == 'm') {
for (int i = 0; i < 1; i++) {
for (int j = 1; j < b+2 ; j++) {
*(*(pdimensions + i) + j) = *(*(pdimensions + i+1) + j);
}
}
for (int i = a+1; i < a+2 ; i++) {
for (int j = 1; j < b+2; j++) {
*(*(pdimensions + i) + j) = *(*(pdimensions + i-1) + j);
}
}
for (int i = 1; i < a+2; i++) {
for (int j = 0; j < 1; j++) {
*(*(pdimensions + i) + j) = *(*(pdimensions + i) + j+1);
}
}
for (int i = 1; i < a+2; i++) {
for (int j = b+1; j < b+2; j++) {
*(*(pdimensions + i) + j) = *(*(pdimensions + i) + j-1);
}
}
*(*(pdimensions + 0) + 0) = *(*(pdimensions + 1) + 1);
*(*(pdimensions + 0) + b+1) = *(*(pdimensions + 1) + b);
*(*(pdimensions + a+1) + 0) = *(*(pdimensions + a) + 1);
*(*(pdimensions + a+1) + b+1) = *(*(pdimensions + a) + b);
} else if (mode == 'D' or mode == 'd') {
for (int i = 0; i < 1; i++) {
for (int j = 1; j < b+2 ; j++) {
*(*(pdimensions + i) + j) = *(*(pdimensions + a) + j);
}
}
for (int i = a+1; i < a+2 ; i++) {
for (int j = 1; j < b+2; j++) {
*(*(pdimensions + i) + j) = *(*(pdimensions + 1) + j);
}
}
for (int i = 1; i < a+2; i++) {
for (int j = 0; j < 1; j++) {
*(*(pdimensions + i) + j) = *(*(pdimensions + i) + b);
}
}
for (int i = 1; i < a+2; i++) {
for (int j = b+1; j < b+2; j++) {
*(*(pdimensions + i) + j) = *(*(pdimensions + i) + 1);
}
}
*(*(pdimensions + 0) + 0) = *(*(pdimensions + a) + b);
*(*(pdimensions + 0) + b+1) = *(*(pdimensions + a) + 1);
*(*(pdimensions + a+1) + 0) = *(*(pdimensions + 1) + b);
*(*(pdimensions + a+1) + b+1) = *(*(pdimensions + 1) + 1);
}
for (int i = 0; i < *ptrr+2; i++) { //copying the current generation
for (int j = 0; j < *ptrc+2; j++) {
*(*(copypdimensions + i) + j) = *(*(pdimensions + i) + j);
}
}
xcount = 0;
for (int i = 1; i < *ptrr+1; i++) {
for (int j = 1; j < *ptrc+1; j++) {
if (*(*(pdimensions + i) + j) == 'X') {
++xcount;
}
}
}
countofx = xcount;
}
fclose (stdout);
if (countofx == 0) {
cout << "The current generation is nice and stable. No more generations possible. Tada!" << endl;
exit(1);
}
}