Dice Game Perl 6

| 3 Comments | No TrackBacks

Following SF, I thought I could present an interesting solution to the dice game as in If you only had one programming language to choose –or– Let the FUD be with you.

SF did rewrite the same algorithm in Perl 6, but I thought I could give a more Perl 6 approach to the problem, leading to the following code:

sub dice($bet, $dice) {
  given $dice {
    when * <=  50 {          0 }
    when * <=  66 {       $bet }
    when * <=  75 { $bet * 1.5 }
    when * <=  99 {   $bet * 2 }
    when * == 100 {   $bet * 3 }
  }
}
sub MAIN($bet, $plays) {
  my $money = 0;
  $money += dice($bet, int(rand() * 100)+1) for ^$plays;
  say "{$bet * $plays}\$ became $money\$ after $plays plays:
     You get {$money / ($bet * $plays)}\$ for a dollar";
}

Let's go through the code step-by-step...

sub MAIN

This is a very handy thing that comes in in Perl 6 natively, if you declare a signature to a specially named subroutine MAIN, this signature will be used as GetOpt instructions, in the code above I asked for two positional arguments, which would mean two parameters:

perl6 dice.pl 40 100

But I could also ask for named parameters and it would require named command-line switches. Very handy.

for ^$plays

The prefix:<^> operator, when used with a number, generates a Range from 0 to that number - 1, so, it would be the same as 0..($plays - 1), but as the number of the play is not important here, it would have the same effect as 1..$plays... Very handy too.

"{$bet * $plays}"

Quotes in Perl 6 are clever, you can open a bracket and type in an expression that will be evaluated.

when * <= 50

This is the Whatever in action. It will generate a closure that will ask for one parameter, when knows about it and sends the "given" value to it. Very handy indeed.

No TrackBacks

TrackBack URL: http://daniel.ruoso.com/cgi-bin/mt/mt-tb.cgi/154

3 Comments

Re; "If you only had one programming language to choose" -- of course, one of the many nice things about Perl 6 will be that you aren't limited to just one language -- you can ultimately use any language you want.

Pm

Okay, in your hands the given/when construct is just plain beautiful. I also had no idea of the MAIN trick, that's great.

The downside of organizing the program that way is that the program is significantly slower (6 min, 51 secs just to run the 1.0 100000 case). I'm thinking it's because you've got a function call in the middle of the loop.

It might be related to the use of Whatever, refactorign the given when inside the MAIN function doesnt change much the performance, otoh, refactoring the when * with if $_ it cuts in half the time

Leave a comment

About this Entry

This page contains a single entry by Daniel Ruoso published on July 6, 2009 6:04 PM.

How do we get out of this mess? was the previous entry in this blog.

Implementing SOAP in Perl today is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.