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