File Coverage

blib/lib/Poker/Dealer.pm
Criterion Covered Total %
statement 18 53 33.9
branch 0 6 0.0
condition n/a
subroutine 6 15 40.0
pod 5 7 71.4
total 29 81 35.8


line stmt bran cond sub pod time code
1             package Poker::Dealer;
2 1     1   3 use strict;
  1         1  
  1         47  
3 1     1   3 use warnings FATAL => 'all';
  1         1  
  1         27  
4 1     1   3 use Moo;
  1         0  
  1         4  
5 1     1   158 use List::Util qw(shuffle);
  1         1  
  1         64  
6 1     1   305 use Poker::Deck;
  1         1  
  1         25  
7 1     1   570 use Storable qw(dclone);
  1         2145  
  1         490  
8              
9             =head1 NAME
10              
11             Poker::Dealer - Simple class to represent a poker dealer
12              
13             =head1 VERSION
14              
15             Version 0.01
16              
17             =cut
18              
19             our $VERSION = '0.01';
20              
21              
22             =head1 SYNOPSIS
23              
24             use Poker::Dealer;
25              
26             my $dealer = Poker::Dealer->new;
27              
28             $dealer->shuffle_deck;
29              
30             # Returns an array_ref of face-up card objects
31             my $cards = $dealer->deal_up(4);
32              
33             # Returns an array_ref of face-down card objects
34             my $cards = $dealer->deal_down(5);
35              
36             # Deal yourself two aces:
37             my $cards = $dealer->deal_named(['As', 'Ah']);
38              
39             =cut
40              
41             has 'id' => (
42             is => 'rw',
43             );
44              
45             has 'master_deck' => (
46             is => 'rw',
47             isa => sub { die "Not a Poker::Deck!" unless $_[0]->isa('Poker::Deck') },
48             builder => '_build_master_deck',
49             );
50              
51             sub _build_master_deck {
52 0     0     return Poker::Deck->new;
53             }
54              
55             has 'deck' => (
56             is => 'rw',
57             isa => sub { die "Not a Poker::Deck!" unless $_[0]->isa('Poker::Deck') },
58             lazy => 1,
59             builder => '_build_deck',
60             );
61              
62             sub _build_deck {
63 0     0     my $self = shift;
64 0           return dclone $self->master_deck;
65             }
66              
67             sub shuffle_cards {
68 0     0 0   my ( $self, $cards ) = @_;
69 0           $cards->cards->Reorder( shuffle $cards->cards->Keys );
70             }
71              
72             =head1 SUBROUTINES/METHODS
73              
74             =head2 shuffle_deck
75              
76             Creates a new deck and randomizes the cards.
77             =cut
78              
79             sub shuffle_deck {
80 0     0 1   my $self = shift;
81 0           $self->deck( $self->_build_deck );
82 0           $self->shuffle_cards( $self->deck );
83             }
84              
85             sub deal {
86 0     0 0   my ($self, $count) = @_;
87 0 0         $count = 1 if !defined $count;
88 0 0         $self->reshuffle if $count > $self->deck->cards->Length;
89 0           my %cards = $self->deck->cards->Splice( 0, $count );
90 0           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             sub reshuffle {
99 0     0 1   my $self = shift;
100 0           while (my $card = shift @{ $self->deck->discards }) {
  0            
101 0           $self->deck->cards->Push( $card->rank . $card->suit => $card )
102             }
103 0           $self->shuffle_cards( $self->deck );
104             }
105              
106             =head2 deal_down
107              
108             Returns an array_ref of Poker::Card objects face down
109             =cut
110              
111              
112             sub deal_down {
113 0     0 1   my ($self, $count) = @_;
114 0           return [ map { $_->up_flag(0); $_ } @{ $self->deal($count) } ];
  0            
  0            
  0            
115             }
116              
117             =head2 deal_up
118              
119             Returns an array_ref of Poker::Card objects face up
120             =cut
121              
122             sub deal_up {
123 0     0 1   my ($self, $count) = @_;
124 0           return [ map { $_->up_flag(1); $_ } @{ $self->deal($count) } ];
  0            
  0            
  0            
125             }
126              
127             =head2 deal_named
128              
129             Fetch a specific set of cards from the deck.
130              
131             =cut
132              
133             sub deal_named {
134 0     0 1   my ( $self, $cards ) = @_;
135 0           my @hand;
136 0           for my $card (@$cards) {
137 0 0         my $val = $self->deck->cards->FETCH($card) or die "No such card: $card";
138 0           push @hand, $val;
139 0           $self->deck->cards->Delete($card);
140             }
141 0           return [@hand];
142             }
143              
144             =head1 AUTHOR
145              
146             Nathaniel Graham, C<< >>
147              
148             =head1 LICENSE AND COPYRIGHT
149              
150             Copyright 2016 Nathaniel Graham.
151              
152             This program is free software; you can redistribute it and/or modify it
153             under the terms of the the Artistic License (2.0). You may obtain a
154             copy of the full license at:
155              
156             L
157              
158             =cut
159              
160             1;