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

              
                  <div id="app"></div>
              
            
!

CSS

              
                h1,
h3,
p
  text-align: center
  font-family: monospace

.center
  margin: 0 auto
  text-align: center
  
table
  margin: 0 auto
  
td
  width: 8px
  height: 8px
  border: 1px solid gray
  
.alive
  background: #3498db
  
.dead
  background: white
              
            
!

JS

              
                var Board = React.createClass({
  getInitialState: function() {
    var board = [];
    for(var i = 1; i <= 50; i++) {
      var row = [];
      for(var j = 1; j <= 50; j++) {
        row.push(Math.random() < 0.5 ? 1 : 0);
      }
      board.push(row);
    }
    return {
      board: board,
      generations: 0,
      timer: undefined
    }
  },
  
  componentWillMount: function() {
    this.startGame();
  },
  
  advanceGeneration: function() {
    var newBoard = [];
    var boardObj = this;
    boardObj.state.board.forEach(function(row, rowIndex) {
      var newRow = [];
      row.forEach(function(cell, cellIndex) {
        var neighbors = 0;
        if(boardObj.state.board[rowIndex - 1] !== undefined) {
          for(var i = -1; i <= 1; i++) {
            if(boardObj.state.board[rowIndex - 1][cellIndex + i] !== undefined) {
              if(boardObj.state.board[rowIndex - 1][cellIndex + i] !== 0) {
                neighbors++;
              }
            }
          }
        }
        if(boardObj.state.board[rowIndex][cellIndex - 1] !== undefined) {
          if(boardObj.state.board[rowIndex][cellIndex - 1] !== 0) {
            neighbors++;
          }
        }
        if(boardObj.state.board[rowIndex][cellIndex + 1] !== undefined) {
          if(boardObj.state.board[rowIndex][cellIndex + 1] !== 0) {
            neighbors++;
          }
        }
        if(boardObj.state.board[rowIndex + 1] !== undefined) {
          for(var i = -1; i <= 1; i++) {
            if(boardObj.state.board[rowIndex + 1][cellIndex + i] !== undefined) {
              if(boardObj.state.board[rowIndex + 1][cellIndex + i] !== 0) {
                neighbors++;
              }
            }
          }
        }
        if(cell === 1) {
          if(neighbors < 2) {
            newRow.push(0);
          } else if(neighbors > 3) {
            newRow.push(0);
          } else {
            newRow.push(1);
          }
        } else {
          if(neighbors === 3) {
            newRow.push(1);
          } else {
            newRow.push(0);
          }
        }
      });
      newBoard.push(newRow);
    });
    this.setState({
      board: newBoard,
      generations: (this.state.generations + 1)
    });
  },
  
  startGame: function() {
    if(this.state.timer === undefined) {
      this.setState({
        timer: setInterval(this.advanceGeneration, 200)
      });
    } else {
      clearInterval(this.state.timer);
      this.setState({
        timer: undefined
      });
    }
  },
  
  clearBoard: function() {
    clearInterval(this.state.timer);
    var board = [];
    for(var i = 1; i <= 50; i++) {
      var row = [];
      for(var j = 1; j <= 50; j++) {
        row.push(0);
      }
      board.push(row);
    }
    this.setState({
      board: board,
      generations: 0,
      timer: undefined
    });
  },
  
  clickHandler: function(row, index, thisBoard) {
    return function() {
      var board = thisBoard.state.board;
      if(board[row][index] === 1) {
        board[row][index] = 0;
      } else {
        board[row][index] = 1;
      }
      thisBoard.setState({
        board: board
      });
    };
  },
  
  render: function() {
    var rows = [];
    var board = this;
    this.state.board.forEach(function(row, rowIndex) {
      var cells = [];
      row.forEach(function(cell, cellIndex) {
        cells.push(<td key={cellIndex}
                     className={cell ? "alive" : "dead"} 
                     onClick={board.clickHandler(rowIndex, cellIndex, board)}></td>);
      });
      rows.push(
        <tr key={rowIndex}>
          {cells}
        </tr>
      );
    });
    return (
      <div>
        <h1>Conway's Game of Life</h1>
        <table>
          <tbody>
            {rows}
          </tbody>
        </table>
        <p>Generations: {this.state.generations}</p>
        <div className="center">
          <button onClick={this.startGame}>Start / Stop</button> &nbsp;
          <button onClick={this.clearBoard}>Clear / Reset</button>
        </div>
      </div>
    );
  }
});

ReactDOM.render(<Board />, document.getElementById('app'));
              
            
!
999px

Console