Today is Wednesday, and I’m sitting here typing away at my keyboard and wondering if you guys are liking these posts. From my perspective, my readers operate much like a “black box” system.

As a tech professional turned internet “busker” I have tons of experience dealing with so called “black box systems” where you only have control over the inputs and are left to observe what affect your actions have on the output without knowing the inner-workings of the system.

As a Tech Professional you are sometimes called upon to enter a business without any prior knowledge of the facility and troubleshoot in a short period of time why the internet/security camera system/email server/(insert complex glowing box here) is not working. You have no idea if the backbone for the building is Fiber or merely Cat 6 riser. Do the cameras use the old Coaxial cabling or modern Ethernet?  Where are all the LAN closets and WIFI access points? Was the satellite dish on the roof a microwave data connection with another facility?

Oh… and nobody can answer these questions for you… now go solve the problem. 😛

You have to be able to formulate tests and try various experiments to tease out what is really going on all the while observing the effects of your actions.

While writing my posts I can decide to write about literally ANY topic that “takes my fancy” or that “sticks in my craw“, so I have control over my “input” but I am left to observe the “output” which is where you guys come in to the equation!

I have no idea what topics you actually want to read about but if I keep trying, something eventually “sticks” and a certain post will stand out among the rest and get more reads or likes than the others… this is a form of indirect feedback because I can observe that a post was read a bunch or liked but often times not WHY you liked it.

Active forms of feedback (like commenting when you like a post) can help me know what you like and want more of. 😉

Conway’s Game of Life is kind of like a blackbox system too in that you never know what “seeds” (initial world or grid configurations) will result in a rapid die off or continue chaotically indefinitely. Will the patterns stabilize and become predictable in the next several generations?

Without playing the game and observing the results, there is no way to know.

UpdateWorld()

Here is the function you will need to compute Conway’s game.

This function has three parameters , $world_array, $world_size and $generation.

$world_array – Accepts a 2D Array populated with 0’s for dead and 1’s for alive.

$world_size- Accepts an integer for the width and height of the array and that will be used to generate the images as well

$generation – Accepts an integer and is used for naming the image after the current generation.

There is no error checking to enforce these conditions as I wanted this to be a clean example and I assume that you will take heed of these requirements in your own implementations.


function UpdateWorld($world_array, $world_size, $generation){
	$world_array_prev = $world_array;
	
	/* Create Image */
	if($GLOBALS['save_images'] == true){
		// Image ///////////////////////////////////
		$image = imagecreatetruecolor($world_size, $world_size); /* New Image */
		$dead = imagecolorallocate($image, 0, 0, 0);  /* Allocate Black as the Dead color */
		$alive = imagecolorallocate($image, 0, 176, 80); /* Allocate Alive color */
		////////////////////////////////////////////
	}
	

	for($row = 0; $row < $world_size; $row++){
		for($col = 0; $col < $world_size ; $col++){
			
	         /* Get Neighborhood Values*/
			 /*
			 NW N NE
			 W [@] E
			 SW S SE 
			 */			 
			 
			// NW
			/* If this is the first row & col in world_array */
			/* Wrap around and set $NW to the last row & col value in array */
			/* Otherwise $NW = one row up and to the left */
			if($row == 0){$r = $world_size - 1;}
			else{$r = $row - 1;}
			if($col == 0){$c = $world_size - 1;}
			else{$c = $col - 1;}
			$NW = $world_array_prev[$r][$c];
					
			// N
			/* If this is the first row in world_array */
			/* Wrap around and set $N to row value in array */
			/* Otherwise $N = one row up  */
			if($row == 0){$r = $world_size - 1;}
			else{$r = $row - 1;}
			$N = $world_array_prev[$r][$col];
			 
			// NE
			if($row == 0){$r = $world_size - 1;}
			else{$r = $row - 1;}
			if($col == $world_size - 1){$c = 0;}
			else{$c = $col + 1;}
			$NE = $world_array_prev[$r][$c];			
			
			// W
			if($col == 0){$c = $world_size - 1;}
			else{$c = $col - 1;}
			$W = $world_array_prev[$row][$c];		
			
			// E
			if($col == $world_size - 1){$c = 0;}
			else{$c = $col + 1;}
			$E = $world_array_prev[$row][$c];	
			
			// SW
			if($row == $world_size - 1){$r = 0;}
			else{$r = $row + 1;}
			if($col == 0){$c = $world_size - 1;}
			else{$c = $col - 1;}
			$SW = $world_array_prev[$r][$c];
			
			// S
			if($row == $world_size - 1){$r = 0;}
			else{$r = $row + 1;}
			$S = $world_array_prev[$r][$col];
			
			// SE
			if($row == $world_size - 1){$r = 0;}
			else{$r = $row + 1;}
			if($col == $world_size - 1){$c = 0;}
			else{$c = $col + 1;}
			$SE = $world_array_prev[$r][$c];
			 
			 
			$neighbours = ($NW + $N + $NE + $W + $E + $SW + $S + $SE);
			$new_value = 0;
			
			// if alive apply survival rules
			if($world_array[$row][$col] == 1){
				foreach($GLOBALS['survival_rules'] as $rule){
					if($neighbours == $rule){
						$new_value = 1;
						break; // stop checking
					}
				}
			}else{ // if dead apply birth rules
				foreach($GLOBALS['birth_rules'] as $rule){
					if($neighbours == $rule){
						$new_value = 1;
						break; // stop checking
					}
				}
			}
			$world_array[$row][$col] = $new_value;
			
						
			/* Draw Image */
			if($GLOBALS['save_images'] == true){
				if($new_value == 1){
					imagesetpixel($image, $row, $col, $alive); /* Set Pixel */
					
				}else{
					imagesetpixel($image, $row, $col, $dead); /* Set Pixel */
				}
			}
		}
	}
	
	/* Save Image */
	if($GLOBALS['save_images'] == true){
		imagepng($image, "images/$generation.png"); /* Output Image */
		imagedestroy($image);/* Free memory */
	}
	return $world_array;
}
/////////////////////////////////////////////////////////////////////////////////////

 

Come back tomorrow and Ill show you how to use my function to unlock the “blackbox” called Conway’s Game of Life.

Please Fund, Subscribe, Like, Share & Comment:

This post was made possible by the generous contributions of my sponsors on Patreon.

 

 

I will see you all in my next post!

Much Love,

~Joy