Game of Life with File Input

I am working on Conway's game of life and I have the code working but I don't have it set up to where it reads in files. I am having trouble determining how to read in a file and make my code work.

In order for your project to work with these inputs you will need to specify a two dimensional array. The book specifies a
22 by 80 array. The GTA project uses a 50 by 100 (row by col) array. So long as your array is larger than the size specified
by the input, your code will work with the input. After creating the array, the code reads in the data from the input and
fills out your LIFE community array with a small twist. The book suggests filling in the grid directly with asterisks for live
cells and blanks for dead cells. We will use class objects instead.
The normal implementation of LIFE uses two identical arrays. One stores the now generation and one is used to store
the next generation. (see the book pgs 446 & 447) We will be using one array which contains LIFE cell objects made from
the simplest useful class we could think of. Our class objects will contain two Boolean data items that store the cell’s life
condition and one function which will age the cell.
Anti-Chaos:
Your LIFE community’s size should be square and an edge length is define globally as const int edge=#. Your class is
named cell and contains the public boolean variables aod_d0, aod_d1 and the void function age(). Create a general
function that counts the number of living neighbors of a cell and declare its type with the following declaration: int
nbors_sum(cell[edge][edge], int, int, int, int);. Your LIFE community ages a day at a time so create a general function that
reads cells at d0 and determines whether that cell is alive or dead (aod) at d1. It’s declaration is: void oneday(cell[edge]
[edge], int, int);. The oneday function will call the nbors_sum function. The GTA version has a fair amount of code in
main() including file input and the while(true) display loop.


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
#include <iostream>
#include <fstream>
using namespace std;

const int edge=5;

struct Shape {
public:
char xCoord;
char yCoord;
char height;
char width;
char **figure;
};


struct Blinker : public Shape {
static const char bheight = 3; //blinker width
static const char bwidth = 1; //blinker height
Blinker( char x , char y );
~Blinker();
};

class cell {
public:
cell( Shape sh );
void print();
void age();
char getState( char state , char xCoord , char yCoord , bool aod_d0);
void iterate(unsigned int iterations);
private:
char world[edge][edge];
char otherWorld[edge][edge];
bool aod_d0, aod_d1;
Shape shape;
};

cell::cell( Shape sh ) :
shape(sh) ,
aod_d0(true)
{
for ( char i = 0; i < edge; i++ ) {
for ( char j = 0; j < edge; j++ ) {
world[i][j] = '.';
}
}
for ( char i = shape.yCoord; i - shape.yCoord < shape.height; i++ ) {
for ( char j = shape.xCoord; j - shape.xCoord < shape.width; j++ ) {
if ( i < edge && j < edge ) {
world[i][j] =
shape.figure[ i - shape.yCoord ][j - shape.xCoord ];
}
}
}
}

void cell::print() {
if (aod_d0) {
for ( char i = 0; i < edge; i++ ) {
for ( char j = 0; j < edge; j++ ) {
std::cout << world[i][j];
}
std::cout << std::endl;
}
} else {
for ( char i = 0; i < edge; i++ ) {
for ( char j = 0; j < edge; j++ ) {
std::cout << otherWorld[i][j];
}
std::cout << std::endl;
}
}
for ( char i = 0; i < edge; i++ ) {
std::cout << ' ';
}
std::cout << std::endl;
}

void cell::age() {
if (aod_d0) {
for ( char i = 0; i < edge; i++ ) {
for ( char j = 0; j < edge; j++ ) {
otherWorld[i][j] =
cell::getState(world[i][j] , i , j , aod_d0);
}
}
aod_d0 = !aod_d0;
} else {
for ( char i = 0; i < edge; i++ ) {
for ( char j = 0; j < edge; j++ ) {
world[i][j] =
cell::getState(otherWorld[i][j] , i , j , aod_d0);
}
}
aod_d0= !aod_d0;
}
}

char cell::getState( char state, char yCoord, char xCoord, bool aod_d0 ) {
char neighbors = 0;
if ( aod_d0 ) {
for ( char i = yCoord - 1; i <= yCoord + 1; i++ ) {
for ( char j = xCoord - 1; j <= xCoord + 1; j++ ) {
if ( i == yCoord && j == xCoord ) {
continue;
}
if ( i > -1 && i < edge && j > -1 && j < edge ) {
if ( world[i][j] == 'x' ) {
neighbors++;
}
}
}
}
} else {
for ( char i = yCoord - 1; i <= yCoord + 1; i++ ) {
for ( char j = xCoord - 1; j <= xCoord + 1; j++ ) {
if ( i == yCoord && j == xCoord ) {
continue;
}
if ( i > -1 && i < edge && j > -1 && j < edge ) {
if ( otherWorld[i][j] == 'x' ) {
neighbors++;
}
}
}
}
}
if (state == 'x') {
return ( neighbors > 1 && neighbors < 4 ) ? 'x' : '.';
}
else {
return ( neighbors == 3 ) ? 'x' : '.';
}
}

void cell::iterate( unsigned int iterations ) {
for ( int i = 0; i < iterations; i++ ) {
print();
age();
}
}



Blinker::Blinker( char x , char y ) {
xCoord = x;
yCoord = y;
height = bheight;
width = bwidth;
figure = new char*[bheight];
for ( char i = 0; i < bheight; i++ ) {
figure[i] = new char[bwidth];
}
for ( char i = 0; i < bheight; i++ ) {
for ( char j = 0; j < bwidth; j++ ) {
figure[i][j] = 'x';
}
}
}

Blinker::~Blinker() {
for ( char i = 0; i < bheight; i++ ) {
delete[] figure[i];
}
delete[] figure;
}


int main() {

ifstream in;
in.open("glidergun.txt");
if(in.fail())
{
cout <<"Input file failed to open.\n";
return 1;
}


Blinker blinker(2,1);
cell cell(blinker);
cell.iterate(3);

in.close();
}

Topic archived. No new replies allowed.