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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <!-- RENDER REACT APP HERE -->
<div id = "app">
</div>
              
            
!

CSS

              
                /*Style body*/

body {
  background-color: #000a17;
}

/* Style content container */

#app {
  background-color: #fafafa;
  width: 800px;
  margin: auto;
  border-left: 2px solid green;
  border-right: 2px solid green;
  padding-bottom: 10px;
}

/*Style title and generation count */

#title,
#generationCount {
  text-align: center;
  padding: 10px;
  font-weight: 900;
  border-bottom: 3px solid green;
}

#generationCount {
  width: 30%;
  margin: auto;
  border-bottom: 3px solid green;
}

/*Style buttons*/

.buttonsContainer {
  text-align: center;
  width: 700px;
  margin: auto;
  display: block;
}

button {
  width: 20%;
  margin: 5px;
  background-color: darkgreen;
  color: white;
  border: 1px solid darkgreen;
}

button:hover {
  background-color: white;
  color: darkgreen;
  border: 1px solid darkgreen;
}

/*Style board*/

#board {
  width: 704px;
  margin: auto;
  margin-top: 10px;
  margin-bottom: 10px;
  border: 2px solid green;
}

.square {
  width: 10px;
  height: 10px;
  border: 1px solid green;
  text-align: center;
  font-size: 10px;
}

/* Style the rules */

#rules {
  padding: 10px;
  width: 704px;
  margin: auto;
  margin-top: 10px;
  margin-bottom: 5px;
  border: 2px solid green;
}

/* Style signature */

#sign {
  text-align: right;
  font-size: 10px;
  color: black;
  width: 800px;
}

#heart {
  color: pink;
}

              
            
!

JS

              
                /*
Rules of the game:
1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
2. Any live cell with two or three live neighbours lives on to the next generation.
3. Any live cell with more than three live neighbours dies, as if by overpopulation.
4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
*/

//Define the dynamically-determined styles for dead and alive cells

const styles = {
  dead: {
    backgroundColor: '#fafafa',
  },
  alive: {
    backgroundColor: 'darkgreen',
  },
};

// Define the size of the board

var length = 70;
var width = 50;

//Define the generation change interval

var interval = 500;

//Generate a board with randomly created dead (0) and alive (1) cells

  var board= [];
  var newBoard = [];

  function generateBoard (l, w) {
  for (var i=0; i<l*w; i++)  {
  var x = Math.floor(Math.random() * 2);
  board.push(x); }
  };

  generateBoard (length, width);


//Define the rules of the game

function play (l, w){
  for (var i=0; i<l*w; i++)  {

//Check the sum of the surrounding cells 
//NOTE: the board acts as a continuum 
    
  var sum;
    
//Check the sum in the CORNERS
    
    //top-left    
  if(i==0) {
      sum = 
        board[1]+
        board[l+1]+
        board[l]+
        board[l*(w-1)]+
        board[l*(w-1)+1]+
        board[l*w-1]+
        board[l-1]+
        board[l*2-1];
    }
    
    //top-right
   else if (i==l-1) {
      sum = 
        board[i-1]+
        board[i+l]+
        board[i+l-1]+
        board[0]+
        board[l]+
        board[l*w-1]+
        board[l*w-2]+
        board[l*(w-1)];
    }
    
    //bottom-left
   else if(i==l*(w-1)) {
      sum = 
        board[i-l]+
        board[(i-l)+1]+
        board[i+1]+
        board[0]+
        board[1]+
        board[l-1]+
        board[l*w-1]+
        board[l*w-l-1];      
    }
    
    //bottom right
   else if(i==(l*w)-1) {
      sum = 
        board[i-1]+
        board[i-l-1]+
        board[i-l]+
        board[0]+
        board[l*(w-1)]+
        board[l*(w-2)]+
        board[l-1]+
        board[l-2];
    }
    
//Check the sum of the SIDES
    
   //top
 else if(i>0&&i<l-1) {
      sum = 
        board[i-1]+
        board[i+1]+
        board[i+l+1]+
        board[i+l]+
        board[i+l-1]+
        board[l*(w-1)+i-1]+
        board[l*(w-1)+i]+
        board[l*(w-1)+i+1];
    }
    
    //bottom
 else if(i>l*(w-1)&&i<l*w-1) {
      sum = 
        board[i-1]+
        board[i-l+1]+
        board[i-l]+
        board[i-l-1]+
        board[i+1]+
        board[i-l*(w-1)-1]+
        board[i-l*(w-1)]+
        board[i-l*(w-1)+1];
    }
    
    //left
 else if(i%l==0){
      sum = 
        board[i-l]+
        board[i-l+1]+
        board[i+1]+
        board[i+l+1]+
        board[i+l]+
        board[i-1]+
        board[i+l-1]+
        board[i+2*l-1];
    }
    
    //right
  else if (i%l==(l-1)){
      sum = 
        board[i-1]+
        board[i-l-1]+
        board[i-l]+
        board[i+l]+
        board[i+l-1]+
        board[i-l+1]+
        board[i+1]+
        board[i-l*2+1];
    }
    
//Check the sum in ALL OTHER CELLS
    
    else {
      sum = 
        board[i-1]+
        board[i-l+1]+
        board[i-l]+
        board[i-l-1]+
        board[i+1]+
        board[i+l+1]+
        board[i+l]+
        board[i+l-1];
    }
    
 //Apply the rules of the game
    
  newBoard[i] = board[i];
    
  if (board[i] == 0&& sum==3) 
        {newBoard[i] = 1;}
    
  else if (board[i] == 1 && sum<2 ) 
        {newBoard[i] = 0;}
    
  else if (board[i] == 1 && sum>3) 
        {newBoard[i] = 0;}
}
  
 //Deep copy the newBoard array in the board variable for the next generation to be able to execute

  board = JSON.parse(JSON.stringify(newBoard)); 
};

