Created
July 7, 2009 00:30
-
-
Save perigrin/141791 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use 5.10.0; | |
use MooseX::Declare; | |
use MooseX::Getopt; | |
class Dice::Game with MooseX::Getopt { | |
use Number::Format qw(format_price); | |
has plays => ( | |
isa => 'Int', | |
is => 'ro', | |
required => 1, | |
documentation => 'How many times to play', | |
); | |
has bet => ( | |
isa => 'Num', | |
is => 'ro', | |
required => 1, | |
documentation => 'How much to bet on each play', | |
); | |
has money => ( | |
isa => 'Num', | |
is => 'rw', | |
traits => ['NoGetopt'], | |
lazy => 1, | |
builder => 'start_money', | |
documentation => 'How much we currently have in play', | |
); | |
method start_money { $self->plays * $self->bet } | |
# these could probably be done with MooseX::AttributeHelpers and currying | |
method win (Num $amount) { $self->money( $self->money + $amount ) } | |
method lose (Num $amount) { $self->money( $self->money - $amount ) } | |
method dice( Int $roll) { | |
given ($roll) | |
{ | |
when ( $_ <= 50 ) { $self->lose( $self->bet ) } | |
when ( $_ <= 65 ) { } | |
when ( $_ <= 75 ) { $self->win( 0.5 * $self->bet ) } | |
when ( $_ <= 99 ) { $self->win( $self->bet ) } | |
when ( $_ == 100 ) { $self->win( 2 * $self->bet ) } | |
} | |
} | |
method run { | |
$self->dice( int( rand(100) ) + 1 ) for 1 .. $self->plays; | |
say "${\format_price($self->start_money)} became " | |
. "${\format_price($self->money)} after ${\$self->plays} plays: " | |
. "You get ${ \format_price($self->money / $self->start_money )} " | |
. "for a dollar"; | |
} | |
} | |
Dice::Game->new_with_options->run; | |
1; | |
__END__ | |
Based on the versions by [Ashkey Pond V](http://sedition.com/a/2774), | |
[SF](http://lastofthecarelessmen.blogspot.com/2009/07/dice-game-perl-6.html), | |
and [Daniel Ruoso](http://daniel.ruoso.com/categoria/perl/dice-game-perl-6). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment