App-Games-Keno
view release on metacpan or search on metacpan
lib/App/Games/Keno.pm view on Meta::CPAN
package App::Games::Keno;
use Moose;
use Types::Standard qw(Int ArrayRef HashRef Bool);
use Carp qw(croak carp);
use List::Compare qw (get_intersection);
use List::Util qw(any uniq);
use Scalar::Util qw(looks_like_number);
# ABSTRACT: Plays Keno
=head1 NAME
App::Games::Keno
=head1 SYNOPSIS
This package plays a game of Keno. The number range is 1 to 80 and the payout amounts are for 1 to 10 spots.
Sample code to play 1000 draws with 5 spots. The spots are chosen for you.
use App::Games::Keno;
my $first_game = App::Games::Keno->new( num_spots => 5, draws => 1000 );
$first_game->PlayKeno;
say "You won \$"
. $first_game->winnings . " on "
. $first_game->draws
. " draws.";
This is how you choose your own spots.
my $second_game = App::Games::Keno->new(
spots => [ 45, 33, 12, 20, 75 ],
draws => 1000
);
$second_game->PlayKeno;
say "You won \$"
. $second_game->winnings . " on "
. $second_game->draws
. " draws.";
Several verbosity levels exist, use theme like this:
my $third_game = App::Games::Keno->new(
spots => [ 45, 33, 12, 20, 75 ],
draws => 1000,
verbose => 1
);
Verbose level 0 is the default and prints nothing.
Verbose level 1 prints the amount won on winning draws.
Verbose level 2 prints evrything from verbose level 1 and the
numbers drawn on each draw and the numbers that matched.
=cut
sub BUILD {
my $self = shift;
croak "Didn't get the number of draws you want"
if ( !defined $self->draws );
croak "Spots or Number of spots (not both)"
if ( defined $self->spots && defined $self->num_spots );
croak "Need spots or number of spots"
if ( !defined $self->spots && !defined $self->num_spots );
if ( !defined $self->verbose ) { $self->verbose(0); }
if ( $self->verbose < 0 || $self->verbose > 2 ) {
warn "'" . $self->verbose . "' is not a valid verbose level, using '0'";
$self->verbose(0);
}
if ( defined $self->spots ) {
if ( any { !looks_like_number($_) } @{ $self->spots } ) {
croak "One of the spots you chose doesn't look like a number.";
}
if ( any { $_ < 1 || $_ > 80 } @{ $self->spots } ) {
croak "You chose a spot that is out of the 1 to 80 range";
}
if ( scalar @{ $self->spots } != uniq @{ $self->spots } ) {
croak "You appear to have chosen two or more of the same spots";
}
if ( scalar @{ $self->spots } < 1 ) {
croak "You must choose at least one spot";
}
if ( my $too_many_spots = scalar @{ $self->spots } > 10 ) {
croak "Too many spots. You must choose between 1 and 10 spots";
}
$self->num_spots( scalar @{ $self->spots } );
}
elsif ( $self->num_spots >= 1 && $self->num_spots <= 10 ) {
$self->spots( get_random_set( $self->num_spots ) );
}
else {
croak "You must ask for between 1 and 10 spots.";
}
if ( !defined $self->verbose ) {
$self->verbose(0);
}
return;
}
has 'draws' => (
is => 'rw',
isa => Int,
);
has 'winnings' => (
is => 'rw',
isa => Int,
default => 0
);
has 'num_won_draws' => (
is => 'rw',
isa => Int,
default => 0
);
has 'num_spots' => (
is => 'rw',
isa => Int,
);
has 'spots' => (
is => 'rw',
isa => ArrayRef,
);
has 'verbose' => (
is => 'rw',
isa => Int,
( run in 0.468 second using v1.01-cache-2.11-cpan-0b5f733616e )