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 }