blib/lib/Game/Dominoes/Notation.pm

filter: Statementstmt 100% 68 / 68 Branchbran 89.5% 43 / 48 Conditioncond 83.3% 10 / 12 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 12 / 12 Podpod 100% 7 / 7 totaltotal 94.1% 145 / 154 CC 31 SCAR 19.2
CC
31
Cov
95%
move_text
11.0
parse_move
10.0
to_layout
5.1
CRAP 6.8
« blib/lib/Game/Dominoes/Layout.pm ↑ Summary blib/lib/Game/Dominoes/Rules.pm »
1 package Game::Dominoes::Notation;
2  
3 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 39 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 39 ▶use Game::Dominoes::Tile;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 39 ▶use Game::Dominoes::Layout;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 39 our $VERSION = '0.05';
12 39 our @EXPORT_OK = qw(
13 tile_text parse_tile
14 move_text parse_move
15 to_text from_text to_layout
16 );
17  
18 1 ▶sub tile_text {
Subroutine tile_text: called CC=2 SCAR=7.1
Pod documented
19 1 my ($tile) = @_;
20 1 ▶ die 'Game::Dominoes::Notation: tile_text wants a tile' unless ref $tile;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tile 0 1 1
21 1 return $tile->stringify;
22 }
23  
24 795 ▶sub parse_tile {
Subroutine parse_tile: called CC=4 SCAR=14.2
Pod documented
25 795 my ($text) = @_;
26 795 ▶ die 'Game::Dominoes::Notation: not a tile: '
Branch: 2 branches
BranchTrueFalseTotal
unless defined $text and $text =~ /\A([0-6])-([0-6])\z/ 2 793 795
defined $text ? : 2 0 2
Condition66%2/3
defined $text and $text =~ /\A([0-6])-([0-6])\z/
!ll&&!rl&&r
02793
MC/DC50%1/2
defined $text and $text =~ /\A([0-6])-([0-6])\z/
defined $text $text =~ /\A([0-6])-([0-6])\z/
Truth table66%2/3
A and B
A= defined $textB= $text =~ /\A([0-6])-([0-6])\z/
ABresult
0X0
100
111
27 . (defined $text ? "'$text'" : 'undef')
28 unless defined $text && $text =~ /\A([0-6])-([0-6])\z/;
29 793 return Game::Dominoes::Tile->of($1, $2);
30 }
31  
32 4513 ▶sub move_text {
Subroutine move_text: called CC=11 SCAR=24.0
Pod documented
33 4513 my ($move) = @_;
34 4513 ▶ die 'Game::Dominoes::Notation: move_text wants a move' unless ref $move;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $move 0 4513 4513
35  
36 4513 ▶ if (ref $move ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $move ne "HASH") 2568 1945 4513
37 2568 my $played = $move->tile->stringify . '@' . $move->arm;
38 2568 ▶ $played .= '*' if $move->spinner;
Branch: 1 branch
BranchTrueFalseTotal
if $move->spinner 191 2377 2568
39 2568 return $played;
40 }
41  
42 1945 ▶ my $kind = $move->{kind} || 'play';
Condition100%2/2
$move->{'kind'} || 'play'
l!l
19432
MC/DC100%1/1
$move->{'kind'} || 'play'
$move->{'kind'}
Truth table100%2/2
A
A= $move->{'kind'}
Aresult
11
00
43 1945 ▶ return 'P' if $kind eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "pass" 328 1617 1945
44 1617 ▶ return '|' if $kind eq 'hand_end';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "hand_end" 102 1515 1617
45 1515 ▶ if ($kind eq 'draw') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "draw") 267 1248 1515
46 267 ▶ my $n = $move->{count} || 1;
Condition100%2/2
$move->{'count'} || 1
l!l
2661
MC/DC100%1/1
$move->{'count'} || 1
$move->{'count'}
Truth table100%2/2
A
A= $move->{'count'}
Aresult
11
00
47 267 ▶ return $n > 1 ? "D$n" : 'D';
Branch: 1 branch
BranchTrueFalseTotal
$n > 1 ? : 140 127 267
48 }
49 1248 my $text = $move->{tile}->stringify . '@' . $move->{arm};
50 1248 ▶ $text .= '*' if $move->{spinner};
Branch: 1 branch
BranchTrueFalseTotal
if $move->{"spinner"} 92 1156 1248
51 1248 return $text;
52 }
53  
54 1021 ▶sub parse_move {
Subroutine parse_move: called CC=10 SCAR=23.0
Pod documented
55 1021 my ($token) = @_;
56 1021 ▶ die 'Game::Dominoes::Notation: empty move' unless defined $token && length $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token and length $token 2 1019 1021
Condition100%3/3
defined $token and length $token
!ll&&!rl&&r
111019
MC/DC100%2/2
defined $token and length $token
defined $token length $token
Truth table100%3/3
A and B
A= defined $tokenB= length $token
ABresult
0X0
100
111
57  
58 1019 ▶ return { kind => 'pass' } if $token eq 'P';
Branch: 1 branch
BranchTrueFalseTotal
if $token eq "P" 117 902 1019
59 902 ▶ return { kind => 'hand_end' } if $token eq '|';
Branch: 1 branch
BranchTrueFalseTotal
if $token eq "|" 33 869 902
60 869 ▶ if ($token =~ /\AD(\d*)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /\AD(\d*)\z/) 76 793 869
61 76 ▶ my $n = length $1 ? $1 + 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
length $1 ? : 43 33 76
62 76 ▶ die "Game::Dominoes::Notation: a draw of $n is not a draw" if $n < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 1 1 75 76
63 75 return { kind => 'draw', count => $n };
64 }
65 793 ▶ if ($token =~ /\A([0-6]-[0-6])\@([LRUD])(\*?)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /\A([0-6]-[0-6])\@([LRUD])(\*?)\z/) 788 5 793
66 return {
67 788 ▶ kind => 'play',
Branch: 1 branch
BranchTrueFalseTotal
$3 ? : 56 732 788
68 tile => parse_tile($1),
69 arm => $2,
70 spinner => $3 ? 1 : 0,
71 };
72 }
73 5 die "Game::Dominoes::Notation: not a move: '$token'";
74 }
75  
76 451 ▶sub to_text {
Subroutine to_text: called CC=2 SCAR=7.0
Pod documented
77 451 my ($moves) = @_;
78 451 ▶ die 'Game::Dominoes::Notation: to_text wants an arrayref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $moves eq "ARRAY" 0 451 451
79 unless ref $moves eq 'ARRAY';
80 451 return join ' ', map { move_text($_) } @$moves;
81 }
82  
83 51 ▶sub from_text {
Subroutine from_text: called CC=3 SCAR=11.0
Pod documented
84 51 my ($text) = @_;
85 51 ▶ return [] unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 1 50 51
86 50 $text =~ s/\A\s+//;
87 50 $text =~ s/\s+\z//;
88 50 ▶ return [] unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 11 39 50
89 39 return [ map { parse_move($_) } split /\s+/, $text ];
90 }
91  
92 1 ▶sub to_layout {
Subroutine to_layout: called CC=5 SCAR=16.3
Pod documented
93 1 my ($text) = @_;
94 1 ▶ my $moves = ref $text eq 'ARRAY' ? $text : from_text($text);
Branch: 1 branch
BranchTrueFalseTotal
ref $text eq 'ARRAY' ? : 0 1 1
95 1 my $layout = Game::Dominoes::Layout->new;
96 1 for my $move (@$moves) {
97 6 ▶ next unless ($move->{kind} || 'play') eq 'play';
Branch: 1 branch
BranchTrueFalseTotal
unless ($move->{"kind"} || "play") eq "play" 2 4 6
Condition50%1/2
$move->{'kind'} || 'play'
l!l
60
MC/DC0%0/1
$move->{'kind'} || 'play'
$move->{'kind'}
Truth table50%1/2
A
A= $move->{'kind'}
Aresult
11
00
98 4 $layout->place($move->{tile}, $move->{arm});
99 }
100 1 return $layout;
101 }
102  
103 39 1;
104  
105 __END__
« blib/lib/Game/Dominoes/Layout.pm ↑ Summary blib/lib/Game/Dominoes/Rules.pm »