@@ -108,6 +108,46 @@ void fillDashboard(TGame* pGame, char with) {
108
108
}
109
109
}
110
110
111
+ void generateNextGeneration (TGame * pGame ) {
112
+ int i ;
113
+ int j ;
114
+
115
+ int aliveNeighbors ;
116
+
117
+ for (i = 0 ; i < pGame -> rows ; i ++ ) {
118
+ for (j = 0 ; j < pGame -> cols ; j ++ ) {
119
+ aliveNeighbors = countAliveNeighbors (pGame , i , j , NEIGHBORHOOD_RADIUS );
120
+
121
+ if (pGame -> dashboard [i ][j ] == DEAD_CELL ) {
122
+ if (aliveNeighbors == 3 ) {
123
+ pGame -> cellsDead -- ;
124
+ pGame -> cellsAlive ++ ;
125
+ pGame -> dashboard [i ][j ] = ALIVE_CELL_NG ;
126
+ };
127
+
128
+ continue ;
129
+ }
130
+
131
+ if (aliveNeighbors < 2 || aliveNeighbors > 3 ) {
132
+ pGame -> cellsAlive -- ;
133
+ pGame -> cellsDead ++ ;
134
+ pGame -> dashboard [i ][j ] = DEAD_CELL_NG ;
135
+ };
136
+ }
137
+ }
138
+
139
+ for (i = 0 ; i < pGame -> rows ; i ++ ) {
140
+ for (j = 0 ; j < pGame -> cols ; j ++ ) {
141
+ if (pGame -> dashboard [i ][j ] == DEAD_CELL_NG ) {
142
+ pGame -> dashboard [i ][j ] = DEAD_CELL ;
143
+ continue ;
144
+ }
145
+
146
+ if (pGame -> dashboard [i ][j ] == ALIVE_CELL_NG ) pGame -> dashboard [i ][j ] = ALIVE_CELL ;
147
+ }
148
+ }
149
+ }
150
+
111
151
char * getUserInputStr (char * message , char * onInvalidMessage , int strLength ,
112
152
int (* validator )(char * userInput )) {
113
153
char * userInput = malloc (strLength * sizeof (char ));
@@ -146,15 +186,34 @@ void printDashboardByConsole(TGame* pGame) {
146
186
int i ;
147
187
int j ;
148
188
189
+ for (i = 0 ; i < pGame -> cols + 2 ; i ++ ) printf ("-" );
190
+ printf ("\n" );
191
+
149
192
for (i = 0 ; i < pGame -> rows ; i ++ ) {
193
+ printf ("|" );
150
194
for (j = 0 ; j < pGame -> cols ; j ++ ) {
151
195
printf ("%c" , pGame -> dashboard [i ][j ]);
152
196
}
153
197
154
- printf ("\n" );
198
+ printf ("| \n" );
155
199
}
200
+
201
+ for (i = 0 ; i < pGame -> cols + 2 ; i ++ ) printf ("-" );
156
202
}
157
203
204
+ void printGame (TGame * pGame ) {
205
+ int i ;
206
+ for (i = 0 ; i < pGame -> cols + 2 ; i ++ ) printf ("-" );
207
+
208
+ printf ("\n| Cells alive: %*d |" , pGame -> cols - 17 + 2 , pGame -> cellsAlive );
209
+ printf ("\n| Cells dead: %*d |" , pGame -> cols - 16 + 2 , pGame -> cellsDead );
210
+ printf ("\n| Generation: %*d |" , pGame -> cols - 16 + 2 , pGame -> generation );
211
+ printf ("\n| Maximum generation: %*d |" , pGame -> cols - 25 + 3 , pGame -> maximumGeneration );
212
+ printf ("\n| Delay between generations: %*d |\n" , pGame -> cols - 32 + 3 ,
213
+ pGame -> delayBetweenGenerations );
214
+
215
+ printDashboardByConsole (pGame );
216
+ }
158
217
void setDashboardCenter (TGame * pGame ) {
159
218
int row = pGame -> rows / 2 ;
160
219
int col = pGame -> cols / 2 ;
@@ -165,64 +224,30 @@ void setDashboardCenter(TGame* pGame) {
165
224
166
225
void sleep (int miliseconds ) {
167
226
clock_t startTime = clock ();
168
- while (clock () < (startTime + miliseconds ))
169
- ;
227
+ while (clock () < (startTime + miliseconds ));
170
228
}
171
229
172
230
void startGameByConsole (TGame * pGame , int maxGeneration , int delayBetweenGenerations ) {
173
- int i ;
174
- int j ;
175
-
176
231
int generation = 0 ;
177
- int aliveNeighbors ;
178
232
179
233
pGame -> generation = 0 ;
234
+ pGame -> maximumGeneration = maxGeneration ;
235
+ pGame -> delayBetweenGenerations = delayBetweenGenerations ;
180
236
181
237
system ("cls" );
182
- printDashboardByConsole (pGame );
238
+ printGame (pGame );
183
239
if (generation == maxGeneration ) return ;
184
240
sleep (delayBetweenGenerations );
185
241
186
242
while (generation < maxGeneration ) {
187
- for (i = 0 ; i < pGame -> rows ; i ++ ) {
188
- for (j = 0 ; j < pGame -> cols ; j ++ ) {
189
- aliveNeighbors = countAliveNeighbors (pGame , i , j , NEIGHBORHOOD_RADIUS );
190
-
191
- if (pGame -> dashboard [i ][j ] == DEAD_CELL ) {
192
- if (aliveNeighbors == 3 ) {
193
- pGame -> cellsDead -- ;
194
- pGame -> cellsAlive ++ ;
195
- pGame -> dashboard [i ][j ] = ALIVE_CELL_NG ;
196
- };
197
-
198
- continue ;
199
- }
200
-
201
- if (aliveNeighbors < 2 || aliveNeighbors > 3 ) {
202
- pGame -> cellsAlive -- ;
203
- pGame -> cellsDead ++ ;
204
- pGame -> dashboard [i ][j ] = DEAD_CELL_NG ;
205
- };
206
- }
207
- }
208
-
209
- for (i = 0 ; i < pGame -> rows ; i ++ ) {
210
- for (j = 0 ; j < pGame -> cols ; j ++ ) {
211
- if (pGame -> dashboard [i ][j ] == DEAD_CELL_NG ) {
212
- pGame -> dashboard [i ][j ] = DEAD_CELL ;
213
- continue ;
214
- }
215
-
216
- if (pGame -> dashboard [i ][j ] == ALIVE_CELL_NG ) pGame -> dashboard [i ][j ] = ALIVE_CELL ;
217
- }
218
- }
243
+ generateNextGeneration (pGame );
219
244
220
245
generation ++ ;
221
246
222
247
pGame -> generation = generation ;
223
248
224
249
system ("cls" );
225
- printDashboardByConsole (pGame );
250
+ printGame (pGame );
226
251
if (generation != maxGeneration ) sleep (delayBetweenGenerations );
227
252
}
228
253
}
0 commit comments