Ad

Changed game of life to take an array of arrays of rows and columns.

Code
Diff
  • function nextGeneration(grid) {
      const neighborsInit = {
        l:true,bl:true,bc:true,br:true,r:true,tr:true,tc:true,tl:true
      };
      let neighbors = JSON.parse(JSON.stringify(neighborsInit));
      let liveNeighbors = 0;
      let isAlive = true;
      var nextGen = JSON.parse(JSON.stringify(grid));
      grid.map(function(row,rowIndex,rows){
        row.map(function(element,index,thisRow){
          liveNeighbors = 0;
          neighbors = JSON.parse(JSON.stringify(neighborsInit));
          if(element == 1){
            isAlive = true;
          } else {
            isAlive = false;
          }
    
          //Where to check
          if(rowIndex === 0){
            //If it is the first row don't look for life above
            neighbors.tr = neighbors.tc = neighbors.tl = false;
          }
          if(rowIndex === grid.length-1){
            //If it is the last row don't look for life below
            neighbors.br = neighbors.bc = neighbors.bl = false;
          }
          if(index == 0){
            //If it is the first column don't look for life left
            neighbors.tl = neighbors.l = neighbors.bl = false;
          }
          if(index == row.length-1){
            //If it is the last column don't look for life right
            neighbors.tr = neighbors.r = neighbors.br = false;
          }
          //Top
          if(neighbors.tl && grid[rowIndex-1][index-1] == 1){
            liveNeighbors++;
          }
          if(neighbors.tc && grid[rowIndex-1][index] == 1){
            liveNeighbors++;
          }
          if(neighbors.tr && grid[rowIndex-1][index+1] == 1){
            liveNeighbors++;
          }
          //right
          if(neighbors.r && grid[rowIndex][index+1] == 1){
            liveNeighbors++;
          }
          //Bottom
          if(neighbors.br && (grid[rowIndex+1][index+1] === 1)){
            liveNeighbors++;
          }
          if(neighbors.bc && grid[rowIndex+1][index] == 1){
            liveNeighbors++;
          }
          if(neighbors.bl && grid[rowIndex+1][index-1] == 1){
            liveNeighbors++;
          }
          //left
          if(neighbors.l && grid[rowIndex][index-1] == 1){
            liveNeighbors++;
          }
    
          //Live?
          if(isAlive){
    
            //Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
            if(liveNeighbors < 2){
              //kill
              //console.log('die from lonliness :(');
              nextGen[rowIndex][index] = 0;
            }
            //Any live cell with more than three live neighbours dies, as if by overcrowding.
            if(liveNeighbors > 3){
              //kill
              //console.log('died from over crowding');
              nextGen[rowIndex][index] = 0;
            }
          }
          //Any dead cell with exactly three live neighbours becomes a live cell.
          if(!isAlive && liveNeighbors == 3){
            nextGen[rowIndex][index] = 1;
          } else {
            //console.info('he stay ded');
          }
        });
      });
      return nextGen;
    }
    • function nextGeneration(grid) {
    • return grid;
    • const neighborsInit = {
    • l:true,bl:true,bc:true,br:true,r:true,tr:true,tc:true,tl:true
    • };
    • let neighbors = JSON.parse(JSON.stringify(neighborsInit));
    • let liveNeighbors = 0;
    • let isAlive = true;
    • var nextGen = JSON.parse(JSON.stringify(grid));
    • grid.map(function(row,rowIndex,rows){
    • row.map(function(element,index,thisRow){
    • liveNeighbors = 0;
    • neighbors = JSON.parse(JSON.stringify(neighborsInit));
    • if(element == 1){
    • isAlive = true;
    • } else {
    • isAlive = false;
    • }
    • //Where to check
    • if(rowIndex === 0){
    • //If it is the first row don't look for life above
    • neighbors.tr = neighbors.tc = neighbors.tl = false;
    • }
    • if(rowIndex === grid.length-1){
    • //If it is the last row don't look for life below
    • neighbors.br = neighbors.bc = neighbors.bl = false;
    • }
    • if(index == 0){
    • //If it is the first column don't look for life left
    • neighbors.tl = neighbors.l = neighbors.bl = false;
    • }
    • if(index == row.length-1){
    • //If it is the last column don't look for life right
    • neighbors.tr = neighbors.r = neighbors.br = false;
    • }
    • //Top
    • if(neighbors.tl && grid[rowIndex-1][index-1] == 1){
    • liveNeighbors++;
    • }
    • if(neighbors.tc && grid[rowIndex-1][index] == 1){
    • liveNeighbors++;
    • }
    • if(neighbors.tr && grid[rowIndex-1][index+1] == 1){
    • liveNeighbors++;
    • }
    • //right
    • if(neighbors.r && grid[rowIndex][index+1] == 1){
    • liveNeighbors++;
    • }
    • //Bottom
    • if(neighbors.br && (grid[rowIndex+1][index+1] === 1)){
    • liveNeighbors++;
    • }
    • if(neighbors.bc && grid[rowIndex+1][index] == 1){
    • liveNeighbors++;
    • }
    • if(neighbors.bl && grid[rowIndex+1][index-1] == 1){
    • liveNeighbors++;
    • }
    • //left
    • if(neighbors.l && grid[rowIndex][index-1] == 1){
    • liveNeighbors++;
    • }
    • //Live?
    • if(isAlive){
    • //Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
    • if(liveNeighbors < 2){
    • //kill
    • //console.log('die from lonliness :(');
    • nextGen[rowIndex][index] = 0;
    • }
    • //Any live cell with more than three live neighbours dies, as if by overcrowding.
    • if(liveNeighbors > 3){
    • //kill
    • //console.log('died from over crowding');
    • nextGen[rowIndex][index] = 0;
    • }
    • }
    • //Any dead cell with exactly three live neighbours becomes a live cell.
    • if(!isAlive && liveNeighbors == 3){
    • nextGen[rowIndex][index] = 1;
    • } else {
    • //console.info('he stay ded');
    • }
    • });
    • });
    • return nextGen;
    • }