blib/lib/Game/Mahjong/Notation.pm

filter: Statementstmt 100% 117 / 117 Branchbran 95.0% 57 / 60 Conditioncond 85.7% 18 / 21 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 100% 12 / 12 Podpod 100% 6 / 6 totaltotal 95.6% 218 / 228 CC 48 SCAR 22.0
CC
48
Cov
97%
parse_hand
14.0
_check_meld
13.0
parse
7.0
CRAP 9.0
« blib/lib/Game/Mahjong/Result.pm ↑ Summary blib/lib/Game/Mahjong/Meld.pm »
1 package Game::Mahjong::Notation;
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 ▶use Game::Mahjong::Tiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 34 our $VERSION = '0.03';
10  
11 34 my %HONOUR = (
12 E => 'we', S => 'ws', W => 'ww', N => 'wn',
13 R => 'dr', G => 'dg', B => 'dw',
14 );
15 34 my %LETTER_OF = reverse %HONOUR;
16  
17 34 my %MELD = map { $_ => 1 } qw(chow pung kong ckong);
18  
19 71 ▶sub parse {
Subroutine parse: called CC=7 SCAR=19.5
Pod documented
20 71 my ($string) = @_;
21 71 ▶ die 'Game::Mahjong::Notation: nothing to parse' unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 1 70 71
22  
23 70 my @ids;
24 70 for my $token (split ' ', $string) {
25 164 push @ids, _parse_token($token);
26 }
27  
28 64 my %count;
29 64 for my $id (@ids) {
30 die 'Game::Mahjong::Notation: more than four of ' . Game::Mahjong::Tiles::code_of($id)
31 394 ▶ if ++$count{$id} > Game::Mahjong::Tiles::PER_KIND;
Branch: 1 branch
BranchTrueFalseTotal
if ++$count{$id} > 4 2 392 394
32 die 'Game::Mahjong::Notation: more than one ' . Game::Mahjong::Tiles::code_of($id)
33 392 ▶ if Game::Mahjong::Tiles::is_bonus($id) && $count{$id} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if Game::Mahjong::Tiles::is_bonus($id) and $count{$id} > 1 1 391 392
Condition100%3/3
Game::Mahjong::Tiles::is_bonus($id) and $count{$id} > 1
!ll&&!rl&&r
366251
MC/DC100%2/2
Game::Mahjong::Tiles::is_bonus($id) and $count{$id} > 1
Game::Mahjong::Tiles::is_bonus($id) $count{$id} > 1
Truth table100%3/3
A and B
A= Game::Mahjong::Tiles::is_bonus($id)B= $count{$id} > 1
ABresult
0X0
100
111
34 }
35 61 return @ids;
36 }
37  
38 3493 ▶sub _parse_token {
Subroutine _parse_token: called CC=4 SCAR=13.9
39 3493 my ($token) = @_;
40  
41 3493 ▶ if ($token =~ /\A([1-9]+)([mps])\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /\A([1-9]+)([mps])\z/) 2828 665 3493
42 2828 my ($ranks, $suit) = ($1, $2);
43 2828 return map { Game::Mahjong::Tiles::id_of($suit . $_) } split //, $ranks;
44 }
45 665 ▶ if ($token =~ /\A[ESWNRGB]+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /\A[ESWNRGB]+\z/) 629 36 665
46 629 return map { Game::Mahjong::Tiles::id_of($HONOUR{$_}) } split //, $token;
47 }
48 36 ▶ if ($token =~ /\A([FT])([1-4])\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /\A([FT])([1-4])\z/) 30 6 36
49 30 return Game::Mahjong::Tiles::id_of(lc($1) . $2);
50 }
51 6 die "Game::Mahjong::Notation: '$token' is not a tile";
52 }
53  
54 593 ▶sub parse_hand {
Subroutine parse_hand: called CC=14 SCAR=26.4
Pod documented
55 593 my ($string) = @_;
56 593 ▶ die 'Game::Mahjong::Notation: nothing to parse' unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 593 593
57  
58 593 my @tokens;
59 593 ▶ while ($string =~ /\G\s*(\w+\([^)]*\)|\S+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\G\s*(\w+\([^)]*\)|\S+)/g) 3329 593 3922
60 3329 push @tokens, $1;
61 }
62  
63 593 my (@concealed, @melds, @flowers);
64 593 for my $token (@tokens) {
65 3329 ▶ if ($token =~ /\A(\w+)\((.*)\)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /\A(\w+)\((.*)\)\z/) 126 3203 3329
66 126 my ($kind, $inside) = ($1, $2);
67 126 ▶ die "Game::Mahjong::Notation: '$kind' is not a meld" unless $MELD{$kind};
Branch: 1 branch
BranchTrueFalseTotal
unless $MELD{$kind} 1 125 126
68 125 my @tiles = map { _parse_token($_) } split ' ', $inside;
69 125 _check_meld($kind, \@tiles);
70 push @melds, {
71 kind => $kind eq 'ckong' ? 'kong' : $kind,
72 118 ▶ tiles => [ sort { $a <=> $b } @tiles ],
Branch: 2 branches
BranchTrueFalseTotal
$kind eq 'ckong' ? : 23 95 118
$kind eq 'ckong' ? : 23 95 118
73 concealed => $kind eq 'ckong' ? 1 : 0,
74 };
75 118 next;
76 }
77 3203 for my $id (_parse_token($token)) {
78 7364 ▶ if (Game::Mahjong::Tiles::is_bonus($id)) { push @flowers, $id }
Branch: 1 branch
BranchTrueFalseTotal
if (Game::Mahjong::Tiles::is_bonus($id)) { } 3 7361 7364
79 7361 else { push @concealed, $id }
80 }
81 }
82  
83 585 my %count;
84 585 for my $id (@concealed, @flowers, map { @{ $_->{tiles} } } @melds) {
85 die 'Game::Mahjong::Notation: more than four of ' . Game::Mahjong::Tiles::code_of($id)
86 7773 ▶ if ++$count{$id} > Game::Mahjong::Tiles::PER_KIND;
Branch: 1 branch
BranchTrueFalseTotal
if ++$count{$id} > 4 1 7772 7773
87 die 'Game::Mahjong::Notation: more than one ' . Game::Mahjong::Tiles::code_of($id)
88 7772 ▶ if Game::Mahjong::Tiles::is_bonus($id) && $count{$id} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if Game::Mahjong::Tiles::is_bonus($id) and $count{$id} > 1 0 7772 7772
Condition66%2/3
Game::Mahjong::Tiles::is_bonus($id) and $count{$id} > 1
!ll&&!rl&&r
776930
MC/DC0%0/2
Game::Mahjong::Tiles::is_bonus($id) and $count{$id} > 1
Game::Mahjong::Tiles::is_bonus($id) $count{$id} > 1
Truth table66%2/3
A and B
A= Game::Mahjong::Tiles::is_bonus($id)B= $count{$id} > 1
ABresult
0X0
100
111
89 }
90  
91 return {
92 16089 concealed => [ sort { $a <=> $b } @concealed ],
93 melds => \@melds,
94 584 flowers => [ sort { $a <=> $b } @flowers ],
95 };
96 }
97  
98 125 ▶sub _check_meld {
Subroutine _check_meld: called CC=13 SCAR=25.7
99 125 my ($kind, $tiles) = @_;
100 125 my $n = @$tiles;
101 125 ▶ my $want = $kind eq 'chow' ? 3 : $kind eq 'pung' ? 3 : 4;
Branch: 2 branches
BranchTrueFalseTotal
$kind eq 'chow' ? : 14 111 125
$kind eq 'pung' ? : 54 57 111
102 125 ▶ die "Game::Mahjong::Notation: a $kind is $want tiles, not $n" unless $n == $want;
Branch: 1 branch
BranchTrueFalseTotal
unless $n == $want 3 122 125
103  
104 122 ▶ if ($kind eq 'chow') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "chow") 14 108 122
105 14 my @sorted = sort { $a <=> $b } @$tiles;
106 14 my $suit = Game::Mahjong::Tiles::suit_of($sorted[0]);
107 14 ▶ die 'Game::Mahjong::Notation: a chow is three suit tiles' unless defined $suit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $suit 1 13 14
108 13 for my $i (0, 1) {
109 25 ▶ die 'Game::Mahjong::Notation: a chow is three consecutive tiles of one suit'
Branch: 1 branch
BranchTrueFalseTotal
unless $sorted[$i + 1] == $sorted[$i] + 1 and defined Game::Mahjong::Tiles::suit_of($sorted[$i + 1]) and Game::Mahjong::Tiles::suit_of($sorted[$i + 1]) eq $suit 2 23 25
Condition83%5/6
$sorted[$i + 1] == $sorted[$i] + 1 and defined Game::Mahjong::Tiles::suit_of($sorted[$i + 1]) and Game::Mahjong::Tiles::suit_of($sorted[$i + 1]) eq $suit
!ll&&!rl&&r
1123
$sorted[$i + 1] == $sorted[$i] + 1 and defined Game::Mahjong::Tiles::suit_of($sorted[$i + 1])
!ll&&!rl&&r
1024
MC/DC66%2/3
$sorted[$i + 1] == $sorted[$i] + 1 and defined Game::Mahjong::Tiles::suit_of($sorted[$i + 1]) and Game::Mahjong::Tiles::suit_of($sorted[$i + 1]) eq $suit
$sorted[$i + 1] == $sorted[$i] + 1 defined Game::Mahjong::Tiles::suit_of($sorted[$i + 1]) Game::Mahjong::Tiles::suit_of($sorted[$i + 1]) eq $suit
Truth table75%3/4
A and B and C
A= $sorted[$i + 1] == $sorted[$i] + 1B= defined Game::Mahjong::Tiles::suit_of($sorted[$i + 1])C= Game::Mahjong::Tiles::suit_of($sorted[$i + 1]) eq $suit
ABCresult
0XX0
10X0
1100
1111
110 unless ($sorted[$i + 1] == $sorted[$i] + 1)
111 && defined Game::Mahjong::Tiles::suit_of($sorted[$i + 1])
112 && Game::Mahjong::Tiles::suit_of($sorted[$i + 1]) eq $suit;
113 }
114 11 return;
115 }
116 108 for my $id (@$tiles) {
117 379 ▶ die "Game::Mahjong::Notation: a $kind is identical tiles"
Branch: 1 branch
BranchTrueFalseTotal
unless $id == $tiles->[0] 1 378 379
118 unless $id == $tiles->[0];
119 }
120 107 ▶ die 'Game::Mahjong::Notation: a bonus tile is never in a meld'
Branch: 1 branch
BranchTrueFalseTotal
if Game::Mahjong::Tiles::is_bonus($tiles->[0]) 0 107 107
121 if Game::Mahjong::Tiles::is_bonus($tiles->[0]);
122 107 return;
123 }
124  
125 43990 ▶sub print {
Subroutine print: called CC=7 SCAR=19.5
Pod documented
126 43990 my (@ids) = @_;
127 43990 my %by_suit;
128 43990 my (@honours, @bonus);
129 43990 for my $id (sort { $a <=> $b } @ids) {
130 44933 Game::Mahjong::Tiles::code_of($id);
131 44933 ▶ if (my $suit = Game::Mahjong::Tiles::suit_of($id)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $suit = Game::Mahjong::Tiles::suit_of($id)) { } 37759 7174 44933
elsif (Game::Mahjong::Tiles::is_honour($id)) { } 1506 5668 7174
132 37759 $by_suit{$suit} .= Game::Mahjong::Tiles::rank_of($id);
133 }
134 elsif (Game::Mahjong::Tiles::is_honour($id)) {
135 1506 push @honours, $LETTER_OF{ Game::Mahjong::Tiles::code_of($id) };
136 }
137 else {
138 5668 push @bonus, uc Game::Mahjong::Tiles::code_of($id);
139 }
140 }
141 43990 my @tokens;
142 43990 for my $suit (qw(m p s)) {
143 131970 ▶ push @tokens, $by_suit{$suit} . $suit if defined $by_suit{$suit};
Branch: 1 branch
BranchTrueFalseTotal
if defined $by_suit{$suit} 37126 94844 131970
144 }
145 43990 ▶ push @tokens, join('', @honours) if @honours;
Branch: 1 branch
BranchTrueFalseTotal
if @honours 1333 42657 43990
146 43990 push @tokens, @bonus;
147 43990 return join ' ', @tokens;
148 }
149  
150 31 ▶sub print_meld {
Subroutine print_meld: called CC=3 SCAR=11.0
Pod documented
151 31 my ($meld) = @_;
152 31 my $kind = $meld->{kind};
153 31 ▶ $kind = 'ckong' if $kind eq 'kong' && $meld->{concealed};
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "kong" and $meld->{"concealed"} 4 27 31
Condition100%3/3
$kind eq "kong" and $meld->{"concealed"}
!ll&&!rl&&r
1984
MC/DC100%2/2
$kind eq "kong" and $meld->{"concealed"}
$kind eq "kong" $meld->{"concealed"}
Truth table100%3/3
A and B
A= $kind eq "kong"B= $meld->{"concealed"}
ABresult
0X0
100
111
154 31 return $kind . '(' . Game::Mahjong::Notation::print(@{ $meld->{tiles} }) . ')';
155 }
156  
157 38 ▶sub print_hand {
Subroutine print_hand: called CC=6 SCAR=17.9
Pod documented
158 38 my ($hand) = @_;
159 38 my @parts;
160 38 ▶ my $concealed = Game::Mahjong::Notation::print(@{ $hand->{concealed} || [] });
Condition100%2/2
$hand->{'concealed'} || []
l!l
371
MC/DC100%1/1
$hand->{'concealed'} || []
$hand->{'concealed'}
Truth table100%2/2
A
A= $hand->{'concealed'}
Aresult
11
00
161 38 ▶ push @parts, $concealed if length $concealed;
Branch: 1 branch
BranchTrueFalseTotal
if length $concealed 37 1 38
162 38 ▶ push @parts, map { print_meld($_) } @{ $hand->{melds} || [] };
Condition50%1/2
$hand->{'melds'} || []
l!l
380
MC/DC0%0/1
$hand->{'melds'} || []
$hand->{'melds'}
Truth table50%1/2
A
A= $hand->{'melds'}
Aresult
11
00
163 38 ▶ my $flowers = Game::Mahjong::Notation::print(@{ $hand->{flowers} || [] });
Condition100%2/2
$hand->{'flowers'} || []
l!l
371
MC/DC100%1/1
$hand->{'flowers'} || []
$hand->{'flowers'}
Truth table100%2/2
A
A= $hand->{'flowers'}
Aresult
11
00
164 38 ▶ push @parts, $flowers if length $flowers;
Branch: 1 branch
BranchTrueFalseTotal
if length $flowers 18 20 38
165 38 return join ' ', @parts;
166 }
167  
168 1 ▶sub letters { return { %HONOUR } }
Subroutine letters: called CC=1 SCAR=0.0
Pod documented
169  
170 34 1;
171  
172 __END__
« blib/lib/Game/Mahjong/Result.pm ↑ Summary blib/lib/Game/Mahjong/Meld.pm »