| 1 | package Game::Cribbage::Score; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use Object::Proto::Sugar -types; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use ntheory qw/forcomb vecsum/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | has total_score => ( | ||||||||||||||||||||||||||||||||
| 11 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 12 | isa => Int | ||||||||||||||||||||||||||||||||
| 13 | 15 | ); | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | has scored => ( | ||||||||||||||||||||||||||||||||
| 16 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 17 | builder => 1 | ||||||||||||||||||||||||||||||||
| 18 | 15 | ); | |||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 57 | ▶ | sub _build_scored { | ||||||||||||||||||||||||||||||
|
Subroutine
_build_scored: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 21 | return { | ||||||||||||||||||||||||||||||||
| 22 | 57 | fifteen => 2, | |||||||||||||||||||||||||||||||
| 23 | pair => 2, | ||||||||||||||||||||||||||||||||
| 24 | three_of_a_kind => 6, | ||||||||||||||||||||||||||||||||
| 25 | four_of_a_kind => 12, | ||||||||||||||||||||||||||||||||
| 26 | run => [3, 4, 5], | ||||||||||||||||||||||||||||||||
| 27 | four_flush => 4, | ||||||||||||||||||||||||||||||||
| 28 | five_flush => 5, | ||||||||||||||||||||||||||||||||
| 29 | nobs => 1 | ||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | has [qw/fifteen pair three_of_a_kind four_of_a_kind run four_flush five_flush nobs cards/] => ( | ||||||||||||||||||||||||||||||||
| 34 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 35 | isa => ArrayRef, | ||||||||||||||||||||||||||||||||
| 36 | default => [] | ||||||||||||||||||||||||||||||||
| 37 | 15 | ); | |||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | has with_starter => ( | ||||||||||||||||||||||||||||||||
| 40 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 41 | isa => Bool | ||||||||||||||||||||||||||||||||
| 42 | 15 | ); | |||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | has crib => ( | ||||||||||||||||||||||||||||||||
| 45 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 46 | isa => Bool | ||||||||||||||||||||||||||||||||
| 47 | 15 | ); | |||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 57 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 50 | 57 | my ($self, $params) = @_; | |||||||||||||||||||||||||||||||
| 51 | 57 | my $starter = $self->cards->[-1]; | |||||||||||||||||||||||||||||||
| 52 | 57 | my @cards = sort { $b->value <=> $a->value } @{$self->cards}; | |||||||||||||||||||||||||||||||
| 53 | 57 | $self->calculate_fifteen(@cards); | |||||||||||||||||||||||||||||||
| 54 | 57 | $self->calculate_of_a_kind(@cards); | |||||||||||||||||||||||||||||||
| 55 | 57 | $self->calculate_run(@cards); | |||||||||||||||||||||||||||||||
| 56 | 57 | $self->calculate_flush(@cards); | |||||||||||||||||||||||||||||||
| 57 | 57 | ▶ | $self->calculate_nob($starter, @cards) if $self->with_starter; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 58 | 57 | $self->calculate_total(); | |||||||||||||||||||||||||||||||
| 59 | 57 | return $self; | |||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 24 | ▶ | sub calculate_nob { | ||||||||||||||||||||||||||||||
|
Subroutine
calculate_nob: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 63 | 24 | my ($self, $starter, @cards) = @_; | |||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | 24 | ▶ | return if ($starter->symbol eq 'J'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | 23 | for (@cards) { | |||||||||||||||||||||||||||||||
| 68 | 91 | ▶ | if ($_->symbol eq 'J' && $starter->suit eq $_->suit) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$_->symbol eq "J" and $starter->suit eq $_->suit
MC/DC100%2/2
$_->symbol eq "J" and $starter->suit eq $_->suit
$_->symbol eq "J"
$starter->suit eq $_->suit
Truth table100%3/3
A and B
A= $_->symbol eq "J"B= $starter->suit eq $_->suit
| |||||||||||||||||||||||||||||||||
| 69 | 6 | push @{$self->nobs}, $_; | |||||||||||||||||||||||||||||||
| 70 | 6 | last; | |||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||
| 73 | }; | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 57 | ▶ | sub calculate_run { | ||||||||||||||||||||||||||||||
|
Subroutine
calculate_run: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 76 | 57 | my ($self, @cards) = @_; | |||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | # By run value, not pip value: J, Q and K are all worth ten, so the | ||||||||||||||||||||||||||||||||
| 79 | # caller's sort leaves them in whatever order they arrived, and a run | ||||||||||||||||||||||||||||||||
| 80 | # is only found when each card is one below the last. | ||||||||||||||||||||||||||||||||
| 81 | 57 | @cards = sort { $b->run_value <=> $a->run_value } @cards; | |||||||||||||||||||||||||||||||
| 82 | 57 | my @values = map { $_->run_value } @cards; | |||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | 57 | my %map; | |||||||||||||||||||||||||||||||
| 85 | 57 | foreach my $n (1 .. @values) { | |||||||||||||||||||||||||||||||
| 86 | 1239 | ▶ | forcomb { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||
| 87 | 1239 | my $first = $values[$_[0]]; | |||||||||||||||||||||||||||||||
| 88 | 1239 | my $match = 1; | |||||||||||||||||||||||||||||||
| 89 | 1239 | ▶ | return if scalar @_ < 3; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 90 | 549 | ▶ | for (my $i = 1; $i < scalar(@_); $i++) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 91 | 1299 | $first = $first - 1; | |||||||||||||||||||||||||||||||
| 92 | 1299 | ▶ | if ($first != $values[$_[$i]]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 93 | 1188 | $match = 0; | |||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | 549 | ▶ | if ($match) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 97 | 27 | ▶ | if (scalar(@_) == 3) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 98 | 19 | push @{$map{three}}, [@cards[@_]]; | |||||||||||||||||||||||||||||||
| 99 | } elsif (scalar(@_) == 4) { | ||||||||||||||||||||||||||||||||
| 100 | 7 | push @{$map{four}}, [@cards[@_]]; | |||||||||||||||||||||||||||||||
| 101 | } else { | ||||||||||||||||||||||||||||||||
| 102 | 1 | push @{$map{five}}, [@cards[@_]]; | |||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||
| 105 | 252 | } @values, $n; | |||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | 57 | ▶ | if ($map{five}) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 109 | 1 | $self->run($map{five}); | |||||||||||||||||||||||||||||||
| 110 | } elsif ($map{four}) { | ||||||||||||||||||||||||||||||||
| 111 | 5 | $self->run($map{four}); | |||||||||||||||||||||||||||||||
| 112 | } elsif ($map{three}) { | ||||||||||||||||||||||||||||||||
| 113 | 6 | $self->run($map{three}); | |||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | 57 | ▶ | sub calculate_flush { | ||||||||||||||||||||||||||||||
|
Subroutine
calculate_flush: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 118 | 57 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | # A flush is the four cards in the HAND sharing a suit; the starter can | ||||||||||||||||||||||||||||||||
| 121 | # only extend it to five. Four of a suit made up with the starter is | ||||||||||||||||||||||||||||||||
| 122 | # nothing, and a crib flush needs all five. | ||||||||||||||||||||||||||||||||
| 123 | 57 | my @all = @{$self->cards}; | |||||||||||||||||||||||||||||||
| 124 | 57 | ▶ | my $starter = $self->with_starter ? pop @all : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 125 | 57 | ▶ | return unless @all == 4; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 126 | 57 | my $suit = $all[0]->suit; | |||||||||||||||||||||||||||||||
| 127 | 57 | ▶ | return if grep { $_->suit ne $suit } @all; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | 7 | ▶ | if ($starter && $starter->suit eq $suit) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$starter and $starter->suit eq $suit
MC/DC100%2/2
$starter and $starter->suit eq $suit
$starter
$starter->suit eq $suit
Truth table100%3/3
A and B
A= $starterB= $starter->suit eq $suit
| |||||||||||||||||||||||||||||||||
| 130 | 3 | push @{$self->five_flush}, [@all, $starter]; | |||||||||||||||||||||||||||||||
| 131 | } elsif (!$self->crib) { | ||||||||||||||||||||||||||||||||
| 132 | 3 | push @{$self->four_flush}, [@all]; | |||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 57 | ▶ | sub calculate_fifteen { | ||||||||||||||||||||||||||||||
|
Subroutine
calculate_fifteen: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 137 | 57 | my ($self, @cards) = @_; | |||||||||||||||||||||||||||||||
| 138 | 57 | my @values = map { $_->value } @cards; | |||||||||||||||||||||||||||||||
| 139 | 57 | foreach my $n (1 .. @values) { | |||||||||||||||||||||||||||||||
| 140 | 1239 | ▶ | forcomb { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 141 | 1239 | ▶ | push @{$self->fifteen}, [@cards[@_]] if vecsum(@values[@_]) == 15; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 142 | 252 | } @values, $n | |||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | 57 | ▶ | sub calculate_of_a_kind { | ||||||||||||||||||||||||||||||
|
Subroutine
calculate_of_a_kind: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 147 | 57 | my ($self, @cards) = @_; | |||||||||||||||||||||||||||||||
| 148 | 57 | my %map = (); | |||||||||||||||||||||||||||||||
| 149 | 57 | push @{$map{$_->symbol}}, $_ for (@cards); | |||||||||||||||||||||||||||||||
| 150 | 57 | for (keys %map) { | |||||||||||||||||||||||||||||||
| 151 | 217 | my $c = scalar @{$map{$_}}; | |||||||||||||||||||||||||||||||
| 152 | 217 | ▶ | next if ($c == 1); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 153 | 24 | ▶ | if ($c == 2) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 154 | 16 | push @{$self->pair}, $map{$_}; | |||||||||||||||||||||||||||||||
| 155 | } elsif ($c == 3) { | ||||||||||||||||||||||||||||||||
| 156 | 5 | push @{$self->three_of_a_kind}, $map{$_}; | |||||||||||||||||||||||||||||||
| 157 | } elsif ($c == 4) { | ||||||||||||||||||||||||||||||||
| 158 | 3 | push @{$self->four_of_a_kind}, $map{$_}; | |||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | 57 | ▶ | sub calculate_total { | ||||||||||||||||||||||||||||||
|
Subroutine
calculate_total: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 164 | 57 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 165 | 57 | my $scored = $self->scored; | |||||||||||||||||||||||||||||||
| 166 | 57 | my $score = 0; | |||||||||||||||||||||||||||||||
| 167 | 57 | for (keys %{$scored}) { | |||||||||||||||||||||||||||||||
| 168 | 456 | ▶ | if (scalar @{$self->$_}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 169 | 81 | ▶ | if ($_ eq 'run') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 170 | 12 | ▶ | $score += scalar @{$self->$_} * scalar @{$self->$_->[0]} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 171 | if $self->$_->[0]; | ||||||||||||||||||||||||||||||||
| 172 | } else { | ||||||||||||||||||||||||||||||||
| 173 | 69 | $score += $scored->{$_} * scalar @{$self->$_}; | |||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||
| 177 | 57 | $self->total_score($score); | |||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | 15 | 1; | |||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | __END__ | ||||||||||||||||||||||||||||||||