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 }

In reply to minimal game of life by mayaTheCat

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.