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 id = 'root'></div>
              
            
!

CSS

              
                * {
  padding: 0;
  margin: 0;
}
body {
  background-color: #151515;
  color: white;
  font-family: "Unica One", cursive;
  font-size: 16px;
}
.gen {
  margin-top: 11px;
}
.life {
  display: flex;
  p {
    font-size: 45px;
    margin: 0 3px;
    font-weight: bold;
  }
  .l {
    color: #f16a70;
  }
  .i {
    color: #b1d877;
  }
  .f {
    color: #8cdcda;
  }
  .e {
    color: #ffff99;
  }
}
.container {
  margin: auto;
  width: 900px;
}

.speed {
  display: flex;
  padding-top: 12px;
  p {
    margin: 0 10px;
  }
}
.controls {
  justify-content: space-between;
  align-content: center;
  display: flex;
  padding: 25px 10px 0;
}
.game-board {
  display: inline-flex;
  flex-wrap: wrap;
  background-color: #565656;
  width: 100%;
}
.buttons {
  padding-top: 10px;
  button {
    background-color: transparent;
    border-style: none;
    border: 1px solid white;
    color: white;
    padding: 5px 15px;
    margin: 0 5px;
    cursor: pointer;
    transition: 0.3s;
    font-family: "Unica One", cursive;
    font-size: 16px;
  }
  .start:hover {
    color: #b1d877;
    border: 1px solid green;
  }
  .stop:hover {
    color: #f16a70;
    border: 1px solid #f16a70;
  }
  .clear:hover {
    color: #8cdcda;
    border: 1px solid #8cdcda;
  }
}

.cell {
  width: 15px;
  height: 15px;
  border: 1px solid #2c2c2c;
  box-sizing: border-box;
  cursor: pointer;
}
.active {
  background-color: #f16a70;
  color: white;
}
input {
  direction: rtl;
}

              
            
!

JS

              
                const height = 40;
const width = 60;
const cells = [];
let timer = null;
let innerTimer = null;

//build array of cells based on dimentions
const buildCells = array => {
  const totalCells = height * width;
  for (let i = 0; i < totalCells; i++) {
    array.push({ active: false });
  }
};

//returns true if no active cells currently
const noActiveCells = cells => {
  let activeCells = 0;
  cells.forEach(cell => {
    cell.active && activeCells++;
  });
  return activeCells == 0 && true;
};

buildCells(cells);

class GameBoard extends React.Component {
  constructor() {
    super();
    this.state = {
      cells: cells,
      speed: 500,
      running: false,
      generation: 0
    };
  }
  start() {
    const that = this;
    const noCells = noActiveCells(that.state.cells);
    const update = this.update.bind(this);
    
    //run if not already running and no active cells currently
    if (!this.state.running && !noCells) {
      this.setState({ running: true });
      timer = setTimeout(function run() {
        update();
        innerTimer = setTimeout(run, that.state.speed);
        const noCells = noActiveCells(that.state.cells);
        
        //clear board if no active cells this interval
        if (noCells) that.clear();
      }, that.state.speed);
    }
  }
  
  //stop board on current gen
  stop() {
    clearTimeout(innerTimer);
    clearTimeout(timer);
    this.setState({ running: false });
  }
  clear() {
    //reset all cells to active: false
    const cells = [...this.state.cells];
    cells.map(cell => {
      cell.active = false;
    });
    this.setState({ cells: cells });
    this.setState({ generation: 0 });
    this.setState({ running: false });
    this.stop();
  }
  
  //sets speed based on speed slider
  speed() {
    const speed = this.slider.value;
    this.setState({ speed });
  }
  
  //activate cells when clicked
  activate(cell) {
    const cells = [...this.state.cells];
    cells[cell].active = true;
    this.setState({ cells });
  }
  
