| 1 | package Poker::Eval; | ||||||||||||
| 2 | 12 | our $VERSION = '0.12'; | |||||||||||
| 3 | |||||||||||||
| 4 | 12 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 12 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 12 | ▶ | use Poker::Hand; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 12 | ▶ | use Poker::Dealer; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 12 | ▶ | use Algorithm::Combinatorics qw(combinations); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 12 | ▶ | use Storable qw(dclone); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | =head1 NAME | ||||||||||||
| 12 | |||||||||||||
| 13 | Poker::Eval - Deal, score, and evaluate poker hands | ||||||||||||
| 14 | |||||||||||||
| 15 | =head1 VERSION | ||||||||||||
| 16 | |||||||||||||
| 17 | 0.12 | ||||||||||||
| 18 | |||||||||||||
| 19 | =cut | ||||||||||||
| 20 | |||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||
| 22 | |||||||||||||
| 23 | use Poker::Game::Holdem; | ||||||||||||
| 24 | use feature qw(say); | ||||||||||||
| 25 | |||||||||||||
| 26 | # choose a game | ||||||||||||
| 27 | my $game = Poker::Game::Holdem->new( iterations => 1000 ); | ||||||||||||
| 28 | |||||||||||||
| 29 | # deal hole cards | ||||||||||||
| 30 | my $hero = $game->deal_hole(['As', 'Kd']); | ||||||||||||
| 31 | my $villain = $game->deal_hole(['7h', '7c']); | ||||||||||||
| 32 | # OR $game->deal_hole() for random cards; | ||||||||||||
| 33 | |||||||||||||
| 34 | # calculate equity for each player | ||||||||||||
| 35 | $game->equity([ $hero, $villain ]); | ||||||||||||
| 36 | |||||||||||||
| 37 | # hero has ~45 percent equity | ||||||||||||
| 38 | say $hero->ev; | ||||||||||||
| 39 | |||||||||||||
| 40 | # villain has ~55 percent equity | ||||||||||||
| 41 | say $villain->ev; | ||||||||||||
| 42 | |||||||||||||
| 43 | # here comes the flop | ||||||||||||
| 44 | $game->flop(['Ah', 'Kc', '2d']); | ||||||||||||
| 45 | # or $game->flop() for random cards; | ||||||||||||
| 46 | |||||||||||||
| 47 | # calculate equity again after the flop | ||||||||||||
| 48 | $game->equity([ $hero, $villain ]); | ||||||||||||
| 49 | |||||||||||||
| 50 | # hero now has ~93 percent equity after making two pair | ||||||||||||
| 51 | say $hero->ev; | ||||||||||||
| 52 | |||||||||||||
| 53 | # turn and river | ||||||||||||
| 54 | $game->turn('9s'); # deal specific card | ||||||||||||
| 55 | $game->river(); # deal random card | ||||||||||||
| 56 | |||||||||||||
| 57 | # see who won and why | ||||||||||||
| 58 | $game->evaluate($hero); | ||||||||||||
| 59 | say $game->board_string; # Community Cards | ||||||||||||
| 60 | say $hero->name; # Two Pair | ||||||||||||
| 61 | say $hero->score; # numerical strength | ||||||||||||
| 62 | say $hero->best_combo_flat; # show cards in human readable form | ||||||||||||
| 63 | |||||||||||||
| 64 | # reset board and shuffle deck for the next game | ||||||||||||
| 65 | $game->reset; | ||||||||||||
| 66 | |||||||||||||
| 67 | =head1 AVAILABLE COMPONENTS | ||||||||||||
| 68 | |||||||||||||
| 69 | =head2 Games (C<Poker::Game::*>) | ||||||||||||
| 70 | |||||||||||||
| 71 | Community Poker::Game::Holdem | ||||||||||||
| 72 | Poker::Game::HoldemJokersWild | ||||||||||||
| 73 | Poker::Game::Pineapple | ||||||||||||
| 74 | Poker::Game::CrazyPineapple | ||||||||||||
| 75 | Poker::Game::Omaha | ||||||||||||
| 76 | Poker::Game::OmahaHiLo | ||||||||||||
| 77 | Poker::Game::Omaha5 | ||||||||||||
| 78 | Poker::Game::Omaha5HiLo | ||||||||||||
| 79 | Poker::Game::Courcheval | ||||||||||||
| 80 | Poker::Game::CourchevalHiLo | ||||||||||||
| 81 | |||||||||||||
| 82 | Draw Poker::Game::FiveCardDraw | ||||||||||||
| 83 | Poker::Game::FiveCardDrawDeucesWild | ||||||||||||
| 84 | Poker::Game::FiveCardDrawJokersWild | ||||||||||||
| 85 | Poker::Game::Low27SingleDraw | ||||||||||||
| 86 | Poker::Game::Low27TripleDraw | ||||||||||||
| 87 | Poker::Game::LowA5SingleDraw | ||||||||||||
| 88 | Poker::Game::LowA5TripleDraw | ||||||||||||
| 89 | |||||||||||||
| 90 | Stud Poker::Game::SevenCardStud | ||||||||||||
| 91 | Poker::Game::SevenCardStudHiLo | ||||||||||||
| 92 | Poker::Game::Razz | ||||||||||||
| 93 | Poker::Game::Stud (base class) | ||||||||||||
| 94 | |||||||||||||
| 95 | Badugi Poker::Game::Badugi | ||||||||||||
| 96 | Poker::Game::Badacey | ||||||||||||
| 97 | Poker::Game::Badeucy | ||||||||||||
| 98 | |||||||||||||
| 99 | =head2 Evaluation engines (C<Poker::Eval::*>) | ||||||||||||
| 100 | |||||||||||||
| 101 | Poker::Eval::Community any hole + community (Hold'em-style) | ||||||||||||
| 102 | Poker::Eval::Omaha exactly 2 hole + 3 community | ||||||||||||
| 103 | Poker::Eval::Wild wild cards (jokers / deuces, etc.) | ||||||||||||
| 104 | Poker::Eval::Badugi Badugi selection | ||||||||||||
| 105 | Poker::Eval::Badugi27 Badugi with aces high / 2-7 flavor | ||||||||||||
| 106 | Poker::Eval::HighSuit high-suit style | ||||||||||||
| 107 | Poker::Eval::BlackMariah Black Mariah | ||||||||||||
| 108 | |||||||||||||
| 109 | =head2 Scoring systems (C<Poker::Score::*>) | ||||||||||||
| 110 | |||||||||||||
| 111 | Poker::Score::High standard highball | ||||||||||||
| 112 | Poker::Score::Low8 8-or-better low | ||||||||||||
| 113 | Poker::Score::Low27 deuce-to-seven low | ||||||||||||
| 114 | Poker::Score::LowA5 ace-to-five low | ||||||||||||
| 115 | Poker::Score::Badugi Badugi rank | ||||||||||||
| 116 | Poker::Score::Badugi27 Badugi 2-7 variant | ||||||||||||
| 117 | Poker::Score::HighSuit high suit | ||||||||||||
| 118 | |||||||||||||
| 119 | Poker::Score::Bring::* bring-in helpers (High, Low, Wild) | ||||||||||||
| 120 | |||||||||||||
| 121 | C<Poker::Game::*> subclasses wire a specific Eval + Scorer for you. | ||||||||||||
| 122 | Advanced use: pair an Eval engine with a Score object yourself. | ||||||||||||
| 123 | |||||||||||||
| 124 | =head1 SEE ALSO | ||||||||||||
| 125 | |||||||||||||
| 126 | Poker::Game, Poker::Score | ||||||||||||
| 127 | |||||||||||||
| 128 | =head1 ATTRIBUTES | ||||||||||||
| 129 | |||||||||||||
| 130 | =head2 community_cards | ||||||||||||
| 131 | |||||||||||||
| 132 | Array ref of Poker::Card objects representing community cards | ||||||||||||
| 133 | |||||||||||||
| 134 | =cut | ||||||||||||
| 135 | |||||||||||||
| 136 | has 'community_cards' => ( | ||||||||||||
| 137 | is => 'rw', | ||||||||||||
| 138 | 14890 | ▶ | isa => sub { die "Not an array ref!" unless ref( $_[0] ) eq 'ARRAY' }, | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||
| 139 | 12 | builder => '_build_community_cards', | |||||||||||
| 140 | ); | ||||||||||||
| 141 | |||||||||||||
| 142 | 43 | ▶ | sub _build_community_cards { | ||||||||||
|
Subroutine
_build_community_cards: called CC=1 SCAR=0.0
| |||||||||||||
| 143 | 43 | return []; | |||||||||||
| 144 | } | ||||||||||||
| 145 | |||||||||||||
| 146 | =head2 scorer | ||||||||||||
| 147 | |||||||||||||
| 148 | Required attribute that identifies the scoring system. Must be a Poker::Score | ||||||||||||
| 149 | object. | ||||||||||||
| 150 | |||||||||||||
| 151 | =cut | ||||||||||||
| 152 | |||||||||||||
| 153 | has 'scorer' => ( | ||||||||||||
| 154 | is => 'rw', | ||||||||||||
| 155 | 43 | ▶ | isa => sub { die "Not an Score object!" unless $_[0]->isa('Poker::Score') }, | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||
| 156 | 12 | ); | |||||||||||
| 157 | |||||||||||||
| 158 | =head2 dealer | ||||||||||||
| 159 | |||||||||||||
| 160 | Standard Poker::Dealer (52-card deck by default; pass joker_count for jokers). | ||||||||||||
| 161 | |||||||||||||
| 162 | =cut | ||||||||||||
| 163 | |||||||||||||
| 164 | has 'dealer' => ( | ||||||||||||
| 165 | is => 'rw', | ||||||||||||
| 166 | 43 | ▶ | isa => sub { die "Not a Poker::Dealer" unless $_[0]->isa('Poker::Dealer') }, | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||
| 167 | 12 | builder => '_build_dealer', | |||||||||||
| 168 | ); | ||||||||||||
| 169 | |||||||||||||
| 170 | =head2 simulations | ||||||||||||
| 171 | |||||||||||||
| 172 | Number of simulations for expected win rate (default 100). | ||||||||||||
| 173 | |||||||||||||
| 174 | =cut | ||||||||||||
| 175 | |||||||||||||
| 176 | has 'simulations' => ( | ||||||||||||
| 177 | is => 'rw', | ||||||||||||
| 178 | 43 | ▶ | default => sub { 100 }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 179 | 12 | ); | |||||||||||
| 180 | |||||||||||||
| 181 | has 'hole_remaining' => ( | ||||||||||||
| 182 | is => 'rw', | ||||||||||||
| 183 | 43 | ▶ | default => sub { 0 }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 184 | 12 | ); | |||||||||||
| 185 | |||||||||||||
| 186 | has 'community_remaining' => ( | ||||||||||||
| 187 | is => 'rw', | ||||||||||||
| 188 | 43 | ▶ | default => sub { 0 }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 189 | 12 | ); | |||||||||||
| 190 | |||||||||||||
| 191 | 41 | ▶ | sub _build_dealer { | ||||||||||
|
Subroutine
_build_dealer: called CC=1 SCAR=0.0
| |||||||||||||
| 192 | 41 | return Poker::Dealer->new; | |||||||||||
| 193 | } | ||||||||||||
| 194 | |||||||||||||
| 195 | =head1 METHODS | ||||||||||||
| 196 | |||||||||||||
| 197 | =head2 best_hand | ||||||||||||
| 198 | |||||||||||||
| 199 | Returns the best Poker::Hand for the given hole cards under this eval's rules. | ||||||||||||
| 200 | |||||||||||||
| 201 | =cut | ||||||||||||
| 202 | |||||||||||||
| 203 | 0 | ▶ | sub best_hand { } | ||||||||||
|
Subroutine
best_hand: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 204 | |||||||||||||
| 205 | 0 | ▶ | sub flatten { | ||||||||||
|
Subroutine
flatten: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 206 | 0 | my ( $self, $cards ) = @_; | |||||||||||
| 207 | 0 | return join( '', map { $_->rank . $_->suit } @{$cards} ); | |||||||||||
| 208 | } | ||||||||||||
| 209 | |||||||||||||
| 210 | 0 | ▶ | sub community_flat { | ||||||||||
|
Subroutine
community_flat: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 211 | 0 | my $self = shift; | |||||||||||
| 212 | 0 | return $self->flatten( $self->community_cards ); | |||||||||||
| 213 | } | ||||||||||||
| 214 | |||||||||||||
| 215 | 14350 | ▶ | sub deal { | ||||||||||
|
Subroutine
deal: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 216 | 14350 | my ( $self, $count ) = @_; | |||||||||||
| 217 | 14350 | return $self->dealer->deal($count); | |||||||||||
| 218 | } | ||||||||||||
| 219 | |||||||||||||
| 220 | 0 | ▶ | sub deal_named { | ||||||||||
|
Subroutine
deal_named: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 221 | 0 | my ( $self, $cards ) = @_; | |||||||||||
| 222 | 0 | return $self->dealer->deal_named($cards); | |||||||||||
| 223 | } | ||||||||||||
| 224 | |||||||||||||
| 225 | =head2 calc_ev | ||||||||||||
| 226 | |||||||||||||
| 227 | Monte-Carlo expected win rate for an array ref of hands. Prefer | ||||||||||||
| 228 | C<Poker::Game>'s C<equity> method for the named-game API. | ||||||||||||
| 229 | |||||||||||||
| 230 | Each simulation awards 1.0 pot unit total. If N hands tie for the best | ||||||||||||
| 231 | score, each receives 1/N. Equity is reported as a percentage of | ||||||||||||
| 232 | simulations (so the C<ev> values across hands sum to approximately 100). | ||||||||||||
| 233 | |||||||||||||
| 234 | The dealer's current deck (already missing dealt hole and board cards) | ||||||||||||
| 235 | is cloned once; each simulation re-clones and shuffles that residual | ||||||||||||
| 236 | pack, then deals C<community_remaining> / C<hole_remaining> from it. | ||||||||||||
| 237 | |||||||||||||
| 238 | =cut | ||||||||||||
| 239 | |||||||||||||
| 240 | 12 | ▶ | sub calc_ev { | ||||||||||
|
Subroutine
calc_ev: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||
| 241 | 12 | my ( $self, $hands ) = @_; | |||||||||||
| 242 | 12 | my $community_orig = dclone( $self->community_cards ); | |||||||||||
| 243 | |||||||||||||
| 244 | # Snapshot undealt cards (hole + board already removed via deal_*) | ||||||||||||
| 245 | 12 | my $residual = dclone( $self->dealer->deck ); | |||||||||||
| 246 | |||||||||||||
| 247 | 12 | for ( 1 .. $self->simulations ) { | |||||||||||
| 248 | 14700 | $self->dealer->deck( dclone($residual) ); | |||||||||||
| 249 | 14700 | $self->dealer->shuffle_cards( $self->dealer->deck ); | |||||||||||
| 250 | |||||||||||||
| 251 | 14700 | ▶ | if ( $self->community_remaining ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 252 | $self->community_cards( | ||||||||||||
| 253 | 14350 | [ @$community_orig, @{ $self->deal( $self->community_remaining ) } ] ); | |||||||||||
| 254 | } | ||||||||||||
| 255 | else { | ||||||||||||
| 256 | 350 | $self->community_cards( [@$community_orig] ); | |||||||||||
| 257 | } | ||||||||||||
| 258 | |||||||||||||
| 259 | 14700 | for my $hand (@$hands) { | |||||||||||
| 260 | 29550 | my $combo = [ @{ $hand->cards } ]; | |||||||||||
| 261 | 29550 | ▶ | if ( $self->hole_remaining ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 262 | 0 | push @$combo, @{ $self->deal( $self->hole_remaining ) }; | |||||||||||
| 263 | } | ||||||||||||
| 264 | 29550 | my $best_hand = $self->best_hand($combo); | |||||||||||
| 265 | 29550 | $hand->temp_score( $best_hand->score ); | |||||||||||
| 266 | } | ||||||||||||
| 267 | |||||||||||||
| 268 | 14700 | my $top_score = 0; | |||||||||||
| 269 | 14700 | for my $hand (@$hands) { | |||||||||||
| 270 | 29550 | ▶ | $top_score = $hand->temp_score if $hand->temp_score > $top_score; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 271 | } | ||||||||||||
| 272 | 14700 | my @winners = grep { $_->temp_score == $top_score } @$hands; | |||||||||||
| 273 | 14700 | my $share = 1 / scalar @winners; | |||||||||||
| 274 | 14700 | for my $hand (@winners) { | |||||||||||
| 275 | 15243 | $hand->wins( $hand->wins + $share ); | |||||||||||
| 276 | } | ||||||||||||
| 277 | } | ||||||||||||
| 278 | |||||||||||||
| 279 | 12 | $self->community_cards($community_orig); | |||||||||||
| 280 | |||||||||||||
| 281 | # Restore undealt pack | ||||||||||||
| 282 | 12 | $self->dealer->deck( dclone($residual) ); | |||||||||||
| 283 | |||||||||||||
| 284 | 12 | ▶ | my $sims = $self->simulations || 1; | ||||||||||
|
Condition50%1/2
$self->simulations || 1
MC/DC0%0/1
$self->simulations || 1
$self->simulations
Truth table50%1/2
A
A= $self->simulations
| |||||||||||||
| 285 | 12 | for my $hand (@$hands) { | |||||||||||
| 286 | 25 | $hand->ev( int( $hand->wins / $sims * 100 + 0.5 ) ); | |||||||||||
| 287 | } | ||||||||||||
| 288 | } | ||||||||||||
| 289 | |||||||||||||
| 290 | 53 | ▶ | sub BUILD { | ||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 291 | 53 | my $self = shift; | |||||||||||
| 292 | 53 | $self->dealer->shuffle_deck; | |||||||||||
| 293 | } | ||||||||||||
| 294 | |||||||||||||
| 295 | =head1 AUTHOR | ||||||||||||
| 296 | |||||||||||||
| 297 | Nathaniel Graham, C<< <ngraham at cpan.org> >> | ||||||||||||
| 298 | |||||||||||||
| 299 | =head1 LICENSE AND COPYRIGHT | ||||||||||||
| 300 | |||||||||||||
| 301 | Copyright 2016-2026 Nathaniel Graham. | ||||||||||||
| 302 | |||||||||||||
| 303 | =cut | ||||||||||||
| 304 | |||||||||||||
| 305 | 12 | 1; | |||||||||||