blib/lib/Game/Checkers/Notation.pm

filter: Statementstmt 98.6% 142 / 144 Branchbran 82.6% 81 / 98 Conditioncond 62.7% 32 / 51 MC/DCmcdc 38.7% 12 / 31 Subroutinesub 100% 11 / 11 Podpod 100% 7 / 7 totaltotal 83.3% 285 / 342 CC 79 SCAR 27.6
CC
79
Cov
87%
position_from_fen
25.8
format_pdn
16.0
parse_move
14.0
CRAP 15.8
« blib/lib/Game/Checkers/Terminal.pm ↑ Summary blib/lib/Game/Checkers/Rules.pm »
1 package Game::Checkers::Notation;
2  
3 28 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 28 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
5  
6 28 ▶use Game::Checkers::Squares;
Subroutine __ANON__: called CC=1 SCAR=0.0
7  
8 28 our $VERSION = '0.03';
9  
10 28 our (@TAG_ORDER, %STANDARD, %RESULT);
11 28 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 28 @TAG_ORDER = qw/Event Site Date Round White Black Result/;
13 28 %STANDARD = map { $_ => 1 } @TAG_ORDER, qw/FEN SetUp/;
14 28 %RESULT = map { $_ => 1 } '1-0', '0-1', '1/2-1/2', '*';
15 }
16  
17 898 ▶sub parse_move {
Subroutine parse_move: called CC=14 SCAR=26.4
Pod documented
18 898 my ($string) = @_;
19 898 ▶ return undef unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 898 898
20 898 (my $clean = $string) =~ s/\s+//g;
21 898 my ($jump, @token);
22 898 ▶ if ($clean =~ m/^([^-x]+)-([^-x]+)$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($clean =~ /^([^-x]+)-([^-x]+)$/i) { } 748 150 898
elsif ($clean =~ /^[^-x]+(?:[xX][^-x]+)+$/i) { } 146 4 150
23 748 ($jump, @token) = (0, $1, $2);
24 } elsif ($clean =~ m/^[^-x]+(?:[xX][^-x]+)+$/i) {
25 146 $jump = 1;
26 146 @token = split m/[xX]/, $clean;
27 } else {
28 4 return undef;
29 }
30 # the two spellings of a square do not mix in one move: '12-a5' is a typo,
31 # not half a move
32 894 my ($numeric, @squares) = (0);
33 894 for my $token (@token) {
34 1810 ▶ if ($token =~ m/^[0-9]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /^[0-9]+$/) 1778 32 1810
35 1778 ▶ return undef if @squares && !$numeric;
Branch: 1 branch
BranchTrueFalseTotal
if @squares and not $numeric 1 1777 1778
Condition100%3/3
@squares and not $numeric
!ll&&!rl&&r
8779001
MC/DC100%2/2
@squares and not $numeric
@squares not $numeric
Truth table100%3/3
A and not(B)
A= @squaresB= not $numeric
ABresult
0X0
100
111
36 1777 ▶ return undef if $token < 1 || $token > 32;
Branch: 1 branch
BranchTrueFalseTotal
if $token < 1 or $token > 32 2 1775 1777
Condition66%2/3
$token < 1 or $token > 32
l!l&&r!l&&!r
021775
MC/DC50%1/2
$token < 1 or $token > 32
$token < 1 $token > 32
Truth table66%2/3
A or B
A= $token < 1B= $token > 32
ABresult
1X1
011
000
37 1775 $numeric = 1;
38 1775 push @squares, $token + 0;
39 1775 next;
40 }
41 32 ▶ return undef if $numeric;
Branch: 1 branch
BranchTrueFalseTotal
if $numeric 1 31 32
42 31 ▶ my $square = Game::Checkers::Squares::coord_square($token)
Branch: 1 branch
BranchTrueFalseTotal
my $square = Game::Checkers::Squares::coord_square($token) or return undef 28 3 31
43 or return undef;
44 28 push @squares, $square;
45 }
46 # a slide to the square it started on is not a move, but a jump that comes
47 # back to it is: a king can capture its way round a loop
48 887 ▶ return undef if !$jump && $squares[0] == $squares[1];
Branch: 1 branch
BranchTrueFalseTotal
if not $jump and $squares[0] == $squares[1] 2 885 887
Condition100%3/3
not $jump and $squares[0] == $squares[1]
!ll&&!rl&&r
1467392
MC/DC100%2/2
not $jump and $squares[0] == $squares[1]
not $jump $squares[0] == $squares[1]
Truth table100%3/3
not(A) and B
A= not $jumpB= $squares[0] == $squares[1]
ABresult
0X0
100
111
49 return {
50 885 from => $squares[0],
51 to => $squares[-1],
52 squares => \@squares,
53 jump => $jump
54 };
55 }
56  
57 7038 ▶sub format_move {
Subroutine format_move: called CC=4 SCAR=14.5
Pod documented
58 7038 my ($move) = @_;
59 7038 ▶ return $move->notation if ref $move && ref $move ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $move and ref $move ne "HASH" 0 7038 7038
Condition33%1/3
ref $move and ref $move ne "HASH"
!ll&&!rl&&r
070380
MC/DC0%0/2
ref $move and ref $move ne "HASH"
ref $move ref $move ne "HASH"
Truth table33%1/3
A and B
A= ref $moveB= ref $move ne "HASH"
ABresult
0X0
100
111
60 7038 my @squares = @{$move->{squares}};
61 return $move->{jump}
62 7038 ▶ ? join 'x', @squares
Branch: 1 branch
BranchTrueFalseTotal
$move->{'jump'} ? : 1902 5136 7038
63 : sprintf '%d-%d', $squares[0], $squares[-1];
64 }
65  
66 2 ▶sub format_coord_move {
Subroutine format_coord_move: called CC=4 SCAR=14.0
Pod documented
67 2 my ($move) = @_;
68 2 ▶ return $move->coord_notation if ref $move && ref $move ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $move and ref $move ne "HASH" 1 1 2
Condition66%2/3
ref $move and ref $move ne "HASH"
!ll&&!rl&&r
011
MC/DC50%1/2
ref $move and ref $move ne "HASH"
ref $move ref $move ne "HASH"
Truth table66%2/3
A and B
A= ref $moveB= ref $move ne "HASH"
ABresult
0X0
100
111
69 2 my @squares = map { Game::Checkers::Squares::coord_name($_) }
70 1 @{$move->{squares}};
71 return $move->{jump}
72 1 ▶ ? join 'x', @squares
Branch: 1 branch
BranchTrueFalseTotal
$move->{'jump'} ? : 0 1 1
73 : sprintf '%s-%s', $squares[0], $squares[-1];
74 }
75  
76 2002 ▶sub fen_from_position {
Subroutine fen_from_position: called CC=10 SCAR=24.1
Pod documented
77 2002 my ($position, $turn) = @_;
78 2002 ▶ die "turn must be black or white, got " . (defined $turn ? "'$turn'" : 'undef')
Branch: 2 branches
BranchTrueFalseTotal
unless defined $turn and $turn eq "black" || $turn eq "white" 0 2002 2002
defined $turn ? : 0 0 0
Condition50%3/6
defined $turn and $turn eq "black" || $turn eq "white"
!ll&&!rl&&r
002002
$turn eq "black" or $turn eq "white"
l!l&&r!l&&!r
11348680
MC/DC0%0/3
defined $turn and $turn eq "black" || $turn eq "white"
defined $turn $turn eq "black" $turn eq "white"
Truth table50%2/4
A and B or C
A= defined $turnB= $turn eq "black"C= $turn eq "white"
ABCresult
0XX0
1000
11X1
1011
79 unless defined $turn && ($turn eq 'black' || $turn eq 'white');
80 2002 my (@black, @white);
81 2002 for my $square (1 .. 32) {
82 64064 ▶ my $value = $position->[$square] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $value = $position->[$square] or next 30442 33622 64064
83 30442 ▶ my $entry = (abs($value) == 2 ? 'K' : '') . $square;
Branch: 1 branch
BranchTrueFalseTotal
abs $value == 2 ? : 2238 28204 30442
84 30442 ▶ push @{$value > 0 ? \@black : \@white}, $entry;
Branch: 1 branch
BranchTrueFalseTotal
$value > 0 ? : 15188 15254 30442
85 }
86 2002 ▶ return sprintf '%s:W%s:B%s',
Branch: 1 branch
BranchTrueFalseTotal
$turn eq 'black' ? : 1134 868 2002
87 ($turn eq 'black' ? 'B' : 'W'),
88 join(',', @white),
89 join(',', @black);
90 }
91  
92 1121 ▶sub position_from_fen {
Subroutine position_from_fen: called CC=25 SCAR=32.5
Pod documented
93 1121 my ($fen, %opt) = @_;
94 1121 ▶ die 'a FEN string is required' unless defined $fen && length $fen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fen and length $fen 0 1121 1121
Condition33%1/3
defined $fen and length $fen
!ll&&!rl&&r
001121
MC/DC0%0/2
defined $fen and length $fen
defined $fen length $fen
Truth table33%1/3
A and B
A= defined $fenB= length $fen
ABresult
0X0
100
111
95 1121 (my $clean = uc $fen) =~ s/\s+//g;
96 1121 my @parts = split m/:/, $clean, -1;
97 1121 ▶ die "malformed FEN '$fen': expected a colour and two piece lists"
Branch: 1 branch
BranchTrueFalseTotal
unless @parts == 3 1 1120 1121
98 unless @parts == 3;
99 1120 my $letter = shift @parts;
100 1120 ▶ die "malformed FEN '$fen': the side to move must be B or W"
Branch: 1 branch
BranchTrueFalseTotal
unless $letter eq "B" or $letter eq "W" 1 1119 1120
Condition100%3/3
$letter eq "B" or $letter eq "W"
l!l&&r!l&&!r
111451
MC/DC100%2/2
$letter eq "B" or $letter eq "W"
$letter eq "B" $letter eq "W"
Truth table100%3/3
A or B
A= $letter eq "B"B= $letter eq "W"
ABresult
1X1
011
000
101 unless $letter eq 'B' || $letter eq 'W';
102 1119 my @position = (0) x 33;
103 1119 my %seen;
104 my %colour;
105 1119 for my $part (@parts) {
106 2235 my $side = substr $part, 0, 1, '';
107 2235 ▶ die "malformed FEN '$fen': a piece list must start with B or W"
Branch: 1 branch
BranchTrueFalseTotal
unless $side eq "B" or $side eq "W" 0 2235 2235
Condition66%2/3
$side eq "B" or $side eq "W"
l!l&&r!l&&!r
111611190
MC/DC0%0/2
$side eq "B" or $side eq "W"
$side eq "B" $side eq "W"
Truth table66%2/3
A or B
A= $side eq "B"B= $side eq "W"
ABresult
1X1
011
000
108 unless $side eq 'B' || $side eq 'W';
109 2235 ▶ die "malformed FEN '$fen': two $side piece lists" if $colour{$side}++;
Branch: 1 branch
BranchTrueFalseTotal
if $colour{$side}++ 0 2235 2235
110 2235 ▶ next unless length $part;
Branch: 1 branch
BranchTrueFalseTotal
unless length $part 3 2232 2235
111 2232 for my $entry (split m/,/, $part) {
112 24739 ▶ my ($king, $square) = $entry =~ m/^(K?)([0-9]+)$/
Branch: 1 branch
BranchTrueFalseTotal
() = $entry =~ /^(K?)([0-9]+)$/ or die "malformed FEN '${fen}': '${entry}' is not a square" 24738 1 24739
113 or die "malformed FEN '$fen': '$entry' is not a square";
114 24738 ▶ die "malformed FEN '$fen': square $square is not 1 .. 32"
Branch: 1 branch
BranchTrueFalseTotal
if $square < 1 or $square > 32 2 24736 24738
Condition100%3/3
$square < 1 or $square > 32
l!l&&r!l&&!r
1124736
MC/DC100%2/2
$square < 1 or $square > 32
$square < 1 $square > 32
Truth table100%3/3
A or B
A= $square < 1B= $square > 32
ABresult
1X1
011
000
115 if $square < 1 || $square > 32;
116 24736 ▶ die "malformed FEN '$fen': square $square twice" if $seen{$square}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$square}++ 1 24735 24736
117 24735 ▶ my $value = $king ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$king ? : 82 24653 24735
118 24735 ▶ $position[$square] = $side eq 'B' ? $value : -$value;
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'B' ? : 12316 12419 24735
119 }
120 }
121 1115 ▶ if ($opt{strict}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"strict"}) 1 1114 1115
122 1 for my $side ('B', 'W') {
123 1 ▶ my $wanted = $side eq 'B' ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'B' ? : 1 0 1
124 1 ▶ my $count = grep { $_ && ($_ > 0 ? 1 : -1) == $wanted } @position;
Branch: 1 branch
BranchTrueFalseTotal
$_ > 0 ? : 13 12 25
Condition100%3/3
$_ && ($_ > 0 ? 1 : -1) == $wanted
!ll&&!rl&&r
81213
MC/DC100%2/2
$_ && ($_ > 0 ? 1 : -1) == $wanted
$_ ($_ > 0 ? 1 : -1) == $wanted
Truth table100%3/3
A && B
A= $_B= ($_ > 0 ? 1 : -1) == $wanted
ABresult
0X0
100
111
125 1 ▶ die "malformed FEN '$fen': $count $side pieces, the game has 12"
Branch: 1 branch
BranchTrueFalseTotal
if $count > 12 1 0 1
126 if $count > 12;
127 }
128 }
129 1114 ▶ return (\@position, $letter eq 'B' ? 'black' : 'white');
Branch: 1 branch
BranchTrueFalseTotal
$letter eq 'B' ? : 1109 5 1114
130 }
131  
132 10 ▶sub parse_pdn {
Subroutine parse_pdn: called CC=13 SCAR=26.3
Pod documented
133 10 my ($text) = @_;
134 10 ▶ die 'PDN text is required' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 10 10
135 10 my (%tag, @movetext);
136 10 for my $line (split m/\n/, $text) {
137 123 ▶ if ($line =~ m/^\s*\[\s*(\w+)\s+"([^"]*)"\s*\]\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*\[\s*(\w+)\s+"([^"]*)"\s*\]\s*$/) 57 66 123
138 57 $tag{$1} = $2;
139 57 next;
140 }
141 # a semicolon comment runs to the end of ITS line, so it goes before
142 # the lines are joined, while a brace comment may span lines and goes
143 # after
144 66 $line =~ s/;.*$//;
145 66 push @movetext, $line;
146 }
147 10 my $movetext = join ' ', @movetext;
148 10 $movetext =~ s/\{[^}]*\}/ /g;
149 10 my $result;
150 10 ▶ if ($movetext =~ s{(1-0|0-1|1/2-1/2|\*)\s*$}{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($movetext =~ s[(1-0|0-1|1/2-1/2|\*)\s*$][]) 9 1 10
151 9 $result = $1;
152 }
153 10 my @moves;
154 10 for my $token (split ' ', $movetext) {
155 784 ▶ next if $token =~ m/^[0-9]+\.+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $token =~ /^[0-9]+\.+$/ 262 522 784
156 522 $token =~ s/^[0-9]+\.+//;
157 522 ▶ next unless length $token;
Branch: 1 branch
BranchTrueFalseTotal
unless length $token 0 522 522
158 522 ▶ my $parsed = parse_move($token)
Branch: 1 branch
BranchTrueFalseTotal
my $parsed = parse_move($token) or die "malformed PDN: '${token}' is not a move" 521 1 522
159 or die "malformed PDN: '$token' is not a move";
160 521 push @moves, format_move($parsed);
161 }
162 9 ▶ $result = $tag{Result} if !defined $result && defined $tag{Result};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $result and defined $tag{"Result"} 0 9 9
Condition33%1/3
not defined $result and defined $tag{"Result"}
!ll&&!rl&&r
900
MC/DC0%0/2
not defined $result and defined $tag{"Result"}
not defined $result defined $tag{"Result"}
Truth table33%1/3
not(A) and B
A= not defined $resultB= defined $tag{"Result"}
ABresult
0X0
100
111
163 9 ▶ $result = '*' unless defined $result && $RESULT{$result};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result and $Game::Checkers::Notation::RESULT{$result} 0 9 9
Condition33%1/3
defined $result and $Game::Checkers::Notation::RESULT{$result}
!ll&&!rl&&r
009
MC/DC0%0/2
defined $result and $Game::Checkers::Notation::RESULT{$result}
defined $result $Game::Checkers::Notation::RESULT{$result}
Truth table33%1/3
A and B
A= defined $resultB= $Game::Checkers::Notation::RESULT{$result}
ABresult
0X0
100
111
164 9 return { tags => \%tag, moves => \@moves, result => $result };
165 }
166  
167 9 ▶sub format_pdn {
Subroutine format_pdn: called CC=15 SCAR=27.7
Pod documented
168 9 my ($game) = @_;
169 9 ▶ my %tag = %{$game->{tags} || {}};
Condition50%1/2
$game->{'tags'} || {}
l!l
90
MC/DC0%0/1
$game->{'tags'} || {}
$game->{'tags'}
Truth table50%1/2
A
A= $game->{'tags'}
Aresult
11
00
170 9 ▶ my $result = $game->{result} || $tag{Result} || '*';
Condition40%2/5
$game->{'result'} || $tag{'Result'} || '*'
l!l
90
$game->{"result"} or $tag{"Result"}
l!l&&r!l&&!r
900
MC/DC0%0/2
$game->{'result'} || $tag{'Result'} || '*'
$game->{"result"} $tag{"Result"}
Truth table33%1/3
A or B
A= $game->{"result"}B= $tag{"Result"}
ABresult
1X1
011
000
171 9 ▶ die "'$result' is not a PDN result" unless $RESULT{$result};
Branch: 1 branch
BranchTrueFalseTotal
unless $Game::Checkers::Notation::RESULT{$result} 0 9 9
172 9 $tag{Result} = $result;
173 9 $tag{$_} = '?' for grep { !defined $tag{$_} } @TAG_ORDER;
174 9 my $pdn = '';
175 9 $pdn .= sprintf qq{[%s "%s"]\n}, $_, $tag{$_} for @TAG_ORDER;
176 $pdn .= sprintf qq{[%s "%s"]\n}, $_, $tag{$_}
177 9 for grep { defined $tag{$_} } 'FEN', 'SetUp';
178 $pdn .= sprintf qq{[%s "%s"]\n}, $_, $tag{$_}
179 9 for sort grep { !$STANDARD{$_} } keys %tag;
180 9 $pdn .= "\n";
181 9 ▶ my @moves = @{$game->{moves} || []};
Condition50%1/2
$game->{'moves'} || []
l!l
90
MC/DC0%0/1
$game->{'moves'} || []
$game->{'moves'}
Truth table50%1/2
A
A= $game->{'moves'}
Aresult
11
00
182 9 my ($line, @lines) = ('');
183 9 my $number = 0;
184 9 ▶ while (@moves) {
Branch: 1 branch
BranchTrueFalseTotal
if (@moves) 148 9 157
185 148 $number++;
186 148 my $pair = join ' ', $number . '.', grep { defined } splice @moves, 0, 2;
187 148 ▶ if (length($line) + length($pair) + 1 > 79) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($line) + length($pair) + 1 > 79) 24 124 148
188 24 push @lines, $line;
189 24 $line = '';
190 }
191 148 ▶ $line .= (length $line ? ' ' : '') . $pair;
Branch: 1 branch
BranchTrueFalseTotal
length $line ? : 116 32 148
192 }
193 9 ▶ if (length($line) + length($result) + 1 > 79) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($line) + length($result) + 1 > 79) 0 9 9
194 0 push @lines, $line;
195 0 $line = '';
196 }
197 9 ▶ $line .= (length $line ? ' ' : '') . $result;
Branch: 1 branch
BranchTrueFalseTotal
length $line ? : 8 1 9
198 9 push @lines, $line;
199 9 $pdn .= join "\n", @lines;
200 9 return $pdn . "\n";
201 }
202  
203 28 1;
204  
205 __END__
« blib/lib/Game/Checkers/Terminal.pm ↑ Summary blib/lib/Game/Checkers/Rules.pm »