/
life.cpp
227 lines (189 loc) · 4.53 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
#include "life.h"
#include <fstream>
int life::toroidalAliveNext( int row, int col)
{
int returnVal = 0;
int count = 0;
for( int rowOffset = -1; rowOffset < 2; rowOffset++ )
{
for( int colOffset = -1; colOffset < 2; colOffset++ )
{
int rowCoord = ( row + rowOffset + rowGridSize ) % rowGridSize;
int colCoord = ( col + colOffset + colGridSize ) % colGridSize;
if( row == rowCoord && col == colCoord)
{
}
else if( current[rowCoord][colCoord] )
count++;
}
}
if( count < 2 || count > 3 )
{
//we die or stay dead
returnVal = 0;
}
else if ( ( count == 2 && current[row][col] ) || count == 3)
{
//we stay alive or are born
returnVal = 1;
}
return returnVal;
};
int life::nonToroidalAliveNext( int row, int col )
{
int returnVal = 0;
int count = 0;
for( int rowOffset = -1; rowOffset < 2 ; rowOffset++ )
{
for( int colOffset = -1; colOffset < 2 ; colOffset++ )
{
int rowCoord = row + rowOffset;
int colCoord = col + colOffset;
if( (rowCoord == row && colCoord == col) )
{
}
else if( rowCoord >= rowGridSize || rowCoord < 0
|| colCoord >= colGridSize || colCoord < 0)
{
}
else if( current[rowCoord][colCoord])
{
count++;
}
}
}
if( count < 2 || count > 3)
{
//we die or stay dead
returnVal = 0;
}
else if( ( count == 2 && current[row][col] ) || count == 3 )
{
//we stay alive or are born
returnVal = 1;
}
return returnVal;
};
void life::transfer()
{
//go through the entire vector, copying to current from the
//next_iteration
for ( int row = 0; row < rowGridSize ; row++ )
{
for ( int col = 0; col < colGridSize ; col++ )
{
current[row][col] = next_iteration[row][col];
}
}
};
void life::update()
{
//call aliveNext for every value in the grid
for( int row = 0; row < rowGridSize ; row++ )
{
for ( int col = 0; col < colGridSize ; col++ )
{
//call the correct AliveNext Function
next_iteration[row][col] = ( toroidal ?
toroidalAliveNext(row, col) : nonToroidalAliveNext(row, col) );
}
}
//transfer the values into the current
transfer();
}
void life::setup( std::string filename)
{
std::fstream inFile(filename);
//create a string to read input
std::string input;
//read in a blank line that will tell us how many rows
inFile >> input;
//set rowgridsize appropriately
rowGridSize = input.length();
//read in a blank line that will tell us how many columns
inFile >> input;
//set yGridSize appropriately
colGridSize = input.length();
//resize both current and next_iteration arrays
//with the correct amount of columns
current.resize( rowGridSize );
next_iteration.resize( rowGridSize );
//resize all of the sub arrays
for ( int row = 0; row < rowGridSize ; row++ )
{
current[row].resize(colGridSize);
next_iteration[row].resize( colGridSize );
}
for ( int row = 0 ; row < rowGridSize ; row++ )
{
//allow for the terminating character
inFile >> input;
for ( int col = 0; col < colGridSize ; col++)
{
//this is important, convert the input into an integer
//then subtract the ascii value of 0
current[row][col] = int (input[col]) - 48;
}
}
};
life::life(std::string filename)
{
setup(filename);
};
life::life()
{
};
life::life( int row, int col)
{
rowGridSize = row;
colGridSize = col;
current.resize( rowGridSize );
next_iteration.resize( rowGridSize );
for ( int row = 0; row < rowGridSize ; row++ )
{
current[row].resize(colGridSize);
next_iteration[row].resize( colGridSize );
}
for ( int row = 0 ; row < rowGridSize ; row++ )
{
for ( int col = 0; col < colGridSize ; col++)
{
//this is important, convert the input into an integer
//then subtract the ascii value of 0
current[row][col] = 0;
}
}
}
void life::setToroidal( bool toroid )
{
toroidal = toroid;
};
bool life::getToroidal()
{
return toroidal;
};
int life::getValAt( int row, int col)
{
if( row < rowGridSize && col < colGridSize )
return current[row][col];
else
return -1;
};
int life::setValAt( int row, int col, int on)
{
if( row < rowGridSize && col < colGridSize )
{
current[row][col] = on;
return 0;
}
else
return 1;
}
int life::getColumns()
{
return colGridSize;
};
int life::getRows()
{
return rowGridSize;
};