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 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

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

CSS

              
                html {
  height: 100%;
}

body {
  margin: 0;
  padding: 0;
  font-family: sans-serif;
  color: #eee;
  background: #163528;
  background: -webkit-radial-gradient(#398564, #23424c); /* 09151b */
  background: -o-radial-gradient(#398564, #23424c);
  background: -moz-radial-gradient(#398564, #23424c);
  background: radial-gradient(#398564, #23424c); 
}

.app h1 {
  text-align: center;
  margin-top: 50px;
  text-shadow: 5px 5px 20px rgba(0, 0, 0, 0.5);
}

.board {
  margin: auto;
  line-height: 0;
  background-color: #555;
  border: 10px solid #555;
  box-shadow: 5px 5px 20px rgba(0, 0, 0, 0.5), -5px -3px 20px rgba(0, 0, 0, 0.5);
}

.board h4 {
  text-align: center;
  background-color: #555;
  font-weight: normal;
  margin-top: 12px;
}

.cell {
  display: inline-block;
  border: 1px solid #000;
  width: 10px;
  height: 10px;
  margin-left: -1px;
  margin-bottom: -1px;
}

.alive {
  background-color: #6dc066;
}

.dead {
  background-color: #808080;
}

.controlsBtn {
  margin: auto;
  margin-bottom: 15px;
  display: flex;
  justify-content: space-between;
  background-color: #555;
  padding: 10px;
  box-shadow: 5px 5px 20px rgba(0, 0, 0, 0.5), -5px -3px 20px rgba(0, 0, 0, 0.5);
}

.controlsBtn button {
  padding: 5px 10px;
  color: #eee;
  font-size: 16px;
  background: #23424c;
  border: none;
  transition: 0.3s;
  outline: none;
}

.controlsBtn button:hover {
  background: #398564;
}
              
            
!

JS

              
                const Cell = (props) => (
  <div 
    className={props.className} 
    onClick={() => props.onSelect(props.id)}
  />
)

class Board extends React.Component {
  render() {
    const { cells, width, generation } = this.props
    const arr = cells.map((cell, i) => (
      <Cell 
        key={i}
        id={i}
        className={cell ? "cell alive" : "cell dead"}
        onSelect={this.props.onSelect}
      />
    ))

    return (
      <div className="board" style={{ width: width }}>
        <h4>Generations: {generation}</h4>
        {arr}
      </div>
    )
  }
}

class ButtonsGroup extends React.Component {
  render() {
    const buttons = this.props.actions.map(action => (
      <button key={action.name} onClick={action.action}>
        {action.name}
      </button>
    ))
    return (
      <div className="controlsBtn" style={{ width: this.props.width }}>
        {buttons}
      </div>
    )
  }
}

class App extends React.Component {
  constructor(props) {
    super(props)
    this.speed = 100
    this.state = {
      rows: 30,
      cols: 50,
      cells: [],
      //speed: 100,
      generation: 0
    }
  }

  componentDidMount() {
    this.seed()
    this.play()
  }

  run() {
    const { rows, cols, cells, generation } = this.state
    const size = rows * cols
    let newCells = cells.slice()

    for (let i = 0; i < size; i++) {
      let counter = 0

      if (cells[i-1]) counter++
      if (cells[i+1]) counter++
      if (cells[i-cols]) counter++
      if (cells[i+cols]) counter++
      if (cells[i-cols-1]) counter++
      if (cells[i-cols+1]) counter++
      if (cells[i+cols-1]) counter++
      if (cells[i+cols+1]) counter++

      if (cells[i] && (counter < 2 || counter > 3)) {
        newCells[i] = false
      }
      if (!cells[i] && counter === 3) {
        newCells[i] = true
      }
    }

    this.setState({
      cells: newCells,
      generation: generation + 1
    })
  }

  play() {
    console.log(this.state.speed)
    clearInterval(this.intervalId)
    this.intervalId = setInterval(() => this.run(), this.speed)
    
  }

  pause() {
    clearInterval(this.intervalId)
  }

  clear() {
    const size = this.state.rows * this.state.cols
    const cells = Array(size).fill(false)

    this.setState({ cells, generation: 0 })
    clearInterval(this.intervalId)
  }

  seed() {
    const size = this.state.rows * this.state.cols
    const cells = Array(size).fill(false).map(cell => 
      cell = Math.floor(Math.random() * 4) === 1 ? true : false
    )

    this.setState({ cells })
  }

  slow() {
    this.speed = 500
    this.play()
  }

  fast() {
    this.speed = 100
    this.play()
  }

  getActions() {
    return [
      { name: 'Play', action: () => this.play() },
      { name: 'Pause', action: () => this.pause() },
      { name: 'Clear', action: () => this.clear() },
      { name: 'Seed', action: () => this.seed() },
      { name: 'Slow', action: () => this.slow() },
      { name: 'Fast', action: () => this.fast() }
    ]
  }

  selectCell(i) {
    let cells = this.state.cells.slice()
    cells[i] = !cells[i]
    this.setState({ cells })
  }

  render() {
    const width = this.state.cols * 11
    const actions = this.getActions()

    return (
      <div className="app">
        <h1>Game of Life</h1>
        <ButtonsGroup 
          actions={actions} 
          width={width}
        />
        <Board
          cells={this.state.cells}
          width={width}
          onSelect={this.selectCell.bind(this)}
          generation={this.state.generation}
        />
      </div>
    )
  }
}

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

Console