| 1 | package Game::Gin::Meld; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 13 | ▶ | use Game::Gin::Card qw(rank_of suit_of); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 13 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 11 | 13 | our @EXPORT_OK = qw(melds_in is_meld is_set is_run MIN_MELD); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 13 | ▶ | use constant MIN_MELD => 3; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 31550 | ▶ | sub is_set { | ||||||||||||||||||||||||||
|
Subroutine
is_set: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 31550 | my ($cards) = @_; | |||||||||||||||||||||||||||
| 17 | 31550 | ▶ | return 0 unless @$cards >= MIN_MELD && @$cards <= 4; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@$cards >= 3 and @$cards <= 4
MC/DC100%2/2
@$cards >= 3 and @$cards <= 4
@$cards >= 3
@$cards <= 4
Truth table100%3/3
A and B
A= @$cards >= 3B= @$cards <= 4
| |||||||||||||||||||||||||||||
| 18 | 21172 | my $rank = rank_of($cards->[0]); | |||||||||||||||||||||||||||
| 19 | 21172 | my %suit; | |||||||||||||||||||||||||||
| 20 | 21172 | for my $c (@$cards) { | |||||||||||||||||||||||||||
| 21 | 68903 | ▶ | return 0 unless rank_of($c) == $rank; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 48283 | ▶ | return 0 if $suit{ suit_of($c) }++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | 552 | return 1; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 31005 | ▶ | sub is_run { | ||||||||||||||||||||||||||
|
Subroutine
is_run: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 31005 | my ($cards) = @_; | |||||||||||||||||||||||||||
| 29 | 31005 | ▶ | return 0 unless @$cards >= MIN_MELD; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 31004 | my $suit = suit_of($cards->[0]); | |||||||||||||||||||||||||||
| 31 | 31004 | my @rank; | |||||||||||||||||||||||||||
| 32 | 31004 | for my $c (@$cards) { | |||||||||||||||||||||||||||
| 33 | 99434 | ▶ | return 0 unless suit_of($c) eq $suit; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 71147 | push @rank, rank_of($c); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 2717 | @rank = sort { $a <=> $b } @rank; | |||||||||||||||||||||||||||
| 37 | 2717 | for my $i (1 .. $#rank) { | |||||||||||||||||||||||||||
| 38 | 5683 | ▶ | return 0 unless $rank[$i] == $rank[ $i - 1 ] + 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 833 | return 1; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 31546 | ▶ | sub is_meld { my ($cards) = @_; return is_set($cards) || is_run($cards) ? 1 : 0 } | ||||||||||||||||||||||||||
|
Subroutine
is_meld: called CC=3 SCAR=11.0
Pod
documented
Branch: 1 branch
Condition100%3/3
is_set($cards) or is_run($cards)
MC/DC100%2/2
is_set($cards) or is_run($cards)
is_set($cards)
is_run($cards)
Truth table100%3/3
A or B
A= is_set($cards)B= is_run($cards)
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 433707 | ▶ | sub melds_in { | ||||||||||||||||||||||||||
|
Subroutine
melds_in: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 433707 | my ($cards) = @_; | |||||||||||||||||||||||||||
| 47 | 433707 | my @out; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | my %by_rank; | ||||||||||||||||||||||||||||
| 50 | 433707 | push @{ $by_rank{ rank_of($_) } }, $_ for @$cards; | |||||||||||||||||||||||||||
| 51 | 433707 | for my $rank (sort { $a <=> $b } keys %by_rank) { | |||||||||||||||||||||||||||
| 52 | 3077294 | my @same = sort { $a <=> $b } @{ $by_rank{$rank} }; | |||||||||||||||||||||||||||
| 53 | 3077294 | ▶ | next unless @same >= MIN_MELD; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 230445 | push @out, [@same]; | |||||||||||||||||||||||||||
| 55 | 230445 | ▶ | if (@same == 4) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 25976 | for my $skip (0 .. 3) { | |||||||||||||||||||||||||||
| 57 | 103904 | push @out, [ map { $same[$_] } grep { $_ != $skip } 0 .. 3 ]; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 433707 | my %by_suit; | |||||||||||||||||||||||||||
| 63 | 433707 | push @{ $by_suit{ suit_of($_) } }, $_ for @$cards; | |||||||||||||||||||||||||||
| 64 | 433707 | for my $suit (sort keys %by_suit) { | |||||||||||||||||||||||||||
| 65 | 1666914 | my @same = sort { rank_of($a) <=> rank_of($b) } @{ $by_suit{$suit} }; | |||||||||||||||||||||||||||
| 66 | 1666914 | ▶ | next unless @same >= MIN_MELD; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 830186 | my $start = 0; | |||||||||||||||||||||||||||
| 69 | 830186 | for my $i (1 .. @same) { | |||||||||||||||||||||||||||
| 70 | 2990601 | ▶ | my $breaks = $i == @same | ||||||||||||||||||||||||||
|
Condition100%3/3
$i == @same || rank_of($same[$i]) != rank_of($same[$i - 1]) + 1
MC/DC100%2/2
$i == @same || rank_of($same[$i]) != rank_of($same[$i - 1]) + 1
$i == @same
rank_of($same[$i]) != rank_of($same[$i - 1]) + 1
Truth table100%3/3
A || B
A= $i == @sameB= rank_of($same[$i]) != rank_of($same[$i - 1]) + 1
| |||||||||||||||||||||||||||||
| 71 | || rank_of($same[$i]) != rank_of($same[ $i - 1 ]) + 1; | ||||||||||||||||||||||||||||
| 72 | 2990601 | ▶ | next unless $breaks; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 2174603 | my @stretch = @same[ $start .. $i - 1 ]; | |||||||||||||||||||||||||||
| 74 | 2174603 | for my $len (MIN_MELD .. scalar @stretch) { | |||||||||||||||||||||||||||
| 75 | 222326 | for my $at (0 .. @stretch - $len) { | |||||||||||||||||||||||||||
| 76 | 279148 | push @out, [ @stretch[ $at .. $at + $len - 1 ] ]; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 2174603 | $start = $i; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 433707 | return @out; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 13 | 1; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | __END__ | ||||||||||||||||||||||||||||