]> git.sesse.net Git - remoteglot/blob - remoteglot.pl
69ba997470827c689068c9909425471c3438257d
[remoteglot] / remoteglot.pl
1 #! /usr/bin/perl
2
3 #
4 # remoteglot - Connects an abitrary UCI-speaking engine to ICS for easier post-game
5 #              analysis, or for live analysis of relayed games. (Do not use for
6 #              cheating! Cheating is bad for your karma, and your abuser flag.)
7 #
8 # Copyright 2007 Steinar H. Gunderson <sgunderson@bigfoot.com>
9 # Licensed under the GNU General Public License, version 2.
10 #
11
12 use Net::Telnet;
13 use FileHandle;
14 use IPC::Open2;
15 use Time::HiRes;
16 use strict;
17 use warnings;
18
19 # Configuration
20 my $server = "freechess.org";
21 my $target = "Sesse";
22 # my $engine = "/usr/games/toga2";
23 my $engine = "wine Rybkav2.3.2a.mp.w32.exe";
24
25 # Program starts here
26 $SIG{ALRM} = sub { output_screen(); };
27
28 $| = 1;
29
30 open(FICSLOG, ">ficslog.txt")
31         or die "ficslog.txt: $!";
32 print FICSLOG "Log starting.\n";
33 select(FICSLOG);
34 $| = 1;
35
36 open(UCILOG, ">ucilog.txt")
37         or die "ucilog.txt: $!";
38 print UCILOG "Log starting.\n";
39 select(UCILOG);
40 $| = 1;
41 select(STDOUT);
42
43 # open the chess engine
44 my $pid = IPC::Open2::open2(*UCIREAD, *UCIWRITE, $engine);
45 my %uciinfo = ();
46 my %uciid = ();
47 my %ficsinfo = ();
48
49 uciprint("uci");
50
51 # gobble the options
52 while (<UCIREAD>) {
53         /uciok/ && last;
54         handle_uci($_);
55 }
56
57 uciprint("setoption name UCI_AnalyseMode value true");
58 uciprint("setoption name NalimovPath value c:\\nalimov");
59 uciprint("setoption name NalimovUsage value Rarely");
60 uciprint("setoption name Hash value 1024");
61 uciprint("setoption name MultiPV value 3");
62 # uciprint("setoption name Contempt value 1000");
63 # uciprint("setoption name Outlook value Ultra Optimistic");
64 uciprint("ucinewgame");
65
66 print "Chess engine ready.\n";
67
68 # now talk to FICS
69 my $t = Net::Telnet->new(Timeout => 10, Prompt => '/fics% /');
70 $t->input_log(\*FICSLOG);
71 $t->open($server);
72 $t->print("guest");
73 $t->waitfor('/Press return to enter the server/');
74 $t->cmd("");
75
76 # set some options
77 $t->cmd("set shout 0");
78 $t->cmd("set seek 0");
79 $t->cmd("set style 12");
80 $t->cmd("observe $target");
81
82 # main loop
83 print "FICS ready.\n";
84 while (1) {
85         my $rin = '';
86         my $rout;
87         vec($rin, fileno(UCIREAD), 1) = 1;
88         vec($rin, fileno($t), 1) = 1;
89
90         my ($nfound, $timeleft) = select($rout=$rin, undef, undef, 5.0);
91         my $sleep = 1.0;
92
93         while (1) {
94                 my $line = $t->getline(Timeout => 0, errmode => 'return');
95                 last if (!defined($line));
96
97                 chomp $line;
98                 $line =~ tr/\r//d;
99                 if ($line =~ /^<12> /) {
100                         my $fen = style12_to_fen($line);
101                         uciprint("stop");
102                         uciprint("position fen $fen");
103                         uciprint("go infinite");
104
105                         # 
106                         # Output a command every move to note that we're
107                         # still paying attention -- this is a good tradeoff,
108                         # since if no move has happened in the last half
109                         # hour, the analysis/relay has most likely stopped
110                         # and we should stop hogging server resources.
111                         #
112                         $t->cmd("date");
113                 }
114                 #print "FICS: [$line]\n";
115                 $sleep = 0;
116         }
117         
118         # any fun on the UCI channel?
119         if ($nfound > 0 && vec($rout, fileno(UCIREAD), 1) == 1) {
120                 my $line = <UCIREAD>;
121                 handle_uci($line);
122                 $sleep = 0;
123
124                 # don't update too often
125                 Time::HiRes::alarm(0.2);
126         }
127
128         sleep $sleep;
129 }
130
131 sub handle_uci {
132         my ($line) = @_;
133
134         chomp $line;
135         $line =~ tr/\r//d;
136         print UCILOG "<= $line\n";
137         if ($line =~ /^info/) {
138                 my (@infos) = split / /, $line;
139                 shift @infos;
140
141                 parse_infos(@infos);
142         }
143         if ($line =~ /^id/) {
144                 my (@ids) = split / /, $line;
145                 shift @ids;
146
147                 parse_ids(@ids);
148         }
149 }
150
151 sub parse_infos {
152         my (@x) = @_;
153         my $mpv = '';
154
155         while (scalar @x > 0) {
156                 if ($x[0] =~ 'multipv') {
157                         shift @x;
158                         $mpv = shift @x;
159                         next;
160                 }
161                 if ($x[0] =~ /^(currmove|currmovenumber|cpuload)$/) {
162                         my $key = shift @x;
163                         my $value = shift @x;
164                         $uciinfo{$key} = $value;
165                         next;
166                 }
167                 if ($x[0] =~ /^(depth|seldepth|hashfull|time|nodes|nps|tbhits)$/) {
168                         my $key = shift @x;
169                         my $value = shift @x;
170                         $uciinfo{$key . $mpv} = $value;
171                         next;
172                 }
173                 if ($x[0] eq 'score') {
174                         shift @x;
175
176                         delete $uciinfo{'score_cp' . $mpv};
177                         delete $uciinfo{'score_mate' . $mpv};
178
179                         while ($x[0] =~ /^(cp|mate|lowerbound|upperbound)$/) {
180                                 if ($x[0] eq 'cp') {
181                                         shift @x;
182                                         $uciinfo{'score_cp' . $mpv} = shift @x;
183                                 } elsif ($x[0] eq 'mate') {
184                                         shift @x;
185                                         $uciinfo{'score_mate' . $mpv} = shift @x;
186                                 } else {
187                                         shift @x;
188                                 }
189                         }
190                         next;
191                 }
192                 if ($x[0] eq 'pv') {
193                         $uciinfo{'pv' . $mpv} = [ @x[1..$#x] ];
194                         last;
195                 }
196                 if ($x[0] eq 'string' || $x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting' || $x[0] eq 'contempt') {
197                         last;
198                 }
199
200                 #print "unknown info '$x[0]', trying to recover...\n";
201                 #shift @x;
202                 die "Unknown info '" . join(',', @x) . "'";
203
204         }
205 }
206
207 sub parse_ids {
208         my (@x) = @_;
209
210         while (scalar @x > 0) {
211                 if ($x[0] =~ /^(name|author)$/) {
212                         my $key = shift @x;
213                         my $value = join(' ', @x);
214                         $uciid{$key} = $value;
215                         last;
216                 }
217
218                 # unknown
219                 shift @x;
220         }
221 }
222
223 sub style12_to_fen {
224         my $str = shift; 
225         my (@x) = split / /, $str;
226         
227         $ficsinfo{'board'} = [ @x[1..8] ];
228         $ficsinfo{'toplay'} = $x[9];
229         
230         # the board itself
231         my (@board) = @x[1..8];
232         for my $rank (0..7) {
233                 $board[$rank] =~ s/(-+)/length($1)/ge;
234         }
235         my $fen = join('/', @board);
236
237         # white/black to move
238         $fen .= " ";
239         $fen .= lc($x[9]);
240
241         # castling
242         my $castling = "";
243         $castling .= "K" if ($x[11] == 1);
244         $castling .= "Q" if ($x[12] == 1);
245         $castling .= "k" if ($x[13] == 1);
246         $castling .= "q" if ($x[14] == 1);
247         $castling = "-" if ($castling eq "");
248         $fen .= " ";
249         $fen .= $castling;
250
251         # en passant
252         my $ep = "-";
253         if ($x[10] != -1) {
254                 my $col = $x[10];
255                 my $nep = (qw(a b c d e f g h))[$col];
256
257                 if ($x[9] eq 'B') {
258                         $nep .= "3";
259                 } else {
260                         $nep .= "6";
261                 }
262                 
263                 #
264                 # Showing the en passant square when actually no capture can be made
265                 # seems to confuse at least Rybka. Thus, check if there's actually
266                 # a pawn of the opposite side that can do the en passant move, and if
267                 # not, just lie -- it doesn't matter anyway. I'm unsure what's the
268                 # "right" thing as per the standard, though.
269                 #
270                 if ($x[9] eq 'B') {
271                         $ep = $nep if ($col > 0 && substr($board[4], $col-1, 1) eq 'p');
272                         $ep = $nep if ($col < 7 && substr($board[4], $col+1, 1) eq 'p');
273                 } else {
274                         $ep = $nep if ($col > 0 && substr($board[3], $col-1, 1) eq 'P');
275                         $ep = $nep if ($col < 7 && substr($board[3], $col+1, 1) eq 'P');
276                 }
277         }
278         $fen .= " ";
279         $fen .= $ep;
280
281         # half-move clock
282         $fen .= " ";
283         $fen .= $x[15];
284
285         # full-move clock
286         $fen .= " ";
287         $fen .= $x[26];
288
289         return $fen;
290 }
291
292 sub prettyprint_pv {
293         my ($board, @pvs) = @_;
294         
295         if (scalar @pvs == 0 || !defined($pvs[0])) {
296                 return ();
297         }
298         
299         my @nb = @$board;
300
301         my $pv = shift @pvs;
302         my $from_col = ord(substr($pv, 0, 1)) - ord('a');
303         my $from_row = 7 - (ord(substr($pv, 1, 1)) - ord('1'));
304         my $to_col   = ord(substr($pv, 2, 1)) - ord('a');
305         my $to_row   = 7 - (ord(substr($pv, 3, 1)) - ord('1'));
306
307         my $pretty;
308         my $piece = substr($board->[$from_row], $from_col, 1);
309
310         if ($piece eq '-') {
311                 die "Invalid move";
312         }
313
314         # white short castling
315         if ($pv eq 'e1g1' && $piece eq 'K') {
316                 # king
317                 substr($nb[7], 4, 1, '-');
318                 substr($nb[7], 6, 1, $piece);
319                 
320                 # rook
321                 substr($nb[7], 7, 1, '-');
322                 substr($nb[7], 5, 1, 'R');
323                                 
324                 return ('0-0', prettyprint_pv(\@nb, @pvs));
325         }
326
327         # white long castling
328         if ($pv eq 'e1c1' && $piece eq 'K') {
329                 # king
330                 substr($nb[7], 4, 1, '-');
331                 substr($nb[7], 2, 1, $piece);
332                 
333                 # rook
334                 substr($nb[7], 0, 1, '-');
335                 substr($nb[7], 3, 1, 'R');
336                                 
337                 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
338         }
339
340         # black short castling
341         if ($pv eq 'e8g8' && $piece eq 'k') {
342                 # king
343                 substr($nb[0], 4, 1, '-');
344                 substr($nb[0], 6, 1, $piece);
345                 
346                 # rook
347                 substr($nb[0], 7, 1, '-');
348                 substr($nb[0], 5, 1, 'r');
349                                 
350                 return ('0-0', prettyprint_pv(\@nb, @pvs));
351         }
352
353         # black long castling
354         if ($pv eq 'e8c8' && $piece eq 'k') {
355                 # king
356                 substr($nb[0], 4, 1, '-');
357                 substr($nb[0], 2, 1, $piece);
358                 
359                 # rook
360                 substr($nb[0], 0, 1, '-');
361                 substr($nb[0], 3, 1, 'r');
362                                 
363                 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
364         }
365
366         # check if the from-piece is a pawn
367         if (lc($piece) eq 'p') {
368                 # attack?
369                 if ($from_col != $to_col) {
370                         $pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
371
372                         # en passant?
373                         if (substr($board->[$to_row], $to_col, 1) eq '-') {
374                                 if ($piece eq 'p') {
375                                         substr($nb[$to_row + 1], $to_col, 1, '-');
376                                 } else {
377                                         substr($nb[$to_row - 1], $to_col, 1, '-');
378                                 }
379                         }
380                 } else {
381                         $pretty = substr($pv, 2, 2);
382
383                         if (length($pv) == 5) {
384                                 # promotion
385                                 $pretty .= "=";
386                                 $pretty .= uc(substr($pv, 4, 1));
387
388                                 if ($piece eq 'p') {
389                                         $piece = substr($pv, 4, 1);
390                                 } else {
391                                         $piece = uc(substr($pv, 4, 1));
392                                 }
393                         }
394                 }
395         } else {
396                 $pretty = uc($piece);
397
398                 # see how many of these pieces could go here, in all
399                 my $num_total = 0;
400                 for my $col (0..7) {
401                         for my $row (0..7) {
402                                 next unless (substr($board->[$row], $col, 1) eq $piece);
403                                 ++$num_total if (can_reach($board, $piece, $row, $col, $to_row, $to_col));
404                         }
405                 }
406
407                 # see how many of these pieces from the given row could go here
408                 my $num_row = 0;
409                 for my $col (0..7) {
410                         next unless (substr($board->[$from_row], $col, 1) eq $piece);
411                         ++$num_row if (can_reach($board, $piece, $from_row, $col, $to_row, $to_col));
412                 }
413                 
414                 # and same for columns
415                 my $num_col = 0;
416                 for my $row (0..7) {
417                         next unless (substr($board->[$row], $from_col, 1) eq $piece);
418                         ++$num_col if (can_reach($board, $piece, $row, $from_col, $to_row, $to_col));
419                 }
420                 
421                 # see if we need to disambiguate
422                 if ($num_total > 1) {
423                         if ($num_col == 1) {
424                                 $pretty .= substr($pv, 0, 1);
425                         } elsif ($num_row == 1) {
426                                 $pretty .= substr($pv, 1, 1);
427                         } else {
428                                 $pretty .= substr($pv, 0, 2);
429                         }
430                 }
431
432                 # attack?
433                 if (substr($board->[$to_row], $to_col, 1) ne '-') {
434                         $pretty .= 'x';
435                 }
436
437                 $pretty .= substr($pv, 2, 2);
438         }
439
440         # update the board
441         substr($nb[$from_row], $from_col, 1, '-');
442         substr($nb[$to_row], $to_col, 1, $piece);
443
444         if (in_mate(\@nb)) {
445                 $pretty .= '#';
446         } elsif (in_check(\@nb) ne 'none') {
447                 $pretty .= '+';
448         }
449
450         return ($pretty, prettyprint_pv(\@nb, @pvs));
451 }
452
453 sub output_screen {
454         #return;
455
456         #
457         # Check the PVs first. if they're invalid, just wait, as our data
458         # is most likely out of sync. This isn't a very good solution, as
459         # it can frequently miss stuff, but it's good enough for most users.
460         #
461         eval {
462                 my $dummy;
463                 if (exists($uciinfo{'pv'})) {
464                         $dummy = prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv'}});
465                 }
466         
467                 my $mpv = 1;
468                 while (exists($uciinfo{'pv' . $mpv})) {
469                         $dummy = prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv' . $mpv}});
470                         ++$mpv;
471                 }
472         };
473         if ($@) {
474                 return;
475         }
476
477         print "\e[H\e[2J"; # clear the screen
478         if (exists($uciid{'name'})) {
479                 print "Analysis by $uciid{'name'}:\n\n";
480         } else {
481                 print "Analysis:\n\n";
482         }
483
484         return unless (exists($ficsinfo{'board'}));
485
486         if (exists($uciinfo{'pv1'}) && exists($uciinfo{'pv2'})) {
487                 # multi-PV
488                 my $mpv = 1;
489                 while (exists($uciinfo{'pv' . $mpv})) {
490                         printf "  PV%2u", $mpv;
491
492                         if (defined($uciinfo{'score_mate' . $mpv})) {
493                                 printf " (M%3d)", $uciinfo{'score_mate' . $mpv};
494                         } else {
495                                 if (exists($uciinfo{'score_cp' . $mpv})) {
496                                         my $score = $uciinfo{'score_cp' . $mpv} * 0.01;
497                                         if ($ficsinfo{'toplay'} eq 'B') {
498                                                 $score = -$score;
499                                         }
500                                         printf " (%+5.2f)", $score;
501                                 }
502                         }
503                         
504                         if (exists($uciinfo{'nodes' . $mpv}) && exists($uciinfo{'nps' . $mpv}) && exists($uciinfo{'depth' . $mpv})) {
505                                 printf " (%5u kn, %3u kn/s, %2u ply)",
506                                         $uciinfo{'nodes' . $mpv} / 1000, $uciinfo{'nps' . $mpv} / 1000, $uciinfo{'depth' . $mpv};
507                         }
508
509                         print ":\n";
510                         print "  ", join(', ', prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv' . $mpv}})), "\n";
511                         print "\n";
512                         ++$mpv;
513                 }
514         } else {
515                 #
516                 # Some programs _always_ report MultiPV, even with only one PV.
517                 # In this case, we simply use that data as if MultiPV was never
518                 # specified.
519                 #
520                 if (exists($uciinfo{'pv1'})) {
521                         for my $key qw(pv score_cp score_mate nodes nps depth seldepth tbhits) {
522                                 if (exists($uciinfo{$key . '1'}) && !exists($uciinfo{$key})) {
523                                         $uciinfo{$key} = $uciinfo{$key . '1'};
524                                 }
525                         }
526                 }
527
528                 # single-PV
529                 if (defined($uciinfo{'score_mate'})) {
530                         my $mate = $uciinfo{'score_mate'};
531                         if ($ficsinfo{'toplay'} eq 'B') {
532                                 $mate = -$mate;
533                         }
534                         if ($mate > 0) {
535                                 printf "  White mates in %u\n", $mate;
536                         } else {
537                                 printf "  Black mates in %u\n", -$mate;
538                         }
539                 } else {
540                         if (exists($uciinfo{'score_cp'})) {
541                                 my $score = $uciinfo{'score_cp'} * 0.01;
542                                 if ($ficsinfo{'toplay'} eq 'B') {
543                                         $score = -$score;
544                                 }
545                                 printf "  Score: %+5.2f\n", $score;
546                         }
547                 }
548
549                 print  "  PV: ", join(', ', prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv'}}));
550                 print  "\n";
551
552                 if (exists($uciinfo{'nodes'}) && exists($uciinfo{'nps'}) && exists($uciinfo{'depth'})) {
553                         printf "  %u nodes, %7u nodes/sec, depth %u ply",
554                                 $uciinfo{'nodes'}, $uciinfo{'nps'}, $uciinfo{'depth'};
555                 }
556                 if (exists($uciinfo{'tbhits'})) {
557                         printf ", %u Nalimov hits", $uciinfo{'tbhits'};
558                 }
559                 if (exists($uciinfo{'seldepth'})) {
560                         printf " (%u selective)", $uciinfo{'seldepth'};
561                 }
562                 print  "\n\n";
563         }
564 }
565
566 sub find_kings {
567         my $board = shift;
568         my ($wkr, $wkc, $bkr, $bkc);
569
570         for my $row (0..7) {
571                 for my $col (0..7) {
572                         my $piece = substr($board->[$row], $col, 1);
573                         if ($piece eq 'K') {
574                                 ($wkr, $wkc) = ($row, $col);
575                         } elsif ($piece eq 'k') {
576                                 ($bkr, $bkc) = ($row, $col);
577                         }
578                 }
579         }
580
581         return ($wkr, $wkc, $bkr, $bkc);
582 }
583
584 sub in_mate {
585         my $board = shift;
586         my $check = in_check($board);
587         return 0 if ($check eq 'none');
588
589         # try all possible moves for the side in check
590         for my $row (0..7) {
591                 for my $col (0..7) {
592                         my $piece = substr($board->[$row], $col, 1);
593                         next if ($piece eq '-');
594
595                         if ($check eq 'white') {
596                                 next if ($piece eq lc($piece));
597                         } else {
598                                 next if ($piece eq uc($piece));
599                         }
600
601                         for my $dest_row (0..7) {
602                                 for my $dest_col (0..7) {
603                                         next if ($row == $dest_row && $col == $dest_col);
604                                         next unless (can_reach($board, $piece, $row, $col, $dest_row, $dest_col));
605
606                                         my @nb = @$board;
607                                         substr($nb[$row], $col, 1, '-');
608                                         substr($nb[$dest_row], $dest_col, 1, $piece);
609
610                                         my $new_check = in_check(\@nb);
611                                         return 0 if ($new_check ne $check && $new_check ne 'both');
612                                 }
613                         }
614                 }
615         }
616
617         # nothing to do; mate
618         return 1;
619 }
620
621 sub in_check {
622         my $board = shift;
623         my ($black_check, $white_check) = (0, 0);
624
625         my ($wkr, $wkc, $bkr, $bkc) = find_kings($board);
626
627         # check all pieces for the possibility of threatening the two kings
628         for my $row (0..7) {
629                 for my $col (0..7) {
630                         my $piece = substr($board->[$row], $col, 1);
631                         next if ($piece eq '-');
632                 
633                         if (uc($piece) eq $piece) {
634                                 # white piece
635                                 $black_check = 1 if (can_reach($board, $piece, $row, $col, $bkr, $bkc));
636                         } else {
637                                 # black piece
638                                 $white_check = 1 if (can_reach($board, $piece, $row, $col, $wkr, $wkc));
639                         }
640                 }
641         }
642
643         if ($black_check && $white_check) {
644                 return 'both';
645         } elsif ($black_check) {
646                 return 'black';
647         } elsif ($white_check) {
648                 return 'white';
649         } else {
650                 return 'none';
651         }
652 }
653
654 sub can_reach {
655         my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
656         
657         # can't eat your own piece
658         my $dest_piece = substr($board->[$to_row], $to_col, 1);
659         if ($dest_piece ne '-') {
660                 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
661         }
662
663         if (lc($piece) eq 'k') {
664                 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
665         }
666         if (lc($piece) eq 'r') {
667                 return 0 unless ($from_row == $to_row || $from_col == $to_col);
668
669                 # check that there's a clear passage
670                 if ($from_row == $to_row) {
671                         if ($from_col > $to_col) {
672                                 ($to_col, $from_col) = ($from_col, $to_col);
673                         }
674
675                         for my $c (($from_col+1)..($to_col-1)) {
676                                 my $middle_piece = substr($board->[$to_row], $c, 1);
677                                 return 0 if ($middle_piece ne '-');     
678                         }
679
680                         return 1;
681                 } else {
682                         if ($from_row > $to_row) {
683                                 ($to_row, $from_row) = ($from_row, $to_row);
684                         }
685
686                         for my $r (($from_row+1)..($to_row-1)) {
687                                 my $middle_piece = substr($board->[$r], $to_col, 1);
688                                 return 0 if ($middle_piece ne '-');     
689                         }
690
691                         return 1;
692                 }
693         }
694         if (lc($piece) eq 'b') {
695                 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
696
697                 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
698                 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
699
700                 my $r = $from_row + $dr;
701                 my $c = $from_col + $dc;
702
703                 while ($r != $to_row) {
704                         my $middle_piece = substr($board->[$r], $c, 1);
705                         return 0 if ($middle_piece ne '-');
706                         
707                         $r += $dr;
708                         $c += $dc;
709                 }
710
711                 return 1;
712         }
713         if (lc($piece) eq 'n') {
714                 my $diff_r = abs($from_row - $to_row);
715                 my $diff_c = abs($from_col - $to_col);
716                 return 1 if ($diff_r == 2 && $diff_c == 1);
717                 return 1 if ($diff_r == 1 && $diff_c == 2);
718                 return 0;
719         }
720         if ($piece eq 'q') {
721                 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
722                         can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
723         }
724         if ($piece eq 'Q') {
725                 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
726                         can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
727         }
728         if ($piece eq 'p') {
729                 # black pawn
730                 if ($to_col == $from_col && $to_row == $from_row + 1) {
731                         return ($dest_piece eq '-');
732                 }
733                 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
734                         return ($dest_piece ne '-');
735                 }
736                 return 0;
737         }
738         if ($piece eq 'P') {
739                 # white pawn
740                 if ($to_col == $from_col && $to_row == $from_row - 1) {
741                         return ($dest_piece eq '-');
742                 }
743                 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
744                         return ($dest_piece ne '-');
745                 }
746                 return 0;
747         }
748         
749         # unknown piece
750         return 0;
751 }
752
753 sub uciprint {
754         my $msg = shift;
755         print UCIWRITE "$msg\n";
756         print UCILOG "=> $msg\n";
757 }