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

              
                
              
            
!

CSS

              
                $alive: red;
$dead: white;
$fontFam: 'Titillium Web', arial, helvetica, sans-serif;
$clickHoverColor: #fbb;
* {
  box-sizing: border-box;
  font-family: $fontFam;
}

h1,
#timer,
#controls {
  text-align: center;
}

h1 {
  margin-bottom: 0;
  
  a{
    text-decoration: none;
    color: #333;
  }
  
  span {
    font-size: 14px;
    color: #fff;
    background: #444;
    width: 20px;
    height: 20px;
    display: inline-block;
    line-height: 1.25;
    border-radius: 50%;
    vertical-align: middle;
  }
}

#gol {
  width: 551px;
  margin: auto;
  display: block;
  text-align: center;
  
  p {
    font-size: 14px;
    margin: 0;
    
    &:last-of-type {
      margin-bottom: 20px;
    }
  }
}

.row {
  display: block;
  font-size: 0;
}

.cell {
  background: $dead;
  width: 11px;
  height: 11px;
  margin: 0;
  padding: 0;
  border-right: 1px solid #000;
  border-bottom: 1px solid #000;
  display: inline-block;
}

.row:first-of-type {
  border-top: 1px solid #000;
}

.cell:first-child {
  border-left: 1px solid #000;
  width: 12px;
}

.cell:before {
  content: ' ';
  display: block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
}

.cell.alive:before {
  background: $alive;
}

.cell:hover {
  background: $clickHoverColor;
}

#timer {
  margin-bottom: 10px;
}

#controls {
  margin-top: 10px;
}

#controls button {
  margin-left: 20px;
  background: none;
  border-image: none;
  border: 1px solid #333;
  font-size: 14px;
}

#controls button:first-of-type {
  margin-left: 0;
}

#controls button:hover {
  cursor: pointer;
  background: $clickHoverColor;
}

#controls button:focus {
  outline: none;
}

#controls #glider-gun {
  display: block;
  margin: 15px auto;
}
              
            
!

