life

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit e69d5606e01c40e8a392e6c5f5b1e1897adacbe9
parent 3a9d72d6e3a6b72a3a95b109d16f0c3f25041176
Author: Wilson Gheen <wilson@wilsonrgheen.com>
Date:   Fri,  9 Dec 2022 03:42:05 -0600

Switch to 32 columns and add functions to concisely set cells

Diffstat:
Mlife.c | 92+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
1 file changed, 53 insertions(+), 39 deletions(-)

diff --git a/life.c b/life.c @@ -1,74 +1,81 @@ #define TB_IMPL +#include <inttypes.h> #include <termbox.h> #include <stdio.h> #include <stddef.h> +#include <stdint.h> #include <string.h> #include <unistd.h> #define WIDTH(x) sizeof(x[0]) * 8 -static unsigned char glider[] = {0, 16, 8, 56, 0, 0, 0, 0}; -static unsigned char modGlider[] = {64, 32, 224, 0, 0, 0, 0, 0}; -static unsigned char vline[] = {0, 0, 16, 16, 16, 0, 0, 0}; -static unsigned char what[] = {0, 0, 48, 48, 48, 48, 48, 0}; -static unsigned char what2[] = {0, 0, 32, 32, 32, 32, 32, 0}; -static unsigned char what3[] = {0, 0, 32, 32, 32, 32, 0, 0}; -static unsigned char what4[] = {0, 0, 120, 0, 0, 0, 0, 0}; -static unsigned char gliderShootsImmortalFlower[] = - {64, 32, 224, 0, 0, 0, 15, 0}; -static unsigned char hline[] = {0, 0, 14, 0, 0, 0, 0, 0}; -static unsigned char sqtoad[] = {0, 96, 96, 24, 24, 0, 0, 0}; +static uint32_t glider[] = {0, 131072, 65536, 458752, 0, 0, 0, 0}; +static uint32_t modGlider[] = {524288, 262144, 1835008, 0, 0, 0, 0, 0}; +static uint32_t vline[] = {0, 0, 131072, 131072, 131072, 0, 0, 0}; +static uint32_t what[] = {0, 0, 393216, 393216, 393216, 393216, 393216, 0}; +static uint32_t what2[] = {0, 0, 262144, 262144, 262144, 262144, 262144, 0}; +static uint32_t what3[] = {0, 0, 262144, 262144, 262144, 262144, 0, 0}; +static uint32_t what4[] = {0, 0, 983040, 0, 0, 0, 0, 0}; +static uint32_t gliderShootsImmortalFlower[] = + {524288, 262144, 1835008, 0, 0, 0, 122880, 0}; +static uint32_t hline[] = {0, 0, 114688, 0, 0, 0, 0, 0}; +static uint32_t sqtoad[] = {0, 786432, 786432, 196608, 196608, 0, 0, 0}; #define BOARD modGlider void print_bw(int x, int y, uint32_t ch) { tb_set_cell(x, y, ch, TB_BLACK, TB_WHITE); } -void print_board(unsigned char board[], int rows, int cols) { +void print_bl(int x, int y, uint32_t ch) { + tb_set_cell(x, y, ch, TB_BLACK, TB_BLACK); +} + +void print_board(uint32_t board[], int rows, int cols) { int x, y; - print_bw(0, 0, 0x250f); //heavy box corner down and right + print_bl(0, 0, 0x250f); //heavy box corner down and right for(x=1; x <= cols*2; x++) - print_bw(x, 0, 0x2501); //heavy box horizontal line - print_bw((cols*2)+1, 0, 0x2513); //heavy box corner down and left + print_bl(x, 0, 0x2501); //heavy box horizontal line + print_bl((cols*2)+1, 0, 0x2513); //heavy box corner down and left for(y=1; y <= rows; y++) { - print_bw(0, y, 0x2503); //heavy box vertical line - print_bw(x, y, 0x2503); //heavy box vertical line + print_bl(0, y, 0x2503); //heavy box vertical line + print_bl(x, y, 0x2503); //heavy box vertical line } - print_bw(x, y, 0x251b); //heavy box corner up and left + print_bl(x, y, 0x251b); //heavy box corner up and left while(--x>0) - print_bw(x, y, 0x2501); //heavy box horizontal line - print_bw(0, y, 0x2517); //heavy box corner up and right + print_bl(x, y, 0x2501); //heavy box horizontal line + print_bl(0, y, 0x2517); //heavy box corner up and right for(y=0; y < rows; y++) { /* for(int n=cols - WIDTH(board); n-->0;) printf(" ");*/ /* For a single byte for example, this value will be 1000 0000.*/ - unsigned char leftBit = 1 << (cols - 1); - unsigned char bit=leftBit; + uint32_t leftBit = 1 << (cols - 1); + uint32_t bit=leftBit; for(x=1; x <= cols*2; bit=bit>>1, x+=2) { if (board[y] & bit) { - print_bw(x, y+1, 0x257a); - print_bw(x+1, y+1, 0x2578); +/* print_bw(x, y+1, 0x257a);*/ +/* print_bw(x+1, y+1, 0x2578);*/ + print_bw(x, y+1, ' '); + print_bw(x+1, y+1, ' '); } } } tb_present(); } -unsigned char *get_next_state(unsigned char board[], int rows, int cols) { -/*int play() {*/ - unsigned char m[8]; +uint32_t *get_next_state(uint32_t board[], int rows, int cols) { + uint32_t m[8]; for(int r=0; r < rows; r++) { - unsigned char newRow = board[r]; + uint32_t newRow = board[r]; /* For a single byte for example, this value will be 1000 0000.*/ - unsigned char leftBit = 1 << (cols - 1); - for(unsigned char bit=leftBit, col=0; col < cols; bit=leftBit>>++col) { + uint32_t leftBit = (uint32_t)(1 << (cols - 1)); + for(uint32_t bit=(uint32_t)leftBit, col=0; col < cols; bit=leftBit>>++col) { /* Get neighbors in previous row, current row, next row. Ignore nonexistent rows. */ int neighbors = 0; for(int a = -1; a <= 1; a++) { if(r+a < 0 || r+a >= rows) continue; - unsigned char row = board[r+a]; + uint32_t row = board[r+a]; if(col > 0 && (row & bit<<1)) neighbors++; /* Skip current cell */ if(a != 0 && (row & bit)) neighbors++; @@ -92,34 +99,41 @@ unsigned char *get_next_state(unsigned char board[], int rows, int cols) { m[r] = newRow; } /* for(int r=0; r < rows; r++) {*/ -/* for(unsigned char bit, col=1; col <= cols; bit=128>>col++) {*/ +/* for(uint32_t bit, col=1; col <= cols; bit=128>>col++) {*/ /* printf("%d ", m[r] & bit ? 1 : 0);*/ /* }*/ /* printf("\n");*/ /* }*/ - static unsigned char tmp[8]; - memcpy(tmp, m, rows); + // rows + static uint32_t tmp[8]; + memcpy(tmp, m, sizeof m); return tmp; } -void play(unsigned char board[], int rows, int cols) { +void play(uint32_t board[], int rows, int cols) { tb_init(); while(1) { tb_clear(); print_board(board, rows, cols); sleep(1); - unsigned char *l2 = get_next_state(board, rows, cols); + uint32_t *l2 = get_next_state(board, rows, cols); memcpy(board, l2, sizeof l2); } +/* for(int i=0; i < rows; i++)*/ +/* printf("row %d:\t% 6x\t(%" PRIu32 ")\n", i, BOARD[i]);*/ } int main() { const size_t rows = sizeof BOARD / sizeof BOARD[0]; const size_t cols = sizeof BOARD[0] * 8; +/* for(int i=0; i < rows; i++)*/ +/* printf("row %d:\t% 6x\t(%ju)\n", i, (uintmax_t)BOARD[i]);*/ +/* printf("row %d:\t% 6x\t(%" PRIu32 ")\n", i, BOARD[i]);*/ +/* printf("%s", (uint32_t)BOARD[0] == (uint32_t)524288 ? "true" : "false");*/ play(BOARD, rows, cols); -/* unsigned char *lp = (unsigned char*)malloc(5 * sizeof(unsigned char));*/ -/* unsigned char *mp = play(l);*/ -/* unsigned char *np = play(mp);*/ +/* uint32_t *lp = (uint32_t*)malloc(5 * sizeof(uint32_t));*/ +/* uint32_t *mp = play(l);*/ +/* uint32_t *np = play(mp);*/ /* play(np);*/ return 0; }