Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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="main_output"></div>
<p id="testDisp">oioi</p>
<p id="testDispTwo">Nothin yet</p>
<input id="targetCellInput"></input>
<button id="targetCellButton">Fire!</button>
<div id="cellItirateButtonOutput"></div>
<div id="cellcounter">Cell counter: </div>

<div class="numbers">
  <div class="columns"><span>0</span><span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span> </div><div class="rows">0
    1
  2
  3
  4
  5
  6
  7
  8
  9 
    <div>
</div>
              
            
!

CSS

              
                .cell {
  border: 1px solid black;
  width: 25px;
  height: 25px;
  background-color: white;
  &.alive {
    background-color: red;
  }
}

table {
  margin: 20px;
}

.numbers {
  z-index: -1;
  //border: 5px dashed lightgreen;
  position: fixed;
  top: 0;
  left: 0;
  & .columns {
    margin-left: 7px;
   // border: 2px dashed blue;
    &>span {
      margin-left: 18px;
    }
  }
  & .rows {
    margin-top: 0px;
    white-space: pre-line;
    line-height: 25px;
 //   border: 2px dashed blue;
  }
}

              
            
!

JS

              
                var newGlobalObj = {
  '"col-0-row-0"': "alive",
  '"col-0-row-1"': "alive",
  '"col-0-row-2"': "alive",
  '"col-0-row-3"': "alive",
  '"col-0-row-4"': "dead",
  '"col-0-row-5"': "dead",
  '"col-0-row-6"': "dead",
  '"col-0-row-7"': "alive",
  '"col-0-row-8"': "alive",
  '"col-0-row-9"': "alive",
  '"col-1-row-0"': "alive",
  '"col-1-row-1"': "dead",
  '"col-1-row-2"': "dead",
  '"col-1-row-3"': "alive",
  '"col-1-row-4"': "alive",
  '"col-1-row-5"': "alive",
  '"col-1-row-6"': "alive",
  '"col-1-row-7"': "alive",
  '"col-1-row-8"': "dead",
  '"col-1-row-9"': "dead",
  '"col-2-row-0"': "dead",
  '"col-2-row-1"': "alive",
  '"col-2-row-2"': "alive",
  '"col-2-row-3"': "alive",
  '"col-2-row-4"': "dead",
  '"col-2-row-5"': "dead",
  '"col-2-row-6"': "alive",
  '"col-2-row-7"': "dead",
  '"col-2-row-8"': "dead",
  '"col-2-row-9"': "alive",
  '"col-3-row-0"': "dead",
  '"col-3-row-1"': "alive",
  '"col-3-row-2"': "alive",
  '"col-3-row-3"': "dead",
  '"col-3-row-4"': "alive",
  '"col-3-row-5"': "alive",
  '"col-3-row-6"': "alive",
  '"col-3-row-7"': "alive",
  '"col-3-row-8"': "alive",
  '"col-3-row-9"': "alive",
  '"col-4-row-0"': "alive",
  '"col-4-row-1"': "dead",
  '"col-4-row-2"': "alive",
  '"col-4-row-3"': "alive",
  '"col-4-row-4"': "dead",
  '"col-4-row-5"': "alive",
  '"col-4-row-6"': "alive",
  '"col-4-row-7"': "dead",
  '"col-4-row-8"': "alive",
  '"col-4-row-9"': "dead",
  '"col-5-row-0"': "alive",
  '"col-5-row-1"': "dead",
  '"col-5-row-2"': "alive",
  '"col-5-row-3"': "dead",
  '"col-5-row-4"': "alive",
  '"col-5-row-5"': "dead",
  '"col-5-row-6"': "alive",
  '"col-5-row-7"': "dead",
  '"col-5-row-8"': "dead",
  '"col-5-row-9"': "alive",
  '"col-6-row-0"': "alive",
  '"col-6-row-1"': "alive",
  '"col-6-row-2"': "alive",
  '"col-6-row-3"': "dead",
  '"col-6-row-4"': "alive",
  '"col-6-row-5"': "alive",
  '"col-6-row-6"': "alive",
  '"col-6-row-7"': "dead",
  '"col-6-row-8"': "alive",
  '"col-6-row-9"': "dead",
  '"col-7-row-0"': "alive",
  '"col-7-row-1"': "dead",
  '"col-7-row-2"': "alive",
  '"col-7-row-3"': "alive",
  '"col-7-row-4"': "dead",
  '"col-7-row-5"': "alive",
  '"col-7-row-6"': "alive",
  '"col-7-row-7"': "alive",
  '"col-7-row-8"': "dead",
  '"col-7-row-9"': "dead",
  '"col-8-row-0"': "alive",
  '"col-8-row-1"': "dead",
  '"col-8-row-2"': "alive",
  '"col-8-row-3"': "alive",
  '"col-8-row-4"': "alive",
  '"col-8-row-5"': "dead",
  '"col-8-row-6"': "dead",
  '"col-8-row-7"': "alive",
  '"col-8-row-8"': "alive",
  '"col-8-row-9"': "alive",
  '"col-9-row-0"': "alive",
  '"col-9-row-1"': "dead",
  '"col-9-row-2"': "dead",
  '"col-9-row-3"': "alive",
  '"col-9-row-4"': "alive",
  '"col-9-row-5"': "alive",
  '"col-9-row-6"': "dead",
  '"col-9-row-7"': "alive",
  '"col-9-row-8"': "alive",
  '"col-9-row-9"': "dead"
};

