cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

            
              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
            
          
!
            
              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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console