|
Game::Life - Plays Conway's Game of Life |
Game::Life - Plays Conway's Game of Life
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();
}
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).
newset_rulesnew above, namely for breeding and living rules
in that order.
get_breeding_rules, get_living_rulesplace_pointsplace_text_pointsplace_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_pointprocessget_gridget_text_grid
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.
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.
$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
Updated README for distribution
Revision 0.02 2001/07/04 02:23:13 mneylon
Added test cases
Added set_text_points, get_text_grid
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
This package was written by Michael K. Neylon
Copyright 2001 by Michael K. Neylon
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.
|
Game::Life - Plays Conway's Game of Life |