//Create the board

class Board extends React.Component {
 constructor(props) {
    super(props);
    this.state=  { 
    board: board,
    count: 0
    };
    }

  componentDidMount() {
    
  //Play the game at an interval

  this.interval = setInterval(function (){ 
    play(length, width);                            
    this.setState({board: newBoard, count:    this.state.count+1}); 
}.bind(this), interval);
  
    };

    render() {
      
//Map over the board variable in order to generate the squares  
      
var generateBoard = this.state.board.map((square, index) => 
<div 
  className = "square" 
  
//Apply dynamic styling do dead and alive cells
  
  style = {this.state.board[index] == 1 ? styles.alive : styles.dead} 
  
//Change the state of a square by clicking on it  
  
  onClick = {() => { 
let cells = this.state.board;
if (this.state.board[index] == 1) 
{cells.splice(index, 1, 0)}
else {cells.splice(index, 1, 1)}
board = JSON.parse(JSON.stringify(cells));
this.setState({board: board})}}
  > 
</div>); 
      return (
      <div>
  <h1 id="title"> Conway's Game of Life</h1>
  <div className = "buttonsContainer">
    <button 
    className = "btn"
  
//Start button function
  
  onClick = {() => {
      clearInterval(this.interval);
      this.interval = setInterval(function (){  
         play(length, width);                            
         this.setState({
           board: newBoard, 
           count: this.state.count+1}
                      );}.bind(this), interval)}} >
  <i className="fa fa-play" aria-hidden="true"></i>
</button>
      <button 
        className = "btn"
        
//Pause button function
        
        onClick = {() => {clearInterval(this.interval)}}>
        <i className="fa fa-pause" aria-hidden="true"></i>
      </button>
    
      <button 
        className = "btn"
        
//Stop button function
        
        onClick = {()=> {
      clearInterval(this.interval)  
      var squares = []
      for (var ind=0; ind<length*width; ind++)   
      {squares.push(0)}; 
      board = JSON.parse(JSON.stringify(squares));
      this.setState({board: squares, count: 0})
      }
       }
        >
<i className="fa fa-stop"></i>
       </button>
    </div>
    <div id = "generationCount"> Generation {this.state.count} </div>
      <div id = "board" className = "row"> {generateBoard}</div>
  <div>                                        
    <p id="rules"><b>Rules:</b>
     <br></br>
     1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
     <br></br>
     2. Any live cell with two or three live neighbours lives on to the next generation.
     <br></br>
     3. Any live cell with more than three live neighbours dies, as if by overpopulation.
     <br></br>
     4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
    </p>
</div>
    </div>
    );
  }
  }

//Create Sign class

class Sign extends React.Component {
   
  render() {
    return <div>
      <div className = "container" id="sign">
    <div>with <i id = "heart" className ="fa fa-heart"> </i> by <a href="https://codepen.io/IvaKop/#" target = "_blank">Iva </a></div>
</div>
      </div>;
  }
}

//Create App class

class App extends React.Component {
 constructor(props) {
    super(props);
    this.state=  { 
    
    };
    }

    render() {
      return (
      <div>
      <Board />
      <Sign />
      </div>
    );
  }
  }

//Render App

ReactDOM.render(
  <App />,
  document.getElementById('app')
);


              
            
!
999px

Console