Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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?
    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 rifling through the Monastery: (14)
    As of 2015-07-07 22:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (93 votes), past polls