Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
That's a great effort, especially without a compiler. Here are a few corrections and some more "idiomatic" solutions.

Comments in bold italics explain corrections on the previous line.
Comments in normal italics explain stylistic choices on the previous line.

my $life = new life: 20;
# Indirect object syntax requires colon after invocant
loop { $life.display() }
# Argumentless &loop is cleaner that &while for infinite loops
class life { has Int $.count; has Int $.max; has Array of Bit @.grid;
# Bit is potentially much smaller than Int for 1/0 values
method BUILD(Int $dim)
# BUILD is the object initializer.
# CREATE is something else entirely (see Apocalypse 12 ;-)
{ $.count = 0; $.max = $dimension-1; my Array of Bit @grid is dim($dim,$dim) is default(0);
# Setting default value to 0 reduces initialization costs
@grid[$dim / 2 - 1][$dim / 2 ] = 1; @grid[$dim / 2 - 1][$dim / 2 + 1] = 1; @grid[$dim / 2 ][$dim / 2 ] = 1; @grid[$dim / 2 ][$dim / 2 - 1] = 1; @grid[$dim / 2 + 1][$dim / 2 ] = 1; @.grid := @grid;
# Can't declare variably dimensioned array as attribute.
# Have to declare it when dimension known (i.e. in
# constructor) and then bind it to attribute
} sub iterate (&block)
# Utility sub to factor out repetitive 2D iterations
{ for 0..$.max -> $x
# Prefer &for over &loop when possible (e.g. here)
{ for 0..$.max -> $y { block($x,$y); } } } method calculate() is private { my @newgrid; iterate { my $live = sum(@.grid[$^x-1..$^x+1][$^y-1..$^y+1]); @newgrid[$^x][$^y] = $live==2 && @.grid[$^x][$^y] || $live==3; }
# Pass &iterate a block with placeholders for $x and $y
@.grid = @newgrid; } method display { iterate { print $.grid[$^x][$^y] ?? '+' :: '.'; print "\n" if $^x == $.max; } print "\n"; print "Turn $(++$.count), press enter to continue or ctl-c to quit +; <$*IN>;
# STDIN is now $*IN
.calculate(); } }
Update: Fixed display method (thanks jkahn)

In reply to Re: Prolegemona To A Future Scripting Language: Game Of Life In Perl 6 by TheDamian
in thread Prolegemona To A Future Scripting Language: Game Of Life In Perl 6 by jaldhar

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, 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
  • 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (3)
    As of 2020-09-25 01:48 GMT
    Find Nodes?
      Voting Booth?
      If at first I donít succeed, I Ö

      Results (136 votes). Check out past polls.