#include <SDL2/SDL.h>
#include <stdio.h>
#include "array.h"
+#include "tick.h"
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;
--- /dev/null
+#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;
+}