summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jonas Gunz <himself@jonasgunz.de> 2020-04-30 23:22:17 +0200
committerGravatar Jonas Gunz <himself@jonasgunz.de> 2020-04-30 23:22:17 +0200
commitdbac3087c6f6e87163b93391d4b4ee870ba8c2fe (patch)
tree78eb5e29deda5dc9a21dca4e303d8d7dc1d4cafa
parentac692a11f3e5c6c227acfc2785f824edb1834a32 (diff)
downloadgameoflife-dbac3087c6f6e87163b93391d4b4ee870ba8c2fe.tar.gz
-rw-r--r--main.c26
1 files changed, 16 insertions, 10 deletions
diff --git a/main.c b/main.c
index fd9c20c..aa7e594 100644
--- a/main.c
+++ b/main.c
@@ -24,18 +24,20 @@ unsigned int calculate_area(int **_field, unsigned int _size_x, unsigned int _si
void iterate(int **_field, unsigned int _size_x, unsigned int _size_y) {
int **next_generation = NULL;
- next_generation = malloc( sizeof( *next_generation ) * _size_x );
+ next_generation = malloc( sizeof( typeof( *next_generation ) ) * _size_x );
for ( unsigned int x = 0; x < _size_x; x++ ) {
- next_generation[x] = malloc( sizeof( **next_generation ) );
+ next_generation[x] = malloc( sizeof( **next_generation ) * _size_y );
}
for (unsigned int x = 0; x < _size_x; x++) {
for (unsigned int y = 0; y < _size_y; y++) {
unsigned int surrdounding_count = calculate_area(_field, _size_x, _size_y, x, y);
- if (surrdounding_count < 2 || surrdounding_count > 3)
+ if ( (surrdounding_count < 2 || surrdounding_count > 3) )
next_generation[x][y] = 0;
- else
+ else if ( surrdounding_count == 3)
next_generation[x][y] = 1;
+ else
+ next_generation[x][y] = _field[x][y];
}
}
@@ -55,12 +57,12 @@ int main(void) {
int **field = NULL;
- unsigned const int size_x = 3;
- unsigned const int size_y = 3;
+ unsigned const int size_x = 7;
+ unsigned const int size_y = 7;
field = malloc( sizeof( *field ) * size_x );
for ( unsigned int x = 0; x < size_x; x++ ) {
- field[x] = malloc( sizeof( **field ) );
+ field[x] = malloc( sizeof( **field) * size_y );
}
for (unsigned int x = 0; x < size_x; x++) {
@@ -68,8 +70,8 @@ int main(void) {
field[x][y] = x == y ? 1 : 0;
}
}
- unsigned int gen_cnt = 0;
- while ( 1 ) {
+
+ for (unsigned int gen_cnt = 0; gen_cnt < 10; gen_cnt++) {
printf("\n\nGeneration %i\n", gen_cnt);
for (unsigned int x = 0; x < size_x; x++) {
for (unsigned int y = 0; y < size_y; y++) {
@@ -78,9 +80,13 @@ int main(void) {
printf("\n");
}
- gen_cnt ++;
iterate(field, size_x, size_y);
}
+ for ( unsigned int x = 0; x < size_x; x++) {
+ free (field[x]);
+ }
+ free(field);
+
return 0;
}