commit 4e2b671b09618b794e3508b97967ff924925d01e
parent 26c0729eeef14accf4f7b01512b772b22bbb97af
Author: Wilson Gheen <wilson@wilsonrgheen.com>
Date: Fri, 9 Dec 2022 09:21:35 -0600
Enable variable array sizes
Diffstat:
M | life.c | | | 53 | ++++++++++++++++++++++++++++++----------------------- |
1 file changed, 30 insertions(+), 23 deletions(-)
diff --git a/life.c b/life.c
@@ -9,19 +9,22 @@
#define WIDTH(x) sizeof(x[0]) * 8
+#define MAX_ROWS 24
-static uint32_t glider[] = {0, 131072, 65536, 458752, 0, 0, 0, 0};
-static uint32_t modGlider[] = {524288, 262144, 1835008, 0, 0, 0, 0, 0, 0, 0, 0, 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
+static uint32_t glider[MAX_ROWS] = {0, 131072, 65536, 458752, 0, 0, 0, 0};
+static uint32_t modGlider[MAX_ROWS] = {524288, 262144, 1835008, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+static uint32_t vline[MAX_ROWS] = {0, 0, 131072, 131072, 131072, 0, 0, 0};
+static uint32_t what[MAX_ROWS] = {0, 0, 393216, 393216, 393216, 393216, 393216, 0};
+static uint32_t what2[MAX_ROWS] = {0, 0, 262144, 262144, 262144, 262144, 262144, 0};
+static uint32_t what3[MAX_ROWS] = {0, 0, 262144, 262144, 262144, 262144, 0, 0};
+static uint32_t what4[MAX_ROWS] = {0, 0, 983040, 0, 0, 0, 0, 0};
+static uint32_t gliderShootsImmortalFlower[MAX_ROWS] =
+ {2097152, 1048576, 7340032, 0, 0, 0, 0, 0, 0, 0, 30720, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+static uint32_t hline[MAX_ROWS] = {0, 0, 114688, 0, 0, 0, 0, 0};
+static uint32_t sqtoad[MAX_ROWS] = {0, 786432, 786432, 196608, 196608, 0, 0, 0};
+/*ec printf('%x', float2nr(@"->str2nr(16)/pow(2,16)))*/
+static uint32_t gosper[MAX_ROWS] = { 0x00000200, 0x00000500, 0x00000686, 0x000006c6, 0x00000680, 0x00000500, 0x00000200, 0};
+#define BOARD gosper
void print_bw(int x, int y, uint32_t ch) {
tb_set_cell(x, y, ch, TB_BLACK, TB_WHITE);
@@ -64,7 +67,7 @@ void print_board(uint32_t board[], int rows, int cols) {
}
uint32_t *get_next_state(uint32_t board[], int rows, int cols) {
- uint32_t m[16];
+ uint32_t m[MAX_ROWS];
for(int r=0; r < rows; r++) {
uint32_t newRow = board[r];
/* For a single byte for example, this value will be 1000 0000.*/
@@ -104,18 +107,22 @@ uint32_t *get_next_state(uint32_t board[], int rows, int cols) {
/* }*/
/* printf("\n");*/
/* }*/
- static uint32_t tmp[16];
+ static uint32_t tmp[MAX_ROWS];
memcpy(tmp, m, sizeof m);
return tmp;
}
void play(uint32_t board[], int rows, int cols) {
+ struct tb_event ev;
tb_init();
- int rounds = 52;
- while(rounds-->0) {
+/* int rounds = 56;*/
+/* while(rounds-->0) {*/
+ while(1) {
tb_clear();
print_board(board, rows, cols);
- sleep(1);
+ if (tb_poll_event(&ev) == TB_OK && ev.type == TB_EVENT_KEY)
+ if(ev.key == TB_KEY_CTRL_C)
+ return;
uint32_t *l2 = get_next_state(board, rows, cols);
memcpy(board, l2, (sizeof *l2) * rows );
}
@@ -126,15 +133,15 @@ void play(uint32_t board[], int rows, int cols) {
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");*/
+ size_t paddingRows = MAX_ROWS - rows;
+ if (paddingRows > 0)
+ memset(BOARD + rows, 0, paddingRows * (sizeof BOARD[0]));
+
play(BOARD, rows, cols);
+ tb_shutdown();
+ return 0;
/* uint32_t *lp = (uint32_t*)malloc(5 * sizeof(uint32_t));*/
/* uint32_t *mp = play(l);*/
/* uint32_t *np = play(mp);*/
/* play(np);*/
- tb_shutdown();
- return 0;
}