blib/lib/Game/Mahjong/Result.pm

filter: Statementstmt 100% 64 / 64 Branchbran 100% 24 / 24 Conditioncond 78.2% 18 / 23 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 92.1% 129 / 140 CC 24 SCAR 23.8
CC
24
Cov
95%
settle
18.1
places
5.0
winner
2.0
CRAP 10.8
« blib/lib/Game/Mahjong/Search.pm ↑ Summary blib/lib/Game/Mahjong/Notation.pm »
1 package Game::Mahjong::Result;
2  
3 34 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 34 our $VERSION = '0.02';
8  
9 34 ▶use constant BASE => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use constant SEATS => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use constant HANDS => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 34 ▶use constant ROUNDS => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 180 ▶sub settle {
Subroutine settle: called CC=18 SCAR=29.0
Pod documented
15 180 my (%o) = @_;
16 180 my @deltas = (0) x SEATS;
17 180 ▶ my $by = $o{by} // '';
Condition50%1/2
$o{"by"} // ""
l!l
1800
MC/DC0%0/1
$o{"by"} // ""
$o{"by"}
Truth table50%1/2
A
A= $o{"by"}
Aresult
11
00
18 180 ▶ return \@deltas if $by eq 'exhausted';
Branch: 1 branch
BranchTrueFalseTotal
if $by eq "exhausted" 1 179 180
19  
20 179 my $winner = $o{winner};
21 179 ▶ die 'Game::Mahjong::Result: the winner is a seat 0 to 3'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $winner and $winner =~ /\A[0-3]\z/ 1 178 179
Condition66%2/3
defined $winner and $winner =~ /\A[0-3]\z/
!ll&&!rl&&r
01178
MC/DC50%1/2
defined $winner and $winner =~ /\A[0-3]\z/
defined $winner $winner =~ /\A[0-3]\z/
Truth table66%2/3
A and B
A= defined $winnerB= $winner =~ /\A[0-3]\z/
ABresult
0X0
100
111
22 unless defined $winner && $winner =~ /\A[0-3]\z/;
23 178 my $points = $o{points};
24 178 ▶ die 'Game::Mahjong::Result: points are a whole number'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $points and $points =~ /\A\d+\z/ 1 177 178
Condition66%2/3
defined $points and $points =~ /\A\d+\z/
!ll&&!rl&&r
01177
MC/DC50%1/2
defined $points and $points =~ /\A\d+\z/
defined $points $points =~ /\A\d+\z/
Truth table66%2/3
A and B
A= defined $pointsB= $points =~ /\A\d+\z/
ABresult
0X0
100
111
25 unless defined $points && $points =~ /\A\d+\z/;
26  
27 177 ▶ if ($by eq 'self') {
Branch: 1 branch
BranchTrueFalseTotal
if ($by eq "self") 52 125 177
28 52 for my $seat (0 .. SEATS - 1) {
29 208 ▶ next if $seat == $winner;
Branch: 1 branch
BranchTrueFalseTotal
if $seat == $winner 52 156 208
30 156 $deltas[$seat] -= BASE + $points;
31 156 $deltas[$winner] += BASE + $points;
32 }
33 52 return \@deltas;
34 }
35 125 ▶ if ($by eq 'discard' || $by eq 'rob') {
Branch: 1 branch
BranchTrueFalseTotal
if ($by eq "discard" or $by eq "rob") 124 1 125
Condition100%3/3
$by eq "discard" or $by eq "rob"
l!l&&r!l&&!r
12221
MC/DC100%2/2
$by eq "discard" or $by eq "rob"
$by eq "discard" $by eq "rob"
Truth table100%3/3
A or B
A= $by eq "discard"B= $by eq "rob"
ABresult
1X1
011
000
36 124 my $from = $o{from};
37 124 ▶ die 'Game::Mahjong::Result: a win by discard names the discarder'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from and $from =~ /\A[0-3]\z/ and $from != $winner 2 122 124
Condition83%5/6
defined $from and $from =~ /\A[0-3]\z/ and $from != $winner
!ll&&!rl&&r
11122
defined $from and $from =~ /\A[0-3]\z/
!ll&&!rl&&r
10123
MC/DC66%2/3
defined $from and $from =~ /\A[0-3]\z/ and $from != $winner
defined $from $from =~ /\A[0-3]\z/ $from != $winner
Truth table75%3/4
A and B and C
A= defined $fromB= $from =~ /\A[0-3]\z/C= $from != $winner
ABCresult
0XX0
10X0
1100
1111
38 unless defined $from && $from =~ /\A[0-3]\z/ && $from != $winner;
39 122 for my $seat (0 .. SEATS - 1) {
40 488 ▶ next if $seat == $winner;
Branch: 1 branch
BranchTrueFalseTotal
if $seat == $winner 122 366 488
41 366 ▶ my $gives = $seat == $from ? BASE + $points : BASE;
Branch: 1 branch
BranchTrueFalseTotal
$seat == $from ? : 122 244 366
42 366 $deltas[$seat] -= $gives;
43 366 $deltas[$winner] += $gives;
44 }
45 122 return \@deltas;
46 }
47 1 die "Game::Mahjong::Result: no such way to win '$by'";
48 }
49  
50 33 ▶sub places {
Subroutine places: called CC=5 SCAR=16.1
Pod documented
51 33 my ($totals) = @_;
52 33 ▶ my @seats = sort { $totals->[$b] <=> $totals->[$a] || $a <=> $b } 0 .. $#$totals;
Condition66%2/3
$totals->[$b] <=> $totals->[$a] || $a <=> $b
l!l&&r!l&&!r
131230
MC/DC0%0/2
$totals->[$b] <=> $totals->[$a] || $a <=> $b
$totals->[$b] <=> $totals->[$a] $a <=> $b
Truth table66%2/3
A || B
A= $totals->[$b] <=> $totals->[$a]B= $a <=> $b
ABresult
1X1
011
000
53 33 my (@places, $place, $last);
54 33 for my $i (0 .. $#seats) {
55 132 my $seat = $seats[$i];
56 132 ▶ if (!defined $last || $totals->[$seat] != $last) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $last or $totals->[$seat] != $last) 115 17 132
Condition100%3/3
not defined $last or $totals->[$seat] != $last
l!l&&r!l&&!r
338217
MC/DC100%2/2
not defined $last or $totals->[$seat] != $last
not defined $last $totals->[$seat] != $last
Truth table100%3/3
not(A) or B
A= not defined $lastB= $totals->[$seat] != $last
ABresult
1X1
011
000
57 115 $place = $i + 1;
58 115 $last = $totals->[$seat];
59 }
60 132 $places[$seat] = $place;
61 }
62 33 return \@places;
63 }
64  
65 16 ▶sub winner {
Subroutine winner: called CC=2 SCAR=6.9
Pod documented
66 16 my ($totals) = @_;
67 16 my $places = places($totals);
68 16 my @first = grep { $places->[$_] == 1 } 0 .. $#$places;
69 16 ▶ return @first == 1 ? $first[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@first == 1 ? : 14 2 16
70 }
71  
72 235 ▶sub finished {
Subroutine finished: called CC=2 SCAR=6.9
Pod documented
73 235 my ($hands_played) = @_;
74 235 ▶ return $hands_played >= HANDS ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$hands_played >= 16 ? : 15 220 235
75 }
76  
77 34 1;
78  
79 __END__
« blib/lib/Game/Mahjong/Search.pm ↑ Summary blib/lib/Game/Mahjong/Notation.pm »