Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <button type="submit" class="play">Play</button>
<button type="submit" class="pause">Pause</button>
<button type="submit" class="clear">Clear</button>
Generation: &nbsp;<p class="gen">0</p>
<div id="render-target"></div>
              
            
!

CSS

              
                .cell {
  width: 30px;
  height: 30px;
  border-color: black;
  border-style: solid;
  border-width: 1px;
  display: inline-block;
}
.activeButton {
  background-color: red;
}
.dead{
  background-color: white;
}
.alive {
  background-color: green !important;
}
.gen {
  display: inline-block;
}
.box {
  line-height: 0 !important;
  font-size: 0 !important;
  width: 310px;
}
#render-target {
  margin-top: 20px;
  line-height: 0;
  width: 330px;
}
              
            
!

JS

              
                  //Initial variables that are going to be put in use later
  
  let gameBoard = [];
  let width = 20;
  let height = 20;
  let cells = width*height;
  let running = 0;
  let speed = 30;
  let generation = 0;

$(document).ready(function() {
    clearGameBoard(); //Starts a blank board
    initialSetup(); //Sets up a random set of cells to start
    createGameBoard(); //Creates the actual game board
    activateGameBoard(); //Initializes everything
    running = 1; //Sets everything and runs it
    runGame();
});




function clearGameBoard() {
  gameBoard = [];
  for (var i = 0; i<cells; i++) {
    gameBoard[i] = {id: i, life: "cell dead"};
  }
  generation = 0;
  $('.gen').val(0);
}


class CellGrid extends React.Component {
      constructor(props){
        super(props);
      }
      update() {
        this.setState({cellBoard: this.props.board});
        console.log("State has been updated");
      }
      render() {
        return (
				<div>
					{this.props.board.map(function(cell, i) {
						return(<div className={cell.life} key={i} id={i}></div>);
					})}
				</div>
			  );
      }
}

function createGameBoard() {
  $('#render-target').empty();
  drawGameBoard(); 
};



function oneGen() {
  let newBoard = [];
  let cellLife = "";
  //The next loop will determine life on neighbors
  for (var i = 0; i<cells; i++) {
    newBoard.push({id: i, life:"cell dead"});
    let neighbors = checkForNeighbors(i);
    if ((gameBoard[i].life == "cell alive") && (neighbors == 3 || neighbors == 2)) {
     newBoard[i] = {id: i, life:"cell alive"};
    }
    if ((gameBoard[i].life == "cell dead") && (neighbors == 3)) {
      newBoard[i] = {id: i, life: "cell alive"};
    }    
  }
  
  for(var i = 0; i<cells; i++) {
    if (gameBoard[i].life == "cell alive") {
      break;
    }
    if (i == cells-1) { //Ends the game if every cell is dead
      alert("Everyone is dead!");
      running = 0;
      clearGameBoard();
      drawGameBoard();
    }
  }
  return newBoard;
}



//Now we draw the game board

function drawGameBoard(passedBoard) {
  ReactDOM.render(<CellGrid board={gameBoard} generation={generation}/>, document.getElementById('render-target'));
}



function activateGameBoard() {
  $('.cell').click(function() {
    var num = $(this).attr('id');
    if (gameBoard[num].life == "cell alive") {
      gameBoard[num].life = "cell dead";
    } else {
      gameBoard[num].life = "cell alive";
    }
    drawGameBoard();
  });
  
  $('.clear').click(function() {
    running = 0;
    generation = 0;
    clearGameBoard();
    drawGameBoard();
    $('.gen').val(0);
  });


$('.play').click(function() {
  running = 1;
  runGame();
});


$('.pause').click(function() {
  running = 0;
});

}
  

function runGame() {
  if (running == 1) {
    setTimeout(function () {
      generation++;
      gameBoard = oneGen();
    }, speed);
  }
  
}


