| 1 | package Game::Backgammon::Notation; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use Game::Backgammon::Move; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use Game::Backgammon::Turn; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 10 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||||||||||||||||
| 11 | 10 | our @EXPORT_OK = qw(parse_turn print_turn); | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2980 | ▶ | sub _place { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_place: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 2980 | my ($text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 2980 | ▶ | return 'bar' if lc $text eq 'bar'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 2883 | ▶ | return 'off' if lc $text eq 'off'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 2783 | ▶ | return undef unless $text =~ /\A\d+\z/ && $text >= 1 && $text <= 24; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$text =~ /\A\d+\z/ and $text >= 1 and $text <= 24
$text =~ /\A\d+\z/ and $text >= 1
MC/DC66%2/3
$text =~ /\A\d+\z/ and $text >= 1 and $text <= 24
$text =~ /\A\d+\z/
$text >= 1
$text <= 24
Truth table75%3/4
A and B and C
A= $text =~ /\A\d+\z/B= $text >= 1C= $text <= 24
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 2781 | return $text + 0; | |||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 809 | ▶ | sub parse_turn { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_turn: called CC=20 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 809 | my ($line, %o) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 23 | 809 | ▶ | $line = '' unless defined $line; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 809 | $line =~ s/\A\s+|\s+\z//g; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 809 | my @moves; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 809 | ▶ | return Game::Backgammon::Turn->new(player => $o{player}, dice => $o{dice} || [], | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
$line eq "" or lc $line eq "(no play)"
$o{'dice'} || []
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)"
Truth table50%1/2
A
A= $o{'dice'}
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | moves => []) | ||||||||||||||||||||||||||||||||||||||||||
| 29 | if $line eq '' || lc $line eq '(no play)'; | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 757 | for my $group (split /\s+/, $line) { | |||||||||||||||||||||||||||||||||||||||||
| 32 | 1493 | my $times = 1; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 1493 | ▶ | if ($group =~ s/\(([^)]*)\)\z//) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 167 | $times = $1; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 167 | ▶ | return _bad("'$group($times)' repeats a move $times times, which is not 1 to 4") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | unless $times =~ /\A[1-4]\z/; | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 165 | $times += 0; | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 1491 | my @hops = split m{/}, $group, -1; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 1491 | ▶ | return _bad("'$group' is not a move") if @hops < 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 1489 | my @pair; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 1489 | for my $i (0 .. $#hops - 1) { | |||||||||||||||||||||||||||||||||||||||||
| 45 | 1490 | my ($from_text, $to_text) = ($hops[$i], $hops[$i + 1]); | |||||||||||||||||||||||||||||||||||||||||
| 46 | 1490 | ▶ | my $hit = ($to_text =~ s/\*\z//) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 1490 | $from_text =~ s/\*\z//; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 1490 | my $from = _place($from_text); | |||||||||||||||||||||||||||||||||||||||||
| 49 | 1490 | my $to = _place($to_text); | |||||||||||||||||||||||||||||||||||||||||
| 50 | 1490 | ▶ | return _bad("'$group' names a place that is not a point, the bar or off") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $from and defined $to
MC/DC100%2/2
defined $from and defined $to
defined $from
defined $to
Truth table100%3/3
A and B
A= defined $fromB= defined $to
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | unless defined $from && defined $to; | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 1488 | ▶ | return _bad("'$group' moves from off the board") if $from eq 'off'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1487 | ▶ | return _bad("'$group' moves onto the bar") if $to eq 'bar'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 1486 | ▶ | my $die = ($from ne 'bar' && $to ne 'off') ? $from - $to : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$from ne "bar" and $to ne "off"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 1486 | ▶ | return _bad("'$group' moves backwards") if defined $die && $die <= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $die and $die <= 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | push @pair, Game::Backgammon::Move->new( | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 1485 | player => $o{player}, from => $from, to => $to, die => $die, hit => $hit); | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 1484 | push @moves, (@pair) x $times; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | return Game::Backgammon::Turn->new(player => $o{player}, | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 748 | ▶ | dice => $o{dice} || [], moves => \@moves); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$o{'dice'} || []
MC/DC0%0/1
$o{'dice'} || []
$o{'dice'}
Truth table50%1/2
A
A= $o{'dice'}
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 9 | ▶ | 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 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | __END__ | ||||||||||||||||||||||||||||||||||||||||||