Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
#!/usr/bin/perl package Game::Life; ###################### # # Game::Life # v0.01 # Michael K. Neylon # mneylon-pm@masemware.com # May 28, 2001 # # Runs Conway's Game of Life # # Suggestions/Comments/Ideas are highly desired and can be # sent to the eamil address above. # # Change History: # # v0.01 - May 28, 2001 # - Initial Release # ###################### use strict; use Exporter; use Clone qw( clone ); use vars qw /$VERSION @ISA @EXPORT @EXPORT_OK/; @ISA = qw /Exporter/; @EXPORT = qw //; @EXPORT_OK = qw //; $VERSION = 0.01; my $default_size = 100; sub new { my $class = shift; my $self = {} ; # No args, set up a blank one $self->{ size } = shift || $default_size; $self->{ grid } = [ map { [ map { 0 } (1..$self->{ size } ) ] } (1..$self->{ size } ) ]; bless $self, $class; return $self; } sub toggle_point { my ( $self, $x, $y ) = @_; return ( $self->{ grid }->[$x]->[$y] = !$self->{ grid }->[$x]->[$y +] ); } sub set_point { my ( $self, $x, $y ) = @_; $self->{ grid }->[$x]->[$y] = 1; } sub unset_point { my ( $self, $x, $y ) = @_; $self->{ grid }->[$x]->[$y] = 0; } sub place_points { my ( $self, $x, $y, $array ) = @_; return if ( $x < 0 || $x >= $self->{ size } || $y < 0 || $y >= $self->{ size } ); my ($i, $j); my $array_x = @$array; my $array_y = @{$$array[0]}; for ( $i = 0 ; $i < $array_x && $i+$x < $self->{ size }; $i++ ) { for ( $j = 0 ; $j < $array_y && $j+$y < $self->{ size }; $j++ +) { $self->{ grid }->[ $x + $i ]->[ $y + $j ] = ($array->[ $i ]->[ $j ] > 0) ? 1 : 0; } } } sub get_grid { my ( $self ) = @_; return clone( $self->{ grid } ); } sub process { my $self = shift; my $times = shift || 1; for (1..$times) { my $new_grid = clone( $self->{ grid } ); for my $i ( 0..$self->{ size }-1 ) { for my $j ( 0..$self->{ size }-1 ) { $new_grid->[$i]->[$j] = $self->_determine_life_status( $i, $j ); } } $self->{ grid } = $new_grid; } } sub _determine_life_status { my ( $self, $x , $y ) = @_; my $n = 0; for my $i ( $x-1, $x, $x+1 ) { for my $j ( $y-1, $y, $y+1 ) { $n++ if ( $i >= 0 && $i < $self->{ size } && $j >= 0 && $j < $self->{ size } ) && ( $self->{ grid }->[ $i ]->[ $j ] ); } } return ( $self->{ grid }->[ $x ]->[ $y ] ) ? ( $n == 3 || $n == 4 ) : ( $n == 3 ); } =head1 NAME Game::Life - Plays Conway's Game of Life =head1 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(); } =head1 DESCRIPTION Conway's Game of Life is a basic example of finding 'living' patterns in rather basic rulesets. 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 ar +e exactly 3 living neighbors to it. With these simple rules, fascinatin +g 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 +. C<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. C<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. C<toggle_point>, C<set_point>, C<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. C<process> - If passed a number, runs the Life simulation that many times, else runs the simulation once. C<get_grid> - Returns a B<copy> of the Life grid as a reference to an array of arrays. =head1 HISTORY Revision 0.01 2001/05/28 Michael K. Neylon Initial revision =head1 AUTHOR This package was written by Michael K. Neylon =head1 COPYRIGHT Copyright 2001 by Michael K. Neylon =head1 LICENSE Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the "Softwar +e"), to deal in the Software without restriction, including without limitat +ion the rights to use, copy, modify, merge, publish, distribute, sublicens +e, 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 include +d in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRES +S OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILIT +Y, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHAL +L 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. =cut 1;

In reply to Game::Life by Masem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (12)
    As of 2014-08-20 13:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (114 votes), past polls