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->{'liveCells'} == 0) {
		print "--- ALL CELLS DIED! --- \n";
		exit;
	};
	if ($stats->{'delta'} == 0) {
		$board->shrinkBoard; 
		$board->crudePrintBoard();
		print "--- STATIC! --- \n";
		exit;
	};
	if ($stats->{'oscilator'} > 1) {
		$board->shrinkBoard;
		$board->crudePrintBoard();
		print "--- OSCILATOR " . $stats->{'oscilator'} . " --- \n";
		$board->tick($oscCheck);
		$board->shrinkBoard;
		$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

  • Colourised life support (Immigration, Quadlife)

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..4 (alive). Standard rules board (not colored) can only use 1 for alive cell. Immigration board can only use 1 or 2.

loadInit

$board->loadInit($filename)

Detects file format and loads a formation from a text file. Uses the function "&format_is" to detect the file format and if a known format is successfully detected, uses the corresponding method (see below) to load the file. For now (0.06) only .cells and .rle format are supported.

loadCells

$board->loadCells($file_array_ref)

Loads an initial grid from an array containing a file in cells (ASCII) format. Live cells are marked with 'O' (upper case o). If the character '2' is found, is used as a second color and board color is set to 'Immigration'. If characters '3' and/or '4' are found, they are used as a third and fourth color and board color is set to 'Quadlife'. All other characters are interpreted as dead cells. The standard .cells files can be loaded this way. Name (line starting with '!Name:') is stored in 'name' attribute. All other lines starting with '!' are stored as an array ref in 'description' attribute (see "ATTRIBUTES").

loadRLE

$board->loadRLE($file_array_ref)

Loads an initial grid from an array containing a file in rle (run length encoded) format. The standard tags (b, o, $) are supported. The 3 first unknown tags found are used as extra cell states and the board color is set to 'Immigration' (one extra state) or 'Quadlife' (two or three extra states). If more unknown tags are found, the file fails to load. Any rules string found in either header line or #r line is honored. #P or #R (upper left corner coordinates) lines are parsed and used. If not found, upper left corner is set to (0,0). Name (#N) is stored in the 'name' attribute (see "ATTRIBUTES"). For more information about rle format see http://www.conwaylife.com/wiki/Run_Length_Encoded.

loadL105

$board->loadL105($file_array_ref)

Loads an initial grid from an array containing a file in Life 1.05 format as described in http://www.conwaylife.com/wiki/Life_1.05. If found, a comment line (#D) containing the string "Name:" is used to fill the 'name' attribute. Color is not supported in this format.

loadL106

$board->loadL106($file_array_ref)

Loads an initial grid from an array containing a file in Life 1.06 format as described in http://www.conwaylife.com/wiki/Life_1.06. Color is not supported in this format.

saveGridTxt

$board->saveGridTxt($filename)

Saves the current board formation as text, using 'O' for live cells and '.' for dead cells. '2', '3' and '4' are used for extra colors. 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. If the board is an Immigration or Quadlife board, characters '2', '3' and '4' are used for the extra colors.

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. Since v. 0.04, dead cells are deleted anyway in each tick so there is no need to call shrinkBoard for speed reasons. shrinkBoard is now usefull when there is need for the boundaries to be adjusted.

setColor

$board->setColor($color);

Defines the use of color. $color can be one of 'Immigration', 'Quadlife' or 'Normal'. Color cannot be changed if the board is older that generation zero. Failure (unknown color or generation > 0) returns undef. Default color setting is 'Normal'. See http://www.conwaylife.com/wiki/Colourised_life for details. The rules of colourised life assume the standard rules for breeding and dying. Using a colourised board with non-standard rules is not yet supported. Loading a colourised formation will reset the board rules to standard. Trying to set non-standard rules on a colorised board will fail.

ACCESSORS

getColor

my $color = $board->getColor();

Returns the color of the board (see setColor above).

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.

FUNCTIONS

format_is

my $result = &format_is($file_array_ref);

my $format = $result->{'format'};

my $version = $result->{'version'}

Expects an array ref holding a file. Returns undef if the file is not recognized or a hash ref with the result. Currently detecting: Life 1.05 and 1.06, MCell, RLE, cells.

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.

description

Used to store a description 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.