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 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

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

              
                <h1>Conway's Game of Life</h1>
<canvas id="canvas" width="500" height="500"></canvas>
<div id="cycle"></div>
<div id="isStable"></div>
<div id="isDone"></div>
<div>
  <p><strong>The Rules of the Game</strong></p>
  <ol>
    <li>Any live cell with fewer than two live neighbors dies, as if caused by under-population.</li>
    <li>Any live cell with two or three live neighbors lives on to the next generation.</li>
    <li>Any live cell with more than three live neighbors dies, as if by overcrowding.</li>
    <li>Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.</li>
  </ol>
  <cite><a href="http://en.wikipedia.org/wiki/Conway's_Game_of_Life#Rules">Conway's Game of Life on Wikipedia</a></cite>
</div>
              
            
!

CSS

              
                @import "compass/css3";

$font-stack: "Lucida Grande", "Lucida Sans Unicode", Tahoma, sans-serif;

canvas{
  display: block;
  background: #444;
  margin: 0 auto;
}

h1{
  font-family: "Droid Sans";
  text-align: center;
}

div{
  font-family: $font-stack;
  font-size: 120%;
  max-width: 36em;
  text-align: justify;
  margin: 0 auto;
}
              
            
!

JS

              
                $(document).ready(function(){
  var width;
  var height;
  var grid = [];
  var ctx;
  var alive;
  var intervalId;
  var cycle;
  
  function checkNeighbors(i, j, xMax, yMax, isLive){
    
    var neighborCount = 0;
    
    // Check left
    if (i - 1 >= 0){
      neighborCount += grid[(i-1) + j*xMax];
    }
    
    // Check right
    if (i + 1 <= xMax){
      neighborCount += grid[(i+1) + j*xMax];
    }
    
    // Check up
    if (j - 1 >= 0){
      neighborCount += grid[i + (j-1)*xMax];
    }
    
    // Check down
    if (j + 1 <= yMax){
      neighborCount += grid[i + (j+1)*xMax];
    }
    
    // Check upper left
    if ( (i - 1 >= 0) && (j - 1 >= 0) ){
      neighborCount += grid[(i-1) + (j-1)*xMax];
    }

    // Check upper right
    if ( (i + 1 <= xMax) && (j - 1 >= 0) ){
      neighborCount += grid[(i+1) + (j-1)*xMax];
    }

    // Check lower left
    if ( (i - 1 >= 0) && (j + 1 <= yMax) ){
      neighborCount += grid[(i - 1) + (j+1)*xMax];
    }
    
    // Check lower right
    if ( (i + 1 <= xMax) && (j + 1 <= yMax) ){
      neighborCount += grid[(i + 1) + (j+1)*xMax];
    }
    
    // Any live cell w/ < 2 neighbors dies
    if ( neighborCount < 2 && isLive == 1){
      return 0;
    }
    
    // Any live cell w/ 2 or 3 neighbors lives
    else if ( neighborCount == 2 && isLive == 1){
      return 1;
    }
    
    else if ( neighborCount == 3 && isLive == 1){
      return 1;
    }
    
    // Any live cell w/ > 3 neighbors dies
    else if ( neighborCount > 3 && isLive == 1){
      return 0;
    }
    
    // Any dead cell w/ exactly 3 live neighbors lives
    if ((neighborCount == 3) && (isLive == 0)){
      return 1;
    }
    
    else {
      return 0;
    }

  }

  function Cell(i, j, r, alive){
    ctx.beginPath();
    ctx.arc(i, j, r, 0, 2*Math.PI);
    ctx.strokeStyle = "#666";
    ctx.stroke();
    if (alive){
      ctx.fillStyle = "#ffcc00";
      ctx.fill();
    }
  }
  
  function setupGame(xMax, yMax, r, n){
    ctx.clearRect(0, 0, width, height);
    for (var y = 0; y < yMax; y++){
      for (var x = 0; x < xMax; x++){
        alive = Math.floor(Math.random()*3);
        alive = Math.floor(alive/2);
        Cell(x*n+n/2, y*n+n/2, r, alive);
        grid.push(alive);
      }
    }
    cycle++;
  }
  
  function printGrid(grid, xMax, yMax){
    var printed = "";
    for (var i = 0; i < grid.length; i++){
      printed += grid[i];
      if (i%yMax == 0 && i != 0){
        printed += "<br>";
      }
    }
    $("#printed").html(printed);
  }

  
  function playGame(xMax, yMax, r, n){
    ctx.clearRect(0, 0, width, height);
    var gridCopy = [];
    for (var y = 0; y < yMax; y++){
      for (var x = 0; x < xMax; x++){
        gridCopy[x+y*xMax] = checkNeighbors(x, y, xMax, yMax, grid[x+y*xMax]);
        /*Cell(x*n+n/2, y*n+n/2, r, gridCopy[x+y*xMax]);*/
      }
    }
    grid = gridCopy.slice(0);
    for (var y = 0; y < yMax; y++){
      for (var x = 0; x < xMax; x++){
        Cell(x*n+n/2, y*n+n/2, r, grid[x+y*xMax]);
      }
    }
    cycle++;
    $("#cycle").text(cycle);
  }
  
  function init(){
    ctx = $("#canvas")[0].getContext('2d');
    height = $("#canvas").width();
    width = $("#canvas").height();
    
    // These values are the most aesthetically pleasing
    var xMax = 75;
    var yMax = 75;
    var n = 6.65;
    var r = 2;

    cycle = 0;
    setupGame(xMax, yMax, r, n);
    intervalId = setInterval(function() {playGame(xMax, yMax, r, n)}, 75);
  }
  
  init();
});
              
            
!
999px

Console