css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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

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

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.

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.

            
              $font-stack: Roboto;
$board-color: #4b535c;
  
body {
  text-align: center;
  font-family: $font-stack;
}

.flex-container {
  display: flex;
  justify-content: center;
  background-color: #4b535c;
  flex-direction: row;
  flex-wrap: wrap;
  max-width: 850px;
  margin: 0 auto;
}

.flex-container>div {
  width: 10px;
  height: 10px;
  border: solid 1px #505963;
  text-align: center;
}

.Xcolor {
  background: #E55EA2;
}

.Ocolor {
  background: $board-color;
}

button {
  border-radius: 5px;
  margin: 5px;
  height: 40px;
  width: 80px;
  font-weight: bold;
  border: solid #4b535c;
}

button:focus {outline:0;}

#pause {
  background: #ffdb58;
}

#resume {
  background: #7cff58;
}

#reset {
  background: #ff587c;
}


            
          
!
            
              class Game extends React.Component {
  constructor(props) {
    super(props);
    this.size = 4900;
    let initialSquares = Array(this.size).fill("O");
    let seed = [75, 145, 215, 410, 479, 480, 481, 775, 776, 777, 845, 916, 1920, 1921, 1922, 1852, 1781, 1204, 1205, 1206, 1275];
    for (let i=0; i<seed.length; i++) {
      initialSquares[seed[i]]="X";
    }
    this.state = {
                  count: 0, 
                  squares: initialSquares,
                  running: true
                 };
  }

  componentDidMount() {
    this.timerID = setInterval(
      () => this.tick(),
      500
    );
  }

  componentWillUnmount() {
    clearInterval(this.timerID);
  }

  tick() {
    if (this.state.running) {
      this.setState({
        count: this.state.count + 1
      });
      this.cycle()
      this.seed()
    }
  }
  
  seed() {
    let s = this.state.squares;
    let n = Math.sqrt(this.size);
    let randomPoint = Math.floor(Math.random() * this.size)  
    const blinker = [randomPoint - 1, randomPoint + 1, randomPoint];
    const block = [randomPoint, randomPoint + 1, randomPoint + n,
                randomPoint + n + 1];
    const tub = [randomPoint, randomPoint + n * 2, 
                 randomPoint + n - 1, randomPoint + n + 1];
    const glider = [randomPoint - 1, randomPoint + 1, randomPoint,
                   randomPoint + n - 1, randomPoint + n * 2];
    let nice = [randomPoint - 1, randomPoint + 1, randomPoint,
               randomPoint + n]
    const patterns = [blinker, block, tub, glider];
    if (Math.random() < .5) {
      let chosen = patterns[Math.floor(Math.random() * patterns.length)];  
      for (let i=0; i<chosen.length; i++) {
        if (chosen[i] >= 0 && chosen[i] <= this.size) {
          s[chosen[i]] = "X";
        }
      }
    }
  }
  
  pause() {
    //https://github.com/facebook/react/issues/5040
    //'this' becomes undefined when called from onClick inside a class that extends React.Component 
    this.setState({
      running: false
    });
  }
  
  resume() { 
    this.setState({
      running: true
    });
  }
  
  reset() {
    let initialSquares = Array(this.size).fill("O");
     this.state = {
                  count: 0, 
                  squares: initialSquares,
                  running: true
                 };  
  }
  
  cycle() {
    let s = this.state.squares;
    let neighborCount = 0;
    let n = Math.sqrt(this.size);
    let births = [];
    let deaths = [];
    //finds the eight neighboring squares 
    
    s.forEach(function(item, i){
      const adjacents = [i-1, i+1, i-n-1, i-n, i-n+1, i+n-1, i+n, i+n+1];
      for (let j=0; j<adjacents.length; j++) {
        if (s[adjacents[j]] == "X") {
          neighborCount++;
        }
      }
    //determines if the central square will be born or die
      if (s[i] == "O" && neighborCount == 3) {
        births.push(i);
      }
      else if ((s[i] == "X" && neighborCount < 2)
      || (s[i] == "X" && neighborCount > 3))
      {
        deaths.push(i);
      } 
      neighborCount = 0;
    });
    //the array updates after all births and deaths for the round have been computer
    for (let k=0; k<births.length; k++) {
      s[births[k]] = "X";
    }
      
    for (let k=0; k<deaths.length; k++) {
      s[deaths[k]] = "O";
    }
    births = [];
    deaths = [];
   this.setState({
    squares: s
   })
  }

  render() {
    return (
      <div>
        <button id="pause" onClick={this.pause.bind(this)}>
        PAUSE
        </button>
        <button id="resume" onClick={this.resume.bind(this)}>
        RESUME
        </button>
        <button id="reset" onClick={this.reset.bind(this)}>
        RESET
        </button>
        <h1>{this.state.count}</h1>
        <div id="board" className="flex-container">
          {this.state.squares.map((square,index) => 
           <div className={square + "color"}  id={"square" + index} key={index}></div>)}
        </div>
      </div>
    );
  }
}

ReactDOM.render(
  <Game />,
  document.getElementById('app')
);

            
          
!
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