Move History

Rooted by: Game of Life
Fork Selected
  • Code
    function getNeighbors(grid, i){
      var neighborHash = [
        [1, 3, 4], [0, 2, 3, 4, 5], [1, 4, 5],
        [0, 1, 4, 6, 7], [0, 1, 2, 3, 5, 6, 7, 8], [1, 2, 4, 7, 8],
        [3, 4, 7], [3, 4, 5, 6, 8], [4, 5, 7]
      ];
      var result = 0;
      var neighbors = neighborHash[i];
      for(var j = 0; j < neighbors.length; j++){
        if(grid[neighbors[j]] === 1){
          result++;
        }
      }
      return result;
    }
    
    function nextGeneration(grid) {
      // grid that gets returned
      if(grid.length === 0){
        return [];
      }
      newGrid = [
        0,0,0,
        0,0,0,
        0,0,0
      ];
      for(var i = 0; i < newGrid.length; i++){
        // gets the number of neighbors that are alive for a given cell from the current generation
        var neighbors = getNeighbors(grid, i);
        // these if statements determine whether a cell is alive or dead in the next generation
        if(neighbors > 3 || neighbors < 2){
          newGrid[i] = 0;
        }
        if(grid[i] === 0 && neighbors === 3){
          newGrid[i] = 1;
        }
        if(grid[i] === 1 && (neighbors === 2 || neighbors === 3)){
          newGrid[i] = 1
        }
      }
      return newGrid;
    }
    Test Cases Failed
    describe("Given empty grid", () => {
      it("when next generation, should return empty", () => {
        assert.deepEqual(nextGeneration([]), []);
      });
    });
    
    describe("Given a single cell", () => {
      it("when next generation, should die", () => {
        assert.deepEqual(nextGeneration([
          0,0,0,
          0,1,0,
          0,0,0,
        ]), [
          0,0,0,
          0,0,0,
          0,0,0,
        ]);
      });
    });
    
    describe("Given a cell with neighbors", () => {
      it("when next generation, it should live", () => {
        assert.deepEqual(nextGeneration([
          0,1,0,
          0,1,0,
          0,1,0,
        ]), [
          0,0,0,
          1,1,1,
          0,0,0,
        ]);
      });
    });
    
    describe("Given a cell with many neighbors", () => {
      it("when next generation, overcrowded cells should die", () => {
        assert.deepEqual(nextGeneration([
          1,1,1,
          1,1,1,
          1,1,1,
        ]), [
          1,0,1,
          0,0,0,
          1,0,1,
        ]);
      });
    });
    
  • Code
    • function getNeighbors(grid, i){
    • var neighborHash = [
    • [1, 3, 4], [0, 2, 3, 4, 5], [1, 4, 5],
    • [0, 1, 4, 6, 7], [0, 1, 2, 3, 5, 6, 7, 8], [1, 2, 4, 7, 8],
    • [3, 4, 7], [3, 4, 5, 6, 8], [4, 5, 7]
    • ];
    • var result = 0;
    • var neighbors = neighborHash[i];
    • for(var j = 0; j < neighbors.length; j++){
    • if(grid[neighbors[j]] === 1){
    • result++;
    • }
    • }
    • return result;
    • }
    • function nextGeneration(grid) {
    • return grid;
    • // grid that gets returned
    • if(grid.length === 0){
    • return [];
    • }
    • newGrid = [
    • 0,0,0,
    • 0,0,0,
    • 0,0,0
    • ];
    • for(var i = 0; i < newGrid.length; i++){
    • // gets the number of neighbors that are alive for a given cell from the current generation
    • var neighbors = getNeighbors(grid, i);
    • // these if statements determine whether a cell is alive or dead in the next generation
    • if(neighbors > 3 || neighbors < 2){
    • newGrid[i] = 0;
    • }
    • if(grid[i] === 0 && neighbors === 3){
    • newGrid[i] = 1;
    • }
    • if(grid[i] === 1 && (neighbors === 2 || neighbors === 3)){
    • newGrid[i] = 1
    • }
    • }
    • return newGrid;
    • }
    Test Cases
    • describe("Given empty grid", () => {
    • it("when next generation, should return empty", () => {
    • assert.deepEqual(nextGeneration([]), []);
    • });
    • });
    • describe("Given a single cell", () => {
    • it("when next generation, should die", () => {
    • assert.deepEqual(nextGeneration([
    • 0,0,0,
    • 0,1,0,
    • 0,0,0,
    • ]), [
    • 0,0,0,
    • 0,0,0,
    • 0,0,0,
    • ]);
    • });
    • });
    • });
    • describe("Given a cell with neighbors", () => {
    • it("when next generation, it should live", () => {
    • assert.deepEqual(nextGeneration([
    • 0,1,0,
    • 0,1,0,
    • 0,1,0,
    • ]), [
    • 0,0,0,
    • 1,1,1,
    • 0,0,0,
    • ]);
    • });
    • });
    • describe("Given a cell with many neighbors", () => {
    • it("when next generation, overcrowded cells should die", () => {
    • assert.deepEqual(nextGeneration([
    • 1,1,1,
    • 1,1,1,
    • 1,1,1,
    • ]), [
    • 1,0,1,
    • 0,0,0,
    • 1,0,1,
    • ]);
    • });
    • });