
$seed = (count($seed) > 16) & 0xFF, ($localProduct > 8) & 0xFF, $localProduct & 0xFF) $onColour = imagecolorallocate($img, $foreground, $foreground, $foreground) Imagefill($img, 0, 0, imagecolorallocate($img, $background, $background, $background)) $img = imagecreate($width * 2, $height * 2) $width = count($state) $height = count($state) Putting together a very quick function to create a static GIF file: private function outputStateToGif(array $state, $generation, $outputDir, $foreground, $background) The next step was to output something more useful than HTML tables - enter GD. al.) that could benefit, however until I could measure the algorithm's performance, this is a decent first attempt. There are obvious improvements that could be made as well as storage alterations ( quad trees et. As mentioned previously, this is the most basic implementation of the algorithm and has not been subject to any optimisation: each cell requires eight lookups and with no heuristic pruning larger grids will take exponentially longer to calculate. This is fine for checking a limited number of states or small grids but when those variables climb higher then browser rendering time and page payload size becomes an issue. I settled on a quick HTML table which had a caption for the current generation of the algorithm and also added co-ordinate references for easy look ups. Initially this was just a pre-formatted output of each state with a 0 representing an inactive cell and a 1 representing an active one this is fine for quickly checking the state but matching up against more detailed debug output (such as neighbours for a specific cell) proved tricky, especially when you take into account line-heights and other annoyances. Part of the development of this involved settling on a suitable debug output. $newH = ($newH = $height) ? ($newH - $height) : $newH $width = count($currentState) $height = count($currentState) įor($h = 0 $h = $width) ? ($newW - $width) : $newW After some wrangling with minor bugs (spelling errors, incorrect typing etc.) an unoptimised first version of the algorithm was complete: private function tick($currentState) Using only the Wikipedia page as reference, I threw together a very basic script that allowed me to enter in some settings (grid dimensions, seed and generation limit) and for it to spit out the states between the seed and the generation cut off. I wanted something that was deterministic and identifiableThe first step was getting the algorithm working, and as I had already decided to make it web-based, that meant a PHP implementation.
