diff options
author | Jonas Gunz <himself@jonasgunz.de> | 2020-04-30 23:22:17 +0200 |
---|---|---|
committer | Jonas Gunz <himself@jonasgunz.de> | 2020-04-30 23:22:17 +0200 |
commit | dbac3087c6f6e87163b93391d4b4ee870ba8c2fe (patch) | |
tree | 78eb5e29deda5dc9a21dca4e303d8d7dc1d4cafa /main.c | |
parent | ac692a11f3e5c6c227acfc2785f824edb1834a32 (diff) | |
download | gameoflife-master.tar.gz |
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 26 |
1 files changed, 16 insertions, 10 deletions
@@ -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; } |