Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

minimal game of life

by mayaTheCat (Scribe)
on Sep 14, 2003 at 00:59 UTC ( [id://291324]=sourcecode: print w/replies, xml ) Need Help??
Category: Fun Stuff
Author/Contact Info mayaTheCat
Description: a minimalist implementation of the "Game of Life".

the reason why I have coded such a stuff is that
I like minimal code (although this code is not so minimal - I have problems with the function n())
and this is just a practice ...

in this code,

  • the grid is defined as a torus,
  • the script outputs to terminal,
  • thus, it prints 24 - the height of the grid.
  • therefore, it is better to define widths and heights that make the grid lay inside a terminal.
use warnings;
use strict;

# the width and height of the torus ...
my ($w, $h) = (10, 10);

# initializing the universe as empty ...
@_ = map { 0 } (1..$w*$h);

# defining some patterns ...
$_[16] =
$_[16+$w-1] =
$_[16+$w-1+$w] =
$_[16+$w-1+$w+1] =
$_[16+$w-1+$w+1+1] =
1;

# printing the current state of the universe (torus) ...
sub p {
    map { print "\n" } (1..24-$h);
    map { $, = $_[$_] ? '*' : '.'; ($_+1)%$w ? print $, : print "$,\n"
+ } (0..$#_)
}

# getting (the indexes of) the neighbours of a cell ...
sub n {
    ($w*$h+$.-$w)%($w*$h)        # N
    , ($.+$w)%($w*$h)        # S
    , ($.%$w+1)%$w+$.-($.%$w)    # E
    , ($.%$w+$w-1)%$w+$.-($.%$w)    # W
    , ((($w*$h+$.-$w)%($w*$h))%$w+$w-1)%$w+(($w*$h+$.-$w)%($w*$h))-(((
+$w*$h+$.-$w)%($w*$h))%$w)    # NW
    , ((($.+$w)%($w*$h))%$w+1)%$w+(($.+$w)%($w*$h))-((($.+$w)%($w*$h))
+%$w)                # SE
    , ((($.+$w)%($w*$h))%$w+$w-1)%$w+(($.+$w)%($w*$h))-((($.+$w)%($w*$
+h))%$w)            # SW
    , ((($w*$h+$.-$w)%($w*$h))%$w+1)%$w+(($w*$h+$.-$w)%($w*$h))-((($w*
+$h+$.-$w)%($w*$h))%$w)    # NE
}

# the rules of the universe ...
sub r {
    $,=0;
    map { $,+=$_[$_] } &n; 
    $_[$.] ? ($,==2||$,==3?1:0) : ($,==3?1:0)
}

my @w;

# one generation of the universe ...
sub l {
    for $. (0..$#_) {
        $w[$.] = &r
    }
    @_ = @w
}

# the life ...
while (1) {
    &p;
    &l;
    sleep 1
}
Replies are listed 'Best First'.
Re: minimal game of life
by jeffa (Bishop) on Sep 14, 2003 at 17:17 UTC

    (a very good website dedicated to John Conway's Game of Life)

    Excellent! I just wanted to pop and warn any control freaks like myself not to be tempted to change your code. For example, i always find the prepended ampersand when calling a sub to be ... ugly. You can imagine my surprise when i changed &p and &l to p() and l() respectively. ;)

    Also, this:

    @_ = map { 0 } (1..$w*$h);
    Can be written more efficiently as:
    @_ = (0) x ($w*$h);
    but that's just a minor nitpick. mayaTheCat++

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://291324]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2025-07-17 03:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.