File Coverage

blib/lib/Poker/Score/Bring/Wild.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package Poker::Score::Bring::Wild;
2 1     1   1748 use Moo;
  1         2  
  1         7  
3 1     1   643 use Algorithm::Combinatorics qw(combinations combinations_with_repetition);
  0            
  0            
4             use List::Util qw(max);
5              
6             =head1 NAME
7              
8             Poker::Score::Bring::Wild - Scoring system used in highball Stud with wildcards to decide which player starts the action.
9              
10             =head1 VERSION
11              
12             Version 0.01
13              
14             =cut
15              
16             our $VERSION = '0.01';
17              
18             =head1 SYNOPSIS
19              
20             See Poker::Score for code example.
21              
22             =cut
23              
24              
25             extends 'Poker::Score::Bring::High';
26              
27             sub score {
28             my ( $self, $hole ) = @_;
29             my $score = 0;
30             my ( @wild, @normal );
31             for my $card (@$hole) {
32             if ( $card->is_wild ) {
33             push @wild, $card;
34             }
35             else {
36             push @normal, $card;
37             }
38             }
39             my $wild_count = scalar @wild;
40             my $norm_count = scalar @normal;
41             my @wild_combos;
42            
43             if ( $norm_count == 0 ) {
44             my $flat_hand = '14' x $wild_count;
45             $score = $self->hand_score($flat_hand);
46             }
47              
48             elsif ( $wild_count == 3 ) {
49             my @ranks = sort { $a <=> $b }
50             map { $self->rank_val( $_->rank ) } @normal;
51             my $high_rank = sprintf( "%02d", pop @ranks );
52             my $flat_hand = join '', ($high_rank) x 4;
53             $score = $self->hand_score($flat_hand);
54             }
55              
56             else {
57             @wild_combos =
58             combinations_with_repetition( [ map { sprintf( "%02d", $_ ) } 2 .. 14 ],
59             $wild_count );
60             my $norm_iter = combinations( [@normal], $norm_count );
61             while ( my $norm_combo = $norm_iter->next ) {
62              
63             my @norm_ranks = map { $self->rank_val( $_->rank ) } @$norm_combo;
64             for my $wild_combo (@wild_combos) {
65             my $flat_combo =
66             join( '', sort { $b <=> $a } ( @$wild_combo, @norm_ranks ) );
67              
68             my $temp_score = $self->hand_score($flat_combo);
69              
70             if ( defined $temp_score && $temp_score >= $score ) {
71             $score = $temp_score;
72             }
73             }
74             }
75             }
76             return $score . '.05';
77             }
78              
79             sub _build_hands { # generates all possible bring hands
80             my $self = shift;
81              
82             # one card
83             my @hands = @{ $self->unpaired1 };
84              
85             # two cards
86             push( @hands, @{ $self->unpaired2 } );
87             push( @hands, @{ $self->one_pair2 } );
88              
89             # three cards
90             push( @hands, @{ $self->unpaired3 } );
91             push( @hands, @{ $self->one_pair3 } );
92             push( @hands, @{ $self->threes3 } );
93              
94             # four cards
95             push( @hands, @{ $self->unpaired4 } );
96             push( @hands, @{ $self->one_pair4 } );
97             push( @hands, @{ $self->two_pair4 } );
98             push( @hands, @{ $self->threes4 } );
99             push( @hands, @{ $self->fours4 } );
100              
101             $self->hands( [@hands] );
102             }
103              
104             ############
105             # one card #
106             ############
107              
108             # unpaired
109              
110             sub unpaired1 {
111             my @temp;
112             for my $card ( 2 .. 14 ) {
113             push @temp, join( '', sprintf( "%02d", $card ) );
114             }
115             return [@temp];
116             }
117              
118             #############
119             # two cards #
120             #############
121              
122             # unpaired
123              
124             sub unpaired2 {
125             my $self = shift;
126             my @scores;
127             my $iter = combinations( [ reverse( 2 .. 14 ) ], 2 );
128             while ( my $c = $iter->next ) {
129             push( @scores,
130             join( "", map { sprintf( "%02d", $_ ) } sort { $b <=> $a } @$c ) );
131             }
132             return [ sort { $a <=> $b } @scores ];
133             }
134              
135             # one pair
136             sub one_pair2 {
137             my @temp;
138             for my $card ( 2 .. 14 ) {
139             push @temp,
140             join( '',
141             map { sprintf( "%02d", $_ ) }
142             sort { $b <=> $a } ( ($card) x 2 ) );
143             }
144             return [@temp];
145             }
146              
147             ###############
148             # three cards #
149             ###############
150              
151             # unpaired
152              
153             sub unpaired3 {
154             my $self = shift;
155             my @scores;
156             my $iter = combinations( [ reverse( 2 .. 14 ) ], 3 );
157             while ( my $c = $iter->next ) {
158             push( @scores,
159             join( "", map { sprintf( "%02d", $_ ) } sort { $b <=> $a } @$c ) );
160             }
161             return [ sort { $a <=> $b } @scores ];
162             }
163              
164             # one pair
165             sub one_pair3 {
166             my @temp;
167             for my $card1 ( 2 .. 14 ) {
168             for my $card2 ( grep { $_ != $card1 } ( 2 .. 14 ) ) {
169             push @temp,
170             join( '',
171             map { sprintf( "%02d", $_ ) }
172             sort { $b <=> $a } ( ($card1) x 2, $card2 ) );
173             }
174             }
175             return [@temp];
176             }
177              
178             # three-of-a-kind
179             sub threes3 {
180             my @temp;
181             for my $card1 ( 2 .. 14 ) {
182             push @temp,
183             join(
184             '', map { sprintf( "%02d", $_ ) }
185             sort { $b <=> $a } ($card1) x 3
186             );
187             }
188             return [@temp];
189             }
190              
191             ##############
192             # four cards #
193             ##############
194              
195             # unpaired
196             sub unpaired4 {
197             my $self = shift;
198             my @scores;
199             my $iter = combinations( [ reverse( 2 .. 14 ) ], 4 );
200             while ( my $c = $iter->next ) {
201             push( @scores,
202             join( "", map { sprintf( "%02d", $_ ) } sort { $b <=> $a } @$c ) );
203             }
204             return [ sort { $a <=> $b } @scores ];
205             }
206              
207             # one pair
208             sub one_pair4 {
209             my @temp;
210             for my $card1 ( 2 .. 14 ) {
211             for my $card2 (
212             reverse combinations( [ reverse grep { $_ != $card1 } ( 2 .. 14 ) ], 2 ) )
213             {
214             push @temp,
215             join( '',
216             map { sprintf( "%02d", $_ ) }
217             sort { $b <=> $a } ( ($card1) x 2, @$card2 ) );
218             }
219             }
220             return [@temp];
221             }
222              
223             # two pair
224             sub two_pair4 {
225             my @temp;
226             for my $card1 ( reverse combinations( [ reverse( 2 .. 14 ) ], 2 ) ) {
227             push @temp,
228             join( '',
229             map { sprintf( "%02d", $_ ) }
230             sort { $b <=> $a } ( ( $card1->[0] ) x 2, ( $card1->[1] ) x 2 ) );
231             }
232             return [@temp];
233             }
234              
235             # three-of-a-kind
236             sub threes4 {
237             my @temp;
238             for my $card1 ( 2 .. 14 ) {
239             for my $card2 ( grep { $_ != $card1 } ( 2 .. 14 ) ) {
240             push @temp,
241             join( '',
242             map { sprintf( "%02d", $_ ) }
243             sort { $b <=> $a } ($card1) x 3, ($card2) );
244             }
245             }
246             return [@temp];
247             }
248              
249             # four-of-a-kind
250             sub fours4 {
251             my @temp;
252             for my $card ( 2 .. 14 ) {
253              
254             #for my $suit ( qw(c d h s) ) {
255             push @temp, join( '', map { sprintf( "%02d", $_ ) } ($card) x 4 );
256              
257             #($card) x 4 ) . $suit;
258             #}
259             }
260             return [@temp];
261             }
262              
263             =head1 AUTHOR
264              
265             Nathaniel Graham, C<< >>
266              
267             =head1 LICENSE AND COPYRIGHT
268              
269             Copyright 2016 Nathaniel Graham.
270              
271             This program is free software; you can redistribute it and/or modify it
272             under the terms of the the Artistic License (2.0). You may obtain a
273             copy of the full license at:
274              
275             L
276              
277             =cut
278              
279             1;