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.

            
              <div class="navbar navbar-fixed-top navbar-inverse">
  <div class="navbar-brand">Game Of Life</div>
</div> 
<div class="container"></div>
            
          
!
            
              $font-stack: "Raleway", cursive
$primary-color: rgba(0,0,0,0.3)
$secondary-color: #333

body
  font-family: $font-stack
  background-color: #fff
  
h3
  text-align: center
  margin-bottom: 15px
  
.container
  margin-top: 60px
  
.navbar-inverse
  background-color: forestgreen
  border-bottom: none
  
.navbar
  .navbar-brand
    color: #fff
    font-weight: bold
  
.cell
  width: 15px
  height: 15px
  border: 1px solid $primary-color
  float: left
  
.dead
  background: none
  
.alive
  background-color: rgb(119, 119, 119)
  
.old
  background-color: $secondary-color
  
.board
  width: 750px
  max-width: 750px
  color: #fff
  
.button-bar
  color: #fff
  margin-top: 10px
  width: 750px
  padding: 20px
            
          
!
            
              var generations = 0;
var notBlanks = [], blanks = [], parts = [];

var Cells = React.createClass({
  handleAddChild: function(e) {
    var cellId = Number(e.target.id);
    
    if (notBlanks.includes(cellId)) {
      $('#' + cellId).removeClass('old').removeClass('alive').addClass('dead');
      notBlanks.splice(notBlanks.indexOf(cellId), 1);
      blanks.push(cellId);
      console.log(cellId + "nth cell dead");
    }
    else{
      $('#' + cellId).removeClass('dead').addClass('alive');
      blanks.splice(blanks.indexOf(cellId), 1);
      notBlanks.push(cellId);
      console.log(cellId + "nth cell alive");
    }
  },
  render() {
  var cells = [];
  for (var i = 1; i <= 1500; i++){
      cells.push(<div className="cell dead" id={i} onClick={this.handleAddChild}></div>);
      blanks.push(i);
  }
    return (
    <div>
      {cells}
    </div>
    );
  }
});
    const checkSides = {
      count: 0,
      north: function(num) {
        if (num >=1 && num <=50) {
          if (notBlanks.includes(num + 1450)) {
           this.count++;
          }
        }
        if (notBlanks.includes(num - 50)) {
          this.count++;
        }
      },
      south: function(num) {
        if (num >=1451 && num <=1500) {
          if (notBlanks.includes(num - 1450)) {
            this.count++;
          }
        }      
        if (notBlanks.includes(num + 50)) {
          this.count++;
        }
      },
      east: function(num) {
        if (num % 50 === 0 && num !== 0) {
          if (notBlanks.includes(num - 49)) {
            this.count++;
          }
        }
        if (notBlanks.includes(num + 1)) {
          this.count++;
        }
      },
      west: function(num) {
        if (num % 50 === 0) {
          if (notBlanks.includes(num + 49)){
            this.count++;
          }
        }
        if (notBlanks.includes(num - 1)){
          this.count++;
        }
      },
      northWest: function(num) {
        if (num >=1 && num <=50) {
          if (notBlanks.includes(1449 + num)) {
            this.count++;
          }
        }
        if (notBlanks.includes(num - 51)  && num % 50 !== 0 && num % 50 !== 1){
          this.count++;
        }
      },
      southWest: function(num) {
        if (num >=1451 && num <=1500) {
          if (notBlanks.includes(num - 1451)) {
            this.count++;
          }
        }
        if (notBlanks.includes(num + 49) && num % 50 !== 0 && num % 50 !== 1) {
          this.count++;
        }
      },
      northEast: function(num) {
        if (num >=1 && num <=50){
          if (notBlanks.includes(1451 + num)) {
            this.count++;
          }
        }
        if (notBlanks.includes(num - 49) && num % 50 !== 0 && num % 50 !== 1) {
          this.count++;
        }
      },
      southEast: function(num) {
        if (num >=1451 && num <=1500) {
          if (notBlanks.includes(num - 1449)) {
            this.count++;
          }
        } 
        if (notBlanks.includes(num + 51) && num % 50 !== 0 && num % 50 !== 1) {
          this.count++;
        } 
      }
      
      };

      function couple(num) {
        /* This function combines all the checkSides to give me a concise value of count */
        checkSides.north(num);
        checkSides.south(num);
        checkSides.east(num);
        checkSides.west(num);
        checkSides.southWest(num);
        checkSides.southEast(num);
        checkSides.northEast(num);
        checkSides.northWest(num);
      }

  var checkNonBlanks =  function() {
    for(var i in notBlanks){
      checkSides.count = 0;
      couple(notBlanks[i]);
      if (checkSides.count === 2 || checkSides.count === 3) {
        parts.push(notBlanks[i]); //parents staying!
        $('#' + notBlanks[i]).removeClass('dead').addClass('old');
      }
      else{
        $('#' + notBlanks[i]).removeClass('alive').removeClass('old').addClass('dead');
        blanks.push(notBlanks[i]);
      }
    }

    notBlanks = parts;
  };

  var checkBlanks = function() {
    var miniparts = []; //Children created
    for(var i in blanks){
      checkSides.count = 0;
      couple(blanks[i]);
      if (checkSides.count === 3) {
        $('#' + blanks[i]).removeClass('dead').addClass('alive');
        miniparts.push(blanks[i]);
      }
    }
    parts = miniparts;
      var filteredBlanks = blanks.filter(function(x){
       return !parts.some(function(y){
         return x === y;  
        });
      });

      blanks = filteredBlanks;
  };

var Board = React.createClass({
  componentDidMount: function() {
    $('.gen').text(generations);
        for(var i = 0; i < 150; i++){
const cellSent = Math.floor(Math.random() * (1500 - 1 + 1) + 1);
      if (!notBlanks.includes(cellSent)){
          notBlanks.push(cellSent);
      }
    }
    this.startGame();
  },
  startGame: function () {
    this.interval = setInterval(this.running, 70);
  },
  slow: function() {
    this.interval = setInterval(this.running, 750);
  },
  fast: function() {
    this.interval = setInterval(this.running, 750);
  },
  pauseGame: function () {
    clearInterval(this.interval);
  },
  clearGame: function () {
    clearInterval(this.interval);
    generations = 0;
    notBlanks.length = 0;
     for (var i = 1; i <= 1500; i++){
        blanks.push(i);
     }
    $('.cell').removeClass('alive').removeClass('old').addClass('dead');
  },
  running: function () {
    $('.gen').text(generations);
    if(notBlanks.length === 0){
      generations = 0;
    }
    else{
      //Do some computations here -->
      checkBlanks();  
      checkNonBlanks();
      generations++;
    }   
  },
  render() {
    return (
    <div className="row">
    <div className="col-md-2" />
    <div className="col-md-8">
      <h3>Generations: <span className="gen"></span></h3>
      <div className="board">
        <Cells/> 
      </div>
      <div className="button-bar text-center">
      <div className="btn-group">
        <button className="btn btn-sm btn-info" onClick={this.startGame}>Run</button>
        <button className="btn btn-sm btn-default" onClick={this.pauseGame}>Pause</button>
        <button className="btn btn-sm btn-danger" onClick={this.clearGame}>Clear</button>
      </div> &nbsp;
      <div className="btn-group">
        <button className="btn btn-sm btn-primary" onClick={this.fast}>Fast</button>
        <button className="btn btn-sm btn-danger" onClick={this.slow}>Slow</button>
      </div>
    </div>
    </div>
    <div className="col-md-2" />
    </div>
    );
  }
});

React.render(<Board />, document.querySelector('.container'));
            
          
!
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