From ce3ff4797ebccb4a969c6f0273ec200daf265aa9 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 18 Mar 2019 16:44:35 -0700 Subject: [PATCH] Tidying up --- conway.c | 83 +++++++++++++++++++++++++------------------------------- 1 file changed, 37 insertions(+), 46 deletions(-) diff --git a/conway.c b/conway.c index c3c1b7a..6b05a2b 100644 --- a/conway.c +++ b/conway.c @@ -1,35 +1,28 @@ // A console conway's game of life program by Bruce Hill // Released under the MIT license, see LICENSE for details. -// This file contains the main code for the table viewer. - #include #include #include #include #include -static char survive[9] = {0,0,1,1,0,0,0,0,0}; -static char birth[9] = {0,0,0,1,0,0,0,0,0}; +#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; static void update(const char *cells, char *future_cells) { - int W = tb_width()/2, H = tb_height(); for (int y = 0; y < H; y++) { for (int x = 0; x < W; x++) { - int neighbors = ( - cells[(y-1 % H)*W + (x-1 % W)] + - cells[(y-1 % H)*W + (x % W)] + - cells[(y-1 % H)*W + (x+1 % W)] + - cells[y*W + (x-1 % W)] + - cells[y*W + (x+1 % W)] + - cells[(y+1 % H)*W + (x-1 % W)] + - cells[(y+1 % H)*W + (x % W)] + - cells[(y+1 % H)*W + (x+1 % W)] - ); - future_cells[y*W + x] = cells[y*W + x] ? survive[neighbors] : birth[neighbors]; - if (future_cells[y*W + x] != cells[y*W + x]) { - tb_change_cell(2*x, y, ' ', 0, future_cells[y*W + x] ? 8 : 0); - tb_change_cell(2*x+1, y, ' ', 0, future_cells[y*W + x] ? 8 : 0); + int neighbors = + CELL(cells,x-1,y-1) + CELL(cells,x,y-1) + CELL(cells,x+1,y-1) + + 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) ? 8 : 0); + tb_change_cell(2*x+1, y, ' ', 0, CELL(future_cells, x, y) ? 8 : 0); } } } @@ -41,16 +34,16 @@ int main(int argc, char **argv) { printf("Conway's Game of Life Viewer\nUsage: conway [-S012345678] [-B012345678]\n"); return 0; } else if (strncmp(argv[i], "-B", 2) == 0) { - memset(birth, 0, sizeof(birth)); + memset(rules[0], 0, sizeof(rules[0])); for (char *p = &argv[i][2]; *p; p++) { if ('0' <= *p && *p <= '8') - birth[*p - '0'] = 1; + rules[0][*p - '0'] = 1; } } else if (strncmp(argv[i], "-S", 2) == 0) { - memset(survive, 0, sizeof(survive)); + memset(rules[1], 0, sizeof(rules[1])); for (char *p = &argv[i][2]; *p; p++) { if ('0' <= *p && *p <= '8') - survive[*p - '0'] = 1; + rules[1][*p - '0'] = 1; } } } @@ -62,54 +55,52 @@ int main(int argc, char **argv) { } tb_select_input_mode(TB_INPUT_MOUSE); - int flipflop = 0; // Cells are 2 characters wide so they look square - int W = tb_width()/2, H = tb_height(); char *buffers[2]; + int flipflop = 0; resize: + W = tb_width()/2, H = tb_height(); buffers[0] = calloc(W*H, sizeof(char)); buffers[1] = calloc(W*H, sizeof(char)); - for (int x = 0; x < W; x++) { - for (int y = 0; y < H; y++) { - buffers[flipflop][y*W+x] = 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; tb_clear(); while (1) { update(buffers[flipflop], buffers[!flipflop]); flipflop = !flipflop; + present: tb_present(); usleep(60000); struct tb_event ev; + int drew = 0; while (tb_peek_event(&ev, 0)) { switch (ev.type) { case TB_EVENT_RESIZE: - W = ev.w/2, H = ev.h; + free(buffers[0]); + free(buffers[1]); goto resize; case TB_EVENT_KEY: - switch (ev.key) { - case TB_KEY_ESC: - if (tb_peek_event(&ev, 50) == 0) - goto done; - else break; - } - switch (ev.ch) { - case 'q': - goto done; - } + if (ev.key == TB_KEY_ESC && tb_peek_event(&ev, 50) == 0) + goto done; + else if (ev.ch == 'q') + goto done; + else if (ev.key == TB_KEY_SPACE) + draw_mode = !draw_mode; break; case TB_EVENT_MOUSE: - buffers[flipflop][ev.y*W + ev.x/2] = !buffers[flipflop][ev.y*W + ev.x/2]; - tb_change_cell(2*(ev.x/2), ev.y, ' ', 0, buffers[flipflop][ev.y*W + ev.x/2] ? 8 : 0); - tb_change_cell(2*(ev.x/2)+1, ev.y, ' ', 0, buffers[flipflop][ev.y*W + ev.x/2] ? 8 : 0); - tb_present(); - usleep(10000); + CELL(buffers[flipflop], ev.x/2, ev.y) = draw_mode; + tb_change_cell(2*(ev.x/2), ev.y, ' ', 0, draw_mode ? 8 : 0); + tb_change_cell(2*(ev.x/2)+1, ev.y, ' ', 0, draw_mode ? 8 : 0); + drew = 1; break; } } + if (drew) goto present; } done: tb_shutdown();