Seems like the
method display as written would actually print all the cells in one row --
method iterate doesn't have an explicit hook for "reached end of line". That's fine, since we have
$.max available within the object.
I think method display should actually look like this:
method display
{
iterate
{
print $.grid[$^x][$^y] ?? '+' :: '.';
print "\n" if ($^x == $.max);
# print each row on own line
}
print "\n"; # blank line follows grid
print "Turn $(++$.count), press enter to continue"
print " or ctl-c to quit";
<$*IN>;
.calculate();
} # end method display
A separate style comment: wouldn't it be a bit cleaner to remove the .calculate(); line from method display and change the loop at the top so that it reads:
loop { $life.display(); $life.calculate(); }
Then there's no nasty surprise when someone does:
# somewhere far, far, away
$life.display;
`shell-script;clear`; # weird stuff on screen tty
$life.display; # refresh terminal *only*
later. I recognize that's personal preference, but Side Effects Are Scary.
-
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 How to display code and escape characters
are good places to start.