From: Maximilian Friedersdorff Date: Fri, 31 Mar 2017 14:39:40 +0000 (+0100) Subject: MINIMAL implementation. Single glider gun X-Git-Url: https://git.friedersdorff.com/?a=commitdiff_plain;h=cfaa794b19d81130959062a0806ea2e95f31f44d;p=max%2Fgol.git MINIMAL implementation. Single glider gun --- 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 a0f9c49..0000000 Binary files a/gol and /dev/null differ 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