diff --git a/gol.c b/gol.c index 9a75a11..39c0477 100644 --- a/gol.c +++ b/gol.c @@ -2,20 +2,27 @@ #define SIZE 512 - - -int main() -{ - bool grid[SIZE][SIZE]; - - for(int i = 0; i < SIZE;){ - for(int j = 0; i < SIZE;){ +void initialize_grid(bool grid[SIZE][SIZE]){ + for(int i = 0; i < SIZE; i++){ + for(int j = 0; i < SIZE; i++){ grid[i][j] = false; - } + } } +} - for(int i = 0; i < SIZE;){ - for(int j = 0; i < SIZE;){ +void mirror_grid(bool grid[SIZE][SIZE], bool mirror[SIZE][SIZE]){ + for(int i = 0; i < SIZE; i++){ + for(int j = 0; i < SIZE; i++){ + mirror[i][j] = grid[i][j]; + } + } +} + +void simulate_grid(bool grid[SIZE][SIZE], bool mirror[SIZE][SIZE]){ + + mirror_grid(grid, mirror); + for(int i = 0; i < SIZE; i++){ + for(int j = 0; i < SIZE; i++){ int neighbours = 0; int nx_prawo = i + 1 > SIZE ? 0 + (i + 1 - SIZE) : i + 1; @@ -30,20 +37,48 @@ int main() int nx_dol = i; int ny_dol = j - 1 < 0 ? SIZE - (j - 1 + SIZE) : j - 1; - neighbours = grid[nx_prawo][ny_prawo] == true ? neighbours + 1 : neighbours; - neighbours = grid[nx_lewo][ny_lewo] == true ? neighbours + 1 : neighbours; - neighbours = grid[nx_gora][ny_gora] == true ? neighbours + 1 : neighbours; - neighbours = grid[nx_dol][ny_dol] == true ? neighbours + 1 : neighbours; + neighbours = mirror[nx_prawo][ny_prawo] == true ? neighbours + 1 : neighbours; + neighbours = mirror[nx_lewo][ny_lewo] == true ? neighbours + 1 : neighbours; + neighbours = mirror[nx_gora][ny_gora] == true ? neighbours + 1 : neighbours; + neighbours = mirror[nx_dol][ny_dol] == true ? neighbours + 1 : neighbours; - int state = 0; + neighbours = mirror[nx_prawo][ny_gora] == true ? neighbours + 1 : neighbours; + neighbours = mirror[nx_prawo][ny_dol] == true ? neighbours + 1 : neighbours; + neighbours = mirror[nx_lewo][ny_gora] == true ? neighbours + 1 : neighbours; + neighbours = mirror[nx_lewo][ny_dol] == true ? neighbours + 1 : neighbours; - state = neighbours < 2 ? 1 : neighbours == 2 || neighbours == 3 ? 2 : neighbours > 3 ? 3 : neighbours == 3 ? 4 : 0; + int state = neighbours < 2 ? 1 : neighbours == 2 || neighbours == 3 ? 2 : neighbours > 3 ? 3 : neighbours == 3 ? 4 : 0; switch (state) { case 1: + grid[i][j] = false; + break; + case 2: + grid[i][j] = grid[i][j]; + break; + case 3: + grid[i][j] = false; + break; + case 4: + grid[i][j] = true; + break; + default: + break; } } } } + +int main() +{ + bool grid[SIZE][SIZE]; + bool mirror[SIZE][SIZE]; + + initialize_grid(grid); + mirror_grid(grid, mirror); + simulate_grid(grid, mirror); + + return 0; +}