blib/lib/Game/Merrills/Notation.pm

filter: Statementstmt 100% 106 / 106 Branchbran 100% 72 / 72 Conditioncond 88.1% 37 / 42 MC/DCmcdc 80.0% 20 / 25 Subroutinesub 100% 11 / 11 Podpod 100% 7 / 7 totaltotal 96.2% 253 / 263 CC 57 SCAR 23.0
CC
57
Cov
98%
parse_move
14.0
format_position
13.0
parse_position
12.0
CRAP 10.0
« blib/lib/Game/Merrills/Bot.pm ↑ Summary blib/lib/Game/Merrills/Result.pm »
1 package Game::Merrills::Notation;
2  
3 84 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 84 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
5  
6 84 ▶use Game::Merrills::Points;
Subroutine __ANON__: called CC=1 SCAR=0.0
7  
8 84 our $VERSION = '0.02';
9  
10 84 our (%CELL, %GLYPH, %TURN, %LETTER);
11  
12 84 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 84 %CELL = ('.' => 0, W => 1, B => -1);
14 84 %GLYPH = (0 => '.', 1 => 'W', -1 => 'B');
15 84 %TURN = (w => 'white', b => 'black');
16 84 %LETTER = (white => 'w', black => 'b');
17 }
18  
19 15781 ▶sub parse_move {
Subroutine parse_move: called CC=14 SCAR=26.4
Pod documented
20 15781 my ($text) = @_;
21 15781 ▶ return undef unless defined $text && !ref $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 5 15776 15781
Condition100%3/3
defined $text and not ref $text
!ll&&!rl&&r
3215776
MC/DC100%2/2
defined $text and not ref $text
defined $text not ref $text
Truth table100%3/3
A and not(B)
A= defined $textB= not ref $text
ABresult
0X0
100
111
22 15776 ▶ my ($from, $to, $remove) = $text =~ m/
Branch: 1 branch
BranchTrueFalseTotal
() = $text =~ / ^\s* ([a-g][1-7]) (?:\s*-\s*([a-g][1-7]))? (?:\s*x\s*([a-g][1-7]))? \s*$ /xi or return undef 15744 32 15776
23 ^\s*
24 ([a-g][1-7])
25 (?:\s*-\s*([a-g][1-7]))?
26 (?:\s*x\s*([a-g][1-7]))?
27 \s*$
28 /xi or return undef;
29 15744 ▶ ($from, $to) = (undef, $from) unless defined $to;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $to 1821 13923 15744
30 15744 my %move;
31 15744 for my $part ([from => $from], [to => $to], [remove => $remove]) {
32 47224 my ($key, $name) = @{$part};
33 47224 ▶ next unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 4555 42669 47224
34 42669 my $point = Game::Merrills::Points::point($name);
35 42669 ▶ return undef unless defined $point;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $point 8 42661 42669
36 42661 $move{$key} = $point;
37 }
38 15736 ▶ return undef if defined $move{from} && $move{from} == $move{to};
Branch: 1 branch
BranchTrueFalseTotal
if defined $move{"from"} and $move{"from"} == $move{"to"} 2 15734 15736
Condition100%3/3
defined $move{"from"} and $move{"from"} == $move{"to"}
!ll&&!rl&&r
1815139192
MC/DC100%2/2
defined $move{"from"} and $move{"from"} == $move{"to"}
defined $move{"from"} $move{"from"} == $move{"to"}
Truth table100%3/3
A and B
A= defined $move{"from"}B= $move{"from"} == $move{"to"}
ABresult
0X0
100
111
39 return undef if defined $move{remove}
40 && ($move{remove} == $move{to}
41 15734 ▶ || (defined $move{from} && $move{remove} == $move{from}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $move{"remove"} and $move{"remove"} == $move{"to"} || defined $move{"from"} && $move{"remove"} == $move{"from"} 3 15731 15734
Condition100%9/9
defined $move{"remove"} and $move{"remove"} == $move{"to"} || defined $move{"from"} && $move{"remove"} == $move{"from"}
!ll&&!rl&&r
2732129993
$move{"remove"} == $move{"to"} or defined $move{"from"} and $move{"remove"} == $move{"from"}
l!l&&r!l&&!r
2112999
defined $move{"from"} and $move{"remove"} == $move{"from"}
!ll&&!rl&&r
618123811
MC/DC100%4/4
defined $move{"remove"} and $move{"remove"} == $move{"to"} || defined $move{"from"} && $move{"remove"} == $move{"from"}
defined $move{"remove"} $move{"remove"} == $move{"to"} defined $move{"from"} $move{"remove"} == $move{"from"}
Truth table100%5/5
A and B or C and D
A= defined $move{"remove"}B= $move{"remove"} == $move{"to"}C= defined $move{"from"}D= $move{"remove"} == $move{"from"}
ABCDresult
0XXX0
100X0
10100
11XX1
10111
42 15731 return \%move;
43 }
44  
45 175026 ▶sub format_move {
Subroutine format_move: called CC=5 SCAR=16.1
Pod documented
46 175026 my ($move) = @_;
47 175026 ▶ die 'a move needs a point to go to' unless ref $move eq 'HASH' && defined $move->{to};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $move eq "HASH" and defined $move->{"to"} 2 175024 175026
Condition100%3/3
ref $move eq "HASH" and defined $move->{"to"}
!ll&&!rl&&r
11175024
MC/DC100%2/2
ref $move eq "HASH" and defined $move->{"to"}
ref $move eq "HASH" defined $move->{"to"}
Truth table100%3/3
A and B
A= ref $move eq "HASH"B= defined $move->{"to"}
ABresult
0X0
100
111
48 175024 my $text = Game::Merrills::Points::name($move->{to});
49 $text = Game::Merrills::Points::name($move->{from}) . '-' . $text
50 175023 ▶ if defined $move->{from};
Branch: 1 branch
BranchTrueFalseTotal
if defined $move->{"from"} 136924 38099 175023
51 $text .= 'x' . Game::Merrills::Points::name($move->{remove})
52 175023 ▶ if defined $move->{remove};
Branch: 1 branch
BranchTrueFalseTotal
if defined $move->{"remove"} 33896 141127 175023
53 175023 return $text;
54 }
55  
56 44395 ▶sub format_position {
Subroutine format_position: called CC=13 SCAR=25.7
Pod documented
57 44395 my ($position) = @_;
58 44395 ▶ die 'position: not a hashref' unless ref $position eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $position eq "HASH" 1 44394 44395
59 44394 my $cells = $position->{cells};
60 die 'position: cells must be an arrayref of 24 values'
61 44394 ▶ unless ref $cells eq 'ARRAY' && @{$cells} == Game::Merrills::Points::POINTS;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cells eq "ARRAY" and @{$cells;} == 24 1 44393 44394
Condition66%2/3
ref $cells eq "ARRAY" and @{$cells;} == 24
!ll&&!rl&&r
1044393
MC/DC50%1/2
ref $cells eq "ARRAY" and @{$cells;} == 24
ref $cells eq "ARRAY" @{$cells;} == 24
Truth table66%2/3
A and B
A= ref $cells eq "ARRAY"B= @{$cells;} == 24
ABresult
0X0
100
111
62 44393 my $board = '';
63 44393 for my $point (Game::Merrills::Points::all_points()) {
64 1065409 my $value = $cells->[$point];
65 die 'position: cell ' . Game::Merrills::Points::name($point) . ' must be -1, 0 or 1'
66 1065409 ▶ unless defined $value && exists $GLYPH{$value};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and exists $Game::Merrills::Notation::GLYPH{$value} 1 1065408 1065409
Condition66%2/3
defined $value and exists $Game::Merrills::Notation::GLYPH{$value}
!ll&&!rl&&r
011065408
MC/DC50%1/2
defined $value and exists $Game::Merrills::Notation::GLYPH{$value}
defined $value exists $Game::Merrills::Notation::GLYPH{$value}
Truth table66%2/3
A and B
A= defined $valueB= exists $Game::Merrills::Notation::GLYPH{$value}
ABresult
0X0
100
111
67 1065408 $board .= $GLYPH{$value};
68 }
69 44392 my $turn = $position->{turn};
70 die 'position: turn must be white or black'
71 44392 ▶ unless defined $turn && exists $LETTER{$turn};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $turn and exists $Game::Merrills::Notation::LETTER{$turn} 2 44390 44392
Condition100%3/3
defined $turn and exists $Game::Merrills::Notation::LETTER{$turn}
!ll&&!rl&&r
1144390
MC/DC100%2/2
defined $turn and exists $Game::Merrills::Notation::LETTER{$turn}
defined $turn exists $Game::Merrills::Notation::LETTER{$turn}
Truth table100%3/3
A and B
A= defined $turnB= exists $Game::Merrills::Notation::LETTER{$turn}
ABresult
0X0
100
111
72 44390 ▶ my $hand = $position->{hand} || {};
Condition100%2/2
$position->{'hand'} || {}
l!l
443891
MC/DC100%1/1
$position->{'hand'} || {}
$position->{'hand'}
Truth table100%2/2
A
A= $position->{'hand'}
Aresult
11
00
73 my $text = join ' ', $board, $LETTER{$turn},
74 88780 ▶ map({ defined $hand->{$_} ? $hand->{$_} : 0 } qw/white black/),
Branch: 1 branch
BranchTrueFalseTotal
defined $hand->{$_} ? : 88777 3 88780
Condition100%4/4
$position->{'no_mill'} || 0
l!l
1523829152
$position->{'ply'} || 0
l!l
2395120439
MC/DC100%1/1
$position->{'no_mill'} || 0
$position->{'no_mill'}
MC/DC100%1/1
$position->{'ply'} || 0
$position->{'ply'}
Truth table100%2/2
A
A= $position->{'no_mill'}
Aresult
11
00
Truth table100%2/2
A
A= $position->{'ply'}
Aresult
11
00
75 44390 $position->{no_mill} || 0, $position->{ply} || 0;
76 44390 parse_position($text);
77 44389 return $text;
78 }
79  
80 64551 ▶sub parse_position {
Subroutine parse_position: called CC=12 SCAR=24.9
Pod documented
81 64551 my ($text) = @_;
82 64551 ▶ die 'position: nothing to read' unless defined $text && !ref $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 1 64550 64551
Condition66%2/3
defined $text and not ref $text
!ll&&!rl&&r
1064550
MC/DC50%1/2
defined $text and not ref $text
defined $text not ref $text
Truth table66%2/3
A and not(B)
A= defined $textB= not ref $text
ABresult
0X0
100
111
83 64550 my @field = split ' ', $text;
84 64550 ▶ die 'position: six fields are needed, got ' . scalar @field unless @field == 6;
Branch: 1 branch
BranchTrueFalseTotal
unless @field == 6 7 64543 64550
85 64543 my ($board, $turn, $white, $black, $no_mill, $ply) = @field;
86  
87 64543 ▶ die 'position: the board must be 24 of W, B and .'
Branch: 1 branch
BranchTrueFalseTotal
unless $board =~ /^[WB.]{24}$/ 4 64539 64543
88 unless $board =~ m/^[WB.]{24}$/;
89 die "position: the side to move must be w or b, got '$turn'"
90 64539 ▶ unless exists $TURN{$turn};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Game::Merrills::Notation::TURN{$turn} 2 64537 64539
91 64537 for my $count ([white => $white], [black => $black]) {
92 129072 ▶ die "position: men in hand for $count->[0] must be 0 .. 9, got '$count->[1]'"
Branch: 1 branch
BranchTrueFalseTotal
unless $count->[1] =~ /^[0-9]$/ 3 129069 129072
93 unless $count->[1] =~ m/^[0-9]$/;
94 }
95 64534 ▶ die "position: plies since a mill must be a number, got '$no_mill'"
Branch: 1 branch
BranchTrueFalseTotal
unless $no_mill =~ /^[0-9]+$/ 1 64533 64534
96 unless $no_mill =~ m/^[0-9]+$/;
97 64533 ▶ die "position: the ply must be a number, got '$ply'"
Branch: 1 branch
BranchTrueFalseTotal
unless $ply =~ /^[0-9]+$/ 1 64532 64533
98 unless $ply =~ m/^[0-9]+$/;
99  
100 64532 my @cells = map { $CELL{$_} } split //, $board;
101 64532 my %on = (white => scalar(grep { $_ == 1 } @cells), black => scalar(grep { $_ == -1 } @cells));
102 64532 ▶ die 'position: white has more than nine men' if $on{white} + $white > 9;
Branch: 1 branch
BranchTrueFalseTotal
if $on{"white"} + $white > 9 1 64531 64532
103 64531 ▶ die 'position: black has more than nine men' if $on{black} + $black > 9;
Branch: 1 branch
BranchTrueFalseTotal
if $on{"black"} + $black > 9 1 64530 64531
104  
105 return {
106 cells => \@cells,
107 64530 turn => $TURN{$turn},
108 hand => { white => $white + 0, black => $black + 0 },
109 no_mill => $no_mill + 0,
110 ply => $ply + 0,
111 };
112 }
113  
114 57 ▶sub format_record {
Subroutine format_record: called CC=9 SCAR=22.0
Pod documented
115 57 my ($moves, %option) = @_;
116 57 ▶ die 'record: moves must be an arrayref' unless ref $moves eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $moves eq "ARRAY" 1 56 57
117 56 my $header = '';
118 56 ▶ if (defined $option{position}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $option{"position"}) 5 51 56
119 5 $header = 'position ' . format_position(parse_position($option{position})) . "\n";
120 }
121 55 my @text;
122 55 for my $i (0 .. $#{$moves}) {
123 840 my $move = $moves->[$i];
124 840 ▶ my $written = !ref $move ? $move
Branch: 2 branches
BranchTrueFalseTotal
!ref($move) ? : 831 9 840
ref $move eq 'HASH' ? : 8 1 9
125 : ref $move eq 'HASH' ? format_move($move)
126 : $move->notation;
127 840 ▶ die 'record: move ' . ($i + 1) . ', ' . (defined $written ? "'$written'" : 'undef')
Branch: 2 branches
BranchTrueFalseTotal
unless parse_move($written) 2 838 840
defined $written ? : 1 1 2
128 . ' is not a move'
129 unless parse_move($written);
130 838 push @text, format_move(parse_move($written));
131 }
132 53 my @line;
133 53 ▶ for (my $i = 0; $i < @text; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @text) 429 53 482
134 429 push @line, join ' ', (($i / 2) + 1) . '.', grep { defined } @text[ $i, $i + 1 ];
135 }
136 53 return join '', $header, map { "$_\n" } @line;
137 }
138  
139 33 ▶sub parse_record {
Subroutine parse_record: called CC=6 SCAR=17.9
Pod documented
140 33 my ($text) = @_;
141 33 ▶ die 'record: nothing to read' unless defined $text && !ref $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 1 32 33
Condition66%2/3
defined $text and not ref $text
!ll&&!rl&&r
1032
MC/DC50%1/2
defined $text and not ref $text
defined $text not ref $text
Truth table66%2/3
A and not(B)
A= defined $textB= not ref $text
ABresult
0X0
100
111
142 32 $text =~ s/^[ \t]*#[^\n]*$//mg;
143 32 $text =~ s/^[ \t]*position[ \t]+[^\n]*$//m;
144 32 my @moves;
145 32 for my $token (split ' ', $text) {
146 816 ▶ next if $token =~ m/^[0-9]+\.$/;
Branch: 1 branch
BranchTrueFalseTotal
if $token =~ /^[0-9]+\.$/ 275 541 816
147 541 my $move = parse_move($token);
148 541 ▶ die 'record: move ' . (@moves + 1) . ", '$token' is not a move" unless $move;
Branch: 1 branch
BranchTrueFalseTotal
unless $move 5 536 541
149 536 push @moves, $move;
150 }
151 27 return \@moves;
152 }
153  
154 24 ▶sub record_position {
Subroutine record_position: called CC=4 SCAR=13.9
Pod documented
155 24 my ($text) = @_;
156 24 ▶ die 'record: nothing to read' unless defined $text && !ref $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 1 23 24
Condition66%2/3
defined $text and not ref $text
!ll&&!rl&&r
1023
MC/DC50%1/2
defined $text and not ref $text
defined $text not ref $text
Truth table66%2/3
A and not(B)
A= defined $textB= not ref $text
ABresult
0X0
100
111
157 23 ▶ my ($position) = $text =~ m/^[ \t]*position[ \t]+([^\n]*)$/m or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my($position) = $text =~ /^[ \t]*position[ \t]+([^\n]*)$/m or return undef 8 15 23
158 8 return format_position(parse_position($position));
159 }
160  
161 84 1;
162  
163 __END__
« blib/lib/Game/Merrills/Bot.pm ↑ Summary blib/lib/Game/Merrills/Result.pm »