Gin/Scoring.pm

filter: Statementstmt 99.1% 114 / 115 Branchbran 100% 30 / 30 Conditioncond 75.7% 25 / 33 MC/DCmcdc 52.9% 9 / 17 Subroutinesub 100% 16 / 16 Podpod 100% 3 / 3 totaltotal 92.0% 197 / 214 CC 35 SCAR 23.0
CC
35
Cov
95%
match_result
19.0
settle
8.1
best_layoff
6.0
CRAP 10.0
« Gin/Bot.pm ↑ Summary Gin/Deal.pm »
1 package Game::Gin::Scoring;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 ▶use Game::Gin::Card qw(deadwood_of);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Game::Gin::Meld qw(is_meld);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Game::Gin::Deadwood qw(best);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 our $VERSION = '0.03';
13 11 our @EXPORT_OK = qw(best_layoff settle match_result
14 GIN_BONUS UNDERCUT_BONUS BIG_GIN_BONUS
15 TARGET BOX_BONUS GAME_BONUS);
16  
17 11 ▶use constant GIN_BONUS => 25;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 11 ▶use constant UNDERCUT_BONUS => 25;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 11 ▶use constant BIG_GIN_BONUS => 31;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 11 ▶use constant TARGET => 100;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 11 ▶use constant BOX_BONUS => 25;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 11 ▶use constant GAME_BONUS => 100;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1944 ▶sub best_layoff {
Subroutine best_layoff: called CC=6 SCAR=18.0
Pod documented
26 1944 my ($unmatched, $melds) = @_;
27 1944 ▶ $unmatched ||= [];
Condition50%1/2
$unmatched ||= []
l!l
19440
MC/DC0%0/1
$unmatched ||= []
$unmatched
Truth table50%1/2
A
A= $unmatched
Aresult
11
00
28 1944 ▶ $melds ||= [];
Condition50%1/2
$melds ||= []
l!l
19440
MC/DC0%0/1
$melds ||= []
$melds
Truth table50%1/2
A
A= $melds
Aresult
11
00
29  
30 1944 my $total = 0;
31 1944 $total += deadwood_of($_) for @$unmatched;
32 1944 ▶ return { laid => [], melds => [ map { [@$_] } @$melds ], deadwood => $total }
Branch: 1 branch
BranchTrueFalseTotal
unless @$unmatched and @$melds 1 1943 1944
Condition66%2/3
@$unmatched and @$melds
!ll&&!rl&&r
011943
MC/DC50%1/2
@$unmatched and @$melds
@$unmatched @$melds
Truth table66%2/3
A and B
A= @$unmatchedB= @$melds
ABresult
0X0
100
111
33 unless @$unmatched && @$melds;
34  
35 1943 my (@best_laid, $best_value, @best_melds);
36 1943 $best_value = 0;
37 1943 @best_melds = map { [@$_] } @$melds;
38  
39 1943 my $walk;
40 17435 ▶ $walk = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
41 17435 my ($i, $piles, $laid, $value) = @_;
42 17435 ▶ if ($value > $best_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value > $best_value) 1171 16264 17435
43 1171 $best_value = $value;
44 1171 @best_laid = @$laid;
45 1171 @best_melds = map { [@$_] } @$piles;
46 }
47 17435 ▶ return if $i > $#$unmatched;
Branch: 1 branch
BranchTrueFalseTotal
if $i > $unmatched->$#* 3321 14114 17435
48  
49 14114 my $card = $unmatched->[$i];
50  
51 14114 $walk->($i + 1, $piles, $laid, $value);
52  
53 14114 for my $m (0 .. $#$piles) {
54 31545 my @grown = (@{ $piles->[$m] }, $card);
55 31545 ▶ next unless is_meld(\@grown);
Branch: 1 branch
BranchTrueFalseTotal
unless is_meld(\@grown) 30167 1378 31545
56 1378 my @next = map { [@$_] } @$piles;
57 1378 $next[$m] = \@grown;
58 1378 $walk->($i + 1, \@next, [ @$laid, $card ], $value + deadwood_of($card));
59 }
60 14114 return;
61 1943 };
62 1943 $walk->(0, [ map { [@$_] } @$melds ], [], 0);
63  
64 return {
65 1943 laid => \@best_laid,
66 melds => \@best_melds,
67 deadwood => $total - $best_value,
68 };
69 }
70  
71 2027 ▶sub settle {
Subroutine settle: called CC=8 SCAR=20.9
Pod documented
72 2027 my (%o) = @_;
73 2027 my ($knocker, $defender) = @o{qw(knocker defender)};
74 2027 ▶ my $k_dead = $o{knocker_deadwood} // 0;
Condition50%1/2
$o{"knocker_deadwood"} // 0
l!l
20270
MC/DC0%0/1
$o{"knocker_deadwood"} // 0
$o{"knocker_deadwood"}
Truth table50%1/2
A
A= $o{"knocker_deadwood"}
Aresult
11
00
75  
76 2027 ▶ my $d_best = best($o{defender_cards} || []);
Condition50%1/2
$o{'defender_cards'} || []
l!l
20270
MC/DC0%0/1
$o{'defender_cards'} || []
$o{'defender_cards'}
Truth table50%1/2
A
A= $o{'defender_cards'}
Aresult
11
00
77 2027 my $d_dead = $d_best->{deadwood};
78 2027 my @laid;
79  
80 2027 ▶ unless ($o{gin}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($o{"gin"}) 1940 87 2027
81 1940 ▶ my $lay = best_layoff($d_best->{unmatched}, $o{knocker_melds} || []);
Condition50%1/2
$o{'knocker_melds'} || []
l!l
19400
MC/DC0%0/1
$o{'knocker_melds'} || []
$o{'knocker_melds'}
Truth table50%1/2
A
A= $o{'knocker_melds'}
Aresult
11
00
82 1940 $d_dead = $lay->{deadwood};
83 1940 @laid = @{ $lay->{laid} };
84 }
85  
86 2027 ▶ if ($o{big_gin}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o{"big_gin"}) 7 2020 2027
87 7 return { winner => $knocker, loser => $defender, kind => 'big_gin',
88 points => BIG_GIN_BONUS + $d_dead,
89 knocker_deadwood => 0, defender_deadwood => $d_dead, laid_off => [] };
90 }
91 2020 ▶ if ($o{gin}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o{"gin"}) 80 1940 2020
92 80 return { winner => $knocker, loser => $defender, kind => 'gin',
93 points => GIN_BONUS + $d_dead,
94 knocker_deadwood => 0, defender_deadwood => $d_dead, laid_off => [] };
95 }
96  
97 1940 ▶ if ($d_dead <= $k_dead) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d_dead <= $k_dead) 136 1804 1940
98 136 return { winner => $defender, loser => $knocker, kind => 'undercut',
99 points => UNDERCUT_BONUS + ($k_dead - $d_dead),
100 knocker_deadwood => $k_dead, defender_deadwood => $d_dead,
101 laid_off => \@laid };
102 }
103  
104 1804 return { winner => $knocker, loser => $defender, kind => 'knock',
105 points => $d_dead - $k_dead,
106 knocker_deadwood => $k_dead, defender_deadwood => $d_dead,
107 laid_off => \@laid };
108 }
109  
110 204 ▶sub match_result {
Subroutine match_result: called CC=19 SCAR=29.5
Pod documented
111 204 my (%o) = @_;
112 204 ▶ my @hands = @{ $o{hands} || [] };
Condition50%1/2
$o{'hands'} || []
l!l
2040
MC/DC0%0/1
$o{'hands'} || []
$o{'hands'}
Truth table50%1/2
A
A= $o{'hands'}
Aresult
11
00
113 204 ▶ my $target = $o{target} // TARGET;
Condition100%2/2
$o{"target"} // 100
l!l
1986
MC/DC100%1/1
$o{"target"} // 100
$o{"target"}
Truth table100%2/2
A
A= $o{"target"}
Aresult
11
00
114  
115 204 my (%points, %won, $winner);
116 204 for my $h (@hands) {
117 2063 ▶ my $seat = $h->{winner} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $seat = $h->{"winner"} or next 2044 19 2063
118 2044 $points{$seat} += $h->{points};
119 2044 $won{$seat}++;
120 2044 ▶ $winner = $seat if !$winner && $points{$seat} >= $target;
Branch: 1 branch
BranchTrueFalseTotal
if not $winner and $points{$seat} >= $target 203 1841 2044
Condition66%2/3
not $winner and $points{$seat} >= $target
!ll&&!rl&&r
01841203
MC/DC50%1/2
not $winner and $points{$seat} >= $target
not $winner $points{$seat} >= $target
Truth table66%2/3
not(A) and B
A= not $winnerB= $points{$seat} >= $target
ABresult
0X0
100
111
121 }
122  
123 204 my @seats = sort keys %{ { map { $_ => 1 } (keys %points, keys %won, 'p1', 'p2') } };
124 204 ▶ my $loser = $winner ? ($winner eq 'p1' ? 'p2' : 'p1') : undef;
Branch: 2 branches
BranchTrueFalseTotal
$winner ? : 203 1 204
$winner eq 'p1' ? : 122 81 203
125  
126 204 ▶ my $shutout = ($winner && !($won{$loser} || 0)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$winner && !($won{$loser} || 0) ? : 7 197 204
Condition100%5/5
$winner and not $won{$loser} || 0
!ll&&!rl&&r
11967
$won{$loser} || 0
l!l
1967
MC/DC100%2/2
$winner and not $won{$loser} || 0
$winner $won{$loser}
Truth table100%3/3
A and not(B)
A= $winnerB= $won{$loser}
ABresult
0X0
110
101
127  
128 204 my %totals;
129 204 for my $seat (@seats) {
130 408 ▶ my $p = $points{$seat} || 0;
Condition100%2/2
$points{$seat} || 0
l!l
4017
MC/DC100%1/1
$points{$seat} || 0
$points{$seat}
Truth table100%2/2
A
A= $points{$seat}
Aresult
11
00
131 408 ▶ $p *= 2 if $shutout;
Branch: 1 branch
BranchTrueFalseTotal
if $shutout 14 394 408
132 408 ▶ $totals{$seat} = $p + BOX_BONUS * ($won{$seat} || 0);
Condition100%2/2
$won{$seat} || 0
l!l
4017
MC/DC100%1/1
$won{$seat} || 0
$won{$seat}
Truth table100%2/2
A
A= $won{$seat}
Aresult
11
00
133 }
134 204 ▶ $totals{$winner} += GAME_BONUS if $winner;
Branch: 1 branch
BranchTrueFalseTotal
if $winner 203 1 204
135  
136 return {
137 winner => $winner,
138 loser => $loser,
139 408 ▶ hand_points => { map { $_ => ($points{$_} || 0) } @seats },
Condition100%2/2
$points{$_} || 0
l!l
4017
MC/DC100%1/1
$points{$_} || 0
$points{$_}
Truth table100%2/2
A
A= $points{$_}
Aresult
11
00
140 204 ▶ hands_won => { map { $_ => ($won{$_} || 0) } @seats },
Condition100%2/2
$won{$_} || 0
l!l
4017
MC/DC100%1/1
$won{$_} || 0
$won{$_}
Truth table100%2/2
A
A= $won{$_}
Aresult
11
00
141 shutout => $shutout,
142 totals => \%totals,
143 target => $target,
144 };
145 }
146  
147 11 1;
148  
149 __END__
« Gin/Bot.pm ↑ Summary Gin/Deal.pm »