var clearGlobalObj = {
  '"col-0-row-0"': "dead",
  '"col-0-row-1"': "dead",
  '"col-0-row-2"': "dead",
  '"col-0-row-3"': "dead",
  '"col-0-row-4"': "dead",
  '"col-0-row-5"': "dead",
  '"col-0-row-6"': "dead",
  '"col-0-row-7"': "dead",
  '"col-0-row-8"': "dead",
  '"col-0-row-9"': "dead",
  '"col-1-row-0"': "dead",
  '"col-1-row-1"': "dead",
  '"col-1-row-2"': "dead",
  '"col-1-row-3"': "dead",
  '"col-1-row-4"': "dead",
  '"col-1-row-5"': "dead",
  '"col-1-row-6"': "dead",
  '"col-1-row-7"': "dead",
  '"col-1-row-8"': "dead",
  '"col-1-row-9"': "dead",
  '"col-2-row-0"': "dead",
  '"col-2-row-1"': "dead",
  '"col-2-row-2"': "dead",
  '"col-2-row-3"': "dead",
  '"col-2-row-4"': "dead",
  '"col-2-row-5"': "dead",
  '"col-2-row-6"': "dead",
  '"col-2-row-7"': "dead",
  '"col-2-row-8"': "dead",
  '"col-2-row-9"': "dead",
  '"col-3-row-0"': "dead",
  '"col-3-row-1"': "dead",
  '"col-3-row-2"': "dead",
  '"col-3-row-3"': "dead",
  '"col-3-row-4"': "dead",
  '"col-3-row-5"': "dead",
  '"col-3-row-6"': "dead",
  '"col-3-row-7"': "dead",
  '"col-3-row-8"': "dead",
  '"col-3-row-9"': "dead",
  '"col-4-row-0"': "dead",
  '"col-4-row-1"': "dead",
  '"col-4-row-2"': "dead",
  '"col-4-row-3"': "dead",
  '"col-4-row-4"': "dead",
  '"col-4-row-5"': "dead",
  '"col-4-row-6"': "dead",
  '"col-4-row-7"': "dead",
  '"col-4-row-8"': "dead",
  '"col-4-row-9"': "dead",
  '"col-5-row-0"': "dead",
  '"col-5-row-1"': "dead",
  '"col-5-row-2"': "dead",
  '"col-5-row-3"': "dead",
  '"col-5-row-4"': "dead",
  '"col-5-row-5"': "dead",
  '"col-5-row-6"': "dead",
  '"col-5-row-7"': "dead",
  '"col-5-row-8"': "dead",
  '"col-5-row-9"': "dead",
  '"col-6-row-0"': "dead",
  '"col-6-row-1"': "dead",
  '"col-6-row-2"': "dead",
  '"col-6-row-3"': "dead",
  '"col-6-row-4"': "dead",
  '"col-6-row-5"': "dead",
  '"col-6-row-6"': "dead",
  '"col-6-row-7"': "dead",
  '"col-6-row-8"': "dead",
  '"col-6-row-9"': "dead",
  '"col-7-row-0"': "dead",
  '"col-7-row-1"': "dead",
  '"col-7-row-2"': "dead",
  '"col-7-row-3"': "dead",
  '"col-7-row-4"': "dead",
  '"col-7-row-5"': "dead",
  '"col-7-row-6"': "dead",
  '"col-7-row-7"': "dead",
  '"col-7-row-8"': "dead",
  '"col-7-row-9"': "dead",
  '"col-8-row-0"': "dead",
  '"col-8-row-1"': "dead",
  '"col-8-row-2"': "dead",
  '"col-8-row-3"': "dead",
  '"col-8-row-4"': "dead",
  '"col-8-row-5"': "dead",
  '"col-8-row-6"': "dead",
  '"col-8-row-7"': "dead",
  '"col-8-row-8"': "dead",
  '"col-8-row-9"': "dead",
  '"col-9-row-0"': "dead",
  '"col-9-row-1"': "dead",
  '"col-9-row-2"': "dead",
  '"col-9-row-3"': "dead",
  '"col-9-row-4"': "dead",
  '"col-9-row-5"': "dead",
  '"col-9-row-6"': "dead",
  '"col-9-row-7"': "dead",
  '"col-9-row-8"': "dead",
  '"col-9-row-9"': "dead"
};