function checkForNeighbors(i) {
  var neighbors = 0;
  var border = 0;
  
//First check for cells going from top that need to be wrapped around to the bottom 
  if (i>=0 && i<=(width-1)) {
    border = 1;
    let topToBottom = width-i;
    //This variable puts each cell in the same column on the bottom row coming from the top
    
    //top center bottom line
    if (gameBoard[cells-topToBottom].life == "cell alive") {
      neighbors++;
    }
    //top left bottom line
    if (i != 0 && gameBoard[cells-topToBottom-1].life == "cell alive") {
      neighbors++;
    }
    //Top right bottom line
    if (i != (width-1) && gameBoard[cells-topToBottom+1].life == "cell alive") {
      neighbors++;
    }
    //Bottom center on line 2
    if(gameBoard[i+width].life == "cell alive") {
      neighbors++;
    }
    //Bottom left
    if(i!=0 && gameBoard[i+width-1].life == "cell alive") {
      neighbors++;
    }
    //Bottom right
    if(i!=(width-1) && gameBoard[i+width+1].life == "cell alive") {
      neighbors++;
    }
    //Direct right
    if(i!=(width-1) && gameBoard[i+1].life=="cell alive") {
      neighbors++;
    }
    //Direct left
    if(i!=0 && gameBoard[i-1].life=="cell alive") {
      neighbors++;
    }
    
  }
  
  //Then check for cells on the bottom line that need to be wrapped to the top
  if (i>=(cells-width) && i<= (cells-1)) {
    border = 1;
    let bottomToTop = i + width - cells; 
    //this variable positions (i) in the position exactly in the same column in the first row
    
    //Bottom center
    if(gameBoard[bottomToTop].life == "cell alive") {
      neighbors++;
    }
    //Bottom left
    if(i != (cells-width) && gameBoard[bottomToTop-1].life == "cell alive") {
      neighbors++;
    }
    //Bottom right
    if(i != cells-1 && gameBoard[bottomToTop + 1].life == "cell alive") {
      neighbors++;
    }
    //Top left
    if(i != (cells-width) && gameBoard[i-width-1].life=="cell alive") {
      neighbors++;
    }
    //Top Right
    if(i != (cells-1) && gameBoard[i - width + 1].life == "cell alive") {
      neighbors++;
    }
    //Top Center
    if(gameBoard[i-width].life == "cell alive") {
      neighbors++;
    }
    //Direct right
    if(i!=(cells-1) && gameBoard[i+1].life == "cell alive") {
      neighbors++;
    }
    //Direct left
    if(i!=(cells-width) && gameBoard[i-1].life == "cell alive") {
      neighbors++;
    }   
  }
  
  //This covers the right wrap: pseudo: if cell to the right of current cell is exactly 1 width away from the first cell in that row, then that cell is now the first cell in that row
  if(((i+1) % width) == 0) {
    border = 1;
    
    //Directly right
    if (gameBoard[i-width+1].life == "cell alive") {
      neighbors++;
    }
    //Low right
    if (i!=cells-1 && gameBoard[i+1].life == "cell alive") {
      neighbors++;
    }
    //Bottom right corner to top left corner
    if (i==(cells-1) && gameBoard[0].life == "cell alive") {
      neighbors++;
    }
    //Top right
    if (i > width && gameBoard[i - (2*width) + 1].life =="cell alive") {
      neighbors++;
    }
    //Top right corner to bottom left corner
    if(i == width - 1 && gameBoard[cells-width].life == "cell alive") {
      neighbors++;
    }
    //Top center
    if (i!= width - 1 && i!= cells - 1 && gameBoard[i-width].life == "cell alive") {
      neighbors++;
    }
    //Bottom center
    if (i!= width - 1 && i!= cells - 1 && gameBoard[i+width].life == "cell alive") {
      neighbors++;
    }
    //Bottom left
    if (i!= width - 1 && i!= cells - 1 && gameBoard[i+width-1].life == "cell alive") {
      neighbors++;
    }
    //Direct left
    if (i!= width - 1 && i!= cells - 1 && gameBoard[i-1].life == "cell alive") {
      neighbors++;
    }
    //Top left
    if (i!= width - 1 && i!= cells - 1 && gameBoard[i-width-1].life=="cell alive") {
      neighbors++;
    }
    
  }
  
  //This is a special case for cells that are on the left wall that need to be moved over
  if(((i % width) == 0) || i==0) {
    border = 1;
    //Direct left
    if (gameBoard[i+width-1].life == "cell alive") {
      neighbors++;
    }
    //Bottom left
    if (i!=(cells-width) && gameBoard[i+(width*2) -1].life == "cell alive") {
      neighbors++;
    }
    //Bottom left corner to top right corner
    if (i==(cells-width) && gameBoard[width-1].life == "cell alive") {
      neighbors++;
    }
    //Top left
    if (i >= width && gameBoard[i-1].life == "cell alive") {
      neighbors++;
    }
    //Top left corner to bottom right corner
    if (i == 0 && gameBoard[cells - 1].life == "cell alive") {
      neighbors++;
    }
    //Top center
    if ((i != (width+1)) && (i!= (cells-width)) && i!= 0 && (gameBoard[i-width].life == "cell alive")) {
      neighbors++;
    }
    //Bottom center
    if (i< cells-width && i!=0 && gameBoard[i+width].life == "cell alive") {
      neighbors++;
    }
    //Direct right
    if (i != 0 && i!= (cells-width) && gameBoard[i+1].life == "cell alive") {
      neighbors++;
    }
    //Bottom right
    if (i<(cells-width) && i != 0 && gameBoard[i+width+1]) {
      neighbors++;
    }
    //Top right
    if (i != 0 && i != (cells - width) && gameBoard[i - width + 1].life == "cell alive") {
        neighbors++;
		}
  }
  
  //Normal cases
  if (border == 0) {
    if (gameBoard[i-width].life == "cell alive") {
      neighbors++;
    }
    if (gameBoard[i-width-1].life == "cell alive") {
      neighbors++;
    }
    if (gameBoard[i-width+1].life == "cell alive") {
      neighbors++;
    }
    if (gameBoard[i-1].life == "cell alive") {
      neighbors++;
    }
    if (gameBoard[i+1].life == "cell alive") {
      neighbors++;
    }
    if (gameBoard[i+width-1].life == "cell alive") {
      neighbors++;
    }
    if (gameBoard[i+width].life == "cell alive") {
      neighbors++;
    }
    if (gameBoard[i+width+1].life == "cell alive") {
      neighbors++;
    }
  }
  return neighbors;
}



//This method randomizes the initial state of the program.
function initialSetup() {
  for (var i = 0; i<cells; i++) {
    var Randoms = Math.floor(Math.random()*2);
    if (Randoms == 0) {
      gameBoard[i] = {id:i, life: "cell alive"};
    } else {
      gameBoard[i] = {id:i, life: "cell dead"};
    }
  }
}  

              
            
!
999px

Console