| 1 | package Poker::Dealer; | ||||||||||
| 2 | 13 | our $VERSION = '0.12'; | |||||||||
| 3 | |||||||||||
| 4 | |||||||||||
| 5 | 13 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 13 | ▶ | use warnings FATAL => 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 13 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 13 | ▶ | use List::Util qw(shuffle); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 13 | ▶ | use Poker::Deck; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 13 | ▶ | use Storable qw(dclone); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | Poker::Dealer - Simple class to represent a poker dealer | ||||||||||
| 15 | |||||||||||
| 16 | =head1 VERSION | ||||||||||
| 17 | |||||||||||
| 18 | Version 0.12 | ||||||||||
| 19 | |||||||||||
| 20 | =cut | ||||||||||
| 21 | |||||||||||
| 22 | |||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||
| 24 | |||||||||||
| 25 | use Poker::Dealer; | ||||||||||
| 26 | |||||||||||
| 27 | my $dealer = Poker::Dealer->new; | ||||||||||
| 28 | my $dealer = Poker::Dealer->new( joker_count => 2 ); | ||||||||||
| 29 | |||||||||||
| 30 | $dealer->shuffle_deck; | ||||||||||
| 31 | my $cards = $dealer->deal_up(4); | ||||||||||
| 32 | my $cards = $dealer->deal_named(['As', 'Ah']); | ||||||||||
| 33 | my $joker = $dealer->deal_named(['Jo1']); | ||||||||||
| 34 | |||||||||||
| 35 | =cut | ||||||||||
| 36 | |||||||||||
| 37 | 13 | has 'id' => ( | |||||||||
| 38 | is => 'rw', | ||||||||||
| 39 | ); | ||||||||||
| 40 | |||||||||||
| 41 | has 'joker_count' => ( | ||||||||||
| 42 | is => 'ro', | ||||||||||
| 43 | 42 | ▶ | default => sub { 0 }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 13 | ); | |||||||||
| 45 | |||||||||||
| 46 | has 'master_deck' => ( | ||||||||||
| 47 | is => 'rw', | ||||||||||
| 48 | 45 | ▶ | isa => sub { die "Not a Poker::Deck!" unless $_[0]->isa('Poker::Deck') }, | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||
| 49 | 13 | builder => '_build_master_deck', | |||||||||
| 50 | ); | ||||||||||
| 51 | |||||||||||
| 52 | 45 | ▶ | sub _build_master_deck { | ||||||||
|
Subroutine
_build_master_deck: called CC=1 SCAR=0.0
| |||||||||||
| 53 | 45 | my $self = shift; | |||||||||
| 54 | 45 | return Poker::Deck->new( joker_count => $self->joker_count ); | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | has 'deck' => ( | ||||||||||
| 58 | is => 'rw', | ||||||||||
| 59 | 14807 | ▶ | isa => sub { die "Not a Poker::Deck!" unless $_[0]->isa('Poker::Deck') }, | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||
| 60 | 13 | lazy => 1, | |||||||||
| 61 | builder => '_build_deck', | ||||||||||
| 62 | ); | ||||||||||
| 63 | |||||||||||
| 64 | 95 | ▶ | sub _build_deck { | ||||||||
|
Subroutine
_build_deck: called CC=1 SCAR=0.0
| |||||||||||
| 65 | 95 | my $self = shift; | |||||||||
| 66 | 95 | return dclone $self->master_deck; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 14794 | ▶ | sub shuffle_cards { | ||||||||
|
Subroutine
shuffle_cards: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 70 | 14794 | my ( $self, $cards ) = @_; | |||||||||
| 71 | 14794 | $cards->cards->Reorder( shuffle $cards->cards->Keys ); | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | =head2 shuffle_deck | ||||||||||
| 75 | |||||||||||
| 76 | Creates a new deck and randomizes the cards. | ||||||||||
| 77 | =cut | ||||||||||
| 78 | |||||||||||
| 79 | 94 | ▶ | sub shuffle_deck { | ||||||||
|
Subroutine
shuffle_deck: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 80 | 94 | my $self = shift; | |||||||||
| 81 | 94 | $self->deck( $self->_build_deck ); | |||||||||
| 82 | 94 | $self->shuffle_cards( $self->deck ); | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 14350 | ▶ | sub deal { | ||||||||
|
Subroutine
deal: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||
| 86 | 14350 | my ($self, $count) = @_; | |||||||||
| 87 | 14350 | ▶ | $count = 1 if !defined $count; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 14350 | ▶ | $self->reshuffle if $count > $self->deck->cards->Length; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 14350 | my %cards = $self->deck->cards->Splice( 0, $count ); | |||||||||
| 90 | 14350 | return [ values %cards ]; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | =head2 reshuffle | ||||||||||
| 94 | |||||||||||
| 95 | Shuffles cards in the discard pile and adds them to the existing deck. | ||||||||||
| 96 | =cut | ||||||||||
| 97 | |||||||||||
| 98 | 0 | ▶ | sub reshuffle { | ||||||||
|
Subroutine
reshuffle: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 99 | 0 | my $self = shift; | |||||||||
| 100 | 0 | ▶ | while (my $card = shift @{ $self->deck->discards }) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 0 | ▶ | my $key = $card->rank eq 'Joker' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | ? 'Jo' . $card->suit | ||||||||||
| 103 | : $card->rank . $card->suit; | ||||||||||
| 104 | 0 | $self->deck->cards->Push( $key => $card ); | |||||||||
| 105 | } | ||||||||||
| 106 | 0 | $self->shuffle_cards( $self->deck ); | |||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | =head2 deal_down | ||||||||||
| 110 | |||||||||||
| 111 | =cut | ||||||||||
| 112 | |||||||||||
| 113 | 0 | ▶ | sub deal_down { | ||||||||
|
Subroutine
deal_down: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 114 | 0 | my ($self, $count) = @_; | |||||||||
| 115 | 0 | return [ map { $_->up_flag(0); $_ } @{ $self->deal($count) } ]; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | =head2 deal_up | ||||||||||
| 119 | |||||||||||
| 120 | =cut | ||||||||||
| 121 | |||||||||||
| 122 | 0 | ▶ | sub deal_up { | ||||||||
|
Subroutine
deal_up: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 123 | 0 | my ($self, $count) = @_; | |||||||||
| 124 | 0 | return [ map { $_->up_flag(1); $_ } @{ $self->deal($count) } ]; | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | =head2 deal_named | ||||||||||
| 128 | |||||||||||
| 129 | Fetch a specific set of cards from the deck. | ||||||||||
| 130 | |||||||||||
| 131 | =cut | ||||||||||
| 132 | |||||||||||
| 133 | 114 | ▶ | sub deal_named { | ||||||||
|
Subroutine
deal_named: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 134 | 114 | my ( $self, $cards ) = @_; | |||||||||
| 135 | 114 | my @hand; | |||||||||
| 136 | 114 | for my $card (@$cards) { | |||||||||
| 137 | 280 | ▶ | my $val = $self->deck->cards->FETCH($card) or die "No such card: $card"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | 280 | push @hand, $val; | |||||||||
| 139 | 280 | $self->deck->cards->Delete($card); | |||||||||
| 140 | } | ||||||||||
| 141 | 114 | return [@hand]; | |||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | =head1 AUTHOR | ||||||||||
| 145 | |||||||||||
| 146 | Nathaniel Graham, C<< <ngraham at cpan.org> >> | ||||||||||
| 147 | |||||||||||
| 148 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 149 | |||||||||||
| 150 | Copyright 2016 Nathaniel Graham. | ||||||||||
| 151 | |||||||||||
| 152 | =cut | ||||||||||
| 153 | |||||||||||
| 154 | 13 | 1; | |||||||||