  //all the game logic, check all cells for neighbors
  update() {
    const generation = this.state.generation + 1;
    this.setState({ generation });
    const cells = [...this.state.cells];
    const cellsToUpdate = [];
    let neighbors = 0;

    cells.map((cell, index) => {
      
      //check for active cells with 4+ or 1-
      if (cell.active) {
        //above
        if (cells[index - width] !== undefined && cells[index - width].active)
          neighbors++;
        //below
        if (cells[index + width] !== undefined && cells[index + width].active)
          neighbors++;
        //left
        if (cells[index - 1] !== undefined && cells[index - 1].active)
          neighbors++;
        //right
        if (cells[index + 1] !== undefined && cells[index + 1].active)
          neighbors++;
        //upper right
        if (cells[index - width + 1] !== undefined && cells[index - width + 1].active)
          neighbors++;
        //lower left
        if (cells[index + width - 1] !== undefined && cells[index + width - 1].active)
          neighbors++;
        //upper left
        if (cells[index - width - 1] !== undefined && cells[index - width - 1].active)
          neighbors++;
        //lower right
        if (cells[index + width + 1] !== undefined && cells[index + width + 1].active)
          neighbors++;
        
        if (neighbors >= 4 || neighbors <= 1) {
          cellsToUpdate.push(index);
          neighbors = 0;
        } else neighbors = 0;
      
      //check dead cells for exactly 3 neighbors
      } else if (!cell.active) {
        //above
        if (cells[index - width] !== undefined && cells[index - width].active)
          neighbors++;
        //below
        if (cells[index + width] !== undefined && cells[index + width].active)
          neighbors++;
        //left
        if (cells[index - 1] !== undefined && cells[index - 1].active)
          neighbors++;
        //right
        if (cells[index + 1] !== undefined && cells[index + 1].active)
          neighbors++;
        //upper right
        if (cells[index - width + 1] !== undefined && cells[index - width + 1].active)
          neighbors++;
        //lower left
        if (cells[index + width - 1] !== undefined && cells[index + width - 1].active)
          neighbors++;
        //upper left
        if (cells[index - width - 1] !== undefined && cells[index - width - 1].active)
          neighbors++;
        //lower right
        if (cells[index + width + 1] !== undefined && cells[index + width + 1].active)
          neighbors++;
        
        if (neighbors == 3) {
          cellsToUpdate.push(index);
          neighbors = 0;
        } else neighbors = 0;
      }
    });

    //update cells based on game rules
    cellsToUpdate.map(cell => {
      cells[cell].active = !cells[cell].active;
      this.setState({ cells });
    });
  }
  render() {
    return (
      <div className="container">
        <div className="controls">
          <div className="life">
            <p className="l">L</p>
            <p className="i">I</p>
            <p className="f">F</p>
            <p className="e">E</p>
          </div>
          <div className="buttons">
            <button className="start" onClick={this.start.bind(this)}>
              START
            </button>
            <button className="stop" onClick={this.stop.bind(this)}>
              STOP
            </button>
            <button className="clear" onClick={this.clear.bind(this)}>
              CLEAR
            </button>
          </div>
          <div className="speed">
            <p>SLOWER</p>
            <input
              onInput={this.speed.bind(this)}
              ref={input => (this.slider = input)}
              type="range"
              min="10"
              max="500"
              defaultValue="250"
            />
            <p>FASTER</p>
          </div>
          <p className="gen">GENERATION: {this.state.generation}</p>
        </div>
        <div className="game-board">
          {this.state.cells.map((cell, index) => {
            return (
              <Cell
                toggleActive={this.activate.bind(this)}
                details={this.state.cells[index]}
                cellKey={index}
              />
            );
          })}
        </div>
      </div>
    );
  }
}

class Cell extends React.Component {
  getKey() {
    this.props.toggleActive(this.props.cellKey);
  }
  render() {
    return (
      <div
        onClick={this.getKey.bind(this)}
        active={this.props.details.active}
        className={this.props.details.active ? "cell active" : "cell"}
      />
    );
  }
}

ReactDOM.render(<GameBoard />, document.querySelector("#root"));

              
            
!
999px

Console