Skip to content
Snippets Groups Projects
Verified Commit b0d71415 authored by raphael.bach's avatar raphael.bach
Browse files

Add sequential `game_of_life.c`

parent a122bef1
No related branches found
No related tags found
No related merge requests found
#include <stdint.h> // uint8_t
#include <stdlib.h> // EXIT_SUCCESS, size_t
#include <string.h> // memcpy()
#define T uint8_t
#define X_LEN 10
#define Y_LEN 10
#define STEP_CNT 16
int main(void)
{
T in[] = {
0,0,0,0,0,0,0,0,0,0,
0,0,1,0,0,0,0,0,0,0,
0,0,0,1,0,0,0,0,0,0,
0,1,1,1,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
};
#define size (sizeof(in)/sizeof(T))
T out[size];
for(size_t step = 0; step < STEP_CNT; step++) {
for(size_t i = 0; i < Y_LEN; i++) {
const size_t idx_y = i * X_LEN;
for(size_t j = 0; j < X_LEN; j++) {
const size_t idx = idx_y + j;
if(i > 0 && i < (Y_LEN-1) && j > 0 && j < (X_LEN-1)) {
const T cnt =
in[idx - 11] + in[idx - 10] + in[idx - 9] +
in[idx - 1] + 0 + in[idx + 1] +
in[idx + 9] + in[idx + 10] + in[idx + 11];
out[idx] = (cnt == 3) || ((in[idx] == 1) && (cnt == 2));
} else {
out[idx] = 0;
}
}
}
memcpy(in, out, size);
}
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment