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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <!--Created for CRCERA by Lyle Muller and Catherine Eng using d3js.-->
<h1 style="margin:0 auto;display:block;width:400px;font-size:3em;">Game of Life</h1>
<div class="nav"><div class="gen">Generation: <span id="status">0</span></div>
<div class="buttons">
<a id="nextGen" href="#"><div class="buttonGame">Next Generation</div></a>
<a id="startSimulation" href="#"><div class="buttonGame">Start Simulation</div></a>
<a id="stopSimulation" href="#"><div class="buttonGame">Stop Simulation</div></a>
<a id="clearMatrix" href="#"><div class="buttonGame">Clear Matrix</div></a>
</div>
</div>

              
            
!

CSS

              
                html,
body { 	width: 100%; height: 100%; margin: 0 auto; font-family: "Open Sans", sans-serif;font-weight: 300;padding-top:0px;
	color: #fff;
	background: linear-gradient(-45deg, #EE7752, #E73C7E, #23A6D5, #23D5AB);
	background-size: 400% 400%;
	/*-webkit-animation: Gradient 15s ease infinite;
	-moz-animation: Gradient 15s ease infinite;
	animation: Gradient 15s ease infinite;*/
}
svg  { position: relative; display: block; width:600px; margin: 30px auto; }
p {	padding: 10px; font-family: "Open Sans", sans-serif;font-weight: 300;	font-size: 12px; }
h1 { padding: 0 10px; font-size: 14px; font-family: "Open Sans", sans-serif;font-weight: 300; }
.nav {dislay:block;width:300px;
            float:left;align-content:right;border:0 !important;}
.gen { display:block;width:180px;text-align:left;margin:0 auto;padding-left: 10px; font-family: "Open Sans", sans-serif;font-weight: 300;font-size:16px; }
.instructions { display:block;width:500px;margin:0 auto;font-family: "Open Sans", sans-serif;font-weight: 300;font-size:.8em; }
.title { display:block;width:290px;margin:0 auto;font-family: "Open Sans", sans-serif;font-weight: 300;font-size:50px;}
.buttons { display:block;width:200px;align-content: right;margin:30px 40px;padding-bottom:100px; }
.buttonGame { float:left;background-color:#fff;color:#000;padding:6px 12px;text-transform:uppercase;font-size:12px;width:130px;text-align: center; margin-right:10px;margin-bottom:5px}
.buttonGame:hover {
	background-color:#000;color:#fff;
	text-decoration: none;
	-webkit-transition: all .3s ease-in;
    -moz-transition: all .3s ease-in;
    -o-transition: all .3s ease-in;
    -ms-transition: all .3s ease-in;
    transition: all .3s ease-in;
}

a {text-decoration: none;color:#000;}
a:hover {text-decoration: none;color:#fff;	
	-webkit-transition: all .3s ease-in;
    -moz-transition: all .3s ease-in;
    -o-transition: all .3s ease-in;
    -ms-transition: all .3s ease-in;
    transition: all .3s ease-in;
}

@-webkit-keyframes Gradient {
	0% {
		background-position: 0% 50%
	}
	50% {
		background-position: 100% 50%
	}
	100% {
		background-position: 0% 50%
	}
}

@-moz-keyframes Gradient {
	0% {
		background-position: 0% 50%
	}
	50% {
		background-position: 100% 50%
	}
	100% {
		background-position: 0% 50%
	}
}

@keyframes Gradient {
	0% {
		background-position: 0% 50%
	}
	50% {
		background-position: 100% 50%
	}
	100% {
		background-position: 0% 50%
	}
}



              
            
!

JS

              
                //if you try to copy and paste this code into a new pen and find it does not work, make sure you have included JS/Jquery under the Settings option//

var gridObject = $("#grid"),    
    size =25,
		cellSize = 10,
		data,
		interval = null,
		generation = 0;

function generateMatrix(size, data, cellSize) { 
	
	data = new Array(size);
	for(var i = 0; i < size; i++) {
		data[i] = new Array(size);
		for(var j = 0; j < size; j++) {
			data[i][j] = { alive: false, next: false, xy:[ i * cellSize, j * cellSize ] };
		}
	}

	return data;

}

function nextGeneration(data) {

	generation++;
	for(var i = 0; i < data.length; i++) {
		for(var j = 0; j < data.length; j++) {
			
			var neighbors = 0;

			// assuming finite 2d space. ottom left
			neighbors += data[i - 1] && data[i - 1][j] && data[i - 1][j].alive ? 1 : 0;//left
			neighbors += data[i - 1] && data[i - 1][j - 1] && data[i - 1][j - 1].alive ? 1 : 0;//b
			neighbors += data[i] && data[i][j - 1] && data[i][j - 1].alive ? 1 : 0;
			neighbors += data[i + 1] && data[i + 1][j - 1] && data[i + 1][j - 1].alive ? 1 : 0;
			neighbors += data[i + 1] && data[i + 1][j] && data[i + 1][j].alive ? 1 : 0;
			neighbors += data[i + 1] && data[i + 1][j + 1] && data[i + 1][j + 1].alive ? 1 : 0;
			neighbors += data[i] && data[i][j + 1] && data[i][j + 1].alive ? 1 : 0;
			neighbors += data[i - 1] && data[i - 1][j + 1] && data[i - 1][j + 1].alive ? 1 : 0;

			// if alive cell 
			if(data[i][j].alive) { 
				if(neighbors < 2) {
					data[i][j].next = false;
				} else if(neighbors == 2 || neighbors == 3) {
					data[i][j].next = true;
				} else {
					// overpopulated -> dies
					data[i][j].next = false;
				}
			} else {
					if(neighbors === 3) {
						data[i][j].next = true;
					}
			}

		}
	}

	for(var i = 0; i < data.length; i++) {
		for(var j = 0; j < data.length; j++) {
			data[i][j].alive = data[i][j].next;
		}
	}

	// update visualization
	var svg = d3.select("svg");

	var g = svg.selectAll("g")
    .data(data);

	var cell = g.selectAll("rect")
		.data(function(d, i) { return d; })
		.transition()
		.style("fill", function(d, i) { return d.alive ? "black" : "white"; });

	// update generation DOM element
	document.getElementById("status").innerHTML = generation;

}

function clearMatrix(data) { 

	for(var i = 0; i < data.length; i++) {
		for(var j = 0; j < data.length; j++) {
			data[i][j].alive = data[i][j].next = false;
		}
	}

	nextGeneration(data);
	generation = 0;
	document.getElementById("status").innerHTML = 0;

}

function clicked(d, i) {

  if (d3.event.defaultPrevented) return;

	if(d3.select(this).datum().alive) {
		d3.select(this)
			.transition()
		  .style("fill", "white");
	} else {
		d3.select(this)
			.transition()
		  .style("fill", "black");
	}

	d3.select(this).datum().alive = !d3.select(this).datum().alive;

}

// generate data matrix
data = generateMatrix(size, data, cellSize); 

var svg = d3.select("body")
  	.append("svg")
	.attr("width", size*cellSize)
	.attr("height", size*cellSize);

var g = svg.selectAll("g")
    .data(data)
  	.enter()
		.append("g")
		.attr("transform", function(d, i) { return "translate(0, " + i * cellSize + ")"; });

var cell = g.selectAll("rect")
		.data(function(d, i) { return d; })
		.enter()
		.append("rect")
		.attr("transform", function(d, i) { return "translate(" + i * cellSize + ", 0)"; })
    	.attr("x", 0)
		.attr("y", 0)
		.attr("width", cellSize)
		.attr("height", cellSize)
    	.style("fill", function(d, i) { return d.alive ? "black" : "white"; })
		.style("stroke", "#888")
		.style("stroke-width", .2)
   		.on("click", clicked);





document.getElementById("nextGen").addEventListener('click', function(e) { 
	e.preventDefault();	nextGeneration(data); });

document.getElementById("startSimulation").addEventListener('click', function(e) {
	interval = setInterval( function() { nextGeneration(data); }, 500); });

document.getElementById("stopSimulation").addEventListener('click', function(e) {
	clearInterval(interval); });

document.getElementById("clearMatrix").addEventListener('click', function(e) {
	clearMatrix(data); });
              
            
!
999px

Console