JS

              
                class GameOfLife extends React.Component {  
  constructor(props) {
    super(props);
    var gameBoard = []
    var setInt;
    for (var i=0; i<this.props.rows; i++) {
      gameBoard.push([]);
      for (var x=0; x<this.props.cols; x++) {
        gameBoard[i].push(Math.round(Math.random()));
      }
    }
    this.state = {
      gameBoard: gameBoard,
      generation: 0,
      isRunning: true
    }
  }
  componentDidMount() {
    //Run game on initial load
    this.setInt = setInterval(this.calcGameBoard.bind(this), 250);
  }
  handleRun() {
    if (!this.state.isRunning) {
      this.setInt = setInterval(this.calcGameBoard.bind(this), 250)
    }
  }
  handlePause() {
    clearInterval(this.setInt)
    this.setState({
      gameBoard: this.state.gameBoard,
      generation: this.state.generation,
      isRunning: false
    })
  }
  handleClear() {
    var cleanBoard = this.state.gameBoard.map((v,i) => {
      return(
        this.state.gameBoard[i].map(() => {
          return 0;
        })
      )
    })
    clearInterval(this.setInt)
    this.setState({
      gameBoard: cleanBoard,
      generation: 0,
      isRunning: false
    })
  }
  handleViewGlider(gliderBoard) {
    clearInterval(this.setInt)
    this.setState({
      gameBoard: gliderBoard,
      generation: 0,
      isRunning: false
    })
    this.setInt = setInterval(this.calcGameBoard.bind(this), 250)
  }
  calcGameBoard() {
    //Copy current gameboard to new array
    var nextBoard = []
    for (var cr=0; cr<this.state.gameBoard.length; cr++) {
      nextBoard.push([])
      for (var cc=0; cc<this.state.gameBoard[cr].length; cc++) {
        nextBoard[cr].push(this.state.gameBoard[cr][cc])
      }
    }
    
    //Calculate next board
    var globalSum = 0
    for (var r=0; r<this.state.gameBoard.length; r++) {
      for (var c=0; c<this.state.gameBoard[r].length; c++) {
        var sum = 0
        if ( r === 0 && c === 0 ) {
          //Top Left Corner
          sum = this.state.gameBoard[this.state.gameBoard.length-1][this.state.gameBoard[r].length-1] +
                this.state.gameBoard[this.state.gameBoard.length-1][c] +
                this.state.gameBoard[this.state.gameBoard.length-1][c+1] +
                this.state.gameBoard[r][this.state.gameBoard[r].length-1] +
                this.state.gameBoard[r][c+1] +
                this.state.gameBoard[r+1][this.state.gameBoard[r].length-1] +
                this.state.gameBoard[r+1][c] +
                this.state.gameBoard[r+1][c+1]
          globalSum += sum
          if (this.state.gameBoard[r][c] === 1) {
            if (sum < 2) { 
              nextBoard[r][c] = 0 
            } else if (sum === 2 || sum === 3) {
              nextBoard[r][c] = 1
            } else if (sum > 3) {
              nextBoard[r][c] = 0
            }
          } else if (this.state.gameBoard[r][c] === 0 && sum === 3) {
              nextBoard[r][c] = 1
          }
        } else if (r === 0 && c === this.state.gameBoard[r].length - 1) {
          //Top Right Corner
          sum = this.state.gameBoard[this.state.gameBoard.length-1][c-1] +
                this.state.gameBoard[this.state.gameBoard.length-1][c] +
                this.state.gameBoard[this.state.gameBoard.length-1][0] +
                this.state.gameBoard[r][c-1] +
                this.state.gameBoard[r][0] +
                this.state.gameBoard[r+1][c-1] +
                this.state.gameBoard[r+1][c] +
                this.state.gameBoard[r+1][0]
          globalSum += sum
          if (this.state.gameBoard[r][c] === 1) {
            if (sum < 2) { 
              nextBoard[r][c] = 0 
            } else if (sum === 2 || sum === 3) {
              nextBoard[r][c] = 1
            } else if (sum > 3) {
              nextBoard[r][c] = 0
            }
          } else if (this.state.gameBoard[r][c] === 0 && sum === 3) {
              nextBoard[r][c] = 1
          } 
        } else if (r === this.state.gameBoard.length - 1 && c === 0 ) {
          //Bottom Left Corner
          sum = this.state.gameBoard[r-1][this.state.gameBoard[r].length-1] +
                this.state.gameBoard[r-1][c] +
                this.state.gameBoard[r-1][c+1] +
                this.state.gameBoard[r][this.state.gameBoard[r].length-1] +
                this.state.gameBoard[r][c+1] +
                this.state.gameBoard[0][this.state.gameBoard[r].length-1] +
                this.state.gameBoard[0][c] +
                this.state.gameBoard[0][c+1]
          globalSum += sum
          if (this.state.gameBoard[r][c] === 1) {
            if (sum < 2) { 
              nextBoard[r][c] = 0 
            } else if (sum === 2 || sum === 3) {
              nextBoard[r][c] = 1
            } else if (sum > 3) {
              nextBoard[r][c] = 0
            }
          } else if (this.state.gameBoard[r][c] === 0 && sum === 3) {
              nextBoard[r][c] = 1
          }
        } else if (r === this.state.gameBoard.length - 1 && c === this.state.gameBoard[r].length - 1) {
          //Bottom Right Corner
          sum = this.state.gameBoard[r-1][c-1] +
                this.state.gameBoard[r-1][c] +
                this.state.gameBoard[r-1][0] +
                this.state.gameBoard[r][c-1] +
                this.state.gameBoard[r][0] +
                this.state.gameBoard[0][c-1] +
                this.state.gameBoard[0][c] +
                this.state.gameBoard[0][0]
          globalSum += sum
          if (this.state.gameBoard[r][c] === 1) {
            if (sum < 2) { 
              nextBoard[r][c] = 0 
            } else if (sum === 2 || sum === 3) {
              nextBoard[r][c] = 1
            } else if (sum > 3) {
              nextBoard[r][c] = 0
            }
          } else if (this.state.gameBoard[r][c] === 0 && sum === 3) {
              nextBoard[r][c] = 1
          }
        } else if (r === 0 ) {
          //Top Edge
          sum = this.state.gameBoard[this.state.gameBoard.length-1][c-1] +
                this.state.gameBoard[this.state.gameBoard.length-1][c] +
                this.state.gameBoard[this.state.gameBoard.length-1][c+1] +
                this.state.gameBoard[r][c-1] +
                this.state.gameBoard[r][c+1] +
                this.state.gameBoard[r+1][c-1] +
                this.state.gameBoard[r+1][c] +
                this.state.gameBoard[r+1][c+1]
          globalSum += sum
          if (this.state.gameBoard[r][c] === 1) {
            if (sum < 2) { 
              nextBoard[r][c] = 0 
            } else if (sum === 2 || sum === 3) {
              nextBoard[r][c] = 1
            } else if (sum > 3) {
              nextBoard[r][c] = 0
            }
          } else if (this.state.gameBoard[r][c] === 0 && sum === 3) {
              nextBoard[r][c] = 1
          } 
        } else if (c === 0) {
          //Left Edge
          sum = this.state.gameBoard[r-1][this.state.gameBoard[r].length-1] +
                this.state.gameBoard[r-1][c] +
                this.state.gameBoard[r-1][c+1] +
                this.state.gameBoard[r][this.state.gameBoard[r].length-1] +
                this.state.gameBoard[r][c+1] +
                this.state.gameBoard[r+1][this.state.gameBoard[r].length-1] +
                this.state.gameBoard[r+1][c] +
                this.state.gameBoard[r+1][c+1]
          globalSum += sum
          if (this.state.gameBoard[r][c] === 1) {
            if (sum < 2) { 
              nextBoard[r][c] = 0 
            } else if (sum === 2 || sum === 3) {
              nextBoard[r][c] = 1
            } else if (sum > 3) {
              nextBoard[r][c] = 0
            }
          } else if (this.state.gameBoard[r][c] === 0 && sum === 3) {
              nextBoard[r][c] = 1
          }
        } else if (c === this.state.gameBoard[r].length - 1) {
          //Right Edge
          sum = this.state.gameBoard[r-1][c-1] +
                this.state.gameBoard[r-1][c] +
                this.state.gameBoard[r-1][0] +
                this.state.gameBoard[r][c-1] +    
                this.state.gameBoard[r][0] +
                this.state.gameBoard[r+1][c-1] +
                this.state.gameBoard[r+1][c] +
                this.state.gameBoard[r+1][0]
          globalSum += sum
          if (this.state.gameBoard[r][c] === 1) {
            if (sum < 2) { 
              nextBoard[r][c] = 0 
            } else if (sum === 2 || sum === 3) {
              nextBoard[r][c] = 1
            } else if (sum > 3) {
              nextBoard[r][c] = 0
            }
          } else if (this.state.gameBoard[r][c] === 0 && sum === 3) {
              nextBoard[r][c] = 1
          }
        } else if (r === this.state.gameBoard.length - 1) {
          //Bottom Edge
          sum = this.state.gameBoard[r-1][c-1] +
                this.state.gameBoard[r-1][c] +
                this.state.gameBoard[r-1][c+1] +
                this.state.gameBoard[r][c-1] +    
                this.state.gameBoard[r][c+1] +
                this.state.gameBoard[0][c-1] +
                this.state.gameBoard[0][c] +
                this.state.gameBoard[0][c+1]
          globalSum += sum
          if (this.state.gameBoard[r][c] === 1) {
            if (sum < 2) { 
              nextBoard[r][c] = 0 
            } else if (sum === 2 || sum === 3) {
              nextBoard[r][c] = 1
            } else if (sum > 3) {
              nextBoard[r][c] = 0
            }
          } else if (this.state.gameBoard[r][c] === 0 && sum === 3) {
              nextBoard[r][c] = 1
          }  
        } else {
          //The MIDDLE
          sum = this.state.gameBoard[r-1][c-1] +
                this.state.gameBoard[r-1][c] +
                this.state.gameBoard[r-1][c+1] +
                this.state.gameBoard[r][c-1] +    
                this.state.gameBoard[r][c+1] +
                this.state.gameBoard[r+1][c-1] +
                this.state.gameBoard[r+1][c] +
                this.state.gameBoard[r+1][c+1]
          globalSum += sum
          if (this.state.gameBoard[r][c] === 1) {
            if (sum < 2) { 
              nextBoard[r][c] = 0 
            } else if (sum === 2 || sum === 3) {
              nextBoard[r][c] = 1
            } else if (sum > 3) {
              nextBoard[r][c] = 0
            }
          } else if (this.state.gameBoard[r][c] === 0 && sum === 3) {
              nextBoard[r][c] = 1
          }  
        }
      } //end inner loop
    } //end outer loop
    if ( globalSum > 0 ) {
      this.state.generation++
      this.setState({
        gameBoard: nextBoard,
        generation: this.state.generation,
        isRunning: true
      })
    } else {
      this.handlePause()
    }
  }
  drawGameBoard() {
    return(
      this.state.gameBoard.map((v,i) => {
        return(
          <div className="row" data-row={i}>
            { this.state.gameBoard[i].map((v,i) => {
                switch(v) {
                  case 0:
                    return <div className="cell" onClick={this.toggleCellLife.bind(this)} data-cell={i}></div>
                  case 1:
                    return <div className="cell alive" onClick={this.toggleCellLife.bind(this)} data-cell={i}></div>
                  default:
                    break;
                }
              }, this)
            }
          </div>
        )
      }, this)
    )
  }
  toggleCellLife(e) {
    var col = e.target.dataset.cell
    var row = e.target.parentElement.dataset.row
    var newBoard = this.state.gameBoard
    switch(newBoard[row][col]) {
      case 0:
        newBoard[row][col] = 1;
        break;
      case 1:
        newBoard[row][col] = 0;
        break;
    }
    this.setState({
      gameBoard: newBoard,
      generation: this.state.generation,
      isPaused: this.state.isPaused
    })
  }
  render() {
    return ( 
      <div id="gol">
        <h1>
          <a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" target="_blank">Conway's Game of Life <span>?</span></a>
        </h1>
        <p>Add cells to game board at any time.</p>
        <p>Pause game to add multiple cells to board.</p>
        <Timer generation={this.state.generation} />
        <div id="game-board">
          {this.drawGameBoard(this.state)}
        </div>
        <Controls board={this.state.gameBoard} run={this.handleRun.bind(this)} pause={this.handlePause.bind(this)} clear={this.handleClear.bind(this)} viewGlider={this.handleViewGlider.bind(this)} />
      </div>
      )
  }
}

