title: game of life in C
body: <p>Just thought I'd take a crack at Conway's Game of Life, but am seriously struggling...which is a surprise! Can someone maybe hint at algorithmic issues? Just a small nudge? This ain't homework.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #define HEIGHT 10
  #define WIDTH 10
  
  int *gb[HEIGHT];
  int *gb2[HEIGHT];
  
  void copy() {
    int i, j;
    for (i = j = 0; i &lt; HEIGHT; i++) {
      for (; j &lt; WIDTH; j++) {
        gb2[i][j] = gb[i][j];
      }
    }
  }
  
  void init() {
    int i, j;
    for (i = 0; i &lt; HEIGHT; i++) {
      gb [i] = malloc(sizeof(int)*WIDTH);
      gb2[i] = malloc(sizeof(int)*WIDTH);
    }
    for (i = j = 0; i &lt; HEIGHT; i++) {
      for (; j &lt; WIDTH; j++) {
        gb [i][j] = 0;
      }
    }
    gb[0][0] = 1;
    gb[0][1] = 1;
    gb[1][0] = 1;
    gb[1][1] = 1;
    copy();
  }
  
  void printg() {
    int i, j;
    printf("    ");
    for (i = 0; i &lt; WIDTH; i++) {
      printf("%2d  ", i);
    }
    printf("\n\n");
    for (i = 0; i &lt; HEIGHT; i++) {
      printf("%d   ", i);
      for (j = 0; j &lt; WIDTH; j++) {
        printf(" %c  ", gb2[i][j]?'+':'-');
      }
      printf("\n");
    }
  }
  
  void ckill(int i, int j) {
    gb2[i][j] = 0;
  }
  
  void clive(int i, int j) {
    gb2[i][j] = 1;
  }
  
  void newgen() {
    int i, j = i = 1, n = 0;
    for (; i &lt; HEIGHT-1; i++) {
      for (j = 0; j &lt; WIDTH-1; j++) {
        if (gb[i][j+1]) n++;
        if (gb[i+1][j]) n++;
        if (gb[i+1][j+1]) n++;
        if (gb[i-1][j-1]) n++;
        if (gb[i][j-1]) n++;
        if (gb[i-1][j]) n++;
        if (gb[i+1][j-1]) n++;
        if (gb[i-1][j+1]) n++;
  
        if (n &lt; 2) ckill(i, j);
        else if ((n == 2 || n == 3) &amp;&amp; gb[i][j]) clive(i, j);
        else if (n &gt; 3) ckill(i, j);
        else if (n == 3 &amp;&amp; gb[i][j] == 0) clive(i, j);
      }
    }
  }
  
  int main() {
    int i;
    init();
    newgen();
    printg();
    for (i = 0; i &lt; HEIGHT; i++) {
      free(gb[i]);
    }
  }
  </code></pre>
  
  <p>This takes no input, but starts off with living cells at (0, 0), (0, 1), (1, 0), and (1, 1). After one generation, it should stay the same, but instead kills the cells so only (0, 0) and (1, 0) are alive.</p>