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/Terminal.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.01';
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 4548 42676 47224
34 42676 my $point = Game::Merrills::Points::point($name);
35 42676 ▶ return undef unless defined $point;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $point 8 42668 42676
36 42668 $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
2725130063
$move{"remove"} == $move{"to"} or defined $move{"from"} and $move{"remove"} == $move{"from"}
l!l&&r!l&&!r
2113006
defined $move{"from"} and $move{"remove"} == $move{"from"}
!ll&&!rl&&r
614123921
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 213357 ▶sub format_move {
Subroutine format_move: called CC=5 SCAR=16.1
Pod documented
46 213357 my ($move) = @_;
47 213357 ▶ 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 213355 213357
Condition100%3/3
ref $move eq "HASH" and defined $move->{"to"}
!ll&&!rl&&r
11213355
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 213355 my $text = Game::Merrills::Points::name($move->{to});
49 $text = Game::Merrills::Points::name($move->{from}) . '-' . $text
50 213354 ▶ if defined $move->{from};
Branch: 1 branch
BranchTrueFalseTotal
if defined $move->{"from"} 175585 37769 213354
51 $text .= 'x' . Game::Merrills::Points::name($move->{remove})
52 213354 ▶ if defined $move->{remove};
Branch: 1 branch
BranchTrueFalseTotal
if defined $move->{"remove"} 36052 177302 213354
53 213354 return $text;
54 }
55  
56 45801 ▶sub format_position {
Subroutine format_position: called CC=13 SCAR=25.7
Pod documented
57 45801 my ($position) = @_;
58 45801 ▶ die 'position: not a hashref' unless ref $position eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $position eq "HASH" 1 45800 45801
59 45800 my $cells = $position->{cells};
60 die 'position: cells must be an arrayref of 24 values'
61 45800 ▶ unless ref $cells eq 'ARRAY' && @{$cells} == Game::Merrills::Points::POINTS;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cells eq "ARRAY" and @{$cells;} == 24 1 45799 45800
Condition66%2/3
ref $cells eq "ARRAY" and @{$cells;} == 24
!ll&&!rl&&r
1045799
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 45799 my $board = '';
63 45799 for my $point (Game::Merrills::Points::all_points()) {
64 1099153 my $value = $cells->[$point];
65 die 'position: cell ' . Game::Merrills::Points::name($point) . ' must be -1, 0 or 1'
66 1099153 ▶ unless defined $value && exists $GLYPH{$value};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and exists $Game::Merrills::Notation::GLYPH{$value} 1 1099152 1099153
Condition66%2/3
defined $value and exists $Game::Merrills::Notation::GLYPH{$value}
!ll&&!rl&&r
011099152
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 1099152 $board .= $GLYPH{$value};
68 }
69 45798 my $turn = $position->{turn};
70 die 'position: turn must be white or black'
71 45798 ▶ unless defined $turn && exists $LETTER{$turn};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $turn and exists $Game::Merrills::Notation::LETTER{$turn} 2 45796 45798
Condition100%3/3
defined $turn and exists $Game::Merrills::Notation::LETTER{$turn}
!ll&&!rl&&r
1145796
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 45796 ▶ my $hand = $position->{hand} || {};
Condition100%2/2
$position->{'hand'} || {}
l!l
457951
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 91592 ▶ map({ defined $hand->{$_} ? $hand->{$_} : 0 } qw/white black/),
Branch: 1 branch
BranchTrueFalseTotal
defined $hand->{$_} ? : 91589 3 91592
Condition100%4/4
$position->{'no_mill'} || 0
l!l
1659629200
$position->{'ply'} || 0
l!l
2535720439
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 45796 $position->{no_mill} || 0, $position->{ply} || 0;
76 45796 parse_position($text);
77 45795 return $text;
78 }
79  
80 65957 ▶sub parse_position {
Subroutine parse_position: called CC=12 SCAR=24.9
Pod documented
81 65957 my ($text) = @_;
82 65957 ▶ die 'position: nothing to read' unless defined $text && !ref $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 1 65956 65957
Condition66%2/3
defined $text and not ref $text
!ll&&!rl&&r
1065956
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 65956 my @field = split ' ', $text;
84 65956 ▶ die 'position: six fields are needed, got ' . scalar @field unless @field == 6;
Branch: 1 branch
BranchTrueFalseTotal
unless @field == 6 7 65949 65956
85 65949 my ($board, $turn, $white, $black, $no_mill, $ply) = @field;
86  
87 65949 ▶ die 'position: the board must be 24 of W, B and .'
Branch: 1 branch
BranchTrueFalseTotal
unless $board =~ /^[WB.]{24}$/ 4 65945 65949
88 unless $board =~ m/^[WB.]{24}$/;
89 die "position: the side to move must be w or b, got '$turn'"
90 65945 ▶ unless exists $TURN{$turn};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Game::Merrills::Notation::TURN{$turn} 2 65943 65945
91 65943 for my $count ([white => $white], [black => $black]) {
92 131884 ▶ 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 131881 131884
93 unless $count->[1] =~ m/^[0-9]$/;
94 }
95 65940 ▶ die "position: plies since a mill must be a number, got '$no_mill'"
Branch: 1 branch
BranchTrueFalseTotal
unless $no_mill =~ /^[0-9]+$/ 1 65939 65940
96 unless $no_mill =~ m/^[0-9]+$/;
97 65939 ▶ die "position: the ply must be a number, got '$ply'"
Branch: 1 branch
BranchTrueFalseTotal
unless $ply =~ /^[0-9]+$/ 1 65938 65939
98 unless $ply =~ m/^[0-9]+$/;
99  
100 65938 my @cells = map { $CELL{$_} } split //, $board;
101 65938 my %on = (white => scalar(grep { $_ == 1 } @cells), black => scalar(grep { $_ == -1 } @cells));
102 65938 ▶ die 'position: white has more than nine men' if $on{white} + $white > 9;
Branch: 1 branch
BranchTrueFalseTotal
if $on{"white"} + $white > 9 1 65937 65938
103 65937 ▶ die 'position: black has more than nine men' if $on{black} + $black > 9;
Branch: 1 branch
BranchTrueFalseTotal
if $on{"black"} + $black > 9 1 65936 65937
104  
105 return {
106 cells => \@cells,
107 65936 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/Terminal.pm ↑ Summary blib/lib/Game/Merrills/Result.pm »