blib/lib/Game/Mahjong/Wall.pm

filter: Statementstmt 100% 103 / 103 Branchbran 93.3% 28 / 30 Conditioncond 55.5% 5 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 16 / 16 Podpod 87.5% 7 / 8 totaltotal 93.5% 160 / 171 CC 24 SCAR 15.9
CC
24
Cov
96%
deal
9.1
BUILD
7.0
order_for
6.0
CRAP 4.9
« blib/lib/Game/Mahjong/Fans.pm ↑ Summary blib/lib/Game/Mahjong/Decompose.pm »
1 package Game::Mahjong::Wall;
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 Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 34 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use Game::Mahjong::Tiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 34 our $VERSION = '0.03';
13  
14 34 ▶use constant DEALT => 53;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 34 has seed => (is => 'ro', isa => Str, required => 1);
17  
18 34 has hand => (is => 'ro', isa => Int, default => 1);
19  
20 34 has tiles => (is => 'rw', isa => ArrayRef);
21  
22 34 has drawn => (is => 'rw', isa => Int, default => 0);
23  
24 34 has replaced => (is => 'rw', isa => Int, default => 0);
25  
26 34 has dealt => (is => 'rw', isa => Int, default => 0);
27  
28 362 ▶sub BUILD {
Subroutine BUILD: called CC=7 SCAR=19.5
Pod undocumented
29 362 my ($self) = @_;
30 362 ▶ die 'Game::Mahjong::Wall: a seed is a non-empty string' unless length $self->seed;
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->seed 1 361 362
31 361 ▶ die 'Game::Mahjong::Wall: a hand is 1 or more' unless $self->hand >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->hand >= 1 1 360 361
32  
33 360 ▶ if (my $given = $self->tiles) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $given = $self->tiles) 46 314 360
34 46 my %count;
35 46 for my $kind (@$given) {
36 280 Game::Mahjong::Tiles::code_of($kind);
37 279 ▶ my $cap = Game::Mahjong::Tiles::is_bonus($kind) ? 1 : Game::Mahjong::Tiles::PER_KIND;
Branch: 1 branch
BranchTrueFalseTotal
Game::Mahjong::Tiles::is_bonus($kind) ? : 20 259 279
38 die 'Game::Mahjong::Wall: a written wall holds more of '
39 . Game::Mahjong::Tiles::code_of($kind) . ' than the set does'
40 279 ▶ if ++$count{$kind} > $cap;
Branch: 1 branch
BranchTrueFalseTotal
if ++$count{$kind} > $cap 2 277 279
41 }
42 43 $self->tiles([@$given]);
43 43 return;
44 }
45  
46 314 my @set = Game::Mahjong::Tiles::set();
47 314 $self->tiles([ map { $set[ $_ - 1 ] } order_for($self->seed, $self->hand) ]);
48 314 return;
49 }
50  
51 321 ▶sub order_for {
Subroutine order_for: called CC=6 SCAR=18.0
Pod documented
52 321 my ($seed, $hand) = @_;
53 321 ▶ die 'Game::Mahjong::Wall: order_for needs a seed and a hand number'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seed and length $seed and defined $hand 1 320 321
Condition66%4/6
defined $seed and length $seed and defined $hand
!ll&&!rl&&r
10320
defined $seed and length $seed
!ll&&!rl&&r
01320
MC/DC33%1/3
defined $seed and length $seed and defined $hand
defined $seed length $seed defined $hand
Truth table50%2/4
A and B and C
A= defined $seedB= length $seedC= defined $hand
ABCresult
0XX0
10X0
1100
1111
54 unless defined $seed && length $seed && defined $hand;
55  
56 320 my ($counter, @words) = (0);
57 45760 ▶ my $next = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
58 45760 ▶ unless (@words) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@words) 5760 40000 45760
59 5760 my $digest = Digest::SHA::sha256($seed . "hand:$hand:" . $counter++);
60 5760 @words = unpack 'N8', $digest;
61 }
62 45760 return shift @words;
63 320 };
64  
65 320 my @order = (1 .. Game::Mahjong::Tiles::TILES);
66 320 ▶ for (my $i = $#order; $i > 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 45760 320 46080
67 45760 my $n = $i + 1;
68 45760 my $limit = int(4294967296 / $n) * $n;
69 45760 my $word;
70 45760 ▶ do { $word = $next->() } while $word >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
$word >= $limit and do { do { $word = &$next() } } 0 45760 45760
71 45760 my $j = $word % $n;
72 45760 @order[$i, $j] = @order[$j, $i];
73 }
74 320 return @order;
75 }
76  
77 319 ▶sub deal {
Subroutine deal: called CC=9 SCAR=22.0
Pod documented
78 319 my ($self, $dealer) = @_;
79 319 ▶ die 'Game::Mahjong::Wall: the dealer is a seat 0 to 3'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dealer and $dealer =~ /\A[0-3]\z/ 0 319 319
Condition33%1/3
defined $dealer and $dealer =~ /\A[0-3]\z/
!ll&&!rl&&r
00319
MC/DC0%0/2
defined $dealer and $dealer =~ /\A[0-3]\z/
defined $dealer $dealer =~ /\A[0-3]\z/
Truth table33%1/3
A and B
A= defined $dealerB= $dealer =~ /\A[0-3]\z/
ABresult
0X0
100
111
80 unless defined $dealer && $dealer =~ /\A[0-3]\z/;
81 319 ▶ die 'Game::Mahjong::Wall: this wall has been dealt' if $self->dealt;
Branch: 1 branch
BranchTrueFalseTotal
if $self->dealt 4 315 319
82 315 ▶ die 'Game::Mahjong::Wall: a wall of ' . $self->remaining . ' cannot deal ' . DEALT
Branch: 1 branch
BranchTrueFalseTotal
if $self->remaining < 53 1 314 315
83 if $self->remaining < DEALT;
84  
85 314 my @seats = map { ($dealer + $_) % 4 } 0 .. 3;
86 314 my %hands = map { $_ => [] } 0 .. 3;
87 314 my $tiles = $self->tiles;
88  
89 314 for my $round (1 .. 3) {
90 942 for my $seat (@seats) {
91 3768 push @{ $hands{$seat} }, splice @$tiles, 0, 4;
92 }
93 }
94 314 for my $seat (@seats) {
95 1256 push @{ $hands{$seat} }, shift @$tiles;
96 }
97 314 push @{ $hands{$dealer} }, shift @$tiles;
98  
99 314 @{ $hands{$_} } = sort { $a <=> $b } @{ $hands{$_} } for keys %hands;
100  
101 314 $self->dealt(1);
102 314 return { hands => \%hands, dealer => $dealer };
103 }
104  
105 14359 ▶sub draw {
Subroutine draw: called CC=2 SCAR=6.9
Pod documented
106 14359 my ($self) = @_;
107 die 'Game::Mahjong::Wall: the wall is empty, so there is nothing to draw'
108 14359 ▶ unless @{ $self->tiles };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->tiles;} 1 14358 14359
109 14358 $self->drawn($self->drawn + 1);
110 14358 return shift @{ $self->tiles };
111 }
112  
113 1881 ▶sub replace {
Subroutine replace: called CC=2 SCAR=6.9
Pod documented
114 1881 my ($self) = @_;
115 die 'Game::Mahjong::Wall: the wall is empty, so there is no replacement'
116 1881 ▶ unless @{ $self->tiles };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->tiles;} 1 1880 1881
117 1880 $self->replaced($self->replaced + 1);
118 1880 return pop @{ $self->tiles };
119 }
120  
121 18528 ▶sub remaining { return scalar @{ $_[0]->tiles } }
Subroutine remaining: called CC=1 SCAR=0.0
Pod documented
122  
123 31568 ▶sub is_empty { return @{ $_[0]->tiles } ? 0 : 1 }
Subroutine is_empty: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@{$_[0]->tiles;} ? : 31408 160 31568
124  
125 4228 ▶sub peek { return [ @{ $_[0]->tiles } ] }
Subroutine peek: called CC=1 SCAR=0.0
Pod documented
126  
127 34 1;
128  
129 __END__
« blib/lib/Game/Mahjong/Fans.pm ↑ Summary blib/lib/Game/Mahjong/Decompose.pm »