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.

            
              
<div class="buttons">
	<h1>The Game Of Life</h1>
	<button  class="btn" id="stop">Stop</button>
	<button  class="btn" id="step">Step</button>
	<button  class="btn" id="resume">Resume</button>


	<p class="info">Generations: <span class="value" id="generations"></span></p>
	<p class="info">Births:<span class="value" id="births"></span></p>
	<p class="info">Living:<span class="value" id="living"></span></p>
	<p class="info">Deaths:<span class="value" id="deaths"></span></p>
</div>
<br />
<canvas id="c">

</canvas>


            
          
!
            
              body {
	background:#222;
	color:#fff;
	margin: 0;
	float:none;
	padding: 0;
	line-height:12px;
	letter-spacing:0.1em;
	height:100%;
	width:100%;
	font-family: 'Electrolize', sans-serif;

}

h1 {
	text-align:center;
	color:#fff;
	font-size:20px;
}

.buttons {
	text-align:left;
	top:0px;
	z-index:1000;
	position:absolute;	
	padding:20px 40px;
	background: rgba(80, 80, 80, 0.9);
	border-radius: 0px 0px 10px 0px;
}

.btn {
	background: #555;
	color:#fff;
	padding: 4px 16px;
	border: 1px solid #aaa;
}

.btn:hover {

	background: rgba(52, 73, 94, 0.89);
	border: 1px solid #ccc;
	color:#fff;

}

.info {

	width:100%;

}
.value {
	text-align:right;
	float: right;
}

.green {
	color: #0fa;
}

.almost  {
	color: #FFAF00;
}
canvas {
	margin:0;
	top:0px;
	position:absolute;	
	z-index:1;
	float:none;
	border:none;
	border-radius:0px;
  background:#fff;

}


            
          
!
            
              $( document ).ready( function() {

	// Force hack canvas object
	//
	var innerHeight = window.innerHeight;

	$("#c").attr("width", window.innerWidth);
	$("#c").attr("height", innerHeight);

	var generations = 0;

	// global (a)live count to determine of evolution has ended
	var alive = 0;
	var no_change = 0;
	// evo_gap : after how many generations has the evolution 
	// obviously stopped
	const evo_gap = 40;

	const cell_size = 6;
	var BOARD_SIZE = Math.floor( $("#c").width() / cell_size );
	var BOARD_HEIGHT = Math.floor( ($("#c").height()) / cell_size );

	var drawTimer;
	startTimer();

	function startTimer() {
		drawTimer =	setInterval( function() { cycle(); }, 1);
	}

 	//
	// controls
	//
	$("#stop").click( function(){ 
		
		clearInterval( drawTimer );

	});
	$("#step").click( function(){ cycle(); });
	$("#resume").click( function(){ startTimer(); });


	//
	// seed
	//
	var creation = new Array(BOARD_SIZE * BOARD_HEIGHT);
	var nextStep = new Array(BOARD_SIZE * BOARD_HEIGHT);


		for (var i = 0; i < creation.length; i++) {

			creation[i] = Math.round(Math.random());
		};


	function cycle() {

		var lifeCount = 0;
		
		// statistics
		var deaths = 0;
		var living = 0;
		var births = 0;

		for (var i = 0; i < creation.length; i++) {

			lifeCount = 0;
			//
			// count live and make world without
			// borders
			//
			if ( i < BOARD_SIZE) {

				var p = BOARD_SIZE * BOARD_HEIGHT - i;

				lifeCount = creation[p-1] ;
				lifeCount += creation[p-BOARD_SIZE] ;
				lifeCount += creation[  p- ( BOARD_SIZE  - 1 )] ;
				lifeCount += creation[  p- ( BOARD_SIZE  + 1 )] ;
				lifeCount += creation[i + 1] ;
				lifeCount += creation[  i + BOARD_SIZE ] ;
				lifeCount += creation[  i + ( BOARD_SIZE  - 1 )] ;
				lifeCount += creation[  i + ( BOARD_SIZE  + 1) ];
			
			} else if ( i > BOARD_SIZE * BOARD_HEIGHT - BOARD_SIZE ) {

				var p = i - BOARD_SIZE * BOARD_HEIGHT + BOARD_SIZE;

				lifeCount = creation[i-1] ; 
				lifeCount += creation[i-BOARD_SIZE] ;
				lifeCount += creation[  i- ( BOARD_SIZE  - 1 )] ;
				lifeCount += creation[  i- ( BOARD_SIZE  + 1 )] ;
				lifeCount += creation[i + 1] ;
				lifeCount += creation[  p + BOARD_SIZE ] ;
				lifeCount += creation[  p + ( BOARD_SIZE  - 1 )] ;
				lifeCount += creation[  p + ( BOARD_SIZE  + 1) ];
      } else {
				lifeCount = creation[i-1] ;
				lifeCount += creation[i-BOARD_SIZE] ;
				lifeCount += creation[  i- ( BOARD_SIZE  - 1 )] ;
				lifeCount += creation[  i- ( BOARD_SIZE  + 1 )] ;
				lifeCount += creation[i + 1] ;
				lifeCount += creation[  i + BOARD_SIZE ] ;
				lifeCount += creation[  i + ( BOARD_SIZE  - 1 )] ;
				lifeCount += creation[  i + ( BOARD_SIZE  + 1) ];

			}
				// 1 == true 
			if ( creation[i] ) {
				if ( lifeCount < 2 || lifeCount > 3) {
						nextStep[i] = 0;
						deaths += 1;
					}
				else if ( lifeCount == 2 || lifeCount == 3 ) {
						nextStep[i] = 1;
						living += 1;
					}
			} else {
				if ( lifeCount == 3 ) {
					nextStep[i] = 1;
					births += 1;
				}  else {
					// by default dead cells stay dead	
					// basically copy this to the next 
					// genration
					nextStep[i] = 0;
					}
				}
		}

		drawBoard(nextStep);

		// copy current state of array
		generations += 1;
		creation = nextStep.slice();

		$("#generations").text(generations);
		$("#births").text(births);
		$("#living").text(living);
		$("#deaths").text(deaths);

		if ( alive == living ) {
			
			no_change += 1;

			$("#living").addClass("almost");
			if ( no_change >= evo_gap ) {
			
				clearInterval( drawTimer );

				$("#generations").text(generations - evo_gap);
				$("#living").removeClass("almost");
				$("#living, #generations").addClass("green");

			}
			
		} else {
			// set global live count to current
			alive = living;
			// reset
			no_change = 0;		
			$("#living").removeClass("almost");
		}
	}


	function drawBoard(cells) {
		var canvas = document.getElementById("c");
		var context = canvas.getContext("2d");
		var x = 0;
		var y = 0;
		var p = 0; // piece count
		var c = 0;
	   	for	(var index = 0; index < cells.length; index++) {

			if (cells[index] == 1) {
				context.fillStyle = "#ffffff";
				context.fillRect(x, y, cell_size,cell_size);

				
			} else { 
				if ( y > BOARD_HEIGHT * cell_size - 400 ) {
					context.fillStyle = "rgb(0, 0, " + c + ")";
				} else {
					context.fillStyle = "rgb(0, 0, 0)";

				}

				context.fillRect(x, y, cell_size,cell_size); 
				}
			x += cell_size;
			p += 1; // next piece
			if (p == BOARD_SIZE) {
				// start next line
				y += cell_size;
				x = 0;
				p = 0;
				if ( c <= 255 && y > BOARD_HEIGHT * cell_size - 400  ) {
					c += 1;
				}
			} 
		}
	}

});

            
          
!
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