summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. 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)
commit67c046e4471ea322cc8ec91b680e222e74bc1f71 (patch)
treec0a9c141d4a3c22fed45b152f6191ec2e8200e47
parentdda6afb4c523a4836bfa2a9effd73c35ff05102e (diff)
downloadcgol-67c046e4471ea322cc8ec91b680e222e74bc1f71.zip
cgol-67c046e4471ea322cc8ec91b680e222e74bc1f71.tar.gz
cgol-67c046e4471ea322cc8ec91b680e222e74bc1f71.tar.bz2
Fix tick corruption that resulted in strangeness.
-rw-r--r--src/cgol.c29
1 files changed, 17 insertions, 12 deletions
diff --git a/src/cgol.c b/src/cgol.c
index 8ef6404..6add490 100644
--- a/src/cgol.c
+++ b/src/cgol.c
@@ -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