/*========================== Actual Code ===============================*/

var globalObj = {};
var timerRunning = false;

var Cell = React.createClass({
  getInitialState() {
    return {
      life: (Math.floor(Math.random() * 2) == 0) ? "dead" : "alive"
    };
  },

  handleClick() {
    var that = this;
    var id = "col-" + this.props.colId + "-row-" + this.props.rowId;
    if (this.state.life === "alive") {
      //console.log("Cell " + id + " is now dead.");
      that.setState({
        life: "dead"
      });
      globalObj[
        '"' + "col-" + this.props.colId + "-row-" + this.props.rowId + '"'
      ] =
        "dead";
    } else if (this.state.life === "dead") {
      //console.log("Cell " + id + " is now alive.");
      that.setState({
        life: "alive"
      });
      globalObj[
        '"' + "col-" + this.props.colId + "-row-" + this.props.rowId + '"'
      ] =
        "alive";
    }
  },

  testDisp() {
    var that = this;
    $("#testDisp").html("Col: " + that.props.colId);
    $("#testDispTwo").html("Row: " + that.props.rowId);
  },

  componentDidMount() {
    var thisId = "col-" + this.props.colId + "-row-" + this.props.rowId;
    //console.log("Cell " + thisId + " mounted.");
    var thisIdOut = '"' + thisId + '"';
    globalObj[thisIdOut] = this.state.life;
  },

  componentWillReceiveProps(nextProps) {
    this.checkLife();
  },

  checkLife() {
    //console.log("checkLife fired");
    //must use self id to check other cells
    var col = this.props.colId;
    var row = this.props.rowId;
    var id = "col-" + col + "-row-" + row;
    //  row-5-col-2
    var topLeft = "col-" + (col - 1) + "-row-" + (row - 1);
    var topCenter = "col-" + col + "-row-" + (row - 1);
    var topRight = "col-" + (col + 1) + "-row-" + (row - 1);

    var centerLeft = "col-" + (col - 1) + "-row-" + row;
    var centerRight = "col-" + (col + 1) + "-row-" + row;

    var bottomLeft = "col-" + (col - 1) + "-row-" + (row + 1);
    var bottomCenter = "col-" + col + "-row-" + (row + 1);
    var bottomRight = "col-" + (col + 1) + "-row-" + (row + 1);

    var checkList = [
      topLeft,
      topCenter,
      topRight,
      centerLeft,
      centerRight,
      bottomLeft,
      bottomCenter,
      bottomRight
    ];

    //console.log("this cell id is: " + id + ", bottom right of it is: " + bottomRight);
    //alive + 2/3 neighbors = life
    //alive + <2 or >3 dies
    //dead + 3 neighbors becomes alive
    var counter = 0;
    for (var i = 0; i < 8; i++) {
      //var check = globalObj[checkList[i]];
      var item = '"' + checkList[i] + '"';
      //console.log(globalObj[item]);
      if (globalObj[item] == "alive") {
        //console.log("Cell " + item + " is alive.");
        counter++;
      }
      $("#cellcounter").html("Cell counter: " + counter);
    }

    var life = this.state.life;
    var that = this;

    if (life == "alive") {
      if (counter === 2 || counter === 3) {
        //console.log("STATUS: Cell continues to live");
      } else {
        //console.log("STATUS: Cell has dies hanously");
        that.handleClick();
      }
    } else if (life == "dead" && counter === 3) {
      //console.log("STATUS: Cell has rebirthed");
      that.handleClick();
    } else {
      //console.log("STATUS: Cell is still dead jim");
    }
  },

  render() {
    if (this.state.life === "alive") {
      return (
        <td
          className="cell alive"
          onClick={this.handleClick}
          onContextMenu={this.checkLife}
          onMouseEnter={this.testDisp}
          id={"col-" + this.props.colId + "-row-" + this.props.rowId}
        />
      );
    } else {
      return (
        <td
          className="cell"
          onClick={this.handleClick}
          onContextMenu={this.checkLife}
          onMouseEnter={this.testDisp}
          id={"col-" + this.props.colId + "-row-" + this.props.rowId}
        />
      );
    }
  }
});

