Now 'c' can clear instead of command line argument '-Z'

This commit is contained in:
Bruce Hill 2019-03-18 17:37:37 -07:00
parent a84fc60e75
commit 8b7846e46e
2 changed files with 20 additions and 17 deletions

View File

@ -6,11 +6,15 @@ Command line flags:
* `-S[0-8]*` the rules for when an alive cell survives * `-S[0-8]*` the rules for when an alive cell survives
* `-B[0-8]*` the rules for when a new cell is born * `-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 * `-A[0-256]` provide a color for alive cells
* `-D[0-256]` provide a color for dead 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). 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). Actions:
Pressing `p` will toggle play/pause and pressing escape or `q` will quit.
* 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

View File

@ -9,7 +9,7 @@
#define CELL(cells, x, y) cells[((y) % H)*W + ((x) % W)] #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 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) 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) + CELL(cells,x+1,y)
+ CELL(cells,x-1,y+1) + CELL(cells,x,y+1) + CELL(cells,x+1,y+1); + 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]; 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, 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+1, y, ' ', 0, CELL(future_cells, x, y) ? ALIVE : DEAD);
}
} }
} }
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
int randomize = 1;
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--help") == 0) { 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<survival rules>] [-B<birth rules>] [-A<alive color>] [-D<dead color>]\n");
return 0; return 0;
} else if (strncmp(argv[i], "-B", 2) == 0) { } else if (strncmp(argv[i], "-B", 2) == 0) {
memset(rules[0], 0, sizeof(rules[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"); fprintf(stderr, "Colors must be between 0-255\n");
return 1; return 1;
} }
} else if (strcmp(argv[i], "-Z") == 0) { } else {
randomize = 0; goto usage;
} }
} }
@ -78,13 +76,12 @@ int main(int argc, char **argv) {
W = tb_width()/2, H = tb_height(); W = tb_width()/2, H = tb_height();
buffers[0] = calloc(W*H, sizeof(char)); buffers[0] = calloc(W*H, sizeof(char));
buffers[1] = calloc(W*H, sizeof(char)); buffers[1] = calloc(W*H, sizeof(char));
if (randomize) for (int x = 0; x < W; x++)
for (int x = 0; x < W; x++) for (int y = 0; y < H; y++)
for (int y = 0; y < H; y++) CELL(buffers[flipflop], x, y) = random() % 2;
CELL(buffers[flipflop], x, y) = random() % 2;
int draw_mode = 1, paused = 0; int draw_mode = 1, paused = 0;
tb_set_clear_attributes(ALIVE, DEAD); tb_set_clear_attributes(0, DEAD);
tb_clear(); tb_clear();
while (1) { while (1) {
if (!paused) { if (!paused) {
@ -110,6 +107,8 @@ int main(int argc, char **argv) {
goto done; goto done;
else if (ev.ch == 'p') else if (ev.ch == 'p')
paused = !paused; paused = !paused;
else if (ev.ch == 'c')
memset(buffers[flipflop], 0, W*H);
else if (ev.key == TB_KEY_SPACE) else if (ev.key == TB_KEY_SPACE)
draw_mode = !draw_mode; draw_mode = !draw_mode;
break; break;