]> git.friedersdorff.com Git - max/gol.git/commitdiff
MINIMAL implementation. Single glider gun
authorMaximilian Friedersdorff <max@friedersdorff.com>
Fri, 31 Mar 2017 14:39:40 +0000 (15:39 +0100)
committerMaximilian Friedersdorff <max@friedersdorff.com>
Fri, 31 Mar 2017 14:39:40 +0000 (15:39 +0100)
.gitignore [new file with mode: 0644]
Makefile
array.c
array.h
gol [deleted file]
gol.c
tick.c [new file with mode: 0644]
tick.h [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..58a5ef7
--- /dev/null
@@ -0,0 +1 @@
+gol
index 7bd0d2c3399256e20bb312660e87ac53dfad8635..0a24b72501f920c2c3d1197d65cce2fdcd56a63d 100644 (file)
--- 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 469be38b088c646e5b266806c80a40b491747e3c..c70c5707a08a295a5ed600f43073bfdaadd52e18 100644 (file)
--- 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 42c5415b24337d219e9da0d9d9c9fb40b8402b58..4cbadce0680b4ac767a303e9997fa86e266e59f0 100644 (file)
--- 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 (executable)
index a0f9c49..0000000
Binary files a/gol and /dev/null differ
diff --git a/gol.c b/gol.c
index 0ad687da31ac3e0ca85e7e1f6373ce051524bce6..eba691465dce3df4c21208363d93c9ce50590eba 100644 (file)
--- a/gol.c
+++ b/gol.c
@@ -1,6 +1,7 @@
 #include <SDL2/SDL.h>
 #include <stdio.h>
 #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 (file)
index 0000000..07cc195
--- /dev/null
+++ b/tick.c
@@ -0,0 +1,49 @@
+#include <stdlib.h>
+#include <stdio.h>
+#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 (file)
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