class Timer extends React.Component {
  render() {
    return(
      <div id="timer">
        Generation: {this.props.generation}
      </div>
    )
  }
}

class Controls extends React.Component {
  run() {
    this.props.run()
  }
  pause() {
    this.props.pause()
  }
  clear() {
    this.props.clear()
  }
  viewGlider() {
    //Create a clean board
    var gliderBoard = this.props.board.map((v,i) => {
      return(
        this.props.board[i].map(() => {
          return 0;
        })
      )
    })
    //Set board values for glider gun layout
    gliderBoard[1][25] = 1;
    gliderBoard[2][23] = 1;
    gliderBoard[2][25] = 1;
    gliderBoard[3][13] = 1;
    gliderBoard[3][14] = 1;
    gliderBoard[3][21] = 1;
    gliderBoard[3][22] = 1;
    gliderBoard[3][35] = 1;
    gliderBoard[3][36] = 1;
    gliderBoard[4][12] = 1;
    gliderBoard[4][16] = 1;
    gliderBoard[4][21] = 1;
    gliderBoard[4][22] = 1;
    gliderBoard[4][35] = 1;
    gliderBoard[4][36] = 1;
    gliderBoard[5][1] = 1;
    gliderBoard[5][2] = 1;
    gliderBoard[5][11] = 1;
    gliderBoard[5][17] = 1;
    gliderBoard[5][21] = 1;
    gliderBoard[5][22] = 1;
    gliderBoard[6][1] = 1;
    gliderBoard[6][2] = 1;
    gliderBoard[6][11] = 1;
    gliderBoard[6][15] = 1;
    gliderBoard[6][17] = 1;
    gliderBoard[6][18] = 1;
    gliderBoard[6][23] = 1;
    gliderBoard[6][25] = 1;
    gliderBoard[7][11] = 1;
    gliderBoard[7][17] = 1;
    gliderBoard[7][25] = 1;
    gliderBoard[8][12] = 1;
    gliderBoard[8][16] = 1;
    gliderBoard[9][13] = 1;
    gliderBoard[9][14] = 1;
    //Send glider gun array up to game board
    this.props.viewGlider(gliderBoard)
  }
  render() {
    return(
      <div id="controls">
        <button id="run" onClick={this.run.bind(this)}>Run</button>
        <button id="pause" onClick={this.pause.bind(this)}>Pause</button>
        <button id="clear" onClick={this.clear.bind(this)}>Clear</button>
        <button id="glider-gun" onClick={this.viewGlider.bind(this)}>View Glider Gun</button>
      </div>
    )
  }
}

ReactDOM.render(<GameOfLife rows={30} cols={50} />, document.body);
              
            
!
999px

Console