| 1 | package Game::Cribbage::Player::Hand; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 14 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 14 | ▶ | use Object::Proto::Sugar -types; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 14 | ▶ | use List::Util qw/first/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 14 | ▶ | use Game::Cribbage::Score; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 14 | ▶ | use Game::Cribbage::Deck::Card; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 14 | ▶ | use Game::Cribbage::Error; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 14 | ▶ | use ntheory qw/forcomb vecsum/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 14 | ▶ | use Array::Diff; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | has [qw/id/] => ( | ||||||||||||||||||||||||||||||||||||||||||
| 15 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 16 | isa => Int | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 14 | ); | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | has [qw/player score crib_score starter/] => ( | ||||||||||||||||||||||||||||||||||||||||||
| 20 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 21 | isa => Object | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 14 | ); | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | has [qw/crib cards play_scored/] => ( | ||||||||||||||||||||||||||||||||||||||||||
| 26 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 27 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||||||||||||||||
| 28 | default => [], | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 14 | ); | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 16 | ▶ | sub get { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 16 | my ($self, $card_index) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 16 | ▶ | my $card = ref $card_index ? $self->match($card_index) : $self->cards->[$card_index]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 16 | ▶ | if (!$card) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | die 'NO CARD FOUND FOR CARD_INDEX ' . $card_index; | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 15 | return $card; | |||||||||||||||||||||||||||||||||||||||||
| 38 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 6 | ▶ | sub match { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
match: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 6 | my ($self, $card) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 6 | for (@{ $self->cards }) { | |||||||||||||||||||||||||||||||||||||||||
| 43 | 13 | my $found = $_->match($card); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 13 | ▶ | if ($found) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 4 | return $_; | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 2 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 588 | ▶ | sub add { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 588 | my ($self, $card) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 588 | push @{$self->cards}, $card; | |||||||||||||||||||||||||||||||||||||||||
| 54 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 2 | ▶ | sub add_by_index { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_by_index: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | my ($self, $index, $card) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | $self->cards->[$index] = $card; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 60 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | ▶ | sub discard_cards { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
discard_cards: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | my ($self, $cards, $crib) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 2 | my $count = scalar @{$self->cards}; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | ▶ | if ($count <= 4) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | die 'CANNOT DISCARD ANY MORE CARDS'; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | my %mapped = (); | |||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | for (@{$cards}) { | |||||||||||||||||||||||||||||||||||||||||
| 70 | 2 | $mapped{$_->suit}{$_->symbol} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | my @cribbed; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | $cards = $self->cards; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | ▶ | for (my $i = 0; $i < scalar @{$cards}; $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 6 | my $card = $cards->[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 6 | ▶ | if (exists $mapped{$card->suit} && exists $mapped{$card->suit}{$card->symbol}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $mapped{$card->suit} and exists $mapped{$card->suit}{$card->symbol}
MC/DC100%2/2
exists $mapped{$card->suit} and exists $mapped{$card->suit}{$card->symbol}
exists $mapped{$card->suit}
exists $mapped{$card->suit}{$card->symbol}
Truth table100%3/3
A and B
A= exists $mapped{$card->suit}B= exists $mapped{$card->suit}{$card->symbol}
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | push @{$crib->crib}, splice(@{$cards}, $i, 1); | |||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | push @cribbed, $card; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 2 | $i--; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 1 | $self->cards($cards); | |||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | return \@cribbed; | |||||||||||||||||||||||||||||||||||||||||
| 84 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 124 | ▶ | sub discard { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
discard: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 124 | my ($self, $card, $crib) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 124 | my $count = scalar @{$self->cards}; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 124 | ▶ | if ($count <= 4) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | die 'CANNOT DISCARD ANY MORE CARDS'; | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 123 | ▶ | $card = $self->cards->[$card] if (!ref $card); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 123 | my $str = $card->stringify; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 123 | ▶ | my $ind = first { $self->cards->[$_]->stringify eq $str } 0 .. $count - 1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 123 | splice @{$self->cards}, $ind, 1; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 123 | $crib->add_crib_card($card); | |||||||||||||||||||||||||||||||||||||||||
| 97 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 132 | ▶ | sub add_crib_card { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_crib_card: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 132 | my ($self, $card) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 132 | push @{$self->crib}, $card; | |||||||||||||||||||||||||||||||||||||||||
| 102 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 14 | ▶ | sub calculate_score { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
calculate_score: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 14 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 14 | ▶ | my $starter = $self->starter ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 14 | ▶ | my @cards = (@{$self->cards}, ($starter ? $self->starter : ())); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 14 | $self->score(Game::Cribbage::Score->new(with_starter => $starter, cards => \@cards)); | |||||||||||||||||||||||||||||||||||||||||
| 109 | 14 | ▶ | if ($self->crib && scalar @{$self->crib}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->crib and scalar @{$self->crib;}
MC/DC0%0/1
$self->crib and scalar @{$self->crib;}
$self->crib
Truth table50%1/2
A
A= $self->crib
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 4 | ▶ | @cards = (@{$self->crib}, ($starter ? $self->starter : ())); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 4 | $self->crib_score(Game::Cribbage::Score->new(with_starter => $starter, crib => 1, cards => \@cards)); | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 14 | ▶ | return $self->score->total_score + ($self->crib_score ? $self->crib_score->total_score : 0); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 3 | ▶ | sub card_exists { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
card_exists: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 3 | my ($self, $card) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 3 | for (@{ $self->cards }) { | |||||||||||||||||||||||||||||||||||||||||
| 120 | 3 | my $found = $_->match($card); | |||||||||||||||||||||||||||||||||||||||||
| 121 | 3 | ▶ | if ($found) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 2 | ▶ | if ($self->crib) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 2 | for (@{ $self->crib }) { | |||||||||||||||||||||||||||||||||||||||||
| 128 | 2 | my $found = $_->match($card); | |||||||||||||||||||||||||||||||||||||||||
| 129 | 2 | ▶ | if ($found) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 136 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 3 | ▶ | sub identify_worst_cards { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
identify_worst_cards: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 3 | ▶ | if (!(scalar @{$self->cards} == 6)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 1 | die 'cards do not exists or two have been moved to the crib already'; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 2 | my @index = 0 .. 5; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 2 | my @cards = @{$self->cards}; | |||||||||||||||||||||||||||||||||||||||||
| 147 | # -1, not 0: a hand where no four cards score at all must still pick | ||||||||||||||||||||||||||||||||||||||||||
| 148 | # four to keep, or the "worst" two come back as all six. | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 2 | my %best = ( | |||||||||||||||||||||||||||||||||||||||||
| 150 | score => -1, | ||||||||||||||||||||||||||||||||||||||||||
| 151 | cards => [] | ||||||||||||||||||||||||||||||||||||||||||
| 152 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 30 | ▶ | forcomb { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 30 | my @test = @cards[@_]; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 30 | my $score = Game::Cribbage::Score->new(with_starter => 0, cards => \@test); | |||||||||||||||||||||||||||||||||||||||||
| 156 | 30 | ▶ | if (($score->total_score + 0) > $best{score}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 3 | $best{score} = $score->total_score; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 3 | $best{cards} = [@_]; | |||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 2 | } @index, 4; | |||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 2 | my $diff = Array::Diff->diff( \@index, $best{cards} ); | |||||||||||||||||||||||||||||||||||||||||
| 163 | 2 | @index = @{ $diff->deleted }; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 2 | @cards = map { $self->get($_) } @index; | |||||||||||||||||||||||||||||||||||||||||
| 165 | 2 | return (\@cards, @index); | |||||||||||||||||||||||||||||||||||||||||
| 166 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 3 | ▶ | sub validate_crib_cards { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_crib_cards: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 3 | my ($self, $cards) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 3 | my $find_all = 1; | |||||||||||||||||||||||||||||||||||||||||
| 172 | 3 | for my $card (@{$cards}) { | |||||||||||||||||||||||||||||||||||||||||
| 173 | 5 | my $found = 0; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 5 | for (@{ $self->crib }) { | |||||||||||||||||||||||||||||||||||||||||
| 175 | 13 | ▶ | if ( $_->match($card) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 4 | $found = 1; | |||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | 5 | ▶ | if (! $found) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 1 | $find_all = 0; | |||||||||||||||||||||||||||||||||||||||||
| 181 | 1 | last; | |||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | 3 | ▶ | if (!$find_all) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 1 | $self->crib([]); | |||||||||||||||||||||||||||||||||||||||||
| 187 | 1 | for (@{$cards}) { | |||||||||||||||||||||||||||||||||||||||||
| 188 | 2 | push @{$self->crib}, Game::Cribbage::Deck::Card->new( | |||||||||||||||||||||||||||||||||||||||||
| 189 | suit => $_->suit, symbol => $_->symbol | ||||||||||||||||||||||||||||||||||||||||||
| 190 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | 3 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 195 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | 4 | ▶ | sub best_run_play { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
best_run_play: called CC=10 SCAR=24.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | 4 | my ($self, $play) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 4 | my ($best, $card); | |||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | 4 | my @available = grep { ! $_->used } @{$self->cards}; | |||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 4 | for (@available) { | |||||||||||||||||||||||||||||||||||||||||
| 205 | 12 | my $test = $play->test_card($self->player, $_); | |||||||||||||||||||||||||||||||||||||||||
| 206 | 12 | ▶ | if (! ref $test && (!$best || $best < $test)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not ref $test and !$best || $best < $test
not $best or $best < $test
MC/DC0%0/3
not ref $test and !$best || $best < $test
not ref $test
not $best
$best < $test
Truth table25%1/4
not(A) and not(B) or C
A= not ref $testB= not $bestC= $best < $test
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 12 | $best = $test; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 12 | $card = $_; | |||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | 4 | ▶ | if (! defined $best) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 1 | return Game::Cribbage::Error->new( | |||||||||||||||||||||||||||||||||||||||||
| 214 | message => 'No valid cards left to play for this run', | ||||||||||||||||||||||||||||||||||||||||||
| 215 | go => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 216 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 3 | ▶ | if ($best == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 3 | my $total = $play->total; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 3 | @available = grep { ($total + $_->value) <= 31 } @available; | |||||||||||||||||||||||||||||||||||||||||
| 222 | 3 | ▶ | @available = grep { $_->value != 5 } @available if scalar @available > 1 && grep { $_->value != 5 } @available; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
scalar @available > 1 and grep {$_->value != 5;} @available
MC/DC0%0/2
scalar @available > 1 and grep {$_->value != 5;} @available
scalar @available > 1
grep {$_->value != 5;} @available
Truth table33%1/3
A and B
A= scalar @available > 1B= grep {$_->value != 5;} @available
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 3 | ▶ | if (! scalar @available) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | return Game::Cribbage::Error->new( | |||||||||||||||||||||||||||||||||||||||||
| 225 | message => 'No valid cards left to play for this run', | ||||||||||||||||||||||||||||||||||||||||||
| 226 | go => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 227 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||
| 229 | 3 | $card = $available[int(rand(scalar @available - 1))]; | |||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 3 | return $card; | |||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | 14 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | __END__ | ||||||||||||||||||||||||||||||||||||||||||