blib/lib/Game/Mahjong/Meld.pm

filter: Statementstmt 100% 82 / 82 Branchbran 97.5% 39 / 40 Conditioncond 91.6% 33 / 36 MC/DCmcdc 85.7% 18 / 21 Subroutinesub 100% 24 / 24 Podpod 94.1% 16 / 17 totaltotal 96.3% 212 / 220 CC 34 SCAR 20.7
CC
34
Cov
97%
BUILD
19.0
equals
6.0
is_outside
3.0
CRAP 8.0
« blib/lib/Game/Mahjong/Notation.pm ↑ Summary blib/lib/Game/Mahjong/Fans.pm »
1 package Game::Mahjong::Meld;
2  
3 34 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 34 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 34 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 34 ▶use Object::Proto::Sugar -types;
Subroutine __ANON__: called CC=1 SCAR=0.0
8  
9 34 ▶use Game::Mahjong::Tiles;
Subroutine __ANON__: called CC=1 SCAR=0.0
10 34 ▶use Game::Mahjong::Notation;
Subroutine __ANON__: called CC=1 SCAR=0.0
11  
12 34 our $VERSION = '0.03';
13  
14 34 our %KIND;
15 34 ▶BEGIN { %KIND = (chow => 3, pung => 3, kong => 4) }
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 34 has kind => (is => 'ro', isa => Str, required => 1);
18  
19 34 has tiles => (is => 'ro', isa => ArrayRef, required => 1);
20  
21 34 has concealed => (is => 'ro', default => 0);
22  
23 34 has claimed_from => (is => 'ro');
24  
25 34 has claimed_tile => (is => 'ro');
26  
27 34 has promoted => (is => 'ro', default => 0);
28  
29 2496 ▶sub BUILD {
Subroutine BUILD: called CC=19 SCAR=29.4
Pod undocumented
30 2496 my ($self) = @_;
31 2496 my $kind = $self->kind;
32 2496 ▶ die "Game::Mahjong::Meld: no such kind '$kind'" unless $KIND{$kind};
Branch: 1 branch
BranchTrueFalseTotal
unless $Game::Mahjong::Meld::KIND{$kind} 1 2495 2496
33  
34 2495 my @tiles = sort { $a <=> $b } @{ $self->tiles };
35 die "Game::Mahjong::Meld: a $kind is $KIND{$kind} tiles, not " . scalar @tiles
36 2495 ▶ unless @tiles == $KIND{$kind};
Branch: 1 branch
BranchTrueFalseTotal
unless @tiles == $Game::Mahjong::Meld::KIND{$kind} 3 2492 2495
37 2492 Game::Mahjong::Tiles::code_of($_) for @tiles;
38 die 'Game::Mahjong::Meld: a bonus tile is never in a meld'
39 2491 ▶ if grep { Game::Mahjong::Tiles::is_bonus($_) } @tiles;
Branch: 1 branch
BranchTrueFalseTotal
if grep {Game::Mahjong::Tiles::is_bonus($_);} @tiles 1 2490 2491
40  
41 2490 ▶ if ($kind eq 'chow') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq 'chow') { } 1470 1020 2490
42 1470 my $suit = Game::Mahjong::Tiles::suit_of($tiles[0]);
43 1470 ▶ die 'Game::Mahjong::Meld: a chow is three consecutive tiles of one suit'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $suit and $tiles[1] == $tiles[0] + 1 and $tiles[2] == $tiles[1] + 1 and Game::Mahjong::Tiles::rank_of($tiles[0]) <= 7 3 1467 1470
Condition88%8/9
defined $suit and $tiles[1] == $tiles[0] + 1 and $tiles[2] == $tiles[1] + 1 and Game::Mahjong::Tiles::rank_of($tiles[0]) <= 7
!ll&&!rl&&r
211467
defined $suit and $tiles[1] == $tiles[0] + 1 and $tiles[2] == $tiles[1] + 1
!ll&&!rl&&r
201468
defined $suit and $tiles[1] == $tiles[0] + 1
!ll&&!rl&&r
111468
MC/DC75%3/4
defined $suit and $tiles[1] == $tiles[0] + 1 and $tiles[2] == $tiles[1] + 1 and Game::Mahjong::Tiles::rank_of($tiles[0]) <= 7
defined $suit $tiles[1] == $tiles[0] + 1 $tiles[2] == $tiles[1] + 1 Game::Mahjong::Tiles::rank_of($tiles[0]) <= 7
Truth table80%4/5
A and B and C and D
A= defined $suitB= $tiles[1] == $tiles[0] + 1C= $tiles[2] == $tiles[1] + 1D= Game::Mahjong::Tiles::rank_of($tiles[0]) <= 7
ABCDresult
0XXX0
10XX0
110X0
11100
11111
44 unless defined $suit
45 && $tiles[1] == $tiles[0] + 1 && $tiles[2] == $tiles[1] + 1
46 && Game::Mahjong::Tiles::rank_of($tiles[0]) <= 7;
47 }
48 else {
49 die "Game::Mahjong::Meld: a $kind is identical tiles"
50 1020 ▶ if grep { $_ != $tiles[0] } @tiles;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ != $tiles[0];} @tiles 1 1019 1020
51 }
52  
53 2486 ▶ die 'Game::Mahjong::Meld: a claimed meld names the tile it took'
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->claimed_from and not defined $self->claimed_tile 1 2485 2486
Condition100%3/3
defined $self->claimed_from and not defined $self->claimed_tile
!ll&&!rl&&r
19322921
MC/DC100%2/2
defined $self->claimed_from and not defined $self->claimed_tile
defined $self->claimed_from not defined $self->claimed_tile
Truth table100%3/3
A and not(B)
A= defined $self->claimed_fromB= not defined $self->claimed_tile
ABresult
0X0
100
111
54 if defined $self->claimed_from && !defined $self->claimed_tile;
55 die 'Game::Mahjong::Meld: the claimed tile is not in the meld'
56 2485 ▶ if defined $self->claimed_tile && !grep { $_ == $self->claimed_tile } @tiles;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->claimed_tile and not grep({$_ == $self->claimed_tile;} @tiles) 1 2484 2485
Condition100%3/3
defined $self->claimed_tile and not grep({$_ == $self->claimed_tile;} @tiles)
!ll&&!rl&&r
19322911
MC/DC100%2/2
defined $self->claimed_tile and not grep({$_ == $self->claimed_tile;} @tiles)
defined $self->claimed_tile not grep({$_ == $self->claimed_tile;} @tiles)
Truth table100%3/3
A and not(B)
A= defined $self->claimed_tileB= not grep({$_ == $self->claimed_tile;} @tiles)
ABresult
0X0
100
111
57 2484 ▶ die 'Game::Mahjong::Meld: a concealed meld was claimed from nobody'
Branch: 1 branch
BranchTrueFalseTotal
if $self->concealed and defined $self->claimed_from 1 2483 2484
Condition100%3/3
$self->concealed and defined $self->claimed_from
!ll&&!rl&&r
2399841
MC/DC100%2/2
$self->concealed and defined $self->claimed_from
$self->concealed defined $self->claimed_from
Truth table100%3/3
A and B
A= $self->concealedB= defined $self->claimed_from
ABresult
0X0
100
111
58 if $self->concealed && defined $self->claimed_from;
59 2483 ▶ die 'Game::Mahjong::Meld: only a kong is promoted'
Branch: 1 branch
BranchTrueFalseTotal
if $self->promoted and $kind ne "kong" 1 2482 2483
Condition100%3/3
$self->promoted and $kind ne "kong"
!ll&&!rl&&r
2440421
MC/DC100%2/2
$self->promoted and $kind ne "kong"
$self->promoted $kind ne "kong"
Truth table100%3/3
A and B
A= $self->promotedB= $kind ne "kong"
ABresult
0X0
100
111
60 if $self->promoted && $kind ne 'kong';
61  
62 2482 @{ $self->tiles } = @tiles;
63 2482 return;
64 }
65  
66 1 ▶sub is_chow { return $_[0]->kind eq 'chow' ? 1 : 0 }
Subroutine is_chow: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->kind eq 'chow' ? : 1 0 1
67  
68 1954 ▶sub is_pung { return $_[0]->kind eq 'chow' ? 0 : 1 }
Subroutine is_pung: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->kind eq 'chow' ? : 433 1521 1954
69  
70 4 ▶sub is_kong { return $_[0]->kind eq 'kong' ? 1 : 0 }
Subroutine is_kong: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->kind eq 'kong' ? : 2 2 4
71  
72 2 ▶sub size { return scalar @{ $_[0]->tiles } }
Subroutine size: called CC=1 SCAR=0.0
Pod documented
73  
74 2 ▶sub counts_as { return 3 }
Subroutine counts_as: called CC=1 SCAR=0.0
Pod documented
75  
76 18826 ▶sub tile { return $_[0]->tiles->[0] }
Subroutine tile: called CC=1 SCAR=0.0
Pod documented
77  
78 2 ▶sub suit { return Game::Mahjong::Tiles::suit_of($_[0]->tiles->[0]) }
Subroutine suit: called CC=1 SCAR=0.0
Pod documented
79  
80 2 ▶sub rank { return Game::Mahjong::Tiles::rank_of($_[0]->tiles->[0]) }
Subroutine rank: called CC=1 SCAR=0.0
Pod documented
81  
82 6 ▶sub is_honour { return Game::Mahjong::Tiles::is_honour($_[0]->tiles->[0]) }
Subroutine is_honour: called CC=1 SCAR=0.0
Pod documented
83  
84 6 ▶sub has_terminal {
Subroutine has_terminal: called CC=2 SCAR=6.9
Pod documented
85 6 my ($self) = @_;
86 6 ▶ return (grep { Game::Mahjong::Tiles::is_terminal($_) } @{ $self->tiles }) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep({Game::Mahjong::Tiles::is_terminal($_);} @{$self->tiles;}) ? : 4 2 6
87 }
88  
89 4 ▶sub is_outside {
Subroutine is_outside: called CC=3 SCAR=11.0
Pod documented
90 4 my ($self) = @_;
91 4 ▶ return $self->is_honour || $self->has_terminal ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_honour || $self->has_terminal ? : 3 1 4
Condition100%3/3
$self->is_honour or $self->has_terminal
l!l&&r!l&&!r
121
MC/DC100%2/2
$self->is_honour or $self->has_terminal
$self->is_honour $self->has_terminal
Truth table100%3/3
A or B
A= $self->is_honourB= $self->has_terminal
ABresult
1X1
011
000
92 }
93  
94 2 ▶sub is_exposed {
Subroutine is_exposed: called CC=2 SCAR=6.9
Pod documented
95 2 my ($self) = @_;
96 2 ▶ return $self->concealed ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->concealed ? : 1 1 2
97 }
98  
99 45 ▶sub promote {
Subroutine promote: called CC=3 SCAR=11.0
Pod documented
100 45 my ($self) = @_;
101 45 ▶ die 'Game::Mahjong::Meld: only an exposed pung is promoted to a kong'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->kind eq "pung" and not $self->concealed 3 42 45
Condition100%3/3
$self->kind eq "pung" and not $self->concealed
!ll&&!rl&&r
2142
MC/DC100%2/2
$self->kind eq "pung" and not $self->concealed
$self->kind eq "pung" not $self->concealed
Truth table100%3/3
A and not(B)
A= $self->kind eq "pung"B= not $self->concealed
ABresult
0X0
100
111
102 unless $self->kind eq 'pung' && !$self->concealed;
103 return ref($self)->new(
104 kind => 'kong',
105 42 tiles => [ @{ $self->tiles }, $self->tile ],
106 claimed_from => $self->claimed_from,
107 claimed_tile => $self->claimed_tile,
108 promoted => 1,
109 );
110 }
111  
112 4 ▶sub equals {
Subroutine equals: called CC=6 SCAR=18.0
Pod documented
113 4 my ($self, $other) = @_;
114 4 ▶ return 0 unless ref $other && $other->can('kind');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $other and $other->can("kind") 1 3 4
Condition66%2/3
ref $other and $other->can("kind")
!ll&&!rl&&r
103
MC/DC50%1/2
ref $other and $other->can("kind")
ref $other $other->can("kind")
Truth table66%2/3
A and B
A= ref $otherB= $other->can("kind")
ABresult
0X0
100
111
115 return $self->kind eq $other->kind
116 && $self->concealed == $other->concealed
117 3 ▶ && join(',', @{ $self->tiles }) eq join(',', @{ $other->tiles }) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->kind eq $other->kind && $self->concealed == $other->concealed && join(',', @{$self->tiles;}) eq join(',', @{$other->tiles;}) ? : 1 2 3
Condition83%5/6
$self->kind eq $other->kind and $self->concealed == $other->concealed and join(",", @{$self->tiles;}) eq join(",", @{$other->tiles;})
!ll&&!rl&&r
111
$self->kind eq $other->kind and $self->concealed == $other->concealed
!ll&&!rl&&r
012
MC/DC66%2/3
$self->kind eq $other->kind and $self->concealed == $other->concealed and join(",", @{$self->tiles;}) eq join(",", @{$other->tiles;})
$self->kind eq $other->kind $self->concealed == $other->concealed join(",", @{$self->tiles;}) eq join(",", @{$other->tiles;})
Truth table75%3/4
A and B and C
A= $self->kind eq $other->kindB= $self->concealed == $other->concealedC= join(",", @{$self->tiles;}) eq join(",", @{$other->tiles;})
ABCresult
0XX0
10X0
1100
1111
118 }
119  
120 27 ▶sub to_hash {
Subroutine to_hash: called CC=2 SCAR=6.9
Pod documented
121 27 my ($self) = @_;
122 return {
123 kind => $self->kind,
124 27 ▶ tiles => [ @{ $self->tiles } ],
Branch: 1 branch
BranchTrueFalseTotal
$self->concealed ? : 3 24 27
125 concealed => $self->concealed ? 1 : 0,
126 };
127 }
128  
129 8 ▶sub to_notation {
Subroutine to_notation: called CC=1 SCAR=0.0
Pod documented
130 8 my ($self) = @_;
131 8 return Game::Mahjong::Notation::print_meld($self->to_hash);
132 }
133  
134 34 1;
135  
136 __END__
« blib/lib/Game/Mahjong/Notation.pm ↑ Summary blib/lib/Game/Mahjong/Fans.pm »