Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
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 taking refuge in the Monastery: (7)
As of 2021-06-20 18:55 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (95 votes). Check out past polls.