commit 3a9d72d6e3a6b72a3a95b109d16f0c3f25041176
Author: Wilson Gheen <wilson@wilsonrgheen.com>
Date: Thu, 8 Dec 2022 09:45:18 -0600
Initial commit of life. Implements termbox2 and currently only supports an 8x8 board
Diffstat:
A | life.c | | | 125 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 125 insertions(+), 0 deletions(-)
diff --git a/life.c b/life.c
@@ -0,0 +1,125 @@
+#define TB_IMPL
+#include <termbox.h>
+#include <stdio.h>
+#include <stddef.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};
+#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) {
+ int x, y;
+ print_bw(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
+ 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_bw(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
+
+ 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;
+ 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);
+ }
+ }
+ }
+ tb_present();
+}
+
+unsigned char *get_next_state(unsigned char board[], int rows, int cols) {
+/*int play() {*/
+ unsigned char m[8];
+ for(int r=0; r < rows; r++) {
+ unsigned char 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) {
+ /* 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];
+ if(col > 0 && (row & bit<<1)) neighbors++;
+ /* Skip current cell */
+ if(a != 0 && (row & bit)) neighbors++;
+ if(col < (cols - 1) && (row & bit>>1)) neighbors++;
+ if(neighbors > 3) break;
+ }
+ switch(neighbors) {
+ case 2:
+ //stay the same
+ break;
+ case 3:
+ //alive
+ newRow |= bit;
+ break;
+ default:
+ //dead
+ newRow &= ~bit;
+ break;
+ }
+ }
+ m[r] = newRow;
+ }
+/* for(int r=0; r < rows; r++) {*/
+/* for(unsigned char 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);
+ return tmp;
+}
+
+void play(unsigned char 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);
+ memcpy(board, l2, sizeof l2);
+ }
+}
+
+int main() {
+ const size_t rows = sizeof BOARD / sizeof BOARD[0];
+ const size_t cols = sizeof BOARD[0] * 8;
+ play(BOARD, rows, cols);
+/* unsigned char *lp = (unsigned char*)malloc(5 * sizeof(unsigned char));*/
+/* unsigned char *mp = play(l);*/
+/* unsigned char *np = play(mp);*/
+/* play(np);*/
+ return 0;
+}