blib/lib/Game/Mahjong/Notation.pm

filter: Statementstmt 14.5% 17 / 117 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 33.3% 4 / 12 Podpod 100% 6 / 6 totaltotal 11.8% 27 / 228 CC 48 SCAR 47.0
CC
48
Cov
9%
parse_hand
210.0
_check_meld
182.0
parse
56.0
CRAP 109.6
↑ Summary blib/lib/Game/Mahjong/Terminal.pm »
1 package Game::Mahjong::Notation;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Game::Mahjong::Tiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.04';
10  
11 1 my %HONOUR = (
12 E => 'we', S => 'ws', W => 'ww', N => 'wn',
13 R => 'dr', G => 'dg', B => 'dw',
14 );
15 1 my %LETTER_OF = reverse %HONOUR;
16  
17 1 my %MELD = map { $_ => 1 } qw(chow pung kong ckong);
18  
19 0 ▶sub parse {
Subroutine parse: not called CC=7 SCAR=40.3
Pod documented
20 0 my ($string) = @_;
21 0 ▶ die 'Game::Mahjong::Notation: nothing to parse' unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 0 0
22  
23 0 my @ids;
24 0 for my $token (split ' ', $string) {
25 0 push @ids, _parse_token($token);
26 }
27  
28 0 my %count;
29 0 for my $id (@ids) {
30 die 'Game::Mahjong::Notation: more than four of ' . Game::Mahjong::Tiles::code_of($id)
31 0 ▶ if ++$count{$id} > Game::Mahjong::Tiles::PER_KIND;
Branch: 1 branch
BranchTrueFalseTotal
if ++$count{$id} > 4 0 0 0
32 die 'Game::Mahjong::Notation: more than one ' . Game::Mahjong::Tiles::code_of($id)
33 0 ▶ 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 0 0
Condition0%0/3
Game::Mahjong::Tiles::is_bonus($id) and $count{$id} > 1
!ll&&!rl&&r
000
MC/DC0%0/2
Game::Mahjong::Tiles::is_bonus($id) and $count{$id} > 1
Game::Mahjong::Tiles::is_bonus($id) $count{$id} > 1
Truth table0%0/3
A and B
A= Game::Mahjong::Tiles::is_bonus($id)B= $count{$id} > 1
ABresult
0X0
100
111
34 }
35 0 return @ids;
36 }
37  
38 0 ▶sub _parse_token {
Subroutine _parse_token: not called CC=4 SCAR=30.0
39 0 my ($token) = @_;
40  
41 0 ▶ if ($token =~ /\A([1-9]+)([mps])\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /\A([1-9]+)([mps])\z/) 0 0 0
42 0 my ($ranks, $suit) = ($1, $2);
43 0 return map { Game::Mahjong::Tiles::id_of($suit . $_) } split //, $ranks;
44 }
45 0 ▶ if ($token =~ /\A[ESWNRGB]+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /\A[ESWNRGB]+\z/) 0 0 0
46 0 return map { Game::Mahjong::Tiles::id_of($HONOUR{$_}) } split //, $token;
47 }
48 0 ▶ if ($token =~ /\A([FT])([1-4])\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /\A([FT])([1-4])\z/) 0 0 0
49 0 return Game::Mahjong::Tiles::id_of(lc($1) . $2);
50 }
51 0 die "Game::Mahjong::Notation: '$token' is not a tile";
52 }
53  
54 0 ▶sub parse_hand {
Subroutine parse_hand: not called CC=14 SCAR=53.5
Pod documented
55 0 my ($string) = @_;
56 0 ▶ die 'Game::Mahjong::Notation: nothing to parse' unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 0 0
57  
58 0 my @tokens;
59 0 ▶ while ($string =~ /\G\s*(\w+\([^)]*\)|\S+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\G\s*(\w+\([^)]*\)|\S+)/g) 0 0 0
60 0 push @tokens, $1;
61 }
62  
63 0 my (@concealed, @melds, @flowers);
64 0 for my $token (@tokens) {
65 0 ▶ if ($token =~ /\A(\w+)\((.*)\)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /\A(\w+)\((.*)\)\z/) 0 0 0
66 0 my ($kind, $inside) = ($1, $2);
67 0 ▶ die "Game::Mahjong::Notation: '$kind' is not a meld" unless $MELD{$kind};
Branch: 1 branch
BranchTrueFalseTotal
unless $MELD{$kind} 0 0 0
68 0 my @tiles = map { _parse_token($_) } split ' ', $inside;
69 0 _check_meld($kind, \@tiles);
70 push @melds, {
71 kind => $kind eq 'ckong' ? 'kong' : $kind,
72 0 ▶ tiles => [ sort { $a <=> $b } @tiles ],
Branch: 2 branches
BranchTrueFalseTotal
$kind eq 'ckong' ? : 0 0 0
$kind eq 'ckong' ? : 0 0 0
73 concealed => $kind eq 'ckong' ? 1 : 0,
74 };
75 0 next;
76 }
77 0 for my $id (_parse_token($token)) {
78 0 ▶ if (Game::Mahjong::Tiles::is_bonus($id)) { push @flowers, $id }
Branch: 1 branch
BranchTrueFalseTotal
if (Game::Mahjong::Tiles::is_bonus($id)) { } 0 0 0
79 0 else { push @concealed, $id }
80 }
81 }
82  
83 0 my %count;
84 0 for my $id (@concealed, @flowers, map { @{ $_->{tiles} } } @melds) {
85 die 'Game::Mahjong::Notation: more than four of ' . Game::Mahjong::Tiles::code_of($id)
86 0 ▶ if ++$count{$id} > Game::Mahjong::Tiles::PER_KIND;
Branch: 1 branch
BranchTrueFalseTotal
if ++$count{$id} > 4 0 0 0
87 die 'Game::Mahjong::Notation: more than one ' . Game::Mahjong::Tiles::code_of($id)
88 0 ▶ 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 0 0
Condition0%0/3
Game::Mahjong::Tiles::is_bonus($id) and $count{$id} > 1
!ll&&!rl&&r
000
MC/DC0%0/2
Game::Mahjong::Tiles::is_bonus($id) and $count{$id} > 1
Game::Mahjong::Tiles::is_bonus($id) $count{$id} > 1
Truth table0%0/3
A and B
A= Game::Mahjong::Tiles::is_bonus($id)B= $count{$id} > 1
ABresult
0X0
100
111
89 }
90  
91 return {
92 0 concealed => [ sort { $a <=> $b } @concealed ],
93 melds => \@melds,
94 0 flowers => [ sort { $a <=> $b } @flowers ],
95 };
96 }
97  
98 0 ▶sub _check_meld {
Subroutine _check_meld: not called CC=13 SCAR=52.0
99 0 my ($kind, $tiles) = @_;
100 0 my $n = @$tiles;
101 0 ▶ my $want = $kind eq 'chow' ? 3 : $kind eq 'pung' ? 3 : 4;
Branch: 2 branches
BranchTrueFalseTotal
$kind eq 'chow' ? : 0 0 0
$kind eq 'pung' ? : 0 0 0
102 0 ▶ die "Game::Mahjong::Notation: a $kind is $want tiles, not $n" unless $n == $want;
Branch: 1 branch
BranchTrueFalseTotal
unless $n == $want 0 0 0
103  
104 0 ▶ if ($kind eq 'chow') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "chow") 0 0 0
105 0 my @sorted = sort { $a <=> $b } @$tiles;
106 0 my $suit = Game::Mahjong::Tiles::suit_of($sorted[0]);
107 0 ▶ die 'Game::Mahjong::Notation: a chow is three suit tiles' unless defined $suit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $suit 0 0 0
108 0 for my $i (0, 1) {
109 0 ▶ 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 0 0 0
Condition0%0/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
000
$sorted[$i + 1] == $sorted[$i] + 1 and defined Game::Mahjong::Tiles::suit_of($sorted[$i + 1])
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/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 0 return;
115 }
116 0 for my $id (@$tiles) {
117 0 ▶ die "Game::Mahjong::Notation: a $kind is identical tiles"
Branch: 1 branch
BranchTrueFalseTotal
unless $id == $tiles->[0] 0 0 0
118 unless $id == $tiles->[0];
119 }
120 0 ▶ 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 0 0
121 if Game::Mahjong::Tiles::is_bonus($tiles->[0]);
122 0 return;
123 }
124  
125 0 ▶sub print {
Subroutine print: not called CC=7 SCAR=40.3
Pod documented
126 0 my (@ids) = @_;
127 0 my %by_suit;
128 0 my (@honours, @bonus);
129 0 for my $id (sort { $a <=> $b } @ids) {
130 0 Game::Mahjong::Tiles::code_of($id);
131 0 ▶ if (my $suit = Game::Mahjong::Tiles::suit_of($id)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $suit = Game::Mahjong::Tiles::suit_of($id)) { } 0 0 0
elsif (Game::Mahjong::Tiles::is_honour($id)) { } 0 0 0
132 0 $by_suit{$suit} .= Game::Mahjong::Tiles::rank_of($id);
133 }
134 elsif (Game::Mahjong::Tiles::is_honour($id)) {
135 0 push @honours, $LETTER_OF{ Game::Mahjong::Tiles::code_of($id) };
136 }
137 else {
138 0 push @bonus, uc Game::Mahjong::Tiles::code_of($id);
139 }
140 }
141 0 my @tokens;
142 0 for my $suit (qw(m p s)) {
143 0 ▶ push @tokens, $by_suit{$suit} . $suit if defined $by_suit{$suit};
Branch: 1 branch
BranchTrueFalseTotal
if defined $by_suit{$suit} 0 0 0
144 }
145 0 ▶ push @tokens, join('', @honours) if @honours;
Branch: 1 branch
BranchTrueFalseTotal
if @honours 0 0 0
146 0 push @tokens, @bonus;
147 0 return join ' ', @tokens;
148 }
149  
150 0 ▶sub print_meld {
Subroutine print_meld: not called CC=3 SCAR=24.8
Pod documented
151 0 my ($meld) = @_;
152 0 my $kind = $meld->{kind};
153 0 ▶ $kind = 'ckong' if $kind eq 'kong' && $meld->{concealed};
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "kong" and $meld->{"concealed"} 0 0 0
Condition0%0/3
$kind eq "kong" and $meld->{"concealed"}
!ll&&!rl&&r
000
MC/DC0%0/2
$kind eq "kong" and $meld->{"concealed"}
$kind eq "kong" $meld->{"concealed"}
Truth table0%0/3
A and B
A= $kind eq "kong"B= $meld->{"concealed"}
ABresult
0X0
100
111
154 0 return $kind . '(' . Game::Mahjong::Notation::print(@{ $meld->{tiles} }) . ')';
155 }
156  
157 0 ▶sub print_hand {
Subroutine print_hand: not called CC=6 SCAR=37.4
Pod documented
158 0 my ($hand) = @_;
159 0 my @parts;
160 0 ▶ my $concealed = Game::Mahjong::Notation::print(@{ $hand->{concealed} || [] });
Condition0%0/2
$hand->{'concealed'} || []
l!l
00
MC/DC0%0/1
$hand->{'concealed'} || []
$hand->{'concealed'}
Truth table0%0/2
A
A= $hand->{'concealed'}
Aresult
11
00
161 0 ▶ push @parts, $concealed if length $concealed;
Branch: 1 branch
BranchTrueFalseTotal
if length $concealed 0 0 0
162 0 ▶ push @parts, map { print_meld($_) } @{ $hand->{melds} || [] };
Condition0%0/2
$hand->{'melds'} || []
l!l
00
MC/DC0%0/1
$hand->{'melds'} || []
$hand->{'melds'}
Truth table0%0/2
A
A= $hand->{'melds'}
Aresult
11
00
163 0 ▶ my $flowers = Game::Mahjong::Notation::print(@{ $hand->{flowers} || [] });
Condition0%0/2
$hand->{'flowers'} || []
l!l
00
MC/DC0%0/1
$hand->{'flowers'} || []
$hand->{'flowers'}
Truth table0%0/2
A
A= $hand->{'flowers'}
Aresult
11
00
164 0 ▶ push @parts, $flowers if length $flowers;
Branch: 1 branch
BranchTrueFalseTotal
if length $flowers 0 0 0
165 0 return join ' ', @parts;
166 }
167  
168 0 ▶sub letters { return { %HONOUR } }
Subroutine letters: not called CC=1 SCAR=6.9
Pod documented
169  
170 1 1;
171  
172 __END__
↑ Summary blib/lib/Game/Mahjong/Terminal.pm »