-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConwayByList.cpp
243 lines (203 loc) · 6.45 KB
/
ConwayByList.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
#include <iostream>
#include <list>
using namespace std;
typedef struct{
int row;
int col;
}coord;
typedef struct{
int row;
int col;
bool isAlive;
int AliveCount;
}cell;
list<coord> returnNeighbourCoords (coord currentCell) ;
int returnAliveCount (list<cell> sparse, list<coord> neighbours) ;
bool isFound ( list<cell> sparse, coord c );
cell returnCell ( list<cell> sparse, coord c );
list<cell> updateConway (list<cell> sparse);
void PrintMat(list<cell>);
int main(){
list<cell> sparse;
cell c;
c.row = 5;
c.col = 4;
c.isAlive = true;
sparse.push_back(c);
c.row = 5;
c.col = 5;
c.isAlive = true;
sparse.push_back(c);
c.row = 5;
c.col = 6;
c.isAlive = true;
sparse.push_back(c);
sparse = updateConway (sparse );
/*list<cell>::iterator it;
for ( it = sparse.begin() ; it != sparse.end() ; it++ ){
cout << "(" << (*it).row << "," << (*it).col << ")" << endl;
}
sparse = updateConway (sparse );
//list<cell>::iterator it;
for ( it = sparse.begin() ; it != sparse.end() ; it++ ){
cout << "(" << (*it).row << "," << (*it).col << ")" << endl;
}*/
PrintMat(sparse);
int count = 0;
while ( count < 20 ){
sparse = updateConway(sparse);
cout << string(50, '\n') << endl;
PrintMat(sparse);
sleep(1);
count++;
}
}
void PrintMat( list<cell> sparse ){
int mat[12][12];
for ( int i = 0; i<12 ; i++ )
for ( int j = 0; j<12 ; j++ )
mat[i][j] = 0;
list<cell>::iterator it;
for ( it = sparse.begin(); it != sparse.end() ; it++ )
mat[(*it).row][(*it).col] = 1;
for ( int i = 0; i<12 ; i++ ){
for ( int j = 0; j<12 ; j++ ){
if ( mat[i][j] == 1 )
cout << "&" ;
else
cout << " ";
}
cout << endl;
}
}
list<cell> updateConway (list<cell> sparse){
list<cell> newsparse;
list<cell>::iterator sp;
for ( sp = sparse.begin() ; sp != sparse.end() ; sp++ ){
coord current;
current.row = (*sp).row;
current.col = (*sp).col;
list<coord> neighbours = returnNeighbourCoords(current);
list<coord>::iterator ng;
for ( ng = neighbours.begin() ; ng != neighbours.end(); ng++ ){
if( !isFound(sparse,*ng) ){ //not a live member
cell newmember;
newmember.row = (*ng).row;
newmember.col = (*ng).col;
newmember.isAlive = false;
newmember.AliveCount = returnAliveCount (sparse,returnNeighbourCoords(*ng));
if ( newmember.AliveCount == 3 ){
newmember.isAlive = false;
if( !isFound(newsparse,*ng) )
newsparse.push_back (newmember);
}
}
else{ // is a live member
cell newmember;
newmember.row = (*ng).row;
newmember.col = (*ng).col;
newmember.isAlive = true;
newmember.AliveCount = returnAliveCount (sparse,returnNeighbourCoords(*ng));
if ( newmember.AliveCount < 2 || newmember.AliveCount > 3 )
newmember.isAlive = false;
else{
if( !isFound(newsparse,*ng) )
newsparse.push_back (newmember);
}
}
}
}
return newsparse;
}
list<coord> returnNeighbourCoords (coord currentCell){
list<coord> neighbours;
coord temp;
int x[] = {-1,0,1,-1,1,-1,0,1};
int y[] = {-1,-1,-1,0,0,1,1,1};
for ( int i = 0; i < 8; i++ ){
temp.row = currentCell.row + x[i];
temp.col = currentCell.col + y[i];
neighbours.push_back(temp);
}
return neighbours;
}
bool isFound ( list<cell> sparse, coord c ){
list<cell>::iterator sp;
for ( sp = sparse.begin() ; sp != sparse.end() ; sp++ ){
if ( (*sp).row == c.row && (*sp).col == c.col )
return true;
}
return false;
}
int returnAliveCount (list<cell> sparse, list<coord> neighbours){
list<cell>::iterator sp;
list<coord>::iterator ng;
int alivecount = 0;
for ( ng = neighbours.begin() ; ng != neighbours.end() ; ng++ ){
if( isFound (sparse,*ng) )
alivecount++;
}
return alivecount;
}
/*void PrintMat( vector< vector<int> > SubMat );
vector< vector<int> > returnSubMat(vector< vector<int> > Original, int row, int col);
int returnAliveNeighbours ( vector< vector<int> > SubMat );
vector< vector<int> > initialize(int MAXSIZE);
vector< vector<int> > makeMap(int MATSIZE);
vector< vector<int> > updateConway(vector< vector<int> > Original, int MAXSIZE);
int main(){
vector< vector<int> > Matrix = makeMap(12);
PrintMat(Matrix);
int count = 0;
while ( count < 20 ){
Matrix = updateConway(Matrix,12);
cout << string(50, '\n') << endl;
PrintMat(Matrix);
sleep(1);
count++;
}
}
vector< vector<int> > updateConway(vector< vector<int> > Original, int MAXSIZE){
vector< vector<int> > initialize(int MAXSIZE){
vector<int> temp;
vector< vector<int> > Mat;
for (int i = 0; i< MAXSIZE; i++){
for (int j = 0; j<MAXSIZE; j++)
temp.push_back(0);
Mat.push_back(temp);
temp.clear();
}
return Mat;
}
void PrintMat( vector< vector<int> > SubMat ){
for (vector< vector<int> >::size_type u = 0; u < SubMat.size(); u++) {
for (vector<int>::size_type v = 0; v < SubMat[u].size(); v++) {
if ( SubMat[u][v] == -1 )
cout << "0";
else if ( SubMat[u][v] == 1 )
cout << "1";
else
cout << " ";
}
cout << endl;
}
}
vector < vector<int> > makeMap(int MATSIZE){
int initiate [12][12];
for ( int i = 0; i <12; i++ )
for ( int j = 0; j < 12 ; j++ )
initiate[i][j] = 0;
initiate[5][4] = 1;
initiate[5][5] = 1;
initiate[5][6] = 1;
vector< vector<int> > MainMap = initialize(MATSIZE);
for ( int i = 0; i < MATSIZE ; i++){
for ( int j= 0; j < MATSIZE ; j++){
if ( i == 0 || j == 0 || i == MATSIZE - 1 || j == MATSIZE - 1 )
MainMap[i][j] = -1;
else
MainMap[i][j] = initiate[i][j];
}
}
return MainMap;
}*/