From 8b7846e46ebcd43a4196d834e93332a3acc5ac1e Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 18 Mar 2019 17:37:37 -0700 Subject: [PATCH] Now 'c' can clear instead of command line argument '-Z' --- README.md | 10 +++++++--- conway.c | 27 +++++++++++++-------------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index c8449ae..b75a68c 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,15 @@ Command line flags: * `-S[0-8]*` the rules for when an alive cell survives * `-B[0-8]*` the rules for when a new cell is born -* `-Z` start the canvas with all dead cells (zeroed out) * `-A[0-256]` provide a color for alive cells * `-D[0-256]` provide a color for dead cells Some interesting rulesets can be found in [this list](http://psoup.math.wisc.edu/mcell/rullex_life.html). -You can draw with the mouse and press space to toggle drawing mode (default: create living cells). -Pressing `p` will toggle play/pause and pressing escape or `q` will quit. +Actions: + +* Mouse: draw (by default: create new cells) +* Space: toggle mouse mode between create/destroy +* `p`: toggle play/pause +* `q` or Escape: quit +* `c`: clear the world diff --git a/conway.c b/conway.c index 023c7a7..ad03eb8 100644 --- a/conway.c +++ b/conway.c @@ -9,7 +9,7 @@ #define CELL(cells, x, y) cells[((y) % H)*W + ((x) % W)] static char rules[2][9] = {{0,0,0,1,0,0,0,0,0}, {0,0,1,1,0,0,0,0,0}}; -static int W, H, DEAD = 0, ALIVE = 8; +static int W, H, DEAD = 0, ALIVE = 7; static void update(const char *cells, char *future_cells) { @@ -20,19 +20,17 @@ static void update(const char *cells, char *future_cells) + CELL(cells,x-1,y) + CELL(cells,x+1,y) + CELL(cells,x-1,y+1) + CELL(cells,x,y+1) + CELL(cells,x+1,y+1); CELL(future_cells, x, y) = rules[CELL(cells, x, y)][neighbors]; - if (CELL(future_cells, x, y) != CELL(cells, x, y)) { - tb_change_cell(2*x, y, ' ', 0, CELL(future_cells, x, y) ? ALIVE : DEAD); - tb_change_cell(2*x+1, y, ' ', 0, CELL(future_cells, x, y) ? ALIVE : DEAD); - } + tb_change_cell(2*x, y, ' ', 0, CELL(future_cells, x, y) ? ALIVE : DEAD); + tb_change_cell(2*x+1, y, ' ', 0, CELL(future_cells, x, y) ? ALIVE : DEAD); } } } int main(int argc, char **argv) { - int randomize = 1; for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "--help") == 0) { - printf("Conway's Game of Life Viewer\nUsage: conway [-Z] [-S012345678] [-B012345678]\n"); + usage: + printf("Conway's Game of Life Viewer\nUsage: conway [-S] [-B] [-A] [-D]\n"); return 0; } else if (strncmp(argv[i], "-B", 2) == 0) { memset(rules[0], 0, sizeof(rules[0])); @@ -58,8 +56,8 @@ int main(int argc, char **argv) { fprintf(stderr, "Colors must be between 0-255\n"); return 1; } - } else if (strcmp(argv[i], "-Z") == 0) { - randomize = 0; + } else { + goto usage; } } @@ -78,13 +76,12 @@ int main(int argc, char **argv) { W = tb_width()/2, H = tb_height(); buffers[0] = calloc(W*H, sizeof(char)); buffers[1] = calloc(W*H, sizeof(char)); - if (randomize) - for (int x = 0; x < W; x++) - for (int y = 0; y < H; y++) - CELL(buffers[flipflop], x, y) = random() % 2; + for (int x = 0; x < W; x++) + for (int y = 0; y < H; y++) + CELL(buffers[flipflop], x, y) = random() % 2; int draw_mode = 1, paused = 0; - tb_set_clear_attributes(ALIVE, DEAD); + tb_set_clear_attributes(0, DEAD); tb_clear(); while (1) { if (!paused) { @@ -110,6 +107,8 @@ int main(int argc, char **argv) { goto done; else if (ev.ch == 'p') paused = !paused; + else if (ev.ch == 'c') + memset(buffers[flipflop], 0, W*H); else if (ev.key == TB_KEY_SPACE) draw_mode = !draw_mode; break;