Today we’re going to cover how to use the function I released yesterday to create your own “Game of Life”!

In case you missed the other posts in this series I have linked to them here for your convenience though you can also find them as well as my other posts over on my Topics & Posts page.

  1. Ancestor Simulations Conway’s Mysterious Game Of Life
  2. Ancestor Simulations Conway’s Mysterious Rules For Life
  3. Ancestor Simulations Building Conway’s Mysterious Game

 

So here is one way of using my UpdateWorld() function, review it and I’ll elaborate below:

Using UpdateWorld()

 

// World Setup ///////////////////////////////////
$exponent = 9;
$world_size = pow(2, $exponent) + 1; // 513
$world = array_fill(0, $world_size, array_fill(0, $world_size, 0));
$number_of_generations = 1000;

// Number of neighbors required for a cellular mitosis "cell birth"
$birth_rules = array(3);

// Number of neighbors required for cell to remain alive
$survival_rules = array(2, 3);

$save_images = true;

//// pick 1 ////////////////////////////////
// 1 = random
// 2 = glider wall
$generate_seed = 1;
////////////////////////////////////////////

if($generate_seed == 1){
    $max_number_of_random_cells_to_generate = ($world_size * $world_size) / 2;
    $number_of_cells = mt_rand($world_size, $max_number_of_random_cells_to_generate);
    for($i = 0; $i <= $number_of_cells; $i++){
        $r = mt_rand(0, $world_size);
        $c = mt_rand(0, $world_size);
        $world[$r][$c] = 1;
    }
}elseif($generate_seed == 2){
    for($i = 1; $i < ($world_size / 2); $i++ ){
        // Glider
        $world[4][2 + ($i * 5)] = 1;
        $world[4][3 + ($i * 5)] = 1;
        $world[4][4 + ($i * 5)] = 1;
        $world[3][4 + ($i * 5)] = 1;
        $world[2][3 + ($i * 5)] = 1;
    }
}


///// Output image of Seed /////////////////////////////////////////////////////////
$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++){
        if($world[$row][$col] == 1){
            imagesetpixel($image, $row, $col, $alive); /* Set Pixel */
        }
        else{
            imagesetpixel($image, $row, $col, $dead); /* Set Pixel */
        }
    }
}
imagepng($image, "images/0.png"); /* Output Image */
imagedestroy($image);/* Free memory */
/////////////////////////////////////////////////////////////////////////////////////

    

///// Proceed for all generations /////////////////////////////////////////////////////////    
for($generation = 1; $generation < $number_of_generations; $generation++){
    $world = UpdateWorld($world, $world_size, $generation);
}
echo PHP_EOL . "Game Complete!" . PHP_EOL;

What’s going on here?

Well I’ll tell you. 😛

First I size the world and then setup the world array and define how many generations we are going to run:


$exponent = 9;
$world_size = pow(2, $exponent) + 1; // 513
$world = array_fill(0, $world_size, array_fill(0, $world_size, 0));
$number_of_generations = 1000;

 

After that I decide what the rules should be, since this is Conway’s Game of life I use his rules though there are other games possible just by changing the rules:

// Number of neighbors required for a cellular mitosis "cell birth"
$birth_rules = array(3);

// Number of neighbors required for cell to remain alive
$survival_rules = array(2, 3);

 

Next, I decide if I want to output the images or not and if the seed will be random or if should a “glider wall” be used.

$save_images = true;

//// pick 1 ////////////////////////////////
// 1 = random
// 2 = glider wall
$generate_seed = 1;
////////////////////////////////////////////

if($generate_seed == 1){
    $max_number_of_random_cells_to_generate = ($world_size * $world_size) / 2;
    $number_of_cells = mt_rand($world_size, $max_number_of_random_cells_to_generate);
    for($i = 0; $i <= $number_of_cells; $i++){
        $r = mt_rand(0, $world_size);
        $c = mt_rand(0, $world_size);
        $world[$r][$c] = 1;
    }
}elseif($generate_seed == 2){
    for($i = 1; $i < ($world_size / 2); $i++ ){
        // Glider
        $world[4][2 + ($i * 5)] = 1;
        $world[4][3 + ($i * 5)] = 1;
        $world[4][4 + ($i * 5)] = 1;
        $world[3][4 + ($i * 5)] = 1;
        $world[2][3 + ($i * 5)] = 1;
    }
}

 

After that I save the seed as image 0.png:

///// Output image of Seed /////////////////////////////////////////////////////////
$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++){
        if($world[$row][$col] == 1){
            imagesetpixel($image, $row, $col, $alive); /* Set Pixel */
        }
        else{
            imagesetpixel($image, $row, $col, $dead); /* Set Pixel */
        }
    }
}
imagepng($image, "images/0.png"); /* Output Image */
imagedestroy($image);/* Free memory */
/////////////////////////////////////////////////////////////////////////////////////

All that’s left is to do is to compute the game generations and follow up with a message when the game is over.

///// Proceed for all generations /////////////////////////////////////////////////////////    
for($generation = 1; $generation < $number_of_generations; $generation++){
    $world = UpdateWorld($world, $world_size, $generation);
}
echo PHP_EOL . "Game Complete!" . PHP_EOL;

 

Life

If you head over to my GitHub you can get the code for this project.

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