life

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

commit 5970b991fb4becf21de116462c1ea56b848354d3
parent 3d740795a3ca1b1f8ca69a8174f095a31f1e8801
Author: Wilson Gheen <wilson@wilsonrgheen.com>
Date:   Sun, 11 Dec 2022 08:52:32 -0600

Add ability to save and load boards to/from a file

Diffstat:
Aglider | 0
AgliderShootsImmortalFlower | 0
Agosper | 0
Ahline | 0
Mlife.c | 60+++++++++++++++++++++++++++++++++++++++++-------------------
AmodGlider | 0
Asqtoad | 0
Avline | 0
8 files changed, 41 insertions(+), 19 deletions(-)

diff --git a/glider b/glider Binary files differ. diff --git a/gliderShootsImmortalFlower b/gliderShootsImmortalFlower Binary files differ. diff --git a/gosper b/gosper Binary files differ. diff --git a/hline b/hline Binary files differ. diff --git a/life.c b/life.c @@ -5,25 +5,14 @@ #include <stddef.h> #include <stdint.h> #include <string.h> +#include <sys/stat.h> #include <unistd.h> #define WIDTH(x) sizeof(x[0]) * 8 #define MAX_ROWS 24 -static uint64_t glider[MAX_ROWS] = {0, 131072, 65536, 458752, 0, 0, 3, 3}; -static uint64_t modGlider[MAX_ROWS] = {524288, 262144, 1835008, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3}; -static uint64_t vline[MAX_ROWS] = {0, 0, 131072, 131072, 131072, 0, 0, 0}; -static uint64_t what[MAX_ROWS] = {0, 0, 393216, 393216, 393216, 393216, 393216, 0}; -static uint64_t what2[MAX_ROWS] = {0, 0, 262144, 262144, 262144, 262144, 262144, 0}; -static uint64_t what3[MAX_ROWS] = {0, 0, 262144, 262144, 262144, 262144, 0, 0}; -static uint64_t what4[MAX_ROWS] = {0, 0, 983040, 0, 0, 0, 0, 0}; -static uint64_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 uint64_t hline[MAX_ROWS] = {0, 0, 114688, 0, 0, 0, 0, 0}; -static uint64_t sqtoad[MAX_ROWS] = {0, 786432, 786432, 196608, 196608, 0, 0, 0}; static uint64_t gosper[MAX_ROWS] = {0x0000000800000000, 0x0000001400000000, 0x0030001a18000000, 0x0028001b18000000, 0x6604001a00000000, 0x6924001400000000, 0x0604000800000000, 0x0028000000000000, 0x0030000000000000}; -static uint64_t gosperToHalfPulsar[MAX_ROWS] = {0x0000000800000000, 0x0000001400000000, 0x0030001a18000000, 0x0028001b18000000, 0x6604001a00000000, 0x6924001400000000, 0x0604000800000000, 0x0028000000000000, 0x0030000000000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x0000000078000000, 0}; #define BOARD gosper void print_bw(int x, int y, uint64_t ch) { @@ -100,6 +89,14 @@ uint64_t *get_next_state(uint64_t *newBoard, uint64_t board[], int rows, int col memcpy(newBoard, m, sizeof m); } +size_t save_board_to_file(uint64_t *board) { + FILE *file = fopen("gghh1", "wb"); + if(!file) return -1; + size_t z = fwrite(board, sizeof(uint64_t), MAX_ROWS, file); + fclose(file); + return z; +} + void play(uint64_t board[], int rows, int cols) { struct tb_event ev; tb_init(); @@ -107,20 +104,45 @@ void play(uint64_t board[], int rows, int cols) { tb_clear(); print_board(board, rows, cols); if (tb_peek_event(&ev, 300) == TB_OK && ev.type == TB_EVENT_KEY) - if(ev.key == TB_KEY_CTRL_C) + switch(ev.key) { + case TB_KEY_CTRL_C: return; + case TB_KEY_CTRL_S: + save_board_to_file(board); + } get_next_state(board, board, rows, cols); } } +size_t load_board_from_file(uint64_t **board, char *filename) { + *board = calloc(MAX_ROWS, sizeof(uint64_t)); + FILE *file = fopen(filename, "rb"); + if(!file) return -1; + size_t z = fread(*board, sizeof(uint64_t), MAX_ROWS, file); + fclose(file); + return z; +} + int main() { - const size_t rows = sizeof BOARD / sizeof BOARD[0]; - const size_t cols = sizeof BOARD[0] * 8; - size_t paddingRows = MAX_ROWS - rows; - if (paddingRows > 0) - memset(BOARD + rows, 0, paddingRows * (sizeof BOARD[0])); +/* uint64_t *board = BOARD;*/ + uint64_t *board = 0; + char *filename = "gliderShootsImmortalFlower"; + const size_t rows = MAX_ROWS; + const size_t cols = sizeof board[0] * 8; + + if(!board) { + if(load_board_from_file(&board, filename) < 1) { + printf("FATAL: Failed to load board from file %s.\n", filename); + return 1; + } + } + else { + size_t paddingRows = MAX_ROWS - (sizeof board / sizeof board[0]); + if (paddingRows > 0) + memset(board + rows, 0, paddingRows * (sizeof board[0])); + } - play(BOARD, rows, cols); + play(board, rows, cols); tb_shutdown(); return 0; } diff --git a/modGlider b/modGlider Binary files differ. diff --git a/sqtoad b/sqtoad Binary files differ. diff --git a/vline b/vline Binary files differ.