blib/lib/Game/Fanorona/Notation.pm

filter: Statementstmt 100% 250 / 250 Branchbran 98.3% 179 / 182 Conditioncond 85.7% 90 / 105 MC/DCmcdc 77.2% 51 / 66 Subroutinesub 100% 26 / 26 Podpod 100% 13 / 13 totaltotal 94.8% 609 / 642 CC 143 SCAR 26.7
CC
143
Cov
97%
parse_move
27.0
parse_position
24.0
parse_game
22.1
CRAP 14.5
↑ Summary blib/lib/Game/Fanorona/Terminal.pm »
1 package Game::Fanorona::Notation;
2  
3 29 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 29 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 29 our $VERSION = '0.01';
10  
11 29 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 29 PAIKA => 0,
13 APPROACH => 1,
14 WITHDRAWAL => 2,
15 29 };
16  
17 29 ▶use constant SETUP => 'bbbbbbbbb/bbbbbbbbb/bwbw1bwbw/wwwwwwwww/wwwwwwwww w';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 29 ▶use constant CHAIN_MAX => 44;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 29 our @EXPORT_OK = qw(
22 SETUP
23 parse_point format_point square_of name_of
24 parse_move format_move move_points mirror_move
25 parse_position format_position mirror_position
26 parse_game format_game
27 );
28 29 our %EXPORT_TAGS = (all => \@EXPORT_OK);
29  
30 29 my %BOARD = (
31 sivy => [ 9, 5 ],
32 dimy => [ 5, 5 ],
33 telo => [ 3, 3 ],
34 );
35  
36 29 my %DIR = (
37 '0,1' => 0,
38 '1,1' => 1,
39 '1,0' => 2,
40 '1,-1' => 3,
41 '0,-1' => 4,
42 '-1,-1' => 5,
43 '-1,0' => 6,
44 '-1,1' => 7,
45 );
46  
47 29 my %LETTER = (APPROACH, 'A', WITHDRAWAL, 'W');
48 29 my %KIND = (A => APPROACH, W => WITHDRAWAL);
49  
50 29 my %HEADER = map { $_ => 1 } qw(variant start quiet_limit repeat ply_cap white black result);
51 29 my @HEADER = qw(variant start quiet_limit repeat ply_cap white black result);
52  
53 332913 ▶my $size_ok = sub {
Subroutine __ANON__: called CC=5 SCAR=17.2
54 332913 my ($cols, $rows) = @_;
55 332913 ▶ return 0 unless defined $cols && defined $rows;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cols and defined $rows 0 332913 332913
Condition33%1/3
defined $cols and defined $rows
!ll&&!rl&&r
00332913
MC/DC0%0/2
defined $cols and defined $rows
defined $cols defined $rows
Truth table33%1/3
A and B
A= defined $colsB= defined $rows
ABresult
0X0
100
111
56 332913 ▶ return 0 unless $cols =~ /\A[3579]\z/ && $rows =~ /\A[35]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cols =~ /\A[3579]\z/u and $rows =~ /\A[35]\z/u 4 332909 332913
Condition66%2/3
$cols =~ /\A[3579]\z/u and $rows =~ /\A[35]\z/u
!ll&&!rl&&r
40332909
MC/DC50%1/2
$cols =~ /\A[3579]\z/u and $rows =~ /\A[35]\z/u
$cols =~ /\A[3579]\z/u $rows =~ /\A[35]\z/u
Truth table66%2/3
A and B
A= $cols =~ /\A[3579]\z/uB= $rows =~ /\A[35]\z/u
ABresult
0X0
100
111
57 332909 return 1;
58 29 };
59  
60 316870 ▶my $size_of = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
61 316870 my (%arg) = @_;
62 316870 my ($cols, $rows) = ($arg{cols}, $arg{rows});
63 316870 ▶ ($cols, $rows) = (9, 5) unless defined $cols || defined $rows;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cols or defined $rows 150395 166475 316870
Condition66%2/3
defined $cols or defined $rows
l!l&&r!l&&!r
1664750150395
MC/DC50%1/2
defined $cols or defined $rows
defined $cols defined $rows
Truth table66%2/3
A or B
A= defined $colsB= defined $rows
ABresult
1X1
011
000
64 316870 ▶ return $size_ok->($cols, $rows) ? ($cols, $rows) : ();
Branch: 1 branch
BranchTrueFalseTotal
&$size_ok($cols, $rows) ? : 316868 2 316870
65 29 };
66  
67 3802 ▶my $fail = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
68 3802 my ($reason, @more) = @_;
69 3802 ▶ return wantarray ? (undef, $reason, @more) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 102 3700 3802
70 29 };
71  
72 271550 ▶sub parse_point {
Subroutine parse_point: called CC=6 SCAR=17.9
Pod documented
73 271550 my ($name, %arg) = @_;
74 271550 ▶ my ($cols, $rows) = $size_of->(%arg) or return;
Branch: 1 branch
BranchTrueFalseTotal
() = &$size_of(%arg) or return 271549 1 271550
75 271549 ▶ return unless defined $name && $name =~ /\A([a-i])([1-5])\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and $name =~ /\A([a-i])([1-5])\z/u 26 271523 271549
Condition100%3/3
defined $name and $name =~ /\A([a-i])([1-5])\z/u
!ll&&!rl&&r
125271523
MC/DC100%2/2
defined $name and $name =~ /\A([a-i])([1-5])\z/u
defined $name $name =~ /\A([a-i])([1-5])\z/u
Truth table100%3/3
A and B
A= defined $nameB= $name =~ /\A([a-i])([1-5])\z/u
ABresult
0X0
100
111
76 271523 my ($file, $rank) = (ord($1) - ord('a'), $2 - 1);
77 271523 ▶ return if $file >= $cols || $rank >= $rows;
Branch: 1 branch
BranchTrueFalseTotal
if $file >= $cols or $rank >= $rows 6 271517 271523
Condition100%3/3
$file >= $cols or $rank >= $rows
l!l&&r!l&&!r
51271517
MC/DC100%2/2
$file >= $cols or $rank >= $rows
$file >= $cols $rank >= $rows
Truth table100%3/3
A or B
A= $file >= $colsB= $rank >= $rows
ABresult
1X1
011
000
78 271517 return ($file, $rank);
79 }
80  
81 2955186 ▶sub format_point {
Subroutine format_point: called CC=5 SCAR=16.1
Pod documented
82 2955186 my ($file, $rank) = @_;
83 2955186 ▶ return undef unless defined $file && defined $rank;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and defined $rank 1 2955185 2955186
Condition66%2/3
defined $file and defined $rank
!ll&&!rl&&r
102955185
MC/DC50%1/2
defined $file and defined $rank
defined $file defined $rank
Truth table66%2/3
A and B
A= defined $fileB= defined $rank
ABresult
0X0
100
111
84 2955185 ▶ return undef unless $file =~ /\A[0-8]\z/ && $rank =~ /\A[0-4]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\A[0-8]\z/u and $rank =~ /\A[0-4]\z/u 6 2955179 2955185
Condition100%3/3
$file =~ /\A[0-8]\z/u and $rank =~ /\A[0-4]\z/u
!ll&&!rl&&r
512955179
MC/DC100%2/2
$file =~ /\A[0-8]\z/u and $rank =~ /\A[0-4]\z/u
$file =~ /\A[0-8]\z/u $rank =~ /\A[0-4]\z/u
Truth table100%3/3
A and B
A= $file =~ /\A[0-8]\z/uB= $rank =~ /\A[0-4]\z/u
ABresult
0X0
100
111
85 2955179 return chr(ord('a') + $file) . ($rank + 1);
86 }
87  
88 150315 ▶sub square_of {
Subroutine square_of: called CC=2 SCAR=6.9
Pod documented
89 150315 my ($name) = @_;
90 150315 ▶ my ($file, $rank) = parse_point($name) or return -1;
Branch: 1 branch
BranchTrueFalseTotal
() = parse_point($name) or return -1 150314 1 150315
91 150314 return ($rank + 1) * 11 + $file + 1;
92 }
93  
94 2045595 ▶sub name_of {
Subroutine name_of: called CC=3 SCAR=11.0
Pod documented
95 2045595 my ($square) = @_;
96 2045595 ▶ return undef unless defined $square && $square =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $square and $square =~ /\A[0-9]+\z/u 1 2045594 2045595
Condition66%2/3
defined $square and $square =~ /\A[0-9]+\z/u
!ll&&!rl&&r
012045594
MC/DC50%1/2
defined $square and $square =~ /\A[0-9]+\z/u
defined $square $square =~ /\A[0-9]+\z/u
Truth table66%2/3
A and B
A= defined $squareB= $square =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
97 2045594 return format_point(($square % 11) - 1, int($square / 11) - 1);
98 }
99  
100 45234 ▶sub parse_move {
Subroutine parse_move: called CC=27 SCAR=33.0
Pod documented
101 45234 my ($string, %arg) = @_;
102 45234 ▶ my ($cols, $rows) = $size_of->(%arg) or return $fail->('bad_size');
Branch: 1 branch
BranchTrueFalseTotal
() = &$size_of(%arg) or return &$fail("bad_size") 45233 1 45234
103 45233 ▶ return $fail->('empty') unless defined $string && length $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string and length $string 4 45229 45233
Condition100%3/3
defined $string and length $string
!ll&&!rl&&r
2245229
MC/DC100%2/2
defined $string and length $string
defined $string length $string
Truth table100%3/3
A and B
A= defined $stringB= length $string
ABresult
0X0
100
111
104 45229 ▶ return $fail->('bad_shape')
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /[^A-Za-z0-9-]/u or $string =~ /\A-|-\z|--/u or not $string =~ /-/u 11 45218 45229
Condition100%6/6
$string =~ /[^A-Za-z0-9-]/u or $string =~ /\A-|-\z|--/u or not $string =~ /-/u
l!l&&r!l&&!r
6545218
$string =~ /[^A-Za-z0-9-]/u or $string =~ /\A-|-\z|--/u
l!l&&r!l&&!r
3345223
MC/DC100%3/3
$string =~ /[^A-Za-z0-9-]/u or $string =~ /\A-|-\z|--/u or not $string =~ /-/u
$string =~ /[^A-Za-z0-9-]/u $string =~ /\A-|-\z|--/u not $string =~ /-/u
Truth table100%4/4
A or B or not(C)
A= $string =~ /[^A-Za-z0-9-]/uB= $string =~ /\A-|-\z|--/uC= not $string =~ /-/u
ABCresult
1XX1
01X1
0011
0000
105 if $string =~ /[^A-Za-z0-9-]/ || $string =~ /\A-|-\z|--/ || $string !~ /-/;
106  
107 45218 my ($first, @rest) = split /-/, $string;
108 45218 ▶ return $fail->('kind_on_start') if $first =~ /\A[a-z][0-9][A-Za-z]\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $first =~ /\A[a-z][0-9][A-Za-z]\z/u 1 45217 45218
109 45217 ▶ return $fail->('bad_point') unless $first =~ /\A[a-z][0-9]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $first =~ /\A[a-z][0-9]\z/u 2 45215 45217
110 45215 ▶ return $fail->('too_long') if @rest > CHAIN_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if @rest > 44 2 45213 45215
111  
112 45213 my @tokens;
113 45213 for my $token (@rest) {
114 75089 ▶ return $fail->('bad_point') unless $token =~ /\A([a-z][0-9])(.*)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $token =~ /\A([a-z][0-9])(.*)\z/u 3 75086 75089
115 75086 my ($point, $letter) = ($1, $2);
116 75086 ▶ return $fail->('bad_kind') unless $letter eq '' || exists $KIND{$letter};
Branch: 1 branch
BranchTrueFalseTotal
unless $letter eq "" or exists $KIND{$letter} 5 75081 75086
Condition100%3/3
$letter eq "" or exists $KIND{$letter}
l!l&&r!l&&!r
18422566595
MC/DC100%2/2
$letter eq "" or exists $KIND{$letter}
$letter eq "" exists $KIND{$letter}
Truth table100%3/3
A or B
A= $letter eq ""B= exists $KIND{$letter}
ABresult
1X1
011
000
117 75081 push @tokens, [ $point, $letter ];
118 }
119  
120 45205 ▶ my @at = parse_point($first, cols => $cols, rows => $rows) or return $fail->('off_board');
Branch: 1 branch
BranchTrueFalseTotal
my(@at) = parse_point($first, "cols", $cols, "rows", $rows) or return &$fail("off_board") 45203 2 45205
121 45203 my @steps;
122 45203 my $from_name = $first;
123 45203 for my $token (@tokens) {
124 75079 my ($point, $letter) = @$token;
125 75079 ▶ my @to = parse_point($point, cols => $cols, rows => $rows) or return $fail->('off_board');
Branch: 1 branch
BranchTrueFalseTotal
my(@to) = parse_point($point, "cols", $cols, "rows", $rows) or return &$fail("off_board") 75072 7 75079
126 75072 my ($df, $dr) = ($to[0] - $at[0], $to[1] - $at[1]);
127 75072 ▶ return $fail->('same_point') if !$df && !$dr;
Branch: 1 branch
BranchTrueFalseTotal
unless $df or $dr 1 75071 75072
Condition100%3/3
$df or $dr
l!l&&r!l&&!r
50160249111
MC/DC100%2/2
$df or $dr
$df $dr
Truth table100%3/3
A or B
A= $dfB= $dr
ABresult
1X1
011
000
128 75071 ▶ return $fail->('not_adjacent') if abs($df) > 1 || abs($dr) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if abs $df > 1 or abs $dr > 1 12 75059 75071
Condition100%3/3
abs $df > 1 or abs $dr > 1
l!l&&r!l&&!r
11175059
MC/DC100%2/2
abs $df > 1 or abs $dr > 1
abs $df > 1 abs $dr > 1
Truth table100%3/3
A or B
A= abs $df > 1B= abs $dr > 1
ABresult
1X1
011
000
129 75059 ▶ return $fail->('no_line') if $df && $dr && ($at[0] + $at[1]) % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $df and $dr and ($at[0] + $at[1]) % 2 3 75056 75059
Condition100%6/6
$df and $dr and ($at[0] + $at[1]) % 2
!ll&&!rl&&r
48462265943
$df and $dr
!ll&&!rl&&r
249072355526597
MC/DC100%3/3
$df and $dr and ($at[0] + $at[1]) % 2
$df $dr ($at[0] + $at[1]) % 2
Truth table100%4/4
A and B and C
A= $dfB= $drC= ($at[0] + $at[1]) % 2
ABCresult
0XX0
10X0
1100
1111
130 push @steps, {
131 from => square_of($from_name),
132 to => square_of($point),
133 dir => $DIR{"$df,$dr"},
134 75056 ▶ kind => $letter eq '' ? PAIKA : $KIND{$letter},
Branch: 1 branch
BranchTrueFalseTotal
$letter eq '' ? : 18401 56655 75056
135 };
136 75056 @at = @to;
137 75056 $from_name = $point;
138 }
139 45180 ▶ return $fail->('paika_in_chain') if @steps > 1 && grep { $_->{kind} == PAIKA } @steps;
Branch: 1 branch
BranchTrueFalseTotal
if @steps > 1 and grep {$_->{"kind"} == 0;} @steps 8 45172 45180
Condition100%3/3
@steps > 1 and grep {$_->{"kind"} == 0;} @steps
!ll&&!rl&&r
30379147938
MC/DC100%2/2
@steps > 1 and grep {$_->{"kind"} == 0;} @steps
@steps > 1 grep {$_->{"kind"} == 0;} @steps
Truth table100%3/3
A and B
A= @steps > 1B= grep {$_->{"kind"} == 0;} @steps
ABresult
0X0
100
111
140  
141 return {
142 from => $steps[0]{from},
143 to => $steps[-1]{to},
144 steps => \@steps,
145 45172 points => [ $first, map { $_->[0] } @tokens ],
146 };
147 }
148  
149 958593 ▶sub format_move {
Subroutine format_move: called CC=12 SCAR=24.9
Pod documented
150 958593 my ($move) = @_;
151 958593 ▶ return undef unless ref $move;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $move 2 958591 958593
152 my ($from, $steps) = ref $move eq 'HASH'
153 ? ($move->{from}, $move->{steps})
154 958591 ▶ : ($move->from, $move->steps);
Branch: 1 branch
BranchTrueFalseTotal
ref $move eq 'HASH' ? : 15531 943060 958591
155 958591 ▶ return undef unless ref $steps eq 'ARRAY' && @$steps;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $steps eq "ARRAY" and @$steps 1 958590 958591
Condition66%2/3
ref $steps eq "ARRAY" and @$steps
!ll&&!rl&&r
01958590
MC/DC50%1/2
ref $steps eq "ARRAY" and @$steps
ref $steps eq "ARRAY" @$steps
Truth table66%2/3
A and B
A= ref $steps eq "ARRAY"B= @$steps
ABresult
0X0
100
111
156 958590 ▶ my $text = name_of($from) // return undef;
Condition100%2/2
name_of($from) // (return undef)
l!l
9585891
MC/DC100%1/1
name_of($from) // (return undef)
name_of($from)
Truth table100%2/2
A
A= name_of($from)
Aresult
11
00
157 958589 for my $s (@$steps) {
158 995008 ▶ my $to = name_of($s->{to}) // return undef;
Condition50%1/2
name_of($s->{"to"}) // (return undef)
l!l
9950080
MC/DC0%0/1
name_of($s->{"to"}) // (return undef)
name_of($s->{"to"})
Truth table50%1/2
A
A= name_of($s->{"to"})
Aresult
11
00
159 995008 ▶ my $kind = $s->{kind} // return undef;
Condition50%1/2
$s->{"kind"} // (return undef)
l!l
9950080
MC/DC0%0/1
$s->{"kind"} // (return undef)
$s->{"kind"}
Truth table50%1/2
A
A= $s->{"kind"}
Aresult
11
00
160 995008 ▶ return undef unless $kind == PAIKA || exists $LETTER{$kind};
Branch: 1 branch
BranchTrueFalseTotal
unless $kind == 0 or exists $LETTER{$kind} 1 995007 995008
Condition100%3/3
$kind == 0 or exists $LETTER{$kind}
l!l&&r!l&&!r
8701971248101
MC/DC100%2/2
$kind == 0 or exists $LETTER{$kind}
$kind == 0 exists $LETTER{$kind}
Truth table100%3/3
A or B
A= $kind == 0B= exists $LETTER{$kind}
ABresult
1X1
011
000
161 995007 ▶ $text .= "-$to" . ($kind == PAIKA ? '' : $LETTER{$kind});
Branch: 1 branch
BranchTrueFalseTotal
$kind == 0 ? : 870197 124810 995007
162 }
163 958588 return $text;
164 }
165  
166 88 ▶sub move_points {
Subroutine move_points: called CC=2 SCAR=6.9
Pod documented
167 88 my ($string, %arg) = @_;
168 88 ▶ my $move = parse_move($string, %arg) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $move = parse_move($string, %arg) or return 87 1 88
169 87 return @{ $move->{points} };
170 }
171  
172 832 ▶my $mirror = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
173 832 my ($how, $cols, $rows, $file, $rank) = @_;
174 832 ▶ return ($cols - 1 - $file, $rank) if $how eq 'files';
Branch: 1 branch
BranchTrueFalseTotal
if $how eq "files" 330 502 832
175 502 ▶ return ($file, $rows - 1 - $rank) if $how eq 'ranks';
Branch: 1 branch
BranchTrueFalseTotal
if $how eq "ranks" 250 252 502
176 252 ▶ return ($cols - 1 - $file, $rows - 1 - $rank) if $how eq 'turn';
Branch: 1 branch
BranchTrueFalseTotal
if $how eq "turn" 250 2 252
177 2 return;
178 29 };
179  
180 86 ▶sub mirror_move {
Subroutine mirror_move: called CC=8 SCAR=20.8
Pod documented
181 86 my ($string, $how, %arg) = @_;
182 86 ▶ my ($cols, $rows) = $size_of->(%arg) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
() = &$size_of(%arg) or return undef 86 0 86
183 86 ▶ my $move = parse_move($string, cols => $cols, rows => $rows) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $move = parse_move($string, "cols", $cols, "rows", $rows) or return undef 85 1 86
184 85 my @names;
185 85 for my $name (@{ $move->{points} }) {
186 232 ▶ my @to = $mirror->($how // '', $cols, $rows, parse_point($name, cols => $cols, rows => $rows))
Branch: 1 branch
BranchTrueFalseTotal
my(@to) = &$mirror($how // "", $cols, $rows, parse_point($name, "cols", $cols, "rows", $rows)) or return undef 231 1 232
Condition50%1/2
$how // ""
l!l
2320
MC/DC0%0/1
$how // ""
$how
Truth table50%1/2
A
A= $how
Aresult
11
00
187 or return undef;
188 231 push @names, format_point(@to);
189 }
190 84 my $text = shift @names;
191 84 for my $s (@{ $move->{steps} }) {
192 147 ▶ $text .= '-' . shift(@names) . ($s->{kind} == PAIKA ? '' : $LETTER{ $s->{kind} });
Branch: 1 branch
BranchTrueFalseTotal
$s->{'kind'} == 0 ? : 1 146 147
193 }
194 84 return $text;
195 }
196  
197 15784 ▶my $write_board = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
198 15784 my ($cols, $rows, $stones, $side) = @_;
199 15784 my @ranks;
200 15784 for my $rank (reverse 0 .. $rows - 1) {
201 78102 my ($out, $run) = ('', 0);
202 78102 for my $file (0 .. $cols - 1) {
203 696236 my $stone = $stones->{ format_point($file, $rank) };
204 696236 ▶ if (!defined $stone) { $run++; next }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $stone) 483117 213119 696236
205 213119 ▶ $out .= $run if $run;
Branch: 1 branch
BranchTrueFalseTotal
if $run 83332 129787 213119
206 213119 $run = 0;
207 213119 $out .= $stone;
208 }
209 78102 ▶ $out .= $run if $run;
Branch: 1 branch
BranchTrueFalseTotal
if $run 49670 28432 78102
210 78102 push @ranks, $out;
211 }
212 15784 return join('/', @ranks) . " $side";
213 29 };
214  
215 19469 ▶sub parse_position {
Subroutine parse_position: called CC=24 SCAR=31.8
Pod documented
216 19469 my ($string) = @_;
217 19469 ▶ return $fail->('empty') unless defined $string && length $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string and length $string 2 19467 19469
Condition100%3/3
defined $string and length $string
!ll&&!rl&&r
1119467
MC/DC100%2/2
defined $string and length $string
defined $string length $string
Truth table100%3/3
A and B
A= defined $stringB= length $string
ABresult
0X0
100
111
218 19467 ▶ return $fail->('too_long') if length $string > 80;
Branch: 1 branch
BranchTrueFalseTotal
if length $string > 80 1 19466 19467
219 19466 my @field = split / /, $string, -1;
220 19466 ▶ return $fail->('bad_shape') unless @field == 2 || @field == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless @field == 2 or @field == 4 579 18887 19466
Condition100%3/3
@field == 2 or @field == 4
l!l&&r!l&&!r
142274660579
MC/DC100%2/2
@field == 2 or @field == 4
@field == 2 @field == 4
Truth table100%3/3
A or B
A= @field == 2B= @field == 4
ABresult
1X1
011
000
221 18887 my ($board, $side, $quiet, $number) = @field;
222  
223 18887 ▶ return $fail->('bad_letter') if $board =~ m{[^wb1-9/]};
Branch: 1 branch
BranchTrueFalseTotal
if $board =~ m[[^wb1-9/]]u 1118 17769 18887
224 17769 my @ranks = split m{/}, $board, -1;
225 17769 ▶ return $fail->('rows') unless @ranks == 3 || @ranks == 5;
Branch: 1 branch
BranchTrueFalseTotal
unless @ranks == 3 or @ranks == 5 479 17290 17769
Condition100%3/3
@ranks == 3 or @ranks == 5
l!l&&r!l&&!r
109516195479
MC/DC100%2/2
@ranks == 3 or @ranks == 5
@ranks == 3 @ranks == 5
Truth table100%3/3
A or B
A= @ranks == 3B= @ranks == 5
ABresult
1X1
011
000
226 17290 ▶ my @width = map { my $n = 0; $n += /[1-9]/ ? $_ : 1 for split //, $_; $n } @ranks;
Branch: 1 branch
BranchTrueFalseTotal
/[1-9]/u ? : 138666 235240 373906
227 17290 my $cols = $width[0];
228 17290 ▶ return $fail->('width') if grep { $_ != $cols } @width;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ != $cols;} @width 1249 16041 17290
229 16041 ▶ return $fail->('width') unless $size_ok->($cols, scalar @ranks);
Branch: 1 branch
BranchTrueFalseTotal
unless &$size_ok($cols, scalar @ranks) 1 16040 16041
230 16040 ▶ return $fail->('side') unless $side eq 'w' || $side eq 'b';
Branch: 1 branch
BranchTrueFalseTotal
unless $side eq "w" or $side eq "b" 285 15755 16040
Condition100%3/3
$side eq "w" or $side eq "b"
l!l&&r!l&&!r
83307425285
MC/DC100%2/2
$side eq "w" or $side eq "b"
$side eq "w" $side eq "b"
Truth table100%3/3
A or B
A= $side eq "w"B= $side eq "b"
ABresult
1X1
011
000
231  
232 15755 ▶ my $counted = @field == 4 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@field == 4 ? : 4647 11108 15755
233 15755 ▶ if ($counted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($counted) 4647 11108 15755
234 4647 ▶ return $fail->('bad_count') unless $quiet =~ /\A(?:0|[1-9][0-9]{0,5})\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $quiet =~ /\A(?:0|[1-9][0-9]{0,5})\z/u 3 4644 4647
235 4644 ▶ return $fail->('bad_count') unless $number =~ /\A[1-9][0-9]{0,5}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $number =~ /\A[1-9][0-9]{0,5}\z/u 2 4642 4644
236 }
237  
238 15750 my $rows = @ranks;
239 15750 my %stones;
240 15750 for my $i (0 .. $#ranks) {
241 77938 my $rank = $rows - 1 - $i;
242 77938 my $file = 0;
243 77938 for my $c (split //, $ranks[$i]) {
244 345335 ▶ if ($c =~ /[1-9]/) { $file += $c; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /[1-9]/u) 132815 212520 345335
245 212520 $stones{ format_point($file, $rank) } = $c;
246 212520 $file++;
247 }
248 }
249 return {
250 15750 ▶ cols => $cols,
Branch: 2 branches
BranchTrueFalseTotal
$counted ? : 4642 11108 15750
$counted ? : 4642 11108 15750
251 rows => $rows,
252 side => $side,
253 stones => \%stones,
254 board => $write_board->($cols, $rows, \%stones, $side),
255 counted => $counted,
256 quiet => $counted ? $quiet + 0 : 0,
257 move => $counted ? $number + 0 : 1,
258 };
259 }
260  
261 4649 ▶sub format_position {
Subroutine format_position: called CC=7 SCAR=19.5
Pod documented
262 4649 my ($board, $quiet, $number) = @_;
263 4649 ▶ $quiet = 0 unless defined $quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $quiet 3 4646 4649
264 4649 ▶ $number = 1 unless defined $number;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $number 3 4646 4649
265 4649 ▶ my $position = parse_position($board) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $position = parse_position($board) or return undef 4648 1 4649
266 4648 ▶ return undef if $position->{counted};
Branch: 1 branch
BranchTrueFalseTotal
if $position->{"counted"} 1 4647 4648
267 4647 ▶ return undef unless $quiet =~ /\A(?:0|[1-9][0-9]{0,5})\z/ && $number =~ /\A[1-9][0-9]{0,5}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $quiet =~ /\A(?:0|[1-9][0-9]{0,5})\z/u and $number =~ /\A[1-9][0-9]{0,5}\z/u 2 4645 4647
Condition100%3/3
$quiet =~ /\A(?:0|[1-9][0-9]{0,5})\z/u and $number =~ /\A[1-9][0-9]{0,5}\z/u
!ll&&!rl&&r
114645
MC/DC100%2/2
$quiet =~ /\A(?:0|[1-9][0-9]{0,5})\z/u and $number =~ /\A[1-9][0-9]{0,5}\z/u
$quiet =~ /\A(?:0|[1-9][0-9]{0,5})\z/u $number =~ /\A[1-9][0-9]{0,5}\z/u
Truth table100%3/3
A and B
A= $quiet =~ /\A(?:0|[1-9][0-9]{0,5})\z/uB= $number =~ /\A[1-9][0-9]{0,5}\z/u
ABresult
0X0
100
111
268 4645 return "$position->{board} $quiet $number";
269 }
270  
271 36 ▶sub mirror_position {
Subroutine mirror_position: called CC=6 SCAR=17.9
Pod documented
272 36 my ($string, $how) = @_;
273 36 ▶ my $position = parse_position($string) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $position = parse_position($string) or return undef 35 1 36
274 35 my ($cols, $rows) = @{$position}{qw(cols rows)};
275 35 my %stones;
276 35 for my $name (keys %{ $position->{stones} }) {
277 600 ▶ my @to = $mirror->($how // '', $cols, $rows, parse_point($name, cols => $cols, rows => $rows))
Branch: 1 branch
BranchTrueFalseTotal
my(@to) = &$mirror($how // "", $cols, $rows, parse_point($name, "cols", $cols, "rows", $rows)) or return undef 599 1 600
Condition50%1/2
$how // ""
l!l
6000
MC/DC0%0/1
$how // ""
$how
Truth table50%1/2
A
A= $how
Aresult
11
00
278 or return undef;
279 599 $stones{ format_point(@to) } = $position->{stones}{$name};
280 }
281 34 my $text = $write_board->($cols, $rows, \%stones, $position->{side});
282 34 ▶ return $position->{counted} ? "$text $position->{quiet} $position->{move}" : $text;
Branch: 1 branch
BranchTrueFalseTotal
$position->{'counted'} ? : 1 33 34
283 }
284  
285 144 ▶my $variant_size = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
286 144 my ($variant) = @_;
287 144 ▶ return @{ $BOARD{$variant} } if exists $BOARD{$variant};
Branch: 1 branch
BranchTrueFalseTotal
if exists $BOARD{$variant} 141 3 144
288 3 ▶ return ($1, $2) if $variant =~ /\A([0-9])x([0-9])\z/ && $size_ok->($1, $2);
Branch: 1 branch
BranchTrueFalseTotal
if $variant =~ /\A([0-9])x([0-9])\z/u and &$size_ok($1, $2) 1 2 3
Condition100%3/3
$variant =~ /\A([0-9])x([0-9])\z/u and &$size_ok($1, $2)
!ll&&!rl&&r
111
MC/DC100%2/2
$variant =~ /\A([0-9])x([0-9])\z/u and &$size_ok($1, $2)
$variant =~ /\A([0-9])x([0-9])\z/u &$size_ok($1, $2)
Truth table100%3/3
A and B
A= $variant =~ /\A([0-9])x([0-9])\z/uB= &$size_ok($1, $2)
ABresult
0X0
100
111
289 2 return;
290 29 };
291  
292 74 ▶sub format_game {
Subroutine format_game: called CC=12 SCAR=24.9
Pod documented
293 74 my ($game) = @_;
294 74 ▶ return undef unless ref $game eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $game eq "HASH" 1 73 74
295 73 my %header = (variant => 'sivy', map { $_ => $game->{$_} } grep { defined $game->{$_} } @HEADER);
296 73 ▶ return undef if grep { !$HEADER{$_} && $_ ne 'moves' } keys %$game;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $HEADER{$_} and $_ ne "moves";} keys %$game 1 72 73
Condition100%3/3
!$HEADER{$_} && $_ ne 'moves'
!ll&&!rl&&r
196731
MC/DC100%2/2
!$HEADER{$_} && $_ ne 'moves'
!$HEADER{$_} $_ ne 'moves'
Truth table100%3/3
not(A) && B
A= !$HEADER{$_}B= $_ ne 'moves'
ABresult
0X0
100
111
297 72 ▶ my $moves = $game->{moves} // [];
Condition50%1/2
$game->{"moves"} // []
l!l
720
MC/DC0%0/1
$game->{"moves"} // []
$game->{"moves"}
Truth table50%1/2
A
A= $game->{"moves"}
Aresult
11
00
298 72 ▶ return undef unless ref $moves eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $moves eq "ARRAY" 1 71 72
299 71 my $text = "fanorona 1\n";
300 71 for my $key (@HEADER) {
301 562 ▶ next unless defined $header{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header{$key} 359 203 562
302 203 ▶ return undef if $header{$key} =~ /[^\x20-\x7e]/ || $header{$key} !~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $header{$key} =~ /[^\x20-\x7e]/u or not $header{$key} =~ /\S/u 3 200 203
Condition100%3/3
$header{$key} =~ /[^\x20-\x7e]/u or not $header{$key} =~ /\S/u
l!l&&r!l&&!r
21200
MC/DC100%2/2
$header{$key} =~ /[^\x20-\x7e]/u or not $header{$key} =~ /\S/u
$header{$key} =~ /[^\x20-\x7e]/u not $header{$key} =~ /\S/u
Truth table100%3/3
A or not(B)
A= $header{$key} =~ /[^\x20-\x7e]/uB= not $header{$key} =~ /\S/u
ABresult
1X1
011
000
303 200 $text .= "$key $header{$key}\n";
304 }
305 68 $text .= "moves\n";
306 68 $text .= "$_\n" for @$moves;
307 68 ▶ return undef unless parse_game($text);
Branch: 1 branch
BranchTrueFalseTotal
unless parse_game($text) 1 67 68
308 67 return $text;
309 }
310  
311 156 ▶sub parse_game {
Subroutine parse_game: called CC=22 SCAR=30.9
Pod documented
312 156 my ($text) = @_;
313 156 ▶ return $fail->('empty', 0) unless defined $text && length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 1 155 156
Condition66%2/3
defined $text and length $text
!ll&&!rl&&r
10155
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
314 155 my @lines = split /\n/, $text;
315 155 ▶ return $fail->('not_a_game', 1) unless @lines && $lines[0] eq 'fanorona 1';
Branch: 1 branch
BranchTrueFalseTotal
unless @lines and $lines[0] eq "fanorona 1" 5 150 155
Condition66%2/3
@lines and $lines[0] eq "fanorona 1"
!ll&&!rl&&r
05150
MC/DC50%1/2
@lines and $lines[0] eq "fanorona 1"
@lines $lines[0] eq "fanorona 1"
Truth table66%2/3
A and B
A= @linesB= $lines[0] eq "fanorona 1"
ABresult
0X0
100
111
316  
317 150 my (%game, $n);
318 150 ▶ for ($n = 1; $n < @lines; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @lines) 550 1 551
319 550 my $line = $lines[$n];
320 550 ▶ last if $line eq 'moves';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "moves" 144 406 550
321 406 ▶ return $fail->('bad_header', $n + 1) unless $line =~ /\A([a-z_]+) (\S.*)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\A([a-z_]+) (\S.*)\z/u 2 404 406
322 404 my ($key, $value) = ($1, $2);
323 404 ▶ return $fail->('unknown_header', $n + 1) unless $HEADER{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $HEADER{$key} 1 403 404
324 403 ▶ return $fail->('repeated_header', $n + 1) if exists $game{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $game{$key} 1 402 403
325 402 ▶ return $fail->('bad_header', $n + 1) if $value =~ /[^\x20-\x7e]/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /[^\x20-\x7e]/u 0 402 402
326 402 ▶ return $fail->('bad_number', $n + 1)
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /\A(?:quiet_limit|repeat|ply_cap)\z/u and not $value =~ /\A(?:0|[1-9][0-9]{0,5})\z/u 1 401 402
Condition100%3/3
$key =~ /\A(?:quiet_limit|repeat|ply_cap)\z/u and not $value =~ /\A(?:0|[1-9][0-9]{0,5})\z/u
!ll&&!rl&&r
39651
MC/DC100%2/2
$key =~ /\A(?:quiet_limit|repeat|ply_cap)\z/u and not $value =~ /\A(?:0|[1-9][0-9]{0,5})\z/u
$key =~ /\A(?:quiet_limit|repeat|ply_cap)\z/u not $value =~ /\A(?:0|[1-9][0-9]{0,5})\z/u
Truth table100%3/3
A and not(B)
A= $key =~ /\A(?:quiet_limit|repeat|ply_cap)\z/uB= not $value =~ /\A(?:0|[1-9][0-9]{0,5})\z/u
ABresult
0X0
100
111
327 if $key =~ /\A(?:quiet_limit|repeat|ply_cap)\z/ && $value !~ /\A(?:0|[1-9][0-9]{0,5})\z/;
328 401 $game{$key} = $value;
329 }
330 145 ▶ return $fail->('no_moves_line', scalar @lines) unless $n < @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless $n < @lines 1 144 145
331 144 my $moves_at = $n;
332  
333 144 ▶ $game{variant} = 'sivy' unless defined $game{variant};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $game{"variant"} 4 140 144
334 my ($cols, $rows) = $variant_size->($game{variant})
335 144 ▶ or return $fail->('bad_variant', 2);
Branch: 1 branch
BranchTrueFalseTotal
() = &$variant_size($game{"variant"}) or return &$fail("bad_variant", 2) 142 2 144
336 142 ▶ if (defined $game{start}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $game{"start"}) 5 137 142
337 5 ▶ my $start = parse_position($game{start}) or return $fail->('bad_start', 2);
Branch: 1 branch
BranchTrueFalseTotal
my $start = parse_position($game{"start"}) or return &$fail("bad_start", 2) 4 1 5
338 4 ▶ return $fail->('bad_start', 2) unless $start->{cols} == $cols && $start->{rows} == $rows;
Branch: 1 branch
BranchTrueFalseTotal
unless $start->{"cols"} == $cols and $start->{"rows"} == $rows 1 3 4
Condition66%2/3
$start->{"cols"} == $cols and $start->{"rows"} == $rows
!ll&&!rl&&r
103
MC/DC50%1/2
$start->{"cols"} == $cols and $start->{"rows"} == $rows
$start->{"cols"} == $cols $start->{"rows"} == $rows
Truth table66%2/3
A and B
A= $start->{"cols"} == $colsB= $start->{"rows"} == $rows
ABresult
0X0
100
111
339 }
340  
341 140 my @moves;
342 140 for my $i ($moves_at + 1 .. $#lines) {
343 4974 my $move = $lines[$i];
344 4974 my ($parsed, $reason) = parse_move($move, cols => $cols, rows => $rows);
345 4974 ▶ return $fail->($reason, $i + 1) unless $parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed 5 4969 4974
346 4969 push @moves, $move;
347 }
348 135 $game{moves} = \@moves;
349 135 $game{moves_at} = $moves_at + 2;
350 135 $game{cols} = $cols;
351 135 $game{rows} = $rows;
352 135 return \%game;
353 }
354  
355 29 1;
356  
357 __END__
↑ Summary blib/lib/Game/Fanorona/Terminal.pm »