Skip to content

Instantly share code, notes, and snippets.

@ytaki0801
Created January 31, 2023 12:08
Show Gist options
  • Save ytaki0801/0f3c3923669cbf3161e520a1c931d900 to your computer and use it in GitHub Desktop.
Save ytaki0801/0f3c3923669cbf3161e520a1c931d900 to your computer and use it in GitHub Desktop.
Elementary Cellular Automata in C
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define CN 180
#define ST 80
int main(void)
{
char r[8], c[CN], cn[CN], b, x, y, z;
for (int rule = 0; rule < 256; rule++) {
for (b = rule, x = 0; x < 8; x++) { r[x] = b & 0x01 ? 1 : 0; b >>= 1; }
for (int i = 0; i < CN; i++) c[i] = 0; c[CN/2] = 1;
system("clear");
for (int j = 0; j < ST; j++) {
for (int i = 0; i < CN; i++) printf("%c", c[i] ? '*' : ' ');
printf("\n");
for (int i = 0; i < CN; i++)
cn[i] = r[(i == 0 ? 0 : c[i-1])*4+c[i]*2+(i == CN-1 ? 0 : c[i+1])];
for (int i = 0; i < CN; i++) c[i] = cn[i];
}
usleep(300000);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment