Michael K. Neylon > Game-Life-0.04 > Game::Life
Module Version: 0.04   Source   Latest Release: Game-Life-0.06

# NAME

Game::Life - Plays Conway's Game of Life

# SYNOPSIS

```        use Game::Life;
my \$game = new Game::Life( 20 );
my \$starting = [
[ 1, 1, 1 ],
[ 1, 0, 0 ],
[ 0, 1, 0 ]
];

\$game->place_points( 10, 10, \$starting );
for (1..20) {
my \$grid = \$game->get_grid();
foreach ( @\$grid ) {
print map { \$_ ? 'X' : '.' } @\$_;
print "\n";
}
print "\n\n";
\$game->process();
}```

# DESCRIPTION

Conway's Game of Life is a basic example of finding 'living' patterns in rather basic rulesets (see NOTES). The Game of Life takes place on a 2-D rectangular grid, with each grid point being either alive or dead. If a living grid point has 2 or 3 neighbors within the surrounding 8 points, the point will remain alive in the next generation; any fewer or more will kill it. A dead grid point will become alive if there are exactly 3 living neighbors to it. With these simple rules, fascinating structures such as gliders that move across the grid, glider guns that generate these gliders, XOR gates, and others have been found.

This module simply provides a way to simulate the Game of Life in Perl.

In terms of coordinate systems as used in `place_points`, `toggle_point` and other functions, the first coodinate is the vertical direction, 0 being the top of the board, and the second is the horizontal direaction, 0 being the left side of the board. Thus, toggling the point of (3,2) will switch the state of the point in the 4th row and 3rd column.

The edges of the board are currently set as "flat"; cells on the edge do not have any neighbors, and thus will 'fall' off the board. Future versions may allow for 'warp' edges (if a cell moves off the left side it reappears on the right side).

`new`

Creates a new Life game board; if passed a scalar, the game board will be a square of that size, otherwise, it will be a default 100x100 units. Two optional array references may be passed to set the breeding and living rules for the Life game, respectively. The arrays should be made of the values for the number of nearest neighbors that should trigger the associated event. By default, Conway's game of life uses [ 3 ] and [ 2,3 ] for these arrays, respectively, but you can play around with these to get other types of automata.

`set_rules`

Takes two array references and uses them to set the rules of the Life game as described in `new` above, namely for breeding and living rules in that order.

`get_breeding_rules`, `get_living_rules`

Returns arrays that are associated with the breeding or living rules above.

`place_points`

Takes two scalars (indicating the position on the grid) and a reference to an array of arrays; this array is placed into the Life grid at the specified position, overwriting any data already there. Within the array of arrays, any non-zero values will be considered as a living square.

`place_text_points`

Takes two scalars (indicated the position on the grid), a character, and an array of strings; as with `place_points`, this array will be placed into the grid at the specified position. The character indicates what cells are to be considered as alive; any other character in the string will be considered dead. Thus, the example given in the SYNOPSIS can be writen optionally as

```        my @starting = qw( XXX
X..
.X. );

\$game->place_text_points( 10, 10, 'X', @starting );```

Note that a implicit method of serialization can be used in conjunction with `get_text_grid`.

`toggle_point`, `set_point`, `unset_point`

Take two scalars that indiciate a specific grid position. These functions toggle, sets, or unsets the life status of the grid point passed, respectively.

`process`

If passed a number, runs the Life simulation that many times, else runs the simulation once.

`get_grid`

Returns a copy of the Life grid as a reference to an array of arrays.

`get_text_grid`

Returns an array of strings that represent the game board. Two optional parameters may be passed as symbols to represent the living and dead states on the board, in that order. If these are not supplied, they will be represented by 'X' and '.', respectively. It's very easy to use this via:

`        print "\$_\n" foreach ( \$game->get_text_grid( ) );`

to follow the progress of the Life simulation, and should be faster than rolling your own based on get_grid.

# NOTES

Conway here is not Damien Conway of Perl fame, but John Horton Conway of mathematics and computer science fame. The 'game' was original designed in the late 60s - early 70s, and became popular due to the interest that Martin Gardner (puzzle editor for Scientific American) had for it.

# HISTORY

```    \$Date: 2001/07/04 02:49:29 \$

\$Log: Life.pm,v \$
Revision 0.04  2001/07/04 02:49:29  mneylon

Fixed distribution problem

Revision 0.03  2001/07/04 02:27:55  mneylon

Revision 0.02  2001/07/04 02:23:13  mneylon

Added set_rules, get_breeding_rules, get_living_rules, and set default
values for these as Conway's rules
Modifications from code as posted on Perlmonks.org```

# AUTHOR

This package was written by Michael K. Neylon