The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Game::Life::Infinite::Board - An infinite board for Conway's Game of Life.

SYNOPSIS

        use Game::Life::Infinite::Board;
        my $oscCheck = 2; 
        my $board = Game::Life::Infinite::Board->new();
        $board->loadInit($filename);
        $board->crudePrintBoard();
        for (1..10000) {
                $board->tick($oscCheck);
                my $stats = $board->statistics;
                if ($stats->{'factor2'} < 0.3) { $board->shrinkBoard; };
                if ($stats->{'liveCells'} == 0) {
                        print "--- ALL CELLS DIED! --- \n";
                        exit;
                };
                if ($stats->{'delta'} == 0) {
                        $board->crudePrintBoard();
                        print "--- STATIC! --- \n";
                        exit;
                };
                if ($stats->{'oscilator'} > 1) {
                        $board->crudePrintBoard();
                        print "--- OSCILATOR " . $stats->{'oscilator'} . " --- \n";
                        $board->tick($oscCheck);
                        $board->crudePrintBoard();
                        exit;
                };
        };

DESCRIPTION

This module implements the well known Conway's Game of Life in Perl. Points of interest:

  • Infinite grid (no "fell over" or "warp").

  • Oscilator detection

  • Rules as parameter

  • Simple load, save and print

METHODS

new

my $board = Game::Life::Infinite::Board->new($rules);

Initializes a new board. $rules is a reference to an array of arrays, containing the rules that will be used to calculate the next generation. Example:

my $rules = [[3,4,5], [1,2,7]];

First array sets the number of neighbours required for a live cell to survive. Second array sets the number of neighbours required for a new cell to be born. If not defined, the standard rules ([[3], [2,3]]) will be used.

setRules

$board->setRules($rules);

Change the rules on an existing board.

updateCell

$board->updateCell($x,$y,$state);

Set the state of the cell with coordinates $x,$y to $state, where $state can be 0 (dead) or 1 (alive).

loadInit

$board->loadInit($filename)

Loads a formation from a text file where live cells are marked with 'O' (upper case o). All other characters are interpreted as dead cells. The standard .cells files can be loaded this way, but name and description are ignored.

saveGridTxt

$board->saveGridTxt($filename)

Saves the current board formation as text, using 'O' for live cells and '.' for dead cells. The resulting file can be loaded using loadInit.

crudePrintBoard

$board->crudePrintBoard;

Prints the board with 'O' for live cells and '.' for dead cells, plus some information about the current state of the board.

tick

$board->tick($oscCheck);

Applies the rules once and transforms the board to the next generation. If $oscCheck is defined and is greater than one, then a history of the board $oscCheck generations long is kept and used to detect oscilating populations with period less or equal than $oscCheck. This detection process can be very CPU time and memory consuming for larger populations, so the whole process is disabled when $oscCheck is less than 2.

shrinkBoard

$board->shrinkBoard;

Shrinks the board by deleting cell entries from the internal grid data structure (which saves memory and speeds up processing) and adjusting minx, maxx, miny, maxy attributes, which speeds up oscilator detection and printing and keeps the file saved by saveGridTxt smaller. Shrinking is very fast and offers considerable speed gains in larger and older populations, so it can be called after each generation or depending on the 'factor2' attribute, which is the ratio of live cells to total (live plus dead) cells.

ACCESSORS

queryCell

my $result = $board->queryCell($x,$y);

Returns the state of the cell with coordinates $x,$y.

statistics

my $stats = $board->statistics;

Returns a reference to a hash containing statistics about the current grid. The attributes included are:

minx, maxx, miny, maxy

The boundaries of the grid. The grid "grows" with each generation that ads cells outside those boundaries. The grid shrinks only when 'shrinkBoard' is used.

gen

The number of generations of this board.

liveCells

The number of live cells on the grid.

usedCells

The total number of cells. Each cell is created the first time it's state is set to 1. When a cell dies, it is not deleted from the internal data structure. Dead cells are removed only when 'shrinkBoard' is used and then only if they don't have any live neighbours.

delta

The total state changes (cells died plus cells born) between the current and previous state.

factor2

The ratio of live cells to total (live plus dead) cells.

oscilator

When an oscilator is detected, this attribute is set to the period of the oscilator, otherwise is zero.

totalTime

The total time in seconds spent calculating generations for this board. Time::HiRes is used internaly.

lastTI

The time in seconds spent calculating the last generation. Time::HiRes is used internaly.

ATTRIBUTES

Some attributes of interest that you can access directly ($board->{'attribute_name'}):

currentFn

Used to store a filename.

name

Used to store a name for the formation.

liveRules

A reference to an array holding the numbers of neighbours that allow the survival of a cell.

breedRules

A reference to an array holding the numbers of neighbours that allow the birth of a new cell.

AUTHOR

This package was written by Theodore J. Soldatos.

COPYRIGHT

Copyright 2014 by Theodore J. Soldatos.

LICENSE

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.