Backgammon/Notation.pm

filter: Statementstmt 100% 55 / 55 Branchbran 96.4% 27 / 28 Conditioncond 72.7% 16 / 22 MC/DCmcdc 61.5% 8 / 13 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 90.7% 117 / 129 CC 25 SCAR 26.0
CC
25
Cov
93%
parse_turn
20.1
_place
6.2
BEGIN
1.0
CRAP 13.5
« Backgammon/Terminal.pm ↑ Summary Backgammon/Rules.pm »
1 package Game::Backgammon::Notation;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Game::Backgammon::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Game::Backgammon::Turn;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 our $VERSION = '0.03';
11 7 our @EXPORT_OK = qw(parse_turn print_turn);
12  
13 288 ▶sub _place {
Subroutine _place: called CC=6 SCAR=18.2
14 288 my ($text) = @_;
15 288 ▶ return 'bar' if lc $text eq 'bar';
Branch: 1 branch
BranchTrueFalseTotal
if lc $text eq "bar" 16 272 288
16 272 ▶ return 'off' if lc $text eq 'off';
Branch: 1 branch
BranchTrueFalseTotal
if lc $text eq "off" 14 258 272
17 258 ▶ return undef unless $text =~ /\A\d+\z/ && $text >= 1 && $text <= 24;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\A\d+\z/ and $text >= 1 and $text <= 24 1 257 258
Condition50%3/6
$text =~ /\A\d+\z/ and $text >= 1 and $text <= 24
!ll&&!rl&&r
01257
$text =~ /\A\d+\z/ and $text >= 1
!ll&&!rl&&r
00258
MC/DC33%1/3
$text =~ /\A\d+\z/ and $text >= 1 and $text <= 24
$text =~ /\A\d+\z/ $text >= 1 $text <= 24
Truth table50%2/4
A and B and C
A= $text =~ /\A\d+\z/B= $text >= 1C= $text <= 24
ABCresult
0XX0
10X0
1100
1111
18 257 return $text + 0;
19 }
20  
21 84 ▶sub parse_turn {
Subroutine parse_turn: called CC=20 SCAR=30.0
Pod documented
22 84 my ($line, %o) = @_;
23 84 ▶ $line = '' unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 84 84
24 84 $line =~ s/\A\s+|\s+\z//g;
25  
26 84 my @moves;
27 84 ▶ return Game::Backgammon::Turn->new(player => $o{player}, dice => $o{dice} || [],
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" or lc $line eq "(no play)" 5 79 84
Condition80%4/5
$line eq "" or lc $line eq "(no play)"
l!l&&r!l&&!r
2379
$o{'dice'} || []
l!l
05
MC/DC100%2/2
$line eq "" or lc $line eq "(no play)"
$line eq "" lc $line eq "(no play)"
MC/DC0%0/1
$o{'dice'} || []
$o{'dice'}
Truth table100%3/3
A or B
A= $line eq ""B= lc $line eq "(no play)"
ABresult
1X1
011
000
Truth table50%1/2
A
A= $o{'dice'}
Aresult
11
00
28 moves => [])
29 if $line eq '' || lc $line eq '(no play)';
30  
31 79 for my $group (split /\s+/, $line) {
32 147 my $times = 1;
33 147 ▶ if ($group =~ s/\(([^)]*)\)\z//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group =~ s/\(([^)]*)\)\z//) 14 133 147
34 14 $times = $1;
35 14 ▶ return _bad("'$group($times)' repeats a move $times times, which is not 1 to 4")
Branch: 1 branch
BranchTrueFalseTotal
unless $times =~ /\A[1-4]\z/ 2 12 14
36 unless $times =~ /\A[1-4]\z/;
37 12 $times += 0;
38 }
39  
40 145 my @hops = split m{/}, $group, -1;
41 145 ▶ return _bad("'$group' is not a move") if @hops < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @hops < 2 2 143 145
42  
43 143 my @pair;
44 143 for my $i (0 .. $#hops - 1) {
45 144 my ($from_text, $to_text) = ($hops[$i], $hops[$i + 1]);
46 144 ▶ my $hit = ($to_text =~ s/\*\z//) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$to_text =~ s/\*\z// ? : 14 130 144
47 144 $from_text =~ s/\*\z//;
48 144 my $from = _place($from_text);
49 144 my $to = _place($to_text);
50 144 ▶ return _bad("'$group' names a place that is not a point, the bar or off")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from and defined $to 1 143 144
Condition66%2/3
defined $from and defined $to
!ll&&!rl&&r
10143
MC/DC50%1/2
defined $from and defined $to
defined $from defined $to
Truth table66%2/3
A and B
A= defined $fromB= defined $to
ABresult
0X0
100
111
51 unless defined $from && defined $to;
52 143 ▶ return _bad("'$group' moves from off the board") if $from eq 'off';
Branch: 1 branch
BranchTrueFalseTotal
if $from eq "off" 1 142 143
53 142 ▶ return _bad("'$group' moves onto the bar") if $to eq 'bar';
Branch: 1 branch
BranchTrueFalseTotal
if $to eq "bar" 1 141 142
54 141 ▶ my $die = ($from ne 'bar' && $to ne 'off') ? $from - $to : undef;
Branch: 1 branch
BranchTrueFalseTotal
$from ne 'bar' && $to ne 'off' ? : 113 28 141
Condition100%3/3
$from ne "bar" and $to ne "off"
!ll&&!rl&&r
1513113
MC/DC100%2/2
$from ne "bar" and $to ne "off"
$from ne "bar" $to ne "off"
Truth table100%3/3
A and B
A= $from ne "bar"B= $to ne "off"
ABresult
0X0
100
111
55 141 ▶ return _bad("'$group' moves backwards") if defined $die && $die <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $die and $die <= 0 1 140 141
Condition100%3/3
defined $die and $die <= 0
!ll&&!rl&&r
281121
MC/DC100%2/2
defined $die and $die <= 0
defined $die $die <= 0
Truth table100%3/3
A and B
A= defined $dieB= $die <= 0
ABresult
0X0
100
111
56 push @pair, Game::Backgammon::Move->new(
57 140 player => $o{player}, from => $from, to => $to, die => $die, hit => $hit);
58 }
59 139 push @moves, (@pair) x $times;
60 }
61 return Game::Backgammon::Turn->new(player => $o{player},
62 71 ▶ dice => $o{dice} || [], moves => \@moves);
Condition50%1/2
$o{'dice'} || []
l!l
071
MC/DC0%0/1
$o{'dice'} || []
$o{'dice'}
Truth table50%1/2
A
A= $o{'dice'}
Aresult
11
00
63 }
64  
65 8 ▶sub _bad { $@ = "Game::Backgammon::Notation: $_[0]\n"; return undef }
Subroutine _bad: called CC=1 SCAR=0.0
66  
67 13 ▶sub print_turn { my ($turn) = @_; return $turn->notation }
Subroutine print_turn: called CC=1 SCAR=0.0
Pod documented
68  
69 7 1;
70  
71 __END__
« Backgammon/Terminal.pm ↑ Summary Backgammon/Rules.pm »