code: function gameOfLife() {
    for (var i=0; i<grid.columns; i++) {
      buffer[i] = []; // reset row
  
      for (var k=0; k<grid.rows; k++) {
        var cnt = 0; // counter
  
        for ( var m=i-1; m<i+2; m++) { // get the states of surrounding cells
          var mm = (m<0) ? grid.columns-1 : ( (m>=grid.columns) ? m-grid.columns : m );
  
          for ( var n=k-1; n<k+2; n++) {
            var nn = (n<0) ? grid.rows-1 : ( (n>=grid.rows) ? n-grid.rows : n );
            if (mm===i && nn===k) continue;
            else if ( !grid.canFit(mm, nn, 1, 1) ) cnt++;
          }
        }
        buffer[i][k] = (grid.canFit(i, k, 1, 1) && cnt==3) || (!grid.canFit(i, k, 1, 1) && cnt>=2 && cnt<=3);
      }
    }
  }
docs: Conway's game of life