var Table = React.createClass({
  getInitialState() {
    return {
      number: 10,
      arr: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
      itiration: 0
    };
  },

  componentDidMount() {
    console.log(globalObj);
    this.toggleTimer();
  },

  nextGen() {
    var count = this.state.itiration;
    this.setState({
      itiration: count + 1
    });
  },

  toggleTimer() {
    if (timerRunning) {
      timerRunning = false;
      console.log("Stopping Timer");
      clearInterval(this.timer);
    } else {
      timerRunning = true;
      console.log("Starting Timer");
      this.timer = setInterval(this.nextGen, 500);
    }
  },

  clearBoard() {
    var itir = this.state.number;
    Object.assign(globalObj, clearGlobalObj);
    this.setState({
      number: itir
    });
  },

  render() {
    return (
      <div>
        <table>
          <tbody>
            {this.state.arr.map((each, rowIndex) => (
              <tr>
                {this.state.arr.map((each, colIndex) => (
                  <Cell
                    rowId={rowIndex}
                    colId={colIndex}
                    itiration={this.state.itiration}
                  />
                ))}
              </tr>
            ))}
          </tbody>
        </table>
        <button onClick={this.nextGen} className="btn btn-default">
          Next Gen
        </button>
        <button onClick={this.toggleTimer} className="btn btn-default">
          Play / Pause
        </button>
        <button onClick={this.clearBoard} className="btn btn-default">
          Clear
        </button>
        <p>Generation: {this.state.itiration}</p>
      </div>
    );
  }
});

ReactDOM.render(<Table />, document.getElementById("main_output"));

$("#targetCellButton").on("click", function() {
  var hbg = $("#targetCellInput").val();
  $("#" + hbg).css("background-color", "blue");
});



              
            
!
999px

Console