blib/lib/Game/Senet/Notation.pm

filter: Statementstmt 100% 216 / 216 Branchbran 92.9% 145 / 156 Conditioncond 78.3% 58 / 74 MC/DCmcdc 52.2% 23 / 44 Subroutinesub 100% 29 / 29 Podpod 100% 8 / 8 totaltotal 90.8% 479 / 527 CC 106 SCAR 29.7
CC
106
Cov
94%
parse_record
37.0
validate_position
30.0
parse_move
11.0
CRAP 19.4
« blib/lib/Game/Senet/Terminal.pm ↑ Summary blib/lib/Game/Senet.pm »
1 package Game::Senet::Notation;
2  
3 62 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 62 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5 62 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 62 ▶use Exporter 'import';
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
8 62 ▶use Scalar::Util ();
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 62 ▶use Game::Senet::Sticks ();
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 64 our $VERSION = '0.02';
13  
14 62 ▶use constant {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
15 62 POS_OK => 0,
16 POS_NULL => 1,
17 POS_GROUPS => 2,
18 POS_WIDTH => 3,
19 POS_LETTER => 4,
20 POS_SIDE => 5,
21 POS_PIECES => 6,
22 POS_EXCESS => 7,
23 POS_FIELD => 8,
24 POS_LONG => 9,
25 62 };
26  
27 64 our (@EXPORT_OK, %EXPORT_TAGS);
28 64 my (@FIELDS, %FIELD_OK, %SIDE_OF, %LETTER_OF, %RESULT_OK, $MOVE_MAX);
29 64 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
30 64 @EXPORT_OK = qw(
31 house_ok parse_move format_move format_display format_forfeit
32 validate_position parse_record format_record
33 POS_OK POS_NULL POS_GROUPS POS_WIDTH POS_LETTER POS_SIDE
34 POS_PIECES POS_EXCESS POS_FIELD POS_LONG
35 );
36 64 %EXPORT_TAGS = (all => \@EXPORT_OK);
37  
38 64 @FIELDS = qw(pieces blockade protect backward reach26 throw_on_26);
39 64 %FIELD_OK = (
40 pieces => qr/\A[1-7]\z/,
41 blockade => qr/\A[023]\z/,
42 protect => qr/\A(?:none|run|ends)\z/,
43 backward => qr/\A[01]\z/,
44 reach26 => qr/\A(?:exact|any)\z/,
45 throw_on_26 => qr/\A[01]\z/,
46 );
47 64 %SIDE_OF = (l => 'light', d => 'dark');
48 64 %LETTER_OF = (light => 'l', dark => 'd');
49 64 %RESULT_OK = map { $_ => 1 }
50 'light home', 'dark home', 'light resign', 'dark resign', 'draw ply_cap';
51 64 $MOVE_MAX = length '30-off';
52 }
53  
54 2826004 ▶sub house_ok {
Subroutine house_ok: called CC=4 SCAR=13.9
Pod documented
55 2826004 my ($text) = @_;
56 2826004 ▶ return 0 unless defined $text && $text =~ /\A[1-9][0-9]?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and $text =~ /\A[1-9][0-9]?\z/ 3 2826001 2826004
Condition66%2/3
defined $text and $text =~ /\A[1-9][0-9]?\z/
!ll&&!rl&&r
032826001
MC/DC50%1/2
defined $text and $text =~ /\A[1-9][0-9]?\z/
defined $text $text =~ /\A[1-9][0-9]?\z/
Truth table66%2/3
A and B
A= defined $textB= $text =~ /\A[1-9][0-9]?\z/
ABresult
0X0
100
111
57 2826001 ▶ return $text <= 30 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$text <= 30 ? : 2825999 2 2826001
58 }
59  
60 2350734 ▶my $place = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
61 2350734 my ($text) = @_;
62 2350734 ▶ return ('off', undef) if lc $text eq 'off';
Branch: 1 branch
BranchTrueFalseTotal
if lc $text eq "off" 34533 2316201 2350734
63 2316201 ▶ return (undef, 'shape') unless $text =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\A[0-9]+\z/ 5 2316196 2316201
64 2316196 ▶ return (undef, 'house') unless house_ok($text);
Branch: 1 branch
BranchTrueFalseTotal
unless house_ok($text) 4 2316192 2316196
65 2316192 return ($text + 0, undef);
66 64 };
67  
68 1182273 ▶sub parse_move {
Subroutine parse_move: called CC=11 SCAR=24.0
Pod documented
69 1182273 my ($text) = @_;
70 1182273 ▶ return (undef, 'empty') unless defined $text && length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 2 1182271 1182273
Condition100%3/3
defined $text and length $text
!ll&&!rl&&r
111182271
MC/DC100%2/2
defined $text and length $text
defined $text length $text
Truth table100%3/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
71 1182271 ▶ return (undef, 'long') if length $text > $MOVE_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if length $text > $MOVE_MAX 6898 1175373 1182271
72 1175373 my @parts = split /-/, $text, -1;
73 1175373 ▶ return (undef, 'shape') unless @parts == 2 && length $parts[0] && length $parts[1];
Branch: 1 branch
BranchTrueFalseTotal
unless @parts == 2 and length $parts[0] and length $parts[1] 4 1175369 1175373
Condition83%5/6
@parts == 2 and length $parts[0] and length $parts[1]
!ll&&!rl&&r
401175369
@parts == 2 and length $parts[0]
!ll&&!rl&&r
311175369
MC/DC66%2/3
@parts == 2 and length $parts[0] and length $parts[1]
@parts == 2 length $parts[0] length $parts[1]
Truth table75%3/4
A and B and C
A= @parts == 2B= length $parts[0]C= length $parts[1]
ABCresult
0XX0
10X0
1100
1111
74 1175369 my ($from, $bad_from) = $place->($parts[0]);
75 1175369 ▶ return (undef, $bad_from) if $bad_from;
Branch: 1 branch
BranchTrueFalseTotal
if $bad_from 4 1175365 1175369
76 1175365 my ($to, $bad_to) = $place->($parts[1]);
77 1175365 ▶ return (undef, $bad_to) if $bad_to;
Branch: 1 branch
BranchTrueFalseTotal
if $bad_to 5 1175360 1175365
78 1175360 ▶ return (undef, 'from_off') if $from eq 'off';
Branch: 1 branch
BranchTrueFalseTotal
if $from eq "off" 1 1175359 1175360
79 1175359 ▶ return (undef, 'same') if $from eq $to;
Branch: 1 branch
BranchTrueFalseTotal
if $from eq $to 1 1175358 1175359
80 1175358 return ({ from => $from, to => $to }, undef);
81 }
82  
83 8407152 ▶my $read = sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
84 8407152 my ($move, $name) = @_;
85 8407152 ▶ return $move->$name if Scalar::Util::blessed($move) && $move->can($name);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($move) and $move->can($name) 6705536 1701616 8407152
Condition66%2/3
builtin::blessed($move) and $move->can($name)
!ll&&!rl&&r
170161606705536
MC/DC50%1/2
builtin::blessed($move) and $move->can($name)
builtin::blessed($move) $move->can($name)
Truth table66%2/3
A and B
A= builtin::blessed($move)B= $move->can($name)
ABresult
0X0
100
111
86 1701616 ▶ return ref $move eq 'HASH' ? $move->{$name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $move eq 'HASH' ? : 1701616 0 1701616
87 64 };
88  
89 2823698 ▶sub format_move {
Subroutine format_move: called CC=2 SCAR=6.9
Pod documented
90 2823698 my ($move) = @_;
91 2823698 ▶ my $to = $read->($move, 'off') ? 'off' : $read->($move, 'to');
Branch: 1 branch
BranchTrueFalseTotal
&$read($move, 'off') ? : 71013 2752685 2823698
92 2823698 return $read->($move, 'from') . '-' . lc $to;
93 }
94  
95 2357 ▶sub format_display {
Subroutine format_display: called CC=4 SCAR=13.9
Pod documented
96 2357 my ($move, $again) = @_;
97 2357 ▶ return $read->($move, 'roll') . ': ' . format_move($move)
Branch: 3 branches
BranchTrueFalseTotal
&$read($move, 'back') ? : 141 2216 2357
&$read($move, 'swap') ? : 413 1944 2357
$again ? : 6 2351 2357
98 . ($read->($move, 'back') ? '<' : '')
99 . ($read->($move, 'swap') ? 'x' : '')
100 . ($again ? '+' : '');
101 }
102  
103 5 ▶sub format_forfeit {
Subroutine format_forfeit: called CC=2 SCAR=6.9
Pod documented
104 5 my ($roll, $again) = @_;
105 5 ▶ return $roll . ': -' . ($again ? '+' : '');
Branch: 1 branch
BranchTrueFalseTotal
$again ? : 4 1 5
106 }
107  
108 752 ▶sub validate_position {
Subroutine validate_position: called CC=30 SCAR=34.0
Pod documented
109 752 my ($text) = @_;
110 752 ▶ return POS_NULL unless defined $text && length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 1 751 752
Condition66%2/3
defined $text and length $text
!ll&&!rl&&r
01751
MC/DC50%1/2
defined $text and length $text
defined $text length $text
Truth table66%2/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
111 751 ▶ return POS_LONG if length $text >= 48;
Branch: 1 branch
BranchTrueFalseTotal
if length $text >= 48 1 750 751
112  
113 750 my @c = split //, $text;
114 750 my ($group, $width, $i) = (0, 0, 0);
115 750 my %count = (l => 0, d => 0);
116 750 ▶ for (; $i < @c && $c[$i] ne ' '; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @c and $c[$i] ne " ") 23281 711 23992
Condition100%3/3
$i < @c and $c[$i] ne " "
!ll&&!rl&&r
171023281
MC/DC100%2/2
$i < @c and $c[$i] ne " "
$i < @c $c[$i] ne " "
Truth table100%3/3
A and B
A= $i < @cB= $c[$i] ne " "
ABresult
0X0
100
111
117 23281 my $ch = $c[$i];
118 23281 ▶ if ($ch eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "/") 1456 21825 23281
119 1456 ▶ return POS_WIDTH if $width != 10;
Branch: 1 branch
BranchTrueFalseTotal
if $width != 10 6 1450 1456
120 1450 ▶ return POS_GROUPS if $group == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $group == 2 1 1449 1450
121 1449 $group++;
122 1449 $width = 0;
123 1449 next;
124 }
125 21825 ▶ return POS_LETTER unless $ch eq 'l' || $ch eq 'd' || $ch eq '.';
Branch: 1 branch
BranchTrueFalseTotal
unless $ch eq "l" or $ch eq "d" or $ch eq "." 31 21794 21825
Condition100%6/6
$ch eq "l" or $ch eq "d" or $ch eq "."
l!l&&r!l&&!r
29021889231
$ch eq "l" or $ch eq "d"
l!l&&r!l&&!r
1447145518923
MC/DC100%3/3
$ch eq "l" or $ch eq "d" or $ch eq "."
$ch eq "l" $ch eq "d" $ch eq "."
Truth table100%4/4
A or B or C
A= $ch eq "l"B= $ch eq "d"C= $ch eq "."
ABCresult
1XX1
01X1
0011
0000
126 21794 ▶ return POS_WIDTH if $width >= 10;
Branch: 1 branch
BranchTrueFalseTotal
if $width >= 10 1 21793 21794
127 21793 ▶ $count{$ch}++ if $ch ne '.';
Branch: 1 branch
BranchTrueFalseTotal
if $ch ne "." 2902 18891 21793
128 21793 $width++;
129 }
130 711 ▶ return POS_GROUPS if $group != 2;
Branch: 1 branch
BranchTrueFalseTotal
if $group != 2 4 707 711
131 707 ▶ return POS_WIDTH if $width != 10;
Branch: 1 branch
BranchTrueFalseTotal
if $width != 10 1 706 707
132  
133 706 ▶ return POS_FIELD unless $i < @c && $c[$i] eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
unless $i < @c and $c[$i] eq " " 1 705 706
Condition66%2/3
$i < @c and $c[$i] eq " "
!ll&&!rl&&r
10705
MC/DC50%1/2
$i < @c and $c[$i] eq " "
$i < @c $c[$i] eq " "
Truth table66%2/3
A and B
A= $i < @cB= $c[$i] eq " "
ABresult
0X0
100
111
134 705 $i++;
135 705 ▶ return POS_FIELD if $i >= @c;
Branch: 1 branch
BranchTrueFalseTotal
if $i >= @c 0 705 705
136 705 ▶ return POS_SIDE unless $c[$i] eq 'l' || $c[$i] eq 'd';
Branch: 1 branch
BranchTrueFalseTotal
unless $c[$i] eq "l" or $c[$i] eq "d" 3 702 705
Condition100%3/3
$c[$i] eq "l" or $c[$i] eq "d"
l!l&&r!l&&!r
648543
MC/DC100%2/2
$c[$i] eq "l" or $c[$i] eq "d"
$c[$i] eq "l" $c[$i] eq "d"
Truth table100%3/3
A or B
A= $c[$i] eq "l"B= $c[$i] eq "d"
ABresult
1X1
011
000
137 702 $i++;
138 702 ▶ return POS_FIELD if $i >= @c;
Branch: 1 branch
BranchTrueFalseTotal
if $i >= @c 1 701 702
139 701 ▶ return POS_SIDE unless $c[$i] eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
unless $c[$i] eq " " 1 700 701
140 700 $i++;
141 700 ▶ return POS_FIELD if $i >= @c;
Branch: 1 branch
BranchTrueFalseTotal
if $i >= @c 1 699 700
142 699 ▶ return POS_PIECES unless $c[$i] =~ /\A[1-7]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $c[$i] =~ /\A[1-7]\z/ 3 696 699
143 696 my $pieces = $c[$i];
144 696 $i++;
145 696 ▶ return POS_PIECES if $i < @c && $c[$i] ne ' ';
Branch: 1 branch
BranchTrueFalseTotal
if $i < @c and $c[$i] ne " " 1 695 696
Condition100%3/3
$i < @c and $c[$i] ne " "
!ll&&!rl&&r
69411
MC/DC100%2/2
$i < @c and $c[$i] ne " "
$i < @c $c[$i] ne " "
Truth table100%3/3
A and B
A= $i < @cB= $c[$i] ne " "
ABresult
0X0
100
111
146 695 ▶ return POS_FIELD if $i < @c;
Branch: 1 branch
BranchTrueFalseTotal
if $i < @c 1 694 695
147 694 ▶ return POS_EXCESS if $count{l} > $pieces || $count{d} > $pieces;
Branch: 1 branch
BranchTrueFalseTotal
if $count{"l"} > $pieces or $count{"d"} > $pieces 3 691 694
Condition100%3/3
$count{"l"} > $pieces or $count{"d"} > $pieces
l!l&&r!l&&!r
21691
MC/DC100%2/2
$count{"l"} > $pieces or $count{"d"} > $pieces
$count{"l"} > $pieces $count{"d"} > $pieces
Truth table100%3/3
A or B
A= $count{"l"} > $piecesB= $count{"d"} > $pieces
ABresult
1X1
011
000
148 691 return POS_OK;
149 }
150  
151 7141 ▶my $variant_text = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
152 7141 my ($variant) = @_;
153 7141 return join ' ', $variant->{name}, map { "$_=$variant->{$_}" } @FIELDS;
154 64 };
155  
156 5690 ▶my $variant_of = sub {
Subroutine __ANON__: called CC=9 SCAR=22.5
157 5690 my ($text) = @_;
158 5690 my ($name, @pairs) = split / /, $text, -1;
159 5690 ▶ return undef unless defined $name && $name =~ /\A[a-z][a-z0-9_]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and $name =~ /\A[a-z][a-z0-9_]*\z/ 0 5690 5690
Condition33%1/3
defined $name and $name =~ /\A[a-z][a-z0-9_]*\z/
!ll&&!rl&&r
005690
MC/DC0%0/2
defined $name and $name =~ /\A[a-z][a-z0-9_]*\z/
defined $name $name =~ /\A[a-z][a-z0-9_]*\z/
Truth table33%1/3
A and B
A= defined $nameB= $name =~ /\A[a-z][a-z0-9_]*\z/
ABresult
0X0
100
111
160 5690 my (%variant, @names) = (name => $name);
161 5690 for my $pair (@pairs) {
162 34134 ▶ my ($field, $value) = $pair =~ /\A([a-z_0-9]+)=([a-z0-9]+)\z/ or return undef;
Branch: 1 branch
BranchTrueFalseTotal
() = $pair =~ /\A([a-z_0-9]+)=([a-z0-9]+)\z/ or return undef 34134 0 34134
163 34134 push @names, $field;
164 34134 ▶ return undef unless $FIELD_OK{$field} && $value =~ $FIELD_OK{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $FIELD_OK{$field} and $value =~ /$FIELD_OK{$field}/ 1 34133 34134
Condition66%2/3
$FIELD_OK{$field} and $value =~ /$FIELD_OK{$field}/
!ll&&!rl&&r
0134133
MC/DC50%1/2
$FIELD_OK{$field} and $value =~ /$FIELD_OK{$field}/
$FIELD_OK{$field} $value =~ /$FIELD_OK{$field}/
Truth table66%2/3
A and B
A= $FIELD_OK{$field}B= $value =~ /$FIELD_OK{$field}/
ABresult
0X0
100
111
165 34133 ▶ $variant{$field} = $value =~ /\A\d+\z/ ? $value + 0 : $value;
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\A\d+\z/ ? : 22755 11378 34133
166 }
167 5689 ▶ return undef unless "@names" eq "@FIELDS";
Branch: 1 branch
BranchTrueFalseTotal
unless join($", @names) eq join($", @FIELDS) 2 5687 5689
168 5687 return \%variant;
169 64 };
170  
171 7141 ▶sub format_record {
Subroutine format_record: called CC=8 SCAR=20.8
Pod documented
172 7141 my ($record) = @_;
173 my @lines = (
174 '[variant ' . $variant_text->($record->{variant}) . ']',
175 7141 '[first ' . $record->{first} . ']',
176 );
177 7141 ▶ push @lines, '[position ' . $record->{position} . ']' if defined $record->{position};
Branch: 1 branch
BranchTrueFalseTotal
if defined $record->{"position"} 47 7094 7141
178 7141 ▶ push @lines, '[seed ' . $record->{seed} . ']' if defined $record->{seed};
Branch: 1 branch
BranchTrueFalseTotal
if defined $record->{"seed"} 7099 42 7141
179 7141 my $n = 0;
180 7141 ▶ for my $ply (@{ $record->{plies} || [] }) {
Condition50%1/2
$record->{'plies'} || []
l!l
71410
MC/DC0%0/1
$record->{'plies'} || []
$record->{'plies'}
Truth table50%1/2
A
A= $record->{'plies'}
Aresult
11
00
181 712448 $n++;
182 push @lines, $n . '. ' . $LETTER_OF{ $ply->{side} } . ' ' . $ply->{roll}
183 . (defined $ply->{sticks} ? ' ' . $ply->{sticks} : '')
184 712448 ▶ . ': ' . (defined $ply->{move} ? $ply->{move} : '-');
Branch: 2 branches
BranchTrueFalseTotal
defined $ply->{'sticks'} ? : 712438 10 712448
defined $ply->{'move'} ? : 706423 6025 712448
185 }
186 push @lines, '[result ' . $record->{result}{winner} . ' ' . $record->{result}{how} . ']'
187 7141 ▶ if $record->{result};
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"result"} 914 6227 7141
188 7141 return join("\n", @lines) . "\n";
189 }
190  
191 5731 ▶sub parse_record {
Subroutine parse_record: called CC=36 SCAR=36.1
Pod documented
192 5731 my ($text) = @_;
193 5731 ▶ my $fail = sub { return (undef, { line => $_[0], error => $_[1] }) };
Subroutine __ANON__: called CC=1 SCAR=0.0
194 5731 ▶ return $fail->(0, 'empty') unless defined $text && length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 0 5731 5731
Condition33%1/3
defined $text and length $text
!ll&&!rl&&r
005731
MC/DC0%0/2
defined $text and length $text
defined $text length $text
Truth table33%1/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
195 5731 ▶ return $fail->(0, 'no_newline') unless $text =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\n\z/ 1 5730 5731
196  
197 5730 my @lines = split /\n/, $text, -1;
198 5730 pop @lines;
199 5730 my %record = (plies => []);
200 5730 my ($at, $done) = (0, 0);
201  
202 22789 ▶ my $header = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
203 22789 my ($name, $pattern) = @_;
204 22789 ▶ return undef unless $at < @lines && $lines[$at] =~ /\A\[\Q$name\E ($pattern)\]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $at < @lines and $lines[$at] =~ /\A\[\Q$name\E ($pattern)\]\z/ 5739 17050 22789
Condition100%3/3
$at < @lines and $lines[$at] =~ /\A\[\Q$name\E ($pattern)\]\z/
!ll&&!rl&&r
1573817050
MC/DC100%2/2
$at < @lines and $lines[$at] =~ /\A\[\Q$name\E ($pattern)\]\z/
$at < @lines $lines[$at] =~ /\A\[\Q$name\E ($pattern)\]\z/
Truth table100%3/3
A and B
A= $at < @linesB= $lines[$at] =~ /\A\[\Q$name\E ($pattern)\]\z/
ABresult
0X0
100
111
205 17050 $at++;
206 17050 return $1;
207 5730 };
208  
209 5730 my $variant = $header->('variant', '[a-z0-9_= ]+');
210 5730 ▶ return $fail->($at + 1, 'variant') unless defined $variant;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $variant 40 5690 5730
211 5690 $record{variant} = $variant_of->($variant);
212 5690 ▶ return $fail->($at, 'variant') unless $record{variant};
Branch: 1 branch
BranchTrueFalseTotal
unless $record{"variant"} 3 5687 5690
213  
214 5687 $record{first} = $header->('first', 'light|dark');
215 5687 ▶ return $fail->($at + 1, 'first') unless defined $record{first};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $record{"first"} 1 5686 5687
216  
217 5686 my $position = $header->('position', '[ld./ 1-7]+');
218 5686 ▶ if (defined $position) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $position) 1 5685 5686
219 1 ▶ return $fail->($at, 'position') if validate_position($position);
Branch: 1 branch
BranchTrueFalseTotal
if validate_position($position) 0 1 1
220 1 my ($side, $pieces) = $position =~ / ([ld]) (\d)\z/;
221 return $fail->($at, 'position')
222 1 ▶ unless $pieces == $record{variant}{pieces} && $side eq substr($record{first}, 0, 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $pieces == $record{"variant"}{"pieces"} and $side eq substr($record{"first"}, 0, 1) 0 1 1
Condition33%1/3
$pieces == $record{"variant"}{"pieces"} and $side eq substr($record{"first"}, 0, 1)
!ll&&!rl&&r
001
MC/DC0%0/2
$pieces == $record{"variant"}{"pieces"} and $side eq substr($record{"first"}, 0, 1)
$pieces == $record{"variant"}{"pieces"} $side eq substr($record{"first"}, 0, 1)
Truth table33%1/3
A and B
A= $pieces == $record{"variant"}{"pieces"}B= $side eq substr($record{"first"}, 0, 1)
ABresult
0X0
100
111
223 1 $record{position} = $position;
224 }
225  
226 5686 my $seed = $header->('seed', '(?:[0-9a-f]{2})+');
227 5686 ▶ $record{seed} = $seed if defined $seed;
Branch: 1 branch
BranchTrueFalseTotal
if defined $seed 5673 13 5686
228 5686 ▶ my $bytes = defined $seed ? pack('H*', $seed) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $seed ? : 5673 13 5686
229  
230 5686 my $expect = $record{first};
231 5686 my $bind = $record{variant}{throw_on_26};
232 5686 ▶ for (; $at < @lines; $at++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($at < @lines) 543417 5676 549093
233 543417 my $line = $lines[$at];
234 543417 my $number = $at + 1;
235 543417 ▶ return $fail->($number, 'after_result') if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 1 543416 543417
236  
237 543416 ▶ if ($line =~ /\A\[result ([a-z_]+ [a-z_]+)\]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A\[result ([a-z_]+ [a-z_]+)\]\z/) 517 542899 543416
238 517 ▶ return $fail->($number, 'result') unless $RESULT_OK{$1};
Branch: 1 branch
BranchTrueFalseTotal
unless $RESULT_OK{$1} 0 517 517
239 517 my ($winner, $how) = split / /, $1;
240 517 $record{result} = { winner => $winner, how => $how };
241 517 $done = 1;
242 517 next;
243 }
244  
245 542899 ▶ my ($n, $letter, $roll, $sticks, $move) =
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /\A([1-9][0-9]*)\. ([ld]) ([1-5])(?: ([01]{4}))?: (\S+)\z/ or return &$fail($number, "ply") 542899 0 542899
246 $line =~ /\A([1-9][0-9]*)\. ([ld]) ([1-5])(?: ([01]{4}))?: (\S+)\z/
247 or return $fail->($number, 'ply');
248 542899 my $throw = scalar @{ $record{plies} };
249 542899 ▶ return $fail->($number, 'number') unless $n == $throw + 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $n == $throw + 1 0 542899 542899
250  
251 542899 my $side = $SIDE_OF{$letter};
252 542899 ▶ return $fail->($number, 'side') unless $side eq $expect;
Branch: 1 branch
BranchTrueFalseTotal
unless $side eq $expect 4 542895 542899
253  
254 542895 ▶ if (defined $sticks) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sticks) 542876 19 542895
255 542876 my $light = ($sticks =~ tr/1//);
256 542876 ▶ return $fail->($number, 'sticks') unless Game::Senet::Sticks::roll_of($light) == $roll;
Branch: 1 branch
BranchTrueFalseTotal
unless Game::Senet::Sticks::roll_of($light) == $roll 1 542875 542876
257 }
258 542894 ▶ if (defined $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bytes) 542871 23 542894
259 542871 my $thrown = Game::Senet::Sticks::throw_for($bytes, $throw);
260 542871 ▶ return $fail->($number, 'roll')
Branch: 1 branch
BranchTrueFalseTotal
unless Game::Senet::Sticks::roll_of(Game::Senet::Sticks::light_of($thrown)) == $roll 3 542868 542871
261 unless Game::Senet::Sticks::roll_of(Game::Senet::Sticks::light_of($thrown)) == $roll;
262 542868 ▶ return $fail->($number, 'sticks') if defined $sticks && $sticks ne join('', @$thrown);
Branch: 1 branch
BranchTrueFalseTotal
if defined $sticks and $sticks ne join("", @$thrown) 0 542868 542868
Condition33%1/3
defined $sticks and $sticks ne join("", @$thrown)
!ll&&!rl&&r
05428680
MC/DC0%0/2
defined $sticks and $sticks ne join("", @$thrown)
defined $sticks $sticks ne join("", @$thrown)
Truth table33%1/3
A and B
A= defined $sticksB= $sticks ne join("", @$thrown)
ABresult
0X0
100
111
263 }
264  
265 542891 my %ply = (side => $side, roll => $roll + 0);
266 542891 ▶ $ply{sticks} = $sticks if defined $sticks;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sticks 542875 16 542891
267 542891 ▶ my $other = $side eq 'light' ? 'dark' : 'light';
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'light' ? : 274215 268676 542891
268 542891 my $again = Game::Senet::Sticks::extra_throw($roll);
269 542891 ▶ if ($move eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($move eq '-') { } 3811 539080 542891
270 3811 $ply{move} = undef;
271 }
272 else {
273 539080 my ($parsed) = parse_move($move);
274 539080 ▶ return $fail->($number, 'move') unless $parsed && $move eq format_move($parsed);
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed and $move eq format_move($parsed) 1 539079 539080
Condition66%2/3
$parsed and $move eq format_move($parsed)
!ll&&!rl&&r
10539079
MC/DC50%1/2
$parsed and $move eq format_move($parsed)
$parsed $move eq format_move($parsed)
Truth table66%2/3
A and B
A= $parsedB= $move eq format_move($parsed)
ABresult
0X0
100
111
275 539079 $ply{move} = $move;
276 539079 ▶ $again ||= $bind && $parsed->{to} ne 'off' && $parsed->{from} < 26 && $parsed->{to} == 26;
Condition100%12/12
$again ||= $bind && $parsed->{"to"} ne "off" && $parsed->{"from"} < 26 && $parsed->{"to"} == 26
l!l&&r!l&&!r
20514712181321751
$bind && $parsed->{'to'} ne 'off' && $parsed->{'from'} < 26 && $parsed->{'to'} == 26
!ll&&!rl&&r
18533113642012181
$bind and $parsed->{"to"} ne "off" and $parsed->{"from"} < 26
!ll&&!rl&&r
16986115470148601
$bind and $parsed->{"to"} ne "off"
!ll&&!rl&&r
1632816580164071
MC/DC0%0/5
$again ||= $bind && $parsed->{"to"} ne "off" && $parsed->{"from"} < 26 && $parsed->{"to"} == 26
$again $bind $parsed->{"to"} ne "off" $parsed->{"from"} < 26 $parsed->{'to'} == 26
Truth table0%0/6
A ||= B and C and D && E
A= $againB= $bindC= $parsed->{"to"} ne "off"D= $parsed->{"from"} < 26E= $parsed->{'to'} == 26
ABCDEresult
1XXXX1
011111
00XXX0
010XX0
0110X0
011100
277 }
278 542890 ▶ $expect = $again ? $side : $other;
Branch: 1 branch
BranchTrueFalseTotal
$again ? : 218556 324334 542890
279 542890 push @{ $record{plies} }, \%ply;
280 }
281 5676 return (\%record, undef);
282 }
283  
284 64 1;
285  
286 __END__
« blib/lib/Game/Senet/Terminal.pm ↑ Summary blib/lib/Game/Senet.pm »