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 class="wrapper">
  <div id="app"></div>
  <div class="footer"> Created by Sebastian Sporek</div>
</div>
              
            
!

CSS

              
                * {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  background-color: dimgray;
}
.wrapper {
  margin: 0 auto;
  width:710px;
}
.box {
  margin-top: 5px;
}
.menu {
  border: 5px solid black;
  padding: 10px;
  background-color: lightblue;
  font-family: monospace;
  margin-bottom: 10px;
  position: relative;
}
.btn {
  width: 100px;
  height: 60px;
  background-color: lightblue;
  border: 5px solid green;
  border-radius: 10px;
  margin-right: 10px;
  font-size: 24px;
  outline: 0;
  text-transform: uppercase;
}
.btn:hover {
  background-color: green;
  cursor: pointer;;
}
.description {
  position: absolute;
  width: 300px;
  height: 60px;
  top: 10px;
  right: 35px;
  font-size: 24px;
  text-align: center;
  background-color: lightblue;
}
.board {
  width: 710px;
  height: 510px;
  border: 5px solid black;
}
.cell {
  float:left;
  width: 10px;
  height: 10px;
  font-size:6px;
  text-align: center;
  border: 1px solid lightblue;
}
.living {
  background-color: green;
}
.dead {
  background-color: lightblue;
}
.footer {
  font-family: monospace;
  font-size: 14px;
  text-align: center;
  color: silver;
  padding-top: 5px;
}

              
            
!

JS

              
                var app = document.getElementById('app');
var boardWidth = 70;
var boardHeight = 50;
var a = boardWidth;
var b = boardHeight;
var board = [];
var boardNextGeneration=[];
var boardToSend=[];
var initialBoard=[];
var intervalID;

for (var i=0; i<a*b;i++) {
  var random = Math.floor(Math.random()*100+1);
  if (random>30) {
    initialBoard.push(0);
  } else {
    initialBoard.push(1);
  }
}

class Board extends React.Component {
  render () {
    var cells = this.props.board;
    return (
      <div>
        <div className="menu">
          <button type="button" className="btn" onClick={this.props.gameContinue} >Start</button>
          <button type="button" className="btn" onClick={this.props.stop} >Stop</button>
          <button type="button" className="btn" onClick={this.props.reset} >Clear</button>
          <div className="description">Game of Life <br />Generations: {this.props.generation}</div>
        </div>
          <div className="board" onClick={this.props.changeCell}>{cells}</div>
      </div>
    );
  }
}

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      board: initialBoard,
      generations: 0,
      status: true //false = simulation stopped
    }
    this.gameContinue = this.gameContinue.bind(this);
    this.gameStops = this.gameStops.bind(this);
    this.gameReset = this.gameReset.bind(this);
    this.changeCell = this.changeCell.bind(this);
  }

  changeCell(e) {
    if (this.state.status===false) {
      console.log(e.target.id);
      var cellID = e.target.id;
      if (board[cellID]===1) {
        board[cellID]=0;
      } else {
        board[cellID]=1;
      }
      this.setState({board: board})
    }
  }

  gameContinue(){
    if (this.state.status===false) {
        console.log("start");
        intervalID = setInterval(() => this.tick(), 333);
        this.setState({
          status: true
        });
    }
  }

  gameStops() {

    if (this.state.status===true) {
        clearInterval(intervalID);
        console.log("stop");
        this.setState({
          status: false
        })
    }
  }

  gameReset() {
      clearInterval(intervalID);
      board=[];
      console.log("reset");
      for (var i=0; i<a*b;i++) {
        board.push(0);
      }
      let emptyBoard = board;
      const clearGenerations = 0;
      this.setState({ board: emptyBoard,
        generations: clearGenerations, status: false });
  }

  tick() {
      generate();
      board=[];
      let newGeneration = this.state.generations + 1;
      this.setState({
        board: boardNextGeneration,
        generations: newGeneration
      });
      boardNextGeneration=[];
  }

  componentDidMount() {
    intervalID = setInterval(() => this.tick(), 333);
  }

  componentWillUnmount() {
    clearInterval(intervalID);
  }

  render() {
    var boardToShow = this.state.board;
    boardToSend=[];
      for (var i=0; i<a*b; i++) {
        var cellStatus;
        if (boardToShow[i]===1) cellStatus="living cell";
        if (boardToShow[i]===0) cellStatus = "dead cell";
        var elementToPush = <div className={cellStatus} id={i} key={i} ></div>;
        boardToSend.push(elementToPush);
        board.push(boardToShow[i]);
      }
    return (
      <div className="box">
        <Board board={boardToSend} generation={this.state.generations} gameContinue={this.gameContinue}
        stop={this.gameStops} reset={this.gameReset} changeCell={this.changeCell} />
      </div>
    );
  }
}

