Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Having read an article about perl 6 I was inspired to write a program in it. Here is an implementation of Conways' (not that Conways') Game of Life (not that game of life or that one.) There is a game of life implementation in the Parrot distribution but this isn't based on that one.

Of course as perl 6 doesn't exist yet I haven't actually tested this. (which also means you can't downvote me for mistakes ha! ha!) but I've tried to write this as if it were a real life program one would write. My original version had comments but I've stripped them out because I thought monks might find it interesting to ty and work out what's going on for themselves. Personally I was impressed by how easily the new features "clicked." Finally I'd like to say I used prolegemona correctly in the title but I kant. Here's the code:

#!/usr/bin/perl use warnings; use strict; my $life = new life(20); while(1) { $life.display(); } class life { has Int $.count; has Int $.dimension; has Array of Int @.grid is dim ($.dimension, $.dimension); method CREATE(Int $dimension) { $.count = 0; $.dimension = $dimension; loop (my $x = 0; $x < $dimenion; $x++) { loop (my $y = 0; $y < $dimension; $y++) { @.grid[$x][$y] = 0; } } @.grid[$dimension / 2 - 1][$dimension / 2] = 1; @.grid[$dimension / 2 - 1][$dimension / 2 + 1] = 1; @.grid[$dimension / 2][$dimension / 2] = 1; @.grid[$dimension / 2][$dimension / 2 - 1] = 1; @.grid[$dimension / 2 + 1][$dimension / 2] = 1; } method calculate() is private { my @newgrid; loop (my $x = 0; $x < .dimension; $x++) { loop (my $y = 0; $y < .dimension; $y++) { my $live = 0; for ($x - 1, $y - 1, $x, $y - 1, $x + 1, $y - 1, $x - 1, $y, $ +x + 1, $y, $x - 1, $y + 1, $x, $y + 1, $x + 1, $y + 1) -> ($nx, $ny +) { next if 0 > $nx > .dimension || 0 > $ny > .dimension; $live++ if @.grid[$nx][$ny] == 1; } $newgrid[$x][$y] = given @.grid[$x][$y] { when 0 { 1 if $live == 3}; when 1 { 1 if 1 < $live < 4 }: } || 0; } } @.grid = @newgrid; } method display { loop (my $x = 0; $x < $.dimension; $x++) { loop (my $y = 0; $y < $.dimension; $y++) { print $.grid[$x][$y] ? '+' : '.'; } print "\n"; } print "Turn $(++$.count), press enter for next turn, ctl-c to quit +'; <STDIN>; .calculate(); } }

--
જલધર


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

Title:
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2021-06-20 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (95 votes). Check out past polls.

    Notices?