blib/lib/Game/Brandubh/Notation.pm

filter: Statementstmt 80.0% 120 / 150 Branchbran 41.5% 49 / 118 Conditioncond 24.5% 14 / 57 MC/DCmcdc 2.7% 1 / 36 Subroutinesub 94.1% 16 / 17 Podpod 100% 10 / 10 totaltotal 54.1% 210 / 388 CC 87 SCAR 41.4
CC
87
Cov
56%
game_parse
141.9
game_string
90.7
_result_line
72.0
CRAP 62.8
↑ Summary blib/lib/Game/Brandubh/Bot.pm »
1 package Game::Brandubh::Notation;
2  
3 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our $VERSION = '0.03';
10  
11 3 our @EXPORT_OK = qw(
12 SETUP
13 square_name square_parse
14 move_wire move_split move_parse move_display
15 position_ok position_cells
16 game_string game_parse
17 );
18 3 our %EXPORT_TAGS = (all => \@EXPORT_OK);
19  
20 3 ▶use constant SETUP => '3a3/3a3/3d3/aadkdaa/3d3/3a3/3a3 a';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 3 my $SQUARE = qr/[a-g][1-7]/;
23  
24 18 ▶sub _plain {
Subroutine _plain: called CC=3 SCAR=11.0
25 18 my ($text) = @_;
26 18 ▶ return undef if !defined $text || ref $text;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $text or ref $text 4 14 18
Condition66%2/3
not defined $text or ref $text
l!l&&r!l&&!r
4014
MC/DC50%1/2
not defined $text or ref $text
not defined $text ref $text
Truth table66%2/3
not(A) or B
A= not defined $textB= ref $text
ABresult
1X1
011
000
27 14 return "$text";
28 }
29  
30 1 ▶sub square_name {
Subroutine square_name: called CC=5 SCAR=19.4
Pod documented
31 1 my ($file, $rank) = @_;
32 1 ▶ return undef unless defined $file && defined $rank;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and defined $rank 0 1 1
Condition33%1/3
defined $file and defined $rank
!ll&&!rl&&r
001
MC/DC0%0/2
defined $file and defined $rank
defined $file defined $rank
Truth table33%1/3
A and B
A= defined $fileB= defined $rank
ABresult
0X0
100
111
33 1 ▶ return undef unless $file =~ /\A[0-6]\z/ && $rank =~ /\A[0-6]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\A[0-6]\z/ and $rank =~ /\A[0-6]\z/ 0 1 1
Condition33%1/3
$file =~ /\A[0-6]\z/ and $rank =~ /\A[0-6]\z/
!ll&&!rl&&r
001
MC/DC0%0/2
$file =~ /\A[0-6]\z/ and $rank =~ /\A[0-6]\z/
$file =~ /\A[0-6]\z/ $rank =~ /\A[0-6]\z/
Truth table33%1/3
A and B
A= $file =~ /\A[0-6]\z/B= $rank =~ /\A[0-6]\z/
ABresult
0X0
100
111
34 1 return chr(ord('a') + $file) . ($rank + 1);
35 }
36  
37 1 ▶sub square_parse {
Subroutine square_parse: called CC=3 SCAR=11.4
Pod documented
38 1 my $text = _plain($_[0]);
39 1 ▶ return unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 1 1
40 1 ▶ my ($f, $r) = lc($text) =~ /\A([a-g])([1-7])\z/ or return;
Branch: 1 branch
BranchTrueFalseTotal
() = lc($text) =~ /\A([a-g])([1-7])\z/ or return 1 0 1
41 1 return (ord($f) - ord('a'), $r - 1);
42 }
43  
44 1 ▶sub move_wire {
Subroutine move_wire: called CC=5 SCAR=18.4
Pod documented
45 1 my $from = _plain($_[0]);
46 1 my $to = _plain($_[1]);
47 1 ▶ return undef unless defined $from && defined $to;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from and defined $to 0 1 1
Condition33%1/3
defined $from and defined $to
!ll&&!rl&&r
001
MC/DC0%0/2
defined $from and defined $to
defined $from defined $to
Truth table33%1/3
A and B
A= defined $fromB= defined $to
ABresult
0X0
100
111
48 1 ($from, $to) = (lc $from, lc $to);
49 1 ▶ return undef unless $from =~ /\A$SQUARE\z/ && $to =~ /\A$SQUARE\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $from =~ /\A$SQUARE\z/ and $to =~ /\A$SQUARE\z/ 0 1 1
Condition33%1/3
$from =~ /\A$SQUARE\z/ and $to =~ /\A$SQUARE\z/
!ll&&!rl&&r
001
MC/DC0%0/2
$from =~ /\A$SQUARE\z/ and $to =~ /\A$SQUARE\z/
$from =~ /\A$SQUARE\z/ $to =~ /\A$SQUARE\z/
Truth table33%1/3
A and B
A= $from =~ /\A$SQUARE\z/B= $to =~ /\A$SQUARE\z/
ABresult
0X0
100
111
50 1 return $from . $to;
51 }
52  
53 6 ▶sub move_split {
Subroutine move_split: called CC=3 SCAR=11.4
Pod documented
54 6 my $text = _plain($_[0]);
55 6 ▶ return unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 6 6
56 6 ▶ my ($from, $to) = $text =~ /\A($SQUARE)($SQUARE)\z/ or return;
Branch: 1 branch
BranchTrueFalseTotal
() = $text =~ /\A($SQUARE)($SQUARE)\z/ or return 6 0 6
57 6 return ($from, $to);
58 }
59  
60 1 ▶sub move_parse {
Subroutine move_parse: called CC=3 SCAR=11.2
Pod documented
61 1 my $text = _plain($_[0]);
62 1 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 1 1
63 1 $text = lc $text;
64 1 $text =~ s/\A\s+//;
65 1 $text =~ s/\s+\z//;
66 1 ▶ my ($from, $to) = $text =~ /\A
Branch: 1 branch
BranchTrueFalseTotal
() = $text =~ /\A k? ($SQUARE) -? ($SQUARE) (?: x $SQUARE (?: , $SQUARE )* )? (?: \# | \+\+ )? \z/x or return undef 1 0 1
67 k?
68 ($SQUARE) -? ($SQUARE)
69 (?: x $SQUARE (?: , $SQUARE )* )?
70 (?: \# | \+\+ )?
71 \z/x or return undef;
72 1 return $from . $to;
73 }
74  
75 1 ▶sub move_display {
Subroutine move_display: called CC=9 SCAR=25.4
Pod documented
76 1 my ($wire, $about) = @_;
77 1 ▶ my ($from, $to) = move_split($wire) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
() = move_split($wire) or return undef 1 0 1
78 1 ▶ $about = {} unless ref $about eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $about eq "HASH" 0 1 1
79  
80 1 ▶ my @captures = sort grep { /\A$SQUARE\z/ } map { lc } grep { defined && !ref }
Condition33%1/3
defined $_ && !ref($_)
!ll&&!rl&&r
001
MC/DC0%0/2
defined $_ && !ref($_)
defined $_ !ref($_)
Truth table33%1/3
A && not(B)
A= defined $_B= !ref($_)
ABresult
0X0
100
111
81 1 ▶ @{ ref $about->{captures} eq 'ARRAY' ? $about->{captures} : [] };
Branch: 1 branch
BranchTrueFalseTotal
ref $about->{'captures'} eq 'ARRAY' ? : 1 0 1
82  
83 1 ▶ my $text = ($about->{king} ? 'K' : '') . $from . '-' . $to;
Branch: 1 branch
BranchTrueFalseTotal
$about->{'king'} ? : 0 1 1
84 1 ▶ $text .= 'x' . join(',', @captures) if @captures;
Branch: 1 branch
BranchTrueFalseTotal
if @captures 1 0 1
85 0 ▶ if ($about->{king_home}) { $text .= '++' }
Branch: 2 branches
BranchTrueFalseTotal
if ($about->{'king_home'}) { } 0 1 1
elsif ($about->{'king_taken'}) { } 0 1 1
86 0 elsif ($about->{king_taken}) { $text .= '#' }
87 1 return $text;
88 }
89  
90 2 ▶sub position_ok {
Subroutine position_ok: called CC=7 SCAR=19.9
Pod documented
91 2 my $text = _plain($_[0]);
92 2 ▶ return 0 unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 2 2
93 2 ▶ return 0 unless $text =~ m{\A ([adk1-7]+ (?: / [adk1-7]+ ){6}) [ ] [ad] \z}x;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ m[\A ([adk1-7]+ (?: / [adk1-7]+ ){6}) [ ] [ad] \z]x 0 2 2
94 2 for my $row (split m{/}, $1) {
95 14 my $width = 0;
96 14 ▶ $width += /[1-7]/ ? $_ : 1 for split //, $row;
Branch: 1 branch
BranchTrueFalseTotal
/[1-7]/ ? : 24 26 50
97 14 ▶ return 0 unless $width == 7;
Branch: 1 branch
BranchTrueFalseTotal
unless $width == 7 0 14 14
98 }
99 2 return 1;
100 }
101  
102 1 ▶sub position_cells {
Subroutine position_cells: called CC=5 SCAR=16.1
Pod documented
103 1 my $text = _plain($_[0]);
104 1 ▶ return unless position_ok($text);
Branch: 1 branch
BranchTrueFalseTotal
unless position_ok($text) 0 1 1
105 1 my ($rows, $side) = split / /, $text;
106 1 my @board;
107 1 for my $row (split m{/}, $rows) {
108 7 my @cells;
109 7 for my $ch (split //, $row) {
110 25 ▶ if ($ch =~ /[1-7]/) { push @cells, ('') x $ch }
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /[1-7]/) { } 12 13 25
111 13 else { push @cells, $ch }
112 }
113 7 push @board, \@cells;
114 }
115 1 return (\@board, $side);
116 }
117  
118 1 ▶sub game_string {
Subroutine game_string: called CC=21 SCAR=45.1
Pod documented
119 1 my ($game) = @_;
120 1 ▶ return undef unless ref $game eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $game eq "HASH" 0 1 1
121  
122 1 my $variant = _plain($game->{variant});
123 1 ▶ $variant = 'brandubh' unless defined $variant && length $variant;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $variant and length $variant 1 0 1
Condition33%1/3
defined $variant and length $variant
!ll&&!rl&&r
100
MC/DC0%0/2
defined $variant and length $variant
defined $variant length $variant
Truth table33%1/3
A and B
A= defined $variantB= length $variant
ABresult
0X0
100
111
124 1 ▶ return undef if $variant =~ /[\r\n]/ || $variant !~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $variant =~ /[\r\n]/ or not $variant =~ /\S/ 0 1 1
Condition33%1/3
$variant =~ /[\r\n]/ or not $variant =~ /\S/
l!l&&r!l&&!r
001
MC/DC0%0/2
$variant =~ /[\r\n]/ or not $variant =~ /\S/
$variant =~ /[\r\n]/ not $variant =~ /\S/
Truth table33%1/3
A or not(B)
A= $variant =~ /[\r\n]/B= not $variant =~ /\S/
ABresult
1X1
011
000
125  
126 1 my @moves;
127 1 ▶ for my $move (@{ $game->{moves} || [] }) {
Condition50%1/2
$game->{'moves'} || []
l!l
10
MC/DC0%0/1
$game->{'moves'} || []
$game->{'moves'}
Truth table50%1/2
A
A= $game->{'moves'}
Aresult
11
00
128 2 ▶ my ($from, $to) = move_split($move) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
() = move_split($move) or return undef 2 0 2
129 2 push @moves, $from . $to;
130 }
131  
132 1 my @lines = ("variant $variant");
133 1 my $attackers = _plain($game->{attackers});
134 1 ▶ if (defined $attackers && $attackers ne 'p1') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attackers and $attackers ne "p1") 0 1 1
Condition33%1/3
defined $attackers and $attackers ne "p1"
!ll&&!rl&&r
100
MC/DC0%0/2
defined $attackers and $attackers ne "p1"
defined $attackers $attackers ne "p1"
Truth table33%1/3
A and B
A= defined $attackersB= $attackers ne "p1"
ABresult
0X0
100
111
135 0 ▶ return undef unless $attackers eq 'p2';
Branch: 1 branch
BranchTrueFalseTotal
unless $attackers eq "p2" 0 0 0
136 0 push @lines, "attackers $attackers";
137 }
138 1 my $start = _plain($game->{start});
139 1 ▶ if (defined $start && $start ne SETUP) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start and $start ne "3a3/3a3/3d3/aadkdaa/3d3/3a3/3a3 a") 0 1 1
Condition33%1/3
defined $start and $start ne "3a3/3a3/3d3/aadkdaa/3d3/3a3/3a3 a"
!ll&&!rl&&r
100
MC/DC0%0/2
defined $start and $start ne "3a3/3a3/3d3/aadkdaa/3d3/3a3/3a3 a"
defined $start $start ne "3a3/3a3/3d3/aadkdaa/3d3/3a3/3a3 a"
Truth table33%1/3
A and B
A= defined $startB= $start ne "3a3/3a3/3d3/aadkdaa/3d3/3a3/3a3 a"
ABresult
0X0
100
111
140 0 ▶ return undef unless position_ok($start);
Branch: 1 branch
BranchTrueFalseTotal
unless position_ok($start) 0 0 0
141 0 push @lines, "start $start";
142 }
143 1 push @lines, join(' ', 'moves', @moves);
144  
145 1 my $result = $game->{result};
146 1 ▶ if (defined $result) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result) 0 1 1
147 0 my $line = _result_line($result);
148 0 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
149 0 push @lines, "result $line";
150 }
151  
152 1 my $offer = _plain($game->{offer});
153 1 ▶ if (defined $offer) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $offer) 0 1 1
154 0 ▶ return undef unless ($offer eq 'p1' || $offer eq 'p2') && !defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $offer eq "p1" || $offer eq "p2" and not defined $result 0 0 0
Condition0%0/6
$offer eq "p1" || $offer eq "p2" and not defined $result
!ll&&!rl&&r
000
$offer eq "p1" or $offer eq "p2"
l!l&&r!l&&!r
000
MC/DC0%0/3
$offer eq "p1" || $offer eq "p2" and not defined $result
$offer eq "p1" $offer eq "p2" not defined $result
Truth table0%0/5
A or B and not(C)
A= $offer eq "p1"B= $offer eq "p2"C= not defined $result
ABCresult
00X0
1X00
0100
1X11
0111
155 0 push @lines, "offer $offer";
156 }
157 1 return join("\n", @lines) . "\n";
158 }
159  
160 0 ▶sub _result_line {
Subroutine _result_line: not called CC=8 SCAR=42.8
161 0 my ($result) = @_;
162 0 ▶ return undef unless ref $result eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $result eq "HASH" 0 0 0
163 0 ▶ my $how = $result->{how} // '';
Condition0%0/2
$result->{"how"} // ""
l!l
00
MC/DC0%0/1
$result->{"how"} // ""
$result->{"how"}
Truth table0%0/2
A
A= $result->{"how"}
Aresult
11
00
164 0 ▶ return 'agreed' if $how eq 'agreed';
Branch: 1 branch
BranchTrueFalseTotal
if $how eq "agreed" 0 0 0
165 0 ▶ if ($how eq 'resign') {
Branch: 1 branch
BranchTrueFalseTotal
if ($how eq "resign") 0 0 0
166 0 ▶ my $by = $result->{by} // '';
Condition0%0/2
$result->{"by"} // ""
l!l
00
MC/DC0%0/1
$result->{"by"} // ""
$result->{"by"}
Truth table0%0/2
A
A= $result->{"by"}
Aresult
11
00
167 0 ▶ return "resign $by" if $by eq 'attackers' || $by eq 'defenders';
Branch: 1 branch
BranchTrueFalseTotal
if $by eq "attackers" or $by eq "defenders" 0 0 0
Condition0%0/3
$by eq "attackers" or $by eq "defenders"
l!l&&r!l&&!r
000
MC/DC0%0/2
$by eq "attackers" or $by eq "defenders"
$by eq "attackers" $by eq "defenders"
Truth table0%0/3
A or B
A= $by eq "attackers"B= $by eq "defenders"
ABresult
1X1
011
000
168 }
169 0 return undef;
170 }
171  
172 1 ▶sub game_parse {
Subroutine game_parse: called CC=26 SCAR=49.6
Pod documented
173 1 my $text = _plain($_[0]);
174 1 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 1 1
175  
176 1 my @lines = grep { /\S/ } split /\r?\n/, $text;
177 1 my %game = (start => SETUP, moves => [], result => undef);
178 1 my %seen;
179  
180 1 for my $line (@lines) {
181 2 ▶ my ($word, $rest) = $line =~ /\A(\w+)(?:[ ](.*))?\z/ or return undef;
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /\A(\w+)(?:[ ](.*))?\z/ or return undef 2 0 2
182 2 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 2 2
183 2 ▶ return undef if $seen{$word}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$word}++ 0 2 2
184  
185 2 ▶ if ($word eq 'variant') {
Branch: 6 branches
BranchTrueFalseTotal
if ($word eq 'variant') { } 1 1 2
elsif ($word eq 'attackers') { } 0 1 1
elsif ($word eq 'offer') { } 0 1 1
elsif ($word eq 'start') { } 0 1 1
elsif ($word eq 'moves') { } 1 0 1
elsif ($word eq 'result') { } 0 0 0
186 1 ▶ return undef unless $rest =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $rest =~ /\S/ 0 1 1
187 1 $game{variant} = $rest;
188 }
189 elsif ($word eq 'attackers') {
190 0 ▶ return undef unless $rest eq 'p1' || $rest eq 'p2';
Branch: 1 branch
BranchTrueFalseTotal
unless $rest eq "p1" or $rest eq "p2" 0 0 0
Condition0%0/3
$rest eq "p1" or $rest eq "p2"
l!l&&r!l&&!r
000
MC/DC0%0/2
$rest eq "p1" or $rest eq "p2"
$rest eq "p1" $rest eq "p2"
Truth table0%0/3
A or B
A= $rest eq "p1"B= $rest eq "p2"
ABresult
1X1
011
000
191 0 $game{attackers} = $rest;
192 }
193 elsif ($word eq 'offer') {
194 0 ▶ return undef unless $rest eq 'p1' || $rest eq 'p2';
Branch: 1 branch
BranchTrueFalseTotal
unless $rest eq "p1" or $rest eq "p2" 0 0 0
Condition0%0/3
$rest eq "p1" or $rest eq "p2"
l!l&&r!l&&!r
000
MC/DC0%0/2
$rest eq "p1" or $rest eq "p2"
$rest eq "p1" $rest eq "p2"
Truth table0%0/3
A or B
A= $rest eq "p1"B= $rest eq "p2"
ABresult
1X1
011
000
195 0 $game{offer} = $rest;
196 }
197 elsif ($word eq 'start') {
198 0 ▶ return undef unless position_ok($rest);
Branch: 1 branch
BranchTrueFalseTotal
unless position_ok($rest) 0 0 0
199 0 $game{start} = $rest;
200 }
201 elsif ($word eq 'moves') {
202 1 for my $move (split ' ', $rest) {
203 2 ▶ my ($from, $to) = move_split($move) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
() = move_split($move) or return undef 2 0 2
204 2 push @{ $game{moves} }, $from . $to;
205 }
206 }
207 elsif ($word eq 'result') {
208 0 ▶ if ($rest eq 'agreed') { $game{result} = { how => 'agreed' } }
Branch: 2 branches
BranchTrueFalseTotal
if ($rest eq 'agreed') { } 0 0 0
elsif ($rest =~ /\Aresign (attackers|defenders)\z/) { } 0 0 0
209 0 elsif ($rest =~ /\Aresign (attackers|defenders)\z/) { $game{result} = { how => 'resign', by => $1 } }
210 0 else { return undef }
211 }
212 else {
213 0 return undef;
214 }
215 }
216 1 ▶ return undef unless $seen{variant} && $seen{moves};
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{"variant"} and $seen{"moves"} 0 1 1
Condition33%1/3
$seen{"variant"} and $seen{"moves"}
!ll&&!rl&&r
001
MC/DC0%0/2
$seen{"variant"} and $seen{"moves"}
$seen{"variant"} $seen{"moves"}
Truth table33%1/3
A and B
A= $seen{"variant"}B= $seen{"moves"}
ABresult
0X0
100
111
217 1 ▶ return undef if defined $game{offer} && defined $game{result};
Branch: 1 branch
BranchTrueFalseTotal
if defined $game{"offer"} and defined $game{"result"} 0 1 1
Condition33%1/3
defined $game{"offer"} and defined $game{"result"}
!ll&&!rl&&r
100
MC/DC0%0/2
defined $game{"offer"} and defined $game{"result"}
defined $game{"offer"} defined $game{"result"}
Truth table33%1/3
A and B
A= defined $game{"offer"}B= defined $game{"result"}
ABresult
0X0
100
111
218 1 return \%game;
219 }
220  
221 3 1;
222  
223 __END__
↑ Summary blib/lib/Game/Brandubh/Bot.pm »