From cfaa794b19d81130959062a0806ea2e95f31f44d Mon Sep 17 00:00:00 2001 From: Maximilian Friedersdorff Date: Fri, 31 Mar 2017 15:39:40 +0100 Subject: [PATCH] MINIMAL implementation. Single glider gun --- .gitignore | 1 + Makefile | 4 ++-- array.c | 12 ++++++++++ array.h | 4 ++++ gol | Bin 15496 -> 0 bytes gol.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tick.c | 49 +++++++++++++++++++++++++++++++++++++ tick.h | 12 ++++++++++ 8 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 .gitignore delete mode 100755 gol create mode 100644 tick.c create mode 100644 tick.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..58a5ef7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +gol diff --git a/Makefile b/Makefile index 7bd0d2c..0a24b72 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,2 @@ -gol: gol.c array.c array.h - $(CC) -o gol gol.c array.c --std=gnu99 -lSDL2 +gol: gol.c array.c tick.c array.h tick.h + $(CC) -o gol gol.c array.c tick.c --std=gnu99 -lSDL2 diff --git a/array.c b/array.c index 469be38..c70c570 100644 --- a/array.c +++ b/array.c @@ -38,5 +38,17 @@ int gol_vivify(struct gol_board *const state, int_least32_t x, int_least32_t y) state->live_cells[state->n + 1] = y; state->n += 2; + + if (x > state->max_x) { + state->max_x = x; + } else if (x < state->min_x) { + state->min_x = x; + } + + if (y > state->max_y) { + state->max_y = y; + } else if (y < state->min_y) { + state->min_y = y; + } return 1; } diff --git a/array.h b/array.h index 42c5415..4cbadce 100644 --- a/array.h +++ b/array.h @@ -14,6 +14,10 @@ struct gol_board { int_least32_t *live_cells; uint_least32_t n; uint_least32_t size; + int_least32_t max_x; + int_least32_t min_x; + int_least32_t max_y; + int_least32_t min_y; }; /* diff --git a/gol b/gol deleted file mode 100755 index a0f9c49125f86a7314f1a7fdea3546d87dc591db..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 15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B9^)80;f z+CSOL=Y4*I(s#-``ENm=#va$7_e1=DduLVqY5q`PeXiGcfZ5CEbAxMYOI<6q=3lJO z^m`8d!uf*V(iyi%;mvwXX+LN$pZ62veoKYx$r-R7^Z$$pVcdTHEif#*|C`c(*rDKH1^FBk=Cohk7OPa+N81ClOh1kY$L(^*@3**dJBYZ`ex%E5 zk6m7P{tW+M_?@bLKI-#=O=dF`-v9;=cji C4fp>5 diff --git a/gol.c b/gol.c index 0ad687d..eba6914 100644 --- a/gol.c +++ b/gol.c @@ -1,6 +1,7 @@ #include #include #include "array.h" +#include "tick.h" void quit(int e_st) { @@ -8,8 +9,76 @@ void quit(int e_st) exit(e_st); } +void init_state(struct gol_board *state); + +void init_state(struct gol_board *state) { + // Create the initial state. This is a glider gun (hopefully) + gol_vivify(state, 12, 0); + gol_vivify(state, 13, 0); + gol_vivify(state, 11, 1); + gol_vivify(state, 15, 1); + gol_vivify(state, 10, 2); + gol_vivify(state, 16, 2); + gol_vivify(state, 24, 2); + gol_vivify(state, 0, 3); + gol_vivify(state, 1, 3); + gol_vivify(state, 10, 3); + gol_vivify(state, 14, 3); + gol_vivify(state, 16, 3); + gol_vivify(state, 17, 3); + gol_vivify(state, 22, 3); + gol_vivify(state, 24, 3); + gol_vivify(state, 0, 4); + gol_vivify(state, 1, 4); + gol_vivify(state, 10, 4); + gol_vivify(state, 16, 4); + gol_vivify(state, 20, 4); + gol_vivify(state, 21, 4); + gol_vivify(state, 11, 5); + gol_vivify(state, 15, 5); + gol_vivify(state, 20, 5); + gol_vivify(state, 21, 5); + gol_vivify(state, 34, 5); + gol_vivify(state, 35, 5); + gol_vivify(state, 12, 6); + gol_vivify(state, 13, 6); + gol_vivify(state, 20, 6); + gol_vivify(state, 21, 6); + gol_vivify(state, 34, 6); + gol_vivify(state, 35, 6); + gol_vivify(state, 22, 7); + gol_vivify(state, 24, 7); + gol_vivify(state, 24, 7); +} + int main(int argc, char* args[]) { + struct gol_board state = { + .live_cells = malloc(10 * sizeof(typeof(*state.live_cells))), + .n = 0, + .size = 10, + .max_x = 0, + .min_x = 0, + .max_y = 0, + .min_y = 0 + }; + if (!state.live_cells) { + return 1; + } + + init_state(&state); + + for (unsigned int i = 0; i < 1000; ++i) { + printf("Generation %d\n", i); + for (unsigned int j = 0; j < state.n; j += 2) { + printf("(%d, %d)\n", state.live_cells[j], + state.live_cells[j + 1]); + } + printf("\n"); + gol_tick(&state); + } + + SDL_Window* window = NULL; SDL_Surface* screen = NULL; diff --git a/tick.c b/tick.c new file mode 100644 index 0000000..07cc195 --- /dev/null +++ b/tick.c @@ -0,0 +1,49 @@ +#include +#include +#include "tick.h" + +int gol_tick(struct gol_board *state) +{ + struct gol_board tmp_state; + tmp_state = (struct gol_board ) { + .live_cells = malloc(10 * sizeof(typeof(*tmp_state.live_cells))), + .n = 0, + .size = 10, + .max_x = 0, + .min_x = 0, + .max_y = 0, + .min_y = 0 + }; + if (!tmp_state.live_cells) { + return 0; + } + unsigned char n_live_adj; + for (int i = state->min_x; i < state->max_x; ++i) { + for (int j = state->min_y; j < state->max_y; ++j) { + n_live_adj = gol_is_live(state, i - 1, j - 1) + + gol_is_live(state, i, j - 1) + + gol_is_live(state, i + 1, j - 1) + + gol_is_live(state, i - 1, j) + + gol_is_live(state, i + 1, j) + + gol_is_live(state, i - 1 , j + 1) + + gol_is_live(state, i, j + 1) + + gol_is_live(state, i + 1, j + 1); + + switch (n_live_adj) { + case (3): + gol_vivify(&tmp_state, i, j); + break; + case(2): + if (gol_is_live(state, i, j)) { + gol_vivify(&tmp_state, i, j); + } + default: + break; + } + + } + } + free(state->live_cells); + *state = tmp_state; + return 1; +} diff --git a/tick.h b/tick.h new file mode 100644 index 0000000..9603cca --- /dev/null +++ b/tick.h @@ -0,0 +1,12 @@ +#ifndef TICK_H +#define TICK_H + +#include "array.h" + +/* + * Calculates the state of a game of life after the next tick, based + * on the passed state. It modifies the passed state. + */ +int gol_tick(struct gol_board *state); + +#endif -- 2.46.2