Merrills/Notation.pm untested

filter: Statementstmt 0.0% 0 / 145 Branchbran 0.0% 0 / 104 Conditioncond 0.0% 0 / 64 MC/DCmcdc 0.0% 0 / 40 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 376 CC 53 SCAR 45.0
CC
53
Cov
0%
format_record
182.0
record_state
132.0
parse_position
110.0
CRAP 89.6
1 package Game::Merrills::Notation;
2  
3 use strict;
4 use warnings;
5  
6 use Game::Merrills::Points;
7  
8 our $VERSION = '0.03';
9  
10 our (%CELL, %GLYPH, %TURN, %LETTER, %SIDE);
11  
12 BEGIN {
13 %CELL = ('.' => 0, W => 1, B => -1);
14 %GLYPH = (0 => '.', 1 => 'W', -1 => 'B');
15 %TURN = (w => 'white', b => 'black');
16 %LETTER = (white => 'w', black => 'b');
17 %SIDE = (white => 1, black => 1);
18 }
19  
20 sub parse_move {
21 my ($text) = @_;
22 return undef unless defined $text && !ref $text;
23 my ($from, $to, $remove) = $text =~ m/
24 ^\s*
25 ([a-g][1-7])
26 (?:\s*-\s*([a-g][1-7]))?
27 (?:\s*x\s*([a-g][1-7]))?
28 \s*$
29 /xi or return undef;
30 ($from, $to) = (undef, $from) unless defined $to;
31 my %move;
32 for my $part ([from => $from], [to => $to], [remove => $remove]) {
33 my ($key, $name) = @{$part};
34 next unless defined $name;
35 my $point = Game::Merrills::Points::point($name);
36 return undef unless defined $point;
37 $move{$key} = $point;
38 }
39 return undef if defined $move{from} && $move{from} == $move{to};
40 return undef if defined $move{remove}
41 && ($move{remove} == $move{to}
42 || (defined $move{from} && $move{remove} == $move{from}));
43 return \%move;
44 }
45  
46 sub format_move {
47 my ($move) = @_;
48 die 'a move needs a point to go to' unless ref $move eq 'HASH' && defined $move->{to};
49 my $text = Game::Merrills::Points::name($move->{to});
50 $text = Game::Merrills::Points::name($move->{from}) . '-' . $text
51 if defined $move->{from};
52 $text .= 'x' . Game::Merrills::Points::name($move->{remove})
53 if defined $move->{remove};
54 return $text;
55 }
56  
57 sub format_position {
58 my ($position) = @_;
59 die 'position: not a hashref' unless ref $position eq 'HASH';
60 my $cells = $position->{cells};
61 die 'position: cells must be an arrayref of 24 values'
62 unless ref $cells eq 'ARRAY' && @{$cells} == Game::Merrills::Points::POINTS;
63 my $board = '';
64 for my $point (Game::Merrills::Points::all_points()) {
65 my $value = $cells->[$point];
66 die 'position: cell ' . Game::Merrills::Points::name($point) . ' must be -1, 0 or 1'
67 unless defined $value && exists $GLYPH{$value};
68 $board .= $GLYPH{$value};
69 }
70 my $turn = $position->{turn};
71 die 'position: turn must be white or black'
72 unless defined $turn && exists $LETTER{$turn};
73 my $hand = $position->{hand} || {};
74 my $text = join ' ', $board, $LETTER{$turn},
75 map({ defined $hand->{$_} ? $hand->{$_} : 0 } qw/white black/),
76 $position->{no_mill} || 0, $position->{ply} || 0;
77 parse_position($text);
78 return $text;
79 }
80  
81 sub parse_position {
82 my ($text) = @_;
83 die 'position: nothing to read' unless defined $text && !ref $text;
84 my @field = split ' ', $text;
85 die 'position: six fields are needed, got ' . scalar @field unless @field == 6;
86 my ($board, $turn, $white, $black, $no_mill, $ply) = @field;
87  
88 die 'position: the board must be 24 of W, B and .'
89 unless $board =~ m/^[WB.]{24}$/;
90 die "position: the side to move must be w or b, got '$turn'"
91 unless exists $TURN{$turn};
92 for my $count ([white => $white], [black => $black]) {
93 die "position: men in hand for $count->[0] must be 0 .. 9, got '$count->[1]'"
94 unless $count->[1] =~ m/^[0-9]$/;
95 }
96 die "position: plies since a mill must be a number, got '$no_mill'"
97 unless $no_mill =~ m/^[0-9]+$/;
98 die "position: the ply must be a number, got '$ply'"
99 unless $ply =~ m/^[0-9]+$/;
100  
101 my @cells = map { $CELL{$_} } split //, $board;
102 my %on = (white => scalar(grep { $_ == 1 } @cells), black => scalar(grep { $_ == -1 } @cells));
103 die 'position: white has more than nine men' if $on{white} + $white > 9;
104 die 'position: black has more than nine men' if $on{black} + $black > 9;
105  
106 return {
107 cells => \@cells,
108 turn => $TURN{$turn},
109 hand => { white => $white + 0, black => $black + 0 },
110 no_mill => $no_mill + 0,
111 ply => $ply + 0,
112 };
113 }
114  
115 sub format_record {
116 my ($moves, %option) = @_;
117 die 'record: moves must be an arrayref' unless ref $moves eq 'ARRAY';
118 my $header = '';
119 if (defined $option{position}) {
120 $header = 'position ' . format_position(parse_position($option{position})) . "\n";
121 }
122 $header .= "flying no\n" if defined $option{flying} && !$option{flying};
123 my $footer = '';
124 if (defined $option{offer}) {
125 die "record: the offer must be white or black, got '$option{offer}'"
126 unless $SIDE{ $option{offer} };
127 $footer .= "offer $option{offer}\n";
128 }
129 if (defined $option{result}) {
130 my ($winner, $reason) = @{ $option{result} };
131 $winner = 'draw' unless defined $winner;
132 die "record: the result must be a winner or draw and a reason, got '$winner $reason'"
133 unless _result_ok($winner, $reason);
134 $footer .= "result $winner $reason\n";
135 }
136 my @text;
137 for my $i (0 .. $#{$moves}) {
138 my $move = $moves->[$i];
139 my $written = !ref $move ? $move
140 : ref $move eq 'HASH' ? format_move($move)
141 : $move->notation;
142 die 'record: move ' . ($i + 1) . ', ' . (defined $written ? "'$written'" : 'undef')
143 . ' is not a move'
144 unless parse_move($written);
145 push @text, format_move(parse_move($written));
146 }
147 my @line;
148 for (my $i = 0; $i < @text; $i += 2) {
149 push @line, join ' ', (($i / 2) + 1) . '.', grep { defined } @text[ $i, $i + 1 ];
150 }
151 return join '', $header, (map { "$_\n" } @line), $footer;
152 }
153  
154 sub parse_record {
155 my ($text) = @_;
156 die 'record: nothing to read' unless defined $text && !ref $text;
157 $text =~ s/^[ \t]*#[^\n]*$//mg;
158 $text =~ s/^[ \t]*position[ \t]+[^\n]*$//m;
159 $text =~ s/^[ \t]*(?:flying|offer|result)[ \t]+[^\n]*$//mg;
160 my @moves;
161 for my $token (split ' ', $text) {
162 next if $token =~ m/^[0-9]+\.$/;
163 my $move = parse_move($token);
164 die 'record: move ' . (@moves + 1) . ", '$token' is not a move" unless $move;
165 push @moves, $move;
166 }
167 return \@moves;
168 }
169  
170 sub record_position {
171 my ($text) = @_;
172 die 'record: nothing to read' unless defined $text && !ref $text;
173 my ($position) = $text =~ m/^[ \t]*position[ \t]+([^\n]*)$/m or return undef;
174 return format_position(parse_position($position));
175 }
176  
177 sub record_state {
178 my ($text) = @_;
179 die 'record: nothing to read' unless defined $text && !ref $text;
180 my %state;
181 for my $line (split m/\n/, $text) {
182 next unless $line =~ m/^[ \t]*(flying|offer|result)[ \t]+(.*?)[ \t]*$/;
183 my ($word, $value) = ($1, $2);
184 die "record: a second $word line" if exists $state{$word};
185 if ($word eq 'flying') {
186 die "record: flying must be yes or no, got '$value'" unless $value =~ m/^(?:yes|no)$/;
187 $state{flying} = $value eq 'yes' ? 1 : 0;
188 }
189 elsif ($word eq 'offer') {
190 die "record: the offer must be white or black, got '$value'" unless $SIDE{$value};
191 $state{offer} = $value;
192 }
193 else {
194 my ($winner, $reason) = split ' ', $value;
195 die "record: the result must be a winner or draw and a reason, got '$value'"
196 unless _result_ok($winner, $reason) && $value =~ m/^\S+ \S+$/;
197 $state{result} = [ $winner eq 'draw' ? undef : $winner, $reason ];
198 }
199 }
200 return \%state;
201 }
202  
203 sub _result_ok {
204 my ($winner, $reason) = @_;
205 return 0 unless defined $winner && defined $reason;
206 return $winner eq 'draw' if $reason eq 'agreement';
207 return $SIDE{$winner} && ($reason eq 'resign' || $reason eq 'timeout') ? 1 : 0;
208 }
209  
210 1;
211  
212 __END__