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

Packages

Add 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 or require. We'll also process your JavaScript with Babel.

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

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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <html lang="en">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>React App</title>
  </head>
  <body>
    <div id="root"></div>
 
  </body>
</html>

            
          
!
            
              $btn-active-hover: "btn:active, btn:hover";

body {
  background-color: #99B898;
  color: white;
  text-align: center;
}

h1, h2, h3 {
  text-align: center;
}

.cell-box {
  border: 1px solid #99B898;
  height: 30px;
  width: 30px;
  float: left;
}

.game-board {
  width: 320px;
  height: 320px;
  margin: auto;
}


.legend-list {
  list-style: none;
  margin: 0;
  padding: 0;
  font-size: 1em;
}

button {
  background-color: #FF847C;
  margin: 2px;
  color: white;
}


.btn:active, .btn:hover, .btn:focus {
  text-decoration: none;
  outline: none;
  border: none;
}

.btn:hover, .btn:active {
  background-color: white;
  color: #FF847C;
}



.rules-list {
  list-style: none;
}

            
          
!
            
              class App extends React.Component {
  render() {
    return (
      <div className="App">
                <div className="App-header">
                    <h1>Game of Life</h1>
                    <div className="container">
                    <div className="row">
                      <div className="key col-xs-12 col-sm-3">
                          <ul className="legend-list">
                              <li><strong>Pink:</strong> <em>alive</em></li>
                              <li><strong>Yellow:</strong> <em>dead</em></li>
                              <li><strong>Black:</strong> <em>inactive</em></li>
                          </ul>
                      </div>
                    <Board className="col-xs-12 col-sm-9" />
                    </div>
                    <div className="rules col-xs-12">
                        <h2>Rules</h2>
                        <ol className="rules-list">
                            <li>Any cell with less than 2 live neighbors will die, as if by underpopulation.</li>
                            <li>Any cell with more than 3 live neighbors will die, as if by overpopulation.</li>
                            <li>Any inactive or dead cell with exactly 3 live neighbors will come to life, as if by reproduction.</li>
                        </ol>
                    </div>
                    </div>
                </div>
            </div>
    );
  }
}

var intervalID;

