diff options
author | P. J. McDermott <pjm@nac.net> | 2011-12-13 02:28:10 (EST) |
---|---|---|
committer | P. J. McDermott <pjm@nac.net> | 2011-12-13 02:28:10 (EST) |
commit | 67c046e4471ea322cc8ec91b680e222e74bc1f71 (patch) | |
tree | c0a9c141d4a3c22fed45b152f6191ec2e8200e47 /src | |
parent | dda6afb4c523a4836bfa2a9effd73c35ff05102e (diff) | |
download | cgol-67c046e4471ea322cc8ec91b680e222e74bc1f71.zip cgol-67c046e4471ea322cc8ec91b680e222e74bc1f71.tar.gz cgol-67c046e4471ea322cc8ec91b680e222e74bc1f71.tar.bz2 |
Fix tick corruption that resulted in strangeness.
Diffstat (limited to 'src')
-rw-r--r-- | src/cgol.c | 29 |
1 files changed, 17 insertions, 12 deletions
@@ -33,7 +33,7 @@ void tick(); int count_live_neighbors(int i, int j); bool get_cell(int i, int j); void set_cell(int i, int j, bool s); -void print_cell(int i, int j); +void print_cell(int i, int j, bool next); void usage(const char *invocation); static int width, height; @@ -145,16 +145,18 @@ init_game() if (r > seedprob) { /* Dead. */ grid_cur[i] = false; + grid_next[i] = false; } else { /* Alive. */ grid_cur[i] = true; + grid_next[i] = true; } } /* Print the grid. */ for (i = 0; i < height; ++i) { for (j = 0; j < width; ++j) { - print_cell(i, j); + print_cell(i, j, false); } } } @@ -181,24 +183,23 @@ tick() for (i = 0; i < height; ++i) { for (j = 0; j < width; ++j) { n = count_live_neighbors(i, j); - if (get_cell(i, j)) { + if (get_cell(i, j) == true) { if (n < 2 || n > 3) { set_cell(i, j, false); - print_cell(i, j); + print_cell(i, j, true); } } else { if (n == 3) { set_cell(i, j, true); - print_cell(i, j); + print_cell(i, j, true); } } } } - /* Swap grids. */ - grid_tmp = grid_cur; - grid_cur = grid_next; - grid_next = grid_tmp; + memcpy(grid_cur, grid_next, sizeof(bool) * width * height); + + refresh(); } int @@ -226,13 +227,17 @@ get_cell(int i, int j) void set_cell(int i, int j, bool s) { - grid_cur[i * width + j] = s; + grid_next[i * width + j] = s; } void -print_cell(int i, int j) +print_cell(int i, int j, bool next) { - mvprintw(1 + i, 1 + j * 2, "%c", grid_cur[i * width + j] ? 'o' : ' '); + if (next) { + mvprintw(1 + i, 1 + j * 2, "%c", grid_next[i * width + j] ? 'o' : ' '); + } else { + mvprintw(1 + i, 1 + j * 2, "%c", grid_cur[i * width + j] ? 'o' : ' '); + } } void |