Referee.pm

filter: Statementstmt 15.1% 22 / 145 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 55.5% 5 / 9 Podpod 0.0% 0 / 4 totaltotal 9.3% 27 / 288 CC 52 SCAR 76.6
CC
52
Cov
8%
ply
2550.0
move
12.0
new_game
2.0
CRAP 2125.8
↑ Summary Piece/Pawn.pm »
1 #
2 # Games::Chess::Referee
3 #
4 # A Perl Module for validating chess moves.
5 #
6 # Copyright (C) 1999-2006 Gregor N. Purdy. All rights reserved.
7 # This program is free software; you can redistribute it and/or
8 # modify it under the same terms as Perl iteself.
9 #
10  
11 package Games::Chess::Referee;
12  
13 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 $VERSION = '0.003';
17 1 @EXPORT = qw(&ply &move &new_game &show_board);
18 1 @EXPORT_OK = @EXPORT;
19  
20 1 ▶use Games::Chess qw(:constants :functions);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 my $board;
24  
25 1 my $occupy = '-';
26 1 my $capture = 'x';
27  
28  
29 #
30 # new_game()
31 #
32  
33 sub new_game ()
34 0 ▶{
Subroutine new_game: not called CC=1 SCAR=6.9
Pod undocumented
35 0 $board = Games::Chess::Position->new();
36  
37 # print STDERR "Game: ", $board->to_FEN(), "\n";
38 }
39  
40  
41 #
42 # show_board()
43 #
44  
45 sub show_board ()
46 0 ▶ { print $board->to_text(), "\n"; }
Subroutine show_board: not called CC=1 SCAR=6.9
Pod undocumented
47  
48  
49 #
50 # ply()
51 #
52  
53 sub ply ($)
54 0 ▶{
Subroutine ply: not called CC=50 SCAR=78.4
Pod undocumented
55 0 my ($ply) = @_;
56 0 my ($piece, $ff, $fr, $act, $tf, $tr, $note);
57 0 my $notation = undef;
58
59 #
60 # Translate castling notations:
61 #
62  
63 0 ▶ if ($ply eq '0-0') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ply eq '0-0') { } 0 0 0
elsif ($ply eq '0-0-0') { } 0 0 0
64 0 ▶ if ($board->player_to_move() eq &WHITE) { $ply = 'E1G1'; }
Branch: 1 branch
BranchTrueFalseTotal
if ($board->player_to_move eq &WHITE) { } 0 0 0
65 0 else { $ply = 'E8G8'; }
66 }
67 elsif ($ply eq '0-0-0') {
68 0 ▶ if ($board->player_to_move() eq &WHITE) { $ply = 'E1C1'; }
Branch: 1 branch
BranchTrueFalseTotal
if ($board->player_to_move eq &WHITE) { } 0 0 0
69 0 else { $ply = 'E8C8'; }
70 }
71  
72 #
73 # Parse the ply notation:
74 #
75  
76 0 ▶ if ($ply =~ m/^([prnbqkPRNBQK]|)([a-hA-H])([1-8])(x|-|)([a-hA-H])([1-8])(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ply =~ /^([prnbqkPRNBQK]|)([a-hA-H])([1-8])(x|-|)([a-hA-H])([1-8])(.*)$/) { } 0 0 0
77 0 ($piece, $ff, $fr, $act, $tf, $tr, $note) = ($ply =~ m/^([prnbqkPRNBQK]|)([a-hA-H])([1-8])(x|-|)([a-hA-H])([1-8])(.*)$/);
78 0 $piece = uc($piece);
79 0 $ff = uc($ff);
80 0 $tf = uc($tf);
81 }
82 else {
83 0 carp "Unsupported notation: `$ply'!";
84 0 return 0;
85 }
86  
87 0 my $from = lc("$ff$fr");
88 0 my $to = lc("$tf$tr");
89  
90 0 my @from = algebraic_to_xy($from);
91 0 my @to = algebraic_to_xy($to);
92  
93 0 my $from_piece = $board->at(@from);
94 0 my $to_piece = $board->at(@to);
95  
96 0 my $from_kind = uc($from_piece->code());
97 0 my $to_kind = uc($to_piece->code());
98  
99 #
100 # Check for attempts to castle:
101 #
102 # 1. Ensure castling is permitted (neither King nor Rook has moved prior).
103 # 2. Ensure the way is clear between the King and Rook.
104 # 3. Move the Rook to its final location (the King's move will be
105 # effected by the later code.
106 #
107 # TODO: Ensure that the King is not in check, and that none of the
108 # relevant squares are under attack.
109 #
110  
111 0 my $castling;
112  
113 0 ▶ if ($ff eq 'E' and $fr == 1 and $tf eq 'G' and $tr == 1) {
Branch: 4 branches
BranchTrueFalseTotal
if ($ff eq 'E' and $fr == 1 and $tf eq 'G' and $tr == 1) { } 0 0 0
elsif ($ff eq 'E' and $fr == 8 and $tf eq 'G' and $tr == 8) { } 0 0 0
elsif ($ff eq 'E' and $fr == 1 and $tf eq 'C' and $tr == 1) { } 0 0 0
elsif ($ff eq 'E' and $fr == 8 and $tf eq 'C' and $tr == 8) { } 0 0 0
Condition0%0/9
$ff eq "E" and $fr == 1 and $tf eq "G" and $tr == 1
!ll&&!rl&&r
000
$ff eq "E" and $fr == 1 and $tf eq "G"
!ll&&!rl&&r
000
$ff eq "E" and $fr == 1
!ll&&!rl&&r
000
MC/DC0%0/4
$ff eq "E" and $fr == 1 and $tf eq "G" and $tr == 1
$ff eq "E" $fr == 1 $tf eq "G" $tr == 1
Truth table0%0/5
A and B and C and D
A= $ff eq "E"B= $fr == 1C= $tf eq "G"D= $tr == 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
114 # print STDERR "ATTEMPT BY WHITE TO CASTLE SHORT...\n";
115  
116 0 ▶ if (!$board->can_castle(&WHITE, &KING)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $board->can_castle(&WHITE, &KING)) { } 0 0 0
elsif (!$board->at(5, 0)->code eq ' ') { } 0 0 0
elsif (!$board->at(6, 0)->code eq ' ') { } 0 0 0
117 0 carp "Castling short by white not permitted!";
118 # print STDERR "Game = ", $board->to_FEN(), "\n";
119 0 return 0;
120 } elsif (!$board->at(5, 0)->code() eq ' ') {
121 0 carp "Way not clear (space `f1') for castling short!";
122 0 return 0;
123 } elsif (!$board->at(6, 0)->code() eq ' ') {
124 0 carp "Way not clear (space `g1') for castling short!";
125 0 return 0;
126 } else {
127 0 $board->at(5, 0, $board->at(7, 0));
128 0 $board->at(7, 0, Games::Chess::Piece->new);
129  
130 0 $notation = '0-0';
131 0 $castling = 'SHORT';
132 }
133 }
134 ▶ elsif ($ff eq 'E' and $fr == 8 and $tf eq 'G' and $tr == 8) {
Condition0%0/9
$ff eq "E" and $fr == 8 and $tf eq "G" and $tr == 8
!ll&&!rl&&r
000
$ff eq "E" and $fr == 8 and $tf eq "G"
!ll&&!rl&&r
000
$ff eq "E" and $fr == 8
!ll&&!rl&&r
000
MC/DC0%0/4
$ff eq "E" and $fr == 8 and $tf eq "G" and $tr == 8
$ff eq "E" $fr == 8 $tf eq "G" $tr == 8
Truth table0%0/5
A and B and C and D
A= $ff eq "E"B= $fr == 8C= $tf eq "G"D= $tr == 8
ABCDresult
0XXX0
10XX0
110X0
11100
11111
135 # print STDERR "ATTEMPT BY BLACK TO CASTLE SHORT...\n";
136  
137 0 ▶ if (!$board->can_castle(&BLACK, &KING)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $board->can_castle(&BLACK, &KING)) { } 0 0 0
elsif (!$board->at(5, 7)->code eq ' ') { } 0 0 0
elsif (!$board->at(6, 7)->code eq ' ') { } 0 0 0
138 0 carp "Castling short by black not permitted!";
139 0 return 0;
140 } elsif (!$board->at(5, 7)->code() eq ' ') {
141 0 carp "Way not clear (space `f8') for castling short!";
142 0 return 0;
143 } elsif (!$board->at(6, 7)->code() eq ' ') {
144 0 carp "Way not clear (space `g8') for castling short!";
145 0 return 0;
146 } else {
147 0 $board->at(5, 7, $board->at(7, 7));
148 0 $board->at(7, 7, Games::Chess::Piece->new);
149  
150 0 $notation = '0-0';
151 0 $castling = 'SHORT';
152 }
153 }
154 ▶ elsif ($ff eq 'E' and $fr == 1 and $tf eq 'C' and $tr == 1) {
Condition0%0/9
$ff eq "E" and $fr == 1 and $tf eq "C" and $tr == 1
!ll&&!rl&&r
000
$ff eq "E" and $fr == 1 and $tf eq "C"
!ll&&!rl&&r
000
$ff eq "E" and $fr == 1
!ll&&!rl&&r
000
MC/DC0%0/4
$ff eq "E" and $fr == 1 and $tf eq "C" and $tr == 1
$ff eq "E" $fr == 1 $tf eq "C" $tr == 1
Truth table0%0/5
A and B and C and D
A= $ff eq "E"B= $fr == 1C= $tf eq "C"D= $tr == 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
155 # print STDERR "ATTEMPT BY WHITE TO CASTLE LONG...\n";
156  
157 0 ▶ if (!$board->can_castle(&WHITE, &QUEEN)) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $board->can_castle(&WHITE, &QUEEN)) { } 0 0 0
elsif (!$board->at(1, 0)->code eq ' ') { } 0 0 0
elsif (!$board->at(2, 0)->code eq ' ') { } 0 0 0
elsif (!$board->at(3, 0)->code eq ' ') { } 0 0 0
158 0 carp "Castling long by white not permitted!";
159 0 return 0;
160 } elsif (!$board->at(1, 0)->code() eq ' ') {
161 0 carp "Way not clear (space `b1') for castling long!";
162 0 return 0;
163 } elsif (!$board->at(2, 0)->code() eq ' ') {
164 0 carp "Way not clear (space `c1') for castling long!";
165 0 return 0;
166 } elsif (!$board->at(3, 0)->code() eq ' ') {
167 0 carp "Way not clear (space `d1') for castling long!";
168 0 return 0;
169 } else {
170 0 $board->at(3, 0, $board->at(0, 0));
171 0 $board->at(0, 0, Games::Chess::Piece->new);
172  
173 0 $notation = '0-0-0';
174 0 $castling = 'LONG';
175 }
176 }
177 ▶ elsif ($ff eq 'E' and $fr == 8 and $tf eq 'C' and $tr == 8) {
Condition0%0/9
$ff eq "E" and $fr == 8 and $tf eq "C" and $tr == 8
!ll&&!rl&&r
000
$ff eq "E" and $fr == 8 and $tf eq "C"
!ll&&!rl&&r
000
$ff eq "E" and $fr == 8
!ll&&!rl&&r
000
MC/DC0%0/4
$ff eq "E" and $fr == 8 and $tf eq "C" and $tr == 8
$ff eq "E" $fr == 8 $tf eq "C" $tr == 8
Truth table0%0/5
A and B and C and D
A= $ff eq "E"B= $fr == 8C= $tf eq "C"D= $tr == 8
ABCDresult
0XXX0
10XX0
110X0
11100
11111
178 # print STDERR "ATTEMPT BY BLACK TO CASTLE LONG...\n";
179  
180 0 ▶ if (!$board->can_castle(&BLACK, &QUEEN)) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $board->can_castle(&BLACK, &QUEEN)) { } 0 0 0
elsif (!$board->at(1, 0)->code eq ' ') { } 0 0 0
elsif (!$board->at(2, 0)->code eq ' ') { } 0 0 0
elsif (!$board->at(3, 0)->code eq ' ') { } 0 0 0
181 0 carp "Castling long by black not permitted!";
182 0 return 0;
183 } elsif (!$board->at(1, 0)->code() eq ' ') {
184 0 carp "Way not clear (space `b8') for castling long!";
185 0 return 0;
186 } elsif (!$board->at(2, 0)->code() eq ' ') {
187 0 carp "Way not clear (space `c8') for castling long!";
188 0 return 0;
189 } elsif (!$board->at(3, 0)->code() eq ' ') {
190 0 carp "Way not clear (space `d8') for castling long!";
191 0 return 0;
192 } else {
193 0 $board->at(3, 7, $board->at(0, 7));
194 0 $board->at(0, 7, Games::Chess::Piece->new);
195  
196 0 $notation = '0-0-0';
197 0 $castling = 'LONG';
198 }
199 }
200 else {
201 # Not castling.
202 }
203  
204 #
205 # Record new castling permissions:
206 #
207 # TODO: Write tests that exercise this code! The warnings weren't printing
208 # when they should.
209 #
210  
211 0 ▶ if ($from eq 'A1') {
Branch: 6 branches
BranchTrueFalseTotal
if ($from eq 'A1') { } 0 0 0
elsif ($from eq 'A8') { } 0 0 0
elsif ($from eq 'H1') { } 0 0 0
elsif ($from eq 'H8') { } 0 0 0
elsif ($from eq 'E1') { } 0 0 0
elsif ($from eq 'E8') { } 0 0 0
212 0 $board->can_castle(&WHITE, &QUEEN, 0);
213 # print STDERR "Warning: Castling long by white no longer permitted.\n";
214 } elsif ($from eq 'A8') {
215 0 $board->can_castle(&BLACK, &QUEEN, 0);
216 # print STDERR "Warning: Castling long by black no longer permitted.\n";
217 } elsif ($from eq 'H1') {
218 0 $board->can_castle(&WHITE, &KING, 0);
219 # print STDERR "Warning: Castling short by white no longer permitted.\n";
220 } elsif ($from eq 'H8') {
221 0 $board->can_castle(&BLACK, &KING, 0);
222 # print STDERR "Warning: Castling short by black no longer permitted.\n";
223 } elsif ($from eq 'E1') {
224 0 $board->can_castle(&WHITE, &QUEEN, 0);
225 0 $board->can_castle(&WHITE, &KING, 0);
226 # print STDERR "Warning: Castling short by white no longer permitted.\n";
227 # print STDERR "Warning: Castling long by white no longer permitted.\n";
228 } elsif ($from eq 'E8') {
229 0 $board->can_castle(&BLACK, &QUEEN, 0);
230 0 $board->can_castle(&BLACK, &KING, 0);
231 # print STDERR "Warning: Castling short by black no longer permitted.\n";
232 # print STDERR "Warning: Castling long by black no longer permitted.\n";
233 } else {
234 # No change to castling status.
235 }
236  
237 #
238 # Detect the piece:
239 #
240  
241 0 ▶ if (!$piece) { $piece = $from_kind; };
Branch: 1 branch
BranchTrueFalseTotal
unless ($piece) 0 0 0
242  
243 0 ▶ if ($piece ne $from_kind) {
Branch: 1 branch
BranchTrueFalseTotal
if ($piece ne $from_kind) 0 0 0
244 # print STDERR "\n";
245 # print STDERR "Piece: $piece\n";
246 # print STDERR "Ply: $ply\n";
247 # print STDERR "From Space: $from\n";
248 # print STDERR "From Kind: $from_kind\n";
249 0 carp "Piece (`$piece') from ply (`$ply') does not match board piece (`$from_kind') at space `$from'!";
250 0 return 0;
251 }
252  
253 #
254 # Detect the action:
255 #
256 # TODO: Make sure we only permit capture of other color's pieces.
257 #
258  
259 0 my $board_act;
260  
261 0 ▶ if ($to_kind eq ' ') { $board_act = $occupy; }
Branch: 1 branch
BranchTrueFalseTotal
if ($to_kind eq ' ') { } 0 0 0
262 0 else { $board_act = $capture; }
263  
264 0 ▶ if (!$act) { $act = $board_act; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($act) 0 0 0
265  
266 0 ▶ if ($act ne $board_act) {
Branch: 1 branch
BranchTrueFalseTotal
if ($act ne $board_act) 0 0 0
267 0 carp "Action (`$act') from ply (`$ply') does not match board (space `$to' contains `$to_kind')!";
268 0 return 0;
269 }
270  
271 #
272 # Effect the move:
273 #
274 # TODO: Deal with en passant target.
275 # TODO: Detect check and checkmate for notes (and validate against those
276 # passed in, if any).
277 # TODO: Detect en passant capture for notes.
278 # TODO: Detect en passant capture for notes.
279 # TODO: Detect illegal moves based on move pattern of piece, or intervening
280 # pieces, etc.
281 # TODO: Detect forced for notes.
282 #
283  
284 0 $board->at(@to, $from_piece);
285 0 $board->at(@from, Games::Chess::Piece->new);
286  
287 #
288 # Print the move:
289 #
290  
291 0 ▶ if (!defined $notation) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $notation) 0 0 0
292 0 ▶ if ($from_kind eq 'P') { $piece = ' '; }
Branch: 1 branch
BranchTrueFalseTotal
if ($from_kind eq "P") 0 0 0
293 0 $notation = $piece . lc($from) . $act . lc($to) . $note;
294 0 $notation = $notation . (' ' x (8 - length($notation)));
295 }
296  
297 0 ▶ if ($board->player_to_move() == &WHITE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($board->player_to_move == &WHITE) { } 0 0 0
298 0 print $board->move_number(), ". $notation ";
299  
300 0 $board->player_to_move(&BLACK);
301 0 $board->halfmove_clock($board->halfmove_clock() + 1);
302 }
303 else {
304 0 print "$notation\n";
305  
306 0 $board->player_to_move(&WHITE);
307 0 $board->halfmove_clock($board->halfmove_clock() + 1);
308 0 $board->move_number($board->move_number() + 1);
309 }
310  
311 0 return 1;
312 }
313  
314  
315 #
316 # move()
317 #
318  
319 sub move ($$)
320 0 ▶{
Subroutine move: not called CC=3 SCAR=24.8
Pod undocumented
321 0 ▶ if (!&ply($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&ply($_[0])) 0 0 0
322 0 carp "First ply (`$_[0]') of move failed.";
323 0 return 0;
324 }
325  
326 0 ▶ if (!&ply($_[1])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&ply($_[1])) 0 0 0
327 0 carp "Second ply (`$_[1]') of move failed.";
328 0 return 0;
329 }
330  
331 0 return 1;
332 }
333  
334  
335 #
336 # Return success:
337 #
338  
339 1 1;
340  
341 #
342 # End of file.
343 #
↑ Summary Piece/Pawn.pm »