The Game of Life

http://d.hatena.ne.jp/giveup/20080505

もう少し練ってみた。

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>

#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))

void l(_Bool mas[2][10][10],int o,int n) {
  for(int x=0;x<10;x++)
    for(int c,y=0;c=0,y<10;mas[n][x][y]=c==3||(c==4&&mas[o][x][y]),y++)
      for(int xx=MAX(x-1,0);xx<=MIN(x+1,9);xx++)
        for(int yy=MAX(y-1,0);yy<=MIN(y+1,9);yy++)
          c+=mas[o][xx][yy];
}

void p(_Bool (*o)[10],int step) {
  printf("\x1b\x5b\x32\x4a\x1b\x5b\x48%d step\n",step);
  for(int x=0;x<10;x++,putchar('\n'))
    for(int y=0;y<10;y++)
      putchar("-O"[o[x][y]]);
}

int main(void) {
  _Bool mas[2][10][10] = {
    {
      {0,1,0,0,1,0,0,1,0,0},
      {0,0,1,0,0,0,1,1,0,0},
      {0,1,1,1,0,0,0,1,1,0},
      {0,1,0,0,0,1,1,0,1,0},
      {0,0,0,1,1,0,0,0,0,0},
      {0,1,1,1,0,0,0,0,1,1},
      {0,0,1,0,1,0,0,1,0,0},
      {1,1,1,0,0,0,1,0,1,0},
      {0,0,0,0,1,0,0,0,0,1},
      {0,0,1,1,1,0,0,1,0,0},
    },
  };

  for(int i=0,o=0,n=1;i<100&&memcmp(mas[0],mas[1],100*sizeof(_Bool));i++,n^=o^=n^=o) {
    l(mas,o,n);
    p(mas[o],i);
    usleep(100000);
  }
  return 0;
}