| 1 | package Poker::Score::High; | ||||||||||||||||||||
| 2 | 13 | our $VERSION = '0.12'; | |||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 13 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 13 | ▶ | use Algorithm::Combinatorics qw(combinations); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | Poker::Score::High - Identify and score specific highball poker hand. | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | Version 0.12 | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | See Poker::Score for code example. | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =cut | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 13 | extends 'Poker::Score'; | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 33 | ▶ | sub _build_hands { | ||||||||||||||||||
|
Subroutine
_build_hands: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | 33 | my $self = shift; | |||||||||||||||||||
| 29 | 33 | my %map = ( 1 => 'a High Card' ); | |||||||||||||||||||
| 30 | 33 | my @hands = @{ $self->unpaired }; | |||||||||||||||||||
| 31 | 33 | $map{ $#hands } = 'One Pair'; | |||||||||||||||||||
| 32 | 33 | push(@hands, @{ $self->one_pair }); | |||||||||||||||||||
| 33 | 33 | $map{ $#hands } = 'Two Pair'; | |||||||||||||||||||
| 34 | 33 | push(@hands, @{ $self->two_pair }); | |||||||||||||||||||
| 35 | 33 | $map{ $#hands } = 'Three-of-a-Kind'; | |||||||||||||||||||
| 36 | 33 | push(@hands, @{ $self->threes }); | |||||||||||||||||||
| 37 | 33 | $map{ $#hands } = 'a Straight'; | |||||||||||||||||||
| 38 | 33 | push(@hands, @{ $self->straights }); | |||||||||||||||||||
| 39 | 33 | $map{ $#hands } = 'a Flush'; | |||||||||||||||||||
| 40 | 33 | push(@hands, @{ $self->flushes }); | |||||||||||||||||||
| 41 | 33 | $map{ $#hands } = 'a Full House'; | |||||||||||||||||||
| 42 | 33 | push(@hands, @{ $self->houses }); | |||||||||||||||||||
| 43 | 33 | $map{ $#hands } = 'Four-of-a-Kind'; | |||||||||||||||||||
| 44 | 33 | push(@hands, @{ $self->fours }); | |||||||||||||||||||
| 45 | 33 | $map{ $#hands } = 'a Straight Flush'; | |||||||||||||||||||
| 46 | 33 | push(@hands, @{ $self->straight_flushes }); | |||||||||||||||||||
| 47 | 33 | $map{ $#hands } = 'Five-of-a-Kind'; | |||||||||||||||||||
| 48 | 33 | push(@hands, @{ $self->fives }); | |||||||||||||||||||
| 49 | 33 | $self->hands( [ @hands ] ); | |||||||||||||||||||
| 50 | 33 | $self->_hand_map( \%map ); | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | # straights | ||||||||||||||||||||
| 54 | has 'straights' => ( | ||||||||||||||||||||
| 55 | is => 'rw', | ||||||||||||||||||||
| 56 | 33 | ▶ | isa => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||
| 57 | 33 | ▶ | die "Not an array.\n" unless ref( $_[0] ) eq 'ARRAY'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | }, | ||||||||||||||||||||
| 59 | 13 | builder => '_build_straights' | |||||||||||||||||||
| 60 | ); | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 31 | ▶ | sub _build_straights { | ||||||||||||||||||
|
Subroutine
_build_straights: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 63 | return [ | ||||||||||||||||||||
| 64 | 31 | '1405040302', '0605040302', '0706050403', '0807060504', '0908070605', | |||||||||||||||||||
| 65 | '1009080706', '1110090807', '1211100908', '1312111009', '1413121110' | ||||||||||||||||||||
| 66 | ]; | ||||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | # unpaired non-straight combinations | ||||||||||||||||||||
| 70 | has 'unpaired' => ( | ||||||||||||||||||||
| 71 | is => 'rw', | ||||||||||||||||||||
| 72 | 33 | ▶ | isa => sub { die "Not an array.\n" unless ref( $_[0] ) eq 'ARRAY' }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||
| 73 | 13 | builder => '_build_unpaired', | |||||||||||||||||||
| 74 | ); | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 33 | ▶ | sub _build_unpaired { | ||||||||||||||||||
|
Subroutine
_build_unpaired: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 77 | 33 | my $self = shift; | |||||||||||||||||||
| 78 | 33 | my %straight = map { $_, 1 } @{ $self->straights }; | |||||||||||||||||||
| 79 | 33 | my $iter = combinations( [ reverse( 2 .. 14 ) ], 5 ); | |||||||||||||||||||
| 80 | 33 | my @unpaired; | |||||||||||||||||||
| 81 | 33 | ▶ | while ( my $c = $iter->next ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | push @unpaired, | ||||||||||||||||||||
| 83 | 42471 | join( '', map { sprintf( "%02d", $_ ) } sort { $b <=> $a } @$c ); | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | 33 | return [ grep { !exists $straight{$_} } sort { $a <=> $b } @unpaired ]; | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | # four-of-a-kind | ||||||||||||||||||||
| 89 | 33 | ▶ | sub fours { | ||||||||||||||||||
|
Subroutine
fours: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 90 | 33 | my @temp; | |||||||||||||||||||
| 91 | 33 | for my $card1 ( 2 .. 14 ) { | |||||||||||||||||||
| 92 | 429 | for my $card2 ( grep { $_ != $card1 } ( 2 .. 14 ) ) { | |||||||||||||||||||
| 93 | push @temp, | ||||||||||||||||||||
| 94 | join( '', | ||||||||||||||||||||
| 95 | 25740 | map { sprintf( "%02d", $_ ) } | |||||||||||||||||||
| 96 | 5148 | sort { $b <=> $a } ( ($card1) x 4, $card2 ) ); | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | 33 | return [@temp]; | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | # five-of-a-kind | ||||||||||||||||||||
| 103 | 33 | ▶ | sub fives { | ||||||||||||||||||
|
Subroutine
fives: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 104 | 33 | my @temp; | |||||||||||||||||||
| 105 | 33 | for my $card ( 2 .. 14 ) { | |||||||||||||||||||
| 106 | push @temp, | ||||||||||||||||||||
| 107 | join( '', | ||||||||||||||||||||
| 108 | 429 | map { sprintf( "%02d", $_ ) } | |||||||||||||||||||
| 109 | ($card) x 5); | ||||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | 33 | return [@temp]; | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | # full house | ||||||||||||||||||||
| 115 | 33 | ▶ | sub houses { | ||||||||||||||||||
|
Subroutine
houses: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 116 | 33 | my @temp; | |||||||||||||||||||
| 117 | 33 | for my $card1 ( 2 .. 14 ) { | |||||||||||||||||||
| 118 | 429 | for my $card2 ( grep { $_ != $card1 } ( 2 .. 14 ) ) { | |||||||||||||||||||
| 119 | push @temp, | ||||||||||||||||||||
| 120 | join( '', | ||||||||||||||||||||
| 121 | 25740 | map { sprintf( "%02d", $_ ) } | |||||||||||||||||||
| 122 | 5148 | sort { $b <=> $a } ( ($card1) x 3, ($card2) x 2 ) ); | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | 33 | return [@temp]; | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | # three-of-a-kind | ||||||||||||||||||||
| 129 | 33 | ▶ | sub threes { | ||||||||||||||||||
|
Subroutine
threes: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 130 | 33 | my @temp; | |||||||||||||||||||
| 131 | 33 | for my $card ( 2 .. 14 ) { | |||||||||||||||||||
| 132 | 429 | for my $c ( | |||||||||||||||||||
| 133 | 5577 | reverse combinations( [ reverse grep { $_ != $card } ( 2 .. 14 ) ], 2 ) ) | |||||||||||||||||||
| 134 | { | ||||||||||||||||||||
| 135 | push @temp, join( | ||||||||||||||||||||
| 136 | '', | ||||||||||||||||||||
| 137 | 141570 | map { sprintf( "%02d", $_ ) } | |||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 28314 | sort { $b <=> $a } ($card) x 3, @$c | |||||||||||||||||||
| 140 | ); | ||||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | 33 | return [@temp]; | |||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | # two pair | ||||||||||||||||||||
| 147 | 33 | ▶ | sub two_pair { | ||||||||||||||||||
|
Subroutine
two_pair: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 148 | 33 | my @temp; | |||||||||||||||||||
| 149 | 33 | for my $c ( reverse combinations( [ reverse( 2 .. 14 ) ], 2 ) ) { | |||||||||||||||||||
| 150 | 2574 | ▶ | for my $card ( grep { $_ != $c->[0] && $_ != $c->[1] } ( 2 .. 14 ) ) { | ||||||||||||||||||
|
Condition100%3/3
$_ != $c->[0] && $_ != $c->[1]
MC/DC100%2/2
$_ != $c->[0] && $_ != $c->[1]
$_ != $c->[0]
$_ != $c->[1]
Truth table100%3/3
A && B
A= $_ != $c->[0]B= $_ != $c->[1]
| |||||||||||||||||||||
| 151 | push @temp, | ||||||||||||||||||||
| 152 | join( '', | ||||||||||||||||||||
| 153 | 141570 | map { sprintf( "%02d", $_ ) } | |||||||||||||||||||
| 154 | 28314 | sort { $b <=> $a } ( ( $c->[0] ) x 2, ( $c->[1] ) x 2, $card ) ); | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | 33 | return [@temp]; | |||||||||||||||||||
| 158 | } | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | # one pair | ||||||||||||||||||||
| 161 | 33 | ▶ | sub one_pair { | ||||||||||||||||||
|
Subroutine
one_pair: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 162 | 33 | my @temp; | |||||||||||||||||||
| 163 | 33 | for my $card ( 2 .. 14 ) { | |||||||||||||||||||
| 164 | 429 | for my $c ( | |||||||||||||||||||
| 165 | 5577 | reverse combinations( [ reverse grep { $_ != $card } ( 2 .. 14 ) ], 3 ) ) | |||||||||||||||||||
| 166 | { | ||||||||||||||||||||
| 167 | push @temp, | ||||||||||||||||||||
| 168 | join( '', | ||||||||||||||||||||
| 169 | 471900 | map { sprintf( "%02d", $_ ) } | |||||||||||||||||||
| 170 | 94380 | sort { $b <=> $a } ( ($card) x 2, @$c ) ); | |||||||||||||||||||
| 171 | } | ||||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | 33 | return [@temp]; | |||||||||||||||||||
| 174 | } | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | # non-straight flushes | ||||||||||||||||||||
| 177 | 33 | ▶ | sub flushes { | ||||||||||||||||||
|
Subroutine
flushes: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 178 | 33 | my $self = shift; | |||||||||||||||||||
| 179 | 33 | return [ map { $_ . 's' } @{ $self->unpaired } ]; | |||||||||||||||||||
| 180 | } | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | # straight flushes | ||||||||||||||||||||
| 183 | 33 | ▶ | sub straight_flushes { | ||||||||||||||||||
|
Subroutine
straight_flushes: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 184 | 33 | my $self = shift; | |||||||||||||||||||
| 185 | 33 | return [ map { $_ . 's' } @{ $self->straights } ]; | |||||||||||||||||||
| 186 | } | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | =head1 AUTHOR | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | Nathaniel Graham, C<< <ngraham at cpan.org> >> | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | Copyright 2016 Nathaniel Graham. | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 197 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||||||||||||
| 198 | copy of the full license at: | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | =cut | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | 13 | 1; | |||||||||||||||||||