function newCell(n,s) {
  if (s===0 && n !== 3) { boardNextGeneration.push(0); }    //nothing
  if (s===0 && n === 3) { boardNextGeneration.push(1); }    // born
  if (s===1 && n <2 )   { boardNextGeneration.push(0); }   // dead-underpopulation
  if (s===1 && (n===2 || n===3)) { boardNextGeneration.push(1); }   // still lives
  if (s===1 && n>3 )  { boardNextGeneration.push(0); }    // dead-overpopulation
}

function generate() {
  for (var i=0; i<a*b; i++) {
    var neighborhood=0;
    if (i===0) {                                  // board: top-left corner
      if(board[ (i-a)+(a*b)   ] === 1) neighborhood++; //N direction for this cell
      if(board[ (i-a+1)+(a*b) ] === 1) neighborhood++; //NE
      if(board[ i+1           ] === 1) neighborhood++; //E
      if(board[ i+a+1         ] === 1) neighborhood++; //SE
      if(board[ i+a           ] === 1) neighborhood++; //S
      if(board[ (i-1+a) + a   ] === 1) neighborhood++; //SW
      if(board[ (i-1)+a       ] === 1) neighborhood++; //W
      if(board[ (i-1)+(a*b)   ] === 1) neighborhood++; //NW
      newCell(neighborhood,board[i]);
    }
    else if (i>0 && i < a-1) {                    // first(top) row without corners
      if(board[ (i-a)+(a*b)   ] === 1) neighborhood++; //N direction for this cell
      if(board[ i+1+(a*(b-1)) ] === 1) neighborhood++; //NE
      if(board[ i+1           ] === 1) neighborhood++; //E
      if(board[ i+a+1         ] === 1) neighborhood++; //SE
      if(board[ i+a           ] === 1) neighborhood++; //S
      if(board[ (i-1+a)       ] === 1) neighborhood++; //SW
      if(board[ (i-1)         ] === 1) neighborhood++; //W
      if(board[ (i-1-a)+(a*b) ] === 1) neighborhood++; //NW
      newCell(neighborhood,board[i]);
    }
    else if (i===a-1) {                           //top-rigth corner
      if(board[ (i-a)+(a*b)    ] === 1) neighborhood++; //N direction for this cell
      if(board[ (i-a+1)+(a*b)-a ] === 1) neighborhood++; //NE
      if(board[ i+1-a           ] === 1) neighborhood++; //E
      if(board[ i+1         ] === 1) neighborhood++; //SE
      if(board[ i+a           ] === 1) neighborhood++; //S
      if(board[ (i-1+a)    ] === 1) neighborhood++; //SW
      if(board[ (i-1)       ] === 1) neighborhood++; //W
      if(board[ (i-1-a)+(a*b)   ] === 1) neighborhood++; //NW
      newCell(neighborhood,board[i]);
    }
    else if ((i+1)%a===0 && i!==a-1 && i!==((a*b)-1)) { //right column without corners
      if(board[ (i-a)          ] === 1) neighborhood++; //N direction for this cell
      if(board[ (i-a+1)-a        ] === 1) neighborhood++; //NE
      if(board[ i+1 -a          ] === 1) neighborhood++; //E
      if(board[ i+1         ] === 1) neighborhood++; //SE
      if(board[ i+a           ] === 1) neighborhood++; //S
      if(board[ (i-1+a)    ] === 1) neighborhood++; //SW
      if(board[ (i-1)       ] === 1) neighborhood++; //W
      if(board[ (i-1-a)   ] === 1) neighborhood++; //NW
      newCell(neighborhood,board[i]);
    }
    else if (i===((a*b)-1)) {                         //bottom-rigth corner
      if(board[ (i-a)          ] === 1) neighborhood++; //N direction for this cell
      if(board[ (i-a+1)-a        ] === 1) neighborhood++; //NE
      if(board[ i+1-a          ] === 1) neighborhood++; //E
      if(board[ i+1 - (a*b)         ] === 1) neighborhood++; //SE
      if(board[ i+a  -(a*b)         ] === 1) neighborhood++; //S
      if(board[ (i-1+a)-(a*b)    ] === 1) neighborhood++; //SW
      if(board[ (i-1)       ] === 1) neighborhood++; //W
      if(board[ (i-1-a)   ] === 1) neighborhood++; //NW
      newCell(neighborhood,board[i]);
    }
    else if (i<((a*b)-1) && i >((a*b)-a)) {           //last(bottom) row without corners
      if(board[ (i-a)          ] === 1) neighborhood++; //N direction for this cell
      if(board[ (i-a+1)        ] === 1) neighborhood++; //NE
      if(board[ i+1           ] === 1) neighborhood++; //E
      if(board[ i+a+1-(a*b)    ] === 1) neighborhood++; //SE
      if(board[ i+a -(a*b)          ] === 1) neighborhood++; //S
      if(board[ (i-1+a)-(a*b)    ] === 1) neighborhood++; //SW
      if(board[ (i-1)       ] === 1) neighborhood++; //W
      if(board[ (i-1-a)   ] === 1) neighborhood++; //NW
      newCell(neighborhood,board[i]);
    }
    else if (i===((a*b)-a)) {                         // bottom-left corner
      if(board[ (i-a)          ] === 1) neighborhood++; //N direction for this cell
      if(board[ (i-a+1)        ] === 1) neighborhood++; //NE
      if(board[ i+1           ] === 1) neighborhood++; //E
      if(board[ i+a+1-(a*b)         ] === 1) neighborhood++; //SE
      if(board[ i+a-(a*b)           ] === 1) neighborhood++; //S
      if(board[ (i-1+a)-((b-1)*a)    ] === 1) neighborhood++; //SW
      if(board[ (i-1)+a       ] === 1) neighborhood++; //W
      if(board[ (i-1-a)+a   ] === 1) neighborhood++; //NW
      newCell(neighborhood,board[i]);
    }
    else if (i%a===0 && i!==0 && i!==((a*b)-a)) {     // left column without cornerns
      if(board[ (i-a)          ] === 1) neighborhood++; //N direction for this cell
      if(board[ (i-a+1)        ] === 1) neighborhood++; //NE
      if(board[ i+1           ] === 1) neighborhood++; //E
      if(board[ i+a+1         ] === 1) neighborhood++; //SE
      if(board[ i+a           ] === 1) neighborhood++; //S
      if(board[ (i-1+a)+a    ] === 1) neighborhood++; //SW
      if(board[ (i-1)+a       ] === 1) neighborhood++; //W
      if(board[ (i-1-a)+a   ] === 1) neighborhood++; //NW
      newCell(neighborhood,board[i]);
      //console.log("Lcol:",i,neighborhood,board[i]);
    }
    else {                                            // board: inner cells
      if(board[ (i-a)          ] === 1) neighborhood++; //N direction for this cell
      if(board[ (i-a+1)        ] === 1) neighborhood++; //NE
      if(board[ i+1           ] === 1) neighborhood++; //E
      if(board[ i+a+1         ] === 1) neighborhood++; //SE
      if(board[ i+a           ] === 1) neighborhood++; //S
      if(board[ (i-1+a)    ] === 1) neighborhood++; //SW
      if(board[ (i-1)       ] === 1) neighborhood++; //W
      if(board[ (i-1-a)   ] === 1) neighborhood++; //NW
      newCell(neighborhood,board[i]);
    }
  }
}

ReactDOM.render(<App />, app);

              
            
!
999px

Console