class Board extends React.Component {
  constructor(props) {
    super(props)
    this.click = this.click.bind(this)
  }
  componentWillMount() {
    //if there is no board
    if (this.state === null) {
      this.setState({
        "cellboard": [
          [{
            "id": 0,
            "value": 0
          }, {
            "id": 1,
            "value": 0
          }, {
            "id": 2,
            "value": 0
          }, {
            "id": 3,
            "value": 0
          }, {
            "id": 4,
            "value": 0
          }, {
            "id": 5,
            "value": 0
          }, {
            "id": 6,
            "value": 0
          }, {
            "id": 7,
            "value": 0
          }, {
            "id": 8,
            "value": 0
          }, {
            "id": 9,
            "value": 0
          }],
          [{
            "id": 10,
            "value": 0
          }, {
            "id": 11,
            "value": 0
          }, {
            "id": 12,
            "value": 0
          }, {
            "id": 13,
            "value": 0
          }, {
            "id": 14,
            "value": 0
          }, {
            "id": 15,
            "value": 0
          }, {
            "id": 16,
            "value": 0
          }, {
            "id": 17,
            "value": 1
          }, {
            "id": 18,
            "value": 0
          }, {
            "id": 19,
            "value": 0
          }],
          [{
            "id": 20,
            "value": 0
          }, {
            "id": 21,
            "value": 0
          }, {
            "id": 22,
            "value": 0
          }, {
            "id": 23,
            "value": 0
          }, {
            "id": 24,
            "value": 0
          }, {
            "id": 25,
            "value": 0
          }, {
            "id": 26,
            "value": 0
          }, {
            "id": 27,
            "value": 1
          }, {
            "id": 28,
            "value": 0
          }, {
            "id": 29,
            "value": 0
          }],
          [{
            "id": 30,
            "value": 0
          }, {
            "id": 31,
            "value": 0
          }, {
            "id": 32,
            "value": 0
          }, {
            "id": 33,
            "value": 0
          }, {
            "id": 34,
            "value": 0
          }, {
            "id": 35,
            "value": 0
          }, {
            "id": 36,
            "value": 0
          }, {
            "id": 37,
            "value": 1
          }, {
            "id": 38,
            "value": 0
          }, {
            "id": 39,
            "value": 0
          }],
          [{
            "id": 40,
            "value": 0
          }, {
            "id": 41,
            "value": 0
          }, {
            "id": 42,
            "value": 0
          }, {
            "id": 43,
            "value": 0
          }, {
            "id": 44,
            "value": 1
          }, {
            "id": 45,
            "value": 1
          }, {
            "id": 46,
            "value": 0
          }, {
            "id": 47,
            "value": 0
          }, {
            "id": 48,
            "value": 0
          }, {
            "id": 49,
            "value": 0
          }],
          [{
            "id": 50,
            "value": 0
          }, {
            "id": 51,
            "value": 0
          }, {
            "id": 52,
            "value": 0
          }, {
            "id": 53,
            "value": 0
          }, {
            "id": 54,
            "value": 1
          }, {
            "id": 55,
            "value": 1
          }, {
            "id": 56,
            "value": 0
          }, {
            "id": 57,
            "value": 0
          }, {
            "id": 58,
            "value": 0
          }, {
            "id": 59,
            "value": 0
          }],
          [{
            "id": 60,
            "value": 0
          }, {
            "id": 61,
            "value": 0
          }, {
            "id": 62,
            "value": 1
          }, {
            "id": 63,
            "value": 1
          }, {
            "id": 64,
            "value": 0
          }, {
            "id": 65,
            "value": 0
          }, {
            "id": 66,
            "value": 0
          }, {
            "id": 67,
            "value": 0
          }, {
            "id": 68,
            "value": 0
          }, {
            "id": 69,
            "value": 0
          }],
          [{
            "id": 70,
            "value": 0
          }, {
            "id": 71,
            "value": 0
          }, {
            "id": 72,
            "value": 1
          }, {
            "id": 73,
            "value": 1
          }, {
            "id": 74,
            "value": 0
          }, {
            "id": 75,
            "value": 0
          }, {
            "id": 76,
            "value": 0
          }, {
            "id": 77,
            "value": 0
          }, {
            "id": 78,
            "value": 0
          }, {
            "id": 79,
            "value": 0
          }],
          [{
            "id": 80,
            "value": 0
          }, {
            "id": 81,
            "value": 0
          }, {
            "id": 82,
            "value": 0
          }, {
            "id": 83,
            "value": 0
          }, {
            "id": 84,
            "value": 0
          }, {
            "id": 85,
            "value": 0
          }, {
            "id": 86,
            "value": 0
          }, {
            "id": 87,
            "value": 0
          }, {
            "id": 88,
            "value": 0
          }, {
            "id": 89,
            "value": 0
          }],
          [{
            "id": 90,
            "value": 0
          }, {
            "id": 91,
            "value": 0
          }, {
            "id": 92,
            "value": 0
          }, {
            "id": 93,
            "value": 0
          }, {
            "id": 94,
            "value": 0
          }, {
            "id": 95,
            "value": 0
          }, {
            "id": 96,
            "value": 0
          }, {
            "id": 97,
            "value": 0
          }, {
            "id": 98,
            "value": 0
          }, {
            "id": 99,
            "value": 0
          }]
        ],
        "generation": 0
      })
    }
  }
  click(currentlyValue, i, j) {
    if (currentlyValue === 0) {
      currentlyValue = 1
    } else if (currentlyValue === 1) {
      currentlyValue = 2
    } else if (currentlyValue === 2) {
      currentlyValue = 0
    }

    var cells = this.state.cellboard
    cells[i][j].value = currentlyValue
    this.setState({
      cellboard: cells
    })
  }
  checkNeighbors() {

      var currentState = this.state
      var newState = JSON.parse(JSON.stringify(currentState))

      for (let i = 0; i < currentState.cellboard.length; i++) {
        for (let j = 0; j < currentState.cellboard[i].length; j++) {
          var aliveNeighbors = 0;

          //find corners & sides
          if (i - 1 < 0 && j - 1 < 0) {
            //top left corner
            if (currentState.cellboard[i][j + 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i + 1][j + 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i + 1][j].value === 1) {
              aliveNeighbors += 1
            }
            //decide what happens to cell next gen
            if (currentState.cellboard[i][j].value === 1) {
              if (aliveNeighbors < 2) {
                newState.cellboard[i][j].value = 2
              } else if ((aliveNeighbors === 2) || (aliveNeighbors === 3)) {
                newState.cellboard[i][j].value = 1
              } else if (aliveNeighbors > 3) {}
            } else {
              if (aliveNeighbors === 3) {
                newState.cellboard[i][j].value = 1
              }
            }
          } else if (i + 1 === 10 && j + 1 === 10) {
            //bottom right corner
            if (currentState.cellboard[i - 1][j - 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i - 1][j].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i][j - 1].value === 1) {
              aliveNeighbors += 1
            }

            //next gen status
            if (currentState.cellboard[i][j].value === 1) {
              if (aliveNeighbors < 2) {
                newState.cellboard[i][j].value = 2
              } else if (aliveNeighbors === 2 || aliveNeighbors === 3) {
                newState.cellboard[i][j].value = 1
              } else if (aliveNeighbors > 3) {
                newState.cellboard[i][j].value = 2
              }
            } else {
              if (aliveNeighbors === 3) {
                newState.cellboard[i][j].value = 1
              }
            }

          } else if (i - 1 < 0 && j + 1 === 10) {
            //top right corner
            if (currentState.cellboard[i][j - 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i + 1][j - 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i + 1][j].value === 1) {
              aliveNeighbors += 1
            }

            //next gen status
            if (currentState.cellboard[i][j].value === 1) {
              if (aliveNeighbors < 2) {
                newState.cellboard[i][j].value = 2
              } else if (aliveNeighbors === 2 || aliveNeighbors === 3) {
                newState.cellboard[i][j].value = 1
              } else if (aliveNeighbors > 3) {
                newState.cellboard[i][j].value = 2
              }
            } else {
              if (aliveNeighbors === 3) {
                newState.cellboard[i][j].value = 1
              }
            }

          } else if (i + 1 === 10 && j - 1 < 0) {
            //bottom left corner
            if (currentState.cellboard[i - 1][j].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i - 1][j + 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i][j + 1].value === 1) {
              aliveNeighbors += 1
            }

            //next gen status
            if (currentState.cellboard[i][j].value === 1) {
              if (aliveNeighbors < 2) {
                newState.cellboard[i][j].value = 2
              } else if (aliveNeighbors === 2 || aliveNeighbors === 3) {
                newState.cellboard[i][j].value = 1
              } else if (aliveNeighbors > 3) {
                newState.cellboard[i][j].value = 2
              }
            } else {
              if (aliveNeighbors === 3) {
                newState.cellboard[i][j].value = 1
              }
            }

          } else if (i - 1 < 0) {
            //top
            if (currentState.cellboard[i][j - 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i + 1][j - 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i + 1][j].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i + 1][j + 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i][j + 1].value === 1) {
              aliveNeighbors += 1
            }

            //next gen status
            if (currentState.cellboard[i][j].value === 1) {
              if (aliveNeighbors < 2) {
                newState.cellboard[i][j].value = 2
              } else if (aliveNeighbors === 2 || aliveNeighbors === 3) {
                newState.cellboard[i][j].value = 1
              } else if (aliveNeighbors > 3) {
                newState.cellboard[i][j].value = 2
              }
            } else {
              if (aliveNeighbors === 3) {
                newState.cellboard[i][j].value = 1
              }
            }

          } else if (i + 1 === 10) {
            //bottom
            if (currentState.cellboard[i][j - 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i - 1][j - 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i - 1][j].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i - 1][j + 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i][j + 1].value === 1) {
              aliveNeighbors += 1
            }
            //next gen status
            if (currentState.cellboard[i][j].value === 1) {
              if (aliveNeighbors < 2) {
                newState.cellboard[i][j].value = 2
              } else if (aliveNeighbors === 2 || aliveNeighbors === 3) {
                newState.cellboard[i][j].value = 1
              } else if (aliveNeighbors > 3) {
                newState.cellboard[i][j].value = 2
              }
            } else {
              if (aliveNeighbors === 3) {
                newState.cellboard[i][j].value = 1
              }
            }

          } else if (j - 1 < 0) {
            //left
            if (currentState.cellboard[i - 1][j].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i - 1][j + 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i][j + 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i + 1][j + 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i + 1][j].value === 1) {
              aliveNeighbors += 1
            }

            //next gen status
            if (currentState.cellboard[i][j].value === 1) {
              if (aliveNeighbors < 2) {
                newState.cellboard[i][j].value = 2
              } else if (aliveNeighbors === 2 || aliveNeighbors === 3) {
                newState.cellboard[i][j].value = 1
              } else if (aliveNeighbors > 3) {
                newState.cellboard[i][j].value = 2
              }
            } else {
              if (aliveNeighbors === 3) {
                newState.cellboard[i][j].value = 1
              }
            }
          } else if (j + 1 === 10) {
            //right
            if (currentState.cellboard[i - 1][j - 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i - 1][j].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i][j - 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i + 1][j - 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i + 1][j].value === 1) {
              aliveNeighbors += 1
            }

            //next gen status
            if (currentState.cellboard[i][j].value === 1) {
              if (aliveNeighbors < 2) {
                newState.cellboard[i][j].value = 2
              } else if (aliveNeighbors === 2 || aliveNeighbors === 3) {
                newState.cellboard[i][j].value = 1
              } else if (aliveNeighbors > 3) {
                newState.cellboard[i][j].value = 2
              }
            } else {
              if (aliveNeighbors === 3) {
                newState.cellboard[i][j].value = 1
              }
            }
          } else {
            //middle
            if (currentState.cellboard[i - 1][j - 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i - 1][j].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i - 1][j + 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i][j - 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i][j + 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i + 1][j - 1].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i + 1][j].value === 1) {
              aliveNeighbors += 1
            }
            if (currentState.cellboard[i + 1][j + 1].value === 1) {
              aliveNeighbors += 1
            }

            //next gen status
            if (currentState.cellboard[i][j].value === 1) {
              if (aliveNeighbors < 2) {
                newState.cellboard[i][j].value = 2
              } else if (aliveNeighbors === 2 || aliveNeighbors === 3) {
                newState.cellboard[i][j].value = 1
              } else if (aliveNeighbors > 3) {
                newState.cellboard[i][j].value = 2
              }
            } else {
              if (aliveNeighbors === 3) {
                newState.cellboard[i][j].value = 1
              } //end if
            } //end else
          } //end last else
        } //end for j
      } //end for i
      newState.generation += 1
      this.setState(newState)
    } //end checkNeighbors
  startBoard(state) {
    intervalID = setInterval(this.checkNeighbors.bind(this), 300, this.state)
  }
  stopBoard() {
    clearInterval(intervalID)
  }
  resetBoard(state) {
    var cells = [];
    var idCount = 0;
    //set initial id numbers
    for (let i = 0; i < 10; i++) {
      cells[i] = [];
      for (let j = 0; j < 10; j++) {
        //set initial values
        cells[i][j] = {
          id: idCount,
          value: 0
        };
        idCount++;

      }
    }
    this.setState({
      cellboard: cells,
      generation: 0
    })
  }
  render() {
    var cellboard = this.state.cellboard;
    var counter = 0;
    return (
      <div>
                <div key={counter} className="game-board">

                    {this.state.cellboard.map(function(array, index) {
                        var i = index;
                        return array.map(function(value, index) {
                            var j = index;
                            var color;
                            if (this.state.cellboard[i][j].value === 0) {
                                color = "#2A363B";
                            } else if (this.state.cellboard[i][j].value === 1) {
                                color = "#E84A5F";
                            } else if (this.state.cellboard[i][j].value === 2) {
                                color = "#FECEA8";
                            }
                            counter++;
                            return (<Cell click={this.click} currentState={cellboard[i][j].value} key={value.id} i={i} j={j} bgcolor={color}/>)
                        }.bind(this))
                    }.bind(this))}
                </div>
                <div className="col-xs-12">
                  <div className="generation">
                      <h3>Generation: {this.state.generation}</h3>
                  </div>
                  <button className="btn" onClick={this.startBoard.bind(this, this.state)}>Start</button>
                  <button className="btn" onClick={this.stopBoard}>Stop</button>
                  <button className="btn" onClick={this.resetBoard.bind(this)}>Clear</button>
                </div>
            </div>
    )
  }
}

class Cell extends React.Component {
  componentWillMount() {
    if (this.state === null) {
      this.setState({
        isCurrently: this.props.currentState,
        bgcolor: "gray"
      })
    }
  }
  render() {
    return ( <
      div className = "cell-box"
      style = {
        {
          backgroundColor: this.props.bgcolor
        }
      }
      onClick = {
        this.props.click.bind(null, this.props.currentState, this.props.i, this.props.j)
      } > < /div>
    )
  }
}

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

Console