File Coverage

blib/lib/Poker/Dealer.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package Poker::Dealer;
2 1     1   5 use strict;
  1         2  
  1         30  
3 1     1   5 use warnings FATAL => 'all';
  1         2  
  1         36  
4 1     1   5 use Moo;
  1         1  
  1         6  
5 1     1   263 use List::Util qw(shuffle);
  1         1  
  1         103  
6 1     1   491 use Poker::Deck;
  0            
  0            
7             use Storable qw(dclone);
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             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             my $self = shift;
64             return dclone $self->master_deck;
65             }
66              
67             sub shuffle_cards {
68             my ( $self, $cards ) = @_;
69             $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             my $self = shift;
81             $self->deck( $self->_build_deck );
82             $self->shuffle_cards( $self->deck );
83             }
84              
85             sub deal {
86             my ($self, $count) = @_;
87             $count = 1 if !defined $count;
88             $self->reshuffle if $count > $self->deck->cards->Length;
89             my %cards = $self->deck->cards->Splice( 0, $count );
90             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             my $self = shift;
100             while (my $card = shift @{ $self->deck->discards }) {
101             $self->deck->cards->Push( $card->rank . $card->suit => $card )
102             }
103             $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             my ($self, $count) = @_;
114             return [ map { $_->up_flag(0); $_ } @{ $self->deal($count) } ];
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             my ($self, $count) = @_;
124             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             sub deal_named {
134             my ( $self, $cards ) = @_;
135             my @hand;
136             for my $card (@$cards) {
137             my $val = $self->deck->cards->FETCH($card) or die "No such card: $card";
138             push @hand, $val;
139             $self->deck->cards->Delete($card);
140             }
141             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;