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