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.)
8 # Copyright 2007 Steinar H. Gunderson <sgunderson@bigfoot.com>
9 # Licensed under the GNU General Public License, version 2.
19 $SIG{ALRM} = sub { output_screen(); };
23 my $server = "freechess.org";
25 # my $engine = "/usr/games/toga2";
26 my $engine = "wine rybka22-mpw32.exe";
28 # open the chess engine
29 my $pid = IPC::Open2::open2(*UCIREAD, *UCIWRITE, $engine);
32 print UCIWRITE "uci\n";
39 print UCIWRITE "setoption name UCI_AnalyseMode value true\n";
40 print UCIWRITE "setoption name NalimovPath value c:\\nalimov\n";
41 print UCIWRITE "setoption name NalimovUsage value Normally\n";
42 # print UCIWRITE "setoption name Contempt value 1000\n";
43 # print UCIWRITE "setoption name Outlook value Ultra Optimistic\n";
44 print UCIWRITE "ucinewgame\n";
46 print "Chess engine ready.\n";
49 my $t = Net::Telnet->new(Timeout => 10, Prompt => '/fics% /');
50 #$t->input_log(\*STDOUT);
53 $t->waitfor('/Press return to enter the server/');
57 $t->cmd("set shout 0");
58 $t->cmd("set seek 0");
59 $t->cmd("set style 12");
60 $t->cmd("observe $target");
63 print "FICS ready.\n";
67 vec($rin, fileno(UCIREAD), 1) = 1;
68 vec($rin, fileno($t), 1) = 1;
70 my ($nfound, $timeleft) = select($rout=$rin, undef, undef, 5.0);
74 my $line = $t->getline(Timeout => 0, errmode => 'return');
75 last if (!defined($line));
79 if ($line =~ /^<12> /) {
80 my $fen = style12_to_fen($line);
81 print UCIWRITE "stop\n";
82 print UCIWRITE "position fen $fen\n";
83 print UCIWRITE "go infinite\n";
85 #print "position fen $fen\n";
86 #print "go infinite\n";
88 #print "FICS: [$line]\n";
92 # any fun on the UCI channel?
93 if ($nfound > 0 && vec($rout, fileno(UCIREAD), 1) == 1) {
97 #print "UCI: $line\n";
98 if ($line =~ /^info/) {
99 my (@infos) = split / /, $line;
106 # don't update too often
107 Time::HiRes::alarm(0.2);
116 while (scalar @x > 0) {
117 if ($x[0] =~ /^(currmove|currmovenumber|time|nodes|nps|cpuload|hashfull|depth|seldepth|multipv|time|tbhits)$/) {
119 my $value = shift @x;
120 $uciinfo{$key} = $value;
123 if ($x[0] eq 'score') {
126 delete $uciinfo{'score_cp'};
127 delete $uciinfo{'score_mate'};
129 while ($x[0] =~ /^(cp|mate|lowerbound|upperbound)$/) {
132 $uciinfo{'score_cp'} = shift @x;
133 } elsif ($x[0] eq 'mate') {
135 $uciinfo{'score_mate'} = shift @x;
143 $uciinfo{'pv'} = [ @x[1..$#x] ];
146 if ($x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting' || $x[0] eq 'contempt') {
150 #print "unknown info '$x[0]', trying to recover...\n";
152 die "Unknown info '" . join(',', @x) . "'";
159 my (@x) = split / /, $str;
161 $ficsinfo{'board'} = [ @x[1..8] ];
162 $ficsinfo{'toplay'} = $x[9];
165 my (@board) = @x[1..8];
166 for my $rank (0..7) {
167 $board[$rank] =~ s/(-+)/length($1)/ge;
169 my $fen = join('/', @board);
171 # white/black to move
177 $castling .= "K" if ($x[11] == 1);
178 $castling .= "Q" if ($x[12] == 1);
179 $castling .= "k" if ($x[13] == 1);
180 $castling .= "q" if ($x[14] == 1);
181 $castling = "-" if ($castling eq "");
188 $ep = (qw(a b c d e f g h))[$x[10]];
210 my ($board, @pvs) = @_;
212 if (scalar @pvs == 0 || !defined($pvs[0])) {
217 my $from_col = ord(substr($pv, 0, 1)) - ord('a');
218 my $from_row = 7 - (ord(substr($pv, 1, 1)) - ord('1'));
219 my $to_col = ord(substr($pv, 2, 1)) - ord('a');
220 my $to_row = 7 - (ord(substr($pv, 3, 1)) - ord('1'));
223 my $piece = substr($board->[$from_row], $from_col, 1);
225 # white short castling
226 if ($pv eq 'e1g1' && $piece eq 'K') {
230 substr($nb[7], 4, 1, '-');
231 substr($nb[7], 6, 1, $piece);
234 substr($nb[7], 7, 1, '-');
235 substr($nb[7], 5, 1, 'R');
237 return ('0-0', prettyprint_pv(\@nb, @pvs));
240 # white long castling
241 if ($pv eq 'e1b1' && $piece eq 'K') {
245 substr($nb[7], 4, 1, '-');
246 substr($nb[7], 2, 1, $piece);
249 substr($nb[7], 0, 1, '-');
250 substr($nb[7], 2, 1, 'R');
252 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
255 # black short castling
256 if ($pv eq 'e8g8' && $piece eq 'k') {
260 substr($nb[0], 4, 1, '-');
261 substr($nb[0], 6, 1, $piece);
264 substr($nb[0], 7, 1, '-');
265 substr($nb[0], 5, 1, 'R');
267 return ('0-0', prettyprint_pv(\@nb, @pvs));
270 # black long castling
271 if ($pv eq 'e8b8' && $piece eq 'k') {
275 substr($nb[0], 4, 1, '-');
276 substr($nb[0], 2, 1, $piece);
279 substr($nb[0], 0, 1, '-');
280 substr($nb[0], 2, 1, 'R');
282 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
285 # check if the from-piece is a pawn
286 if (lc($piece) eq 'p') {
288 if (substr($board->[$to_row], $to_col, 1) ne '-') {
289 $pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
291 $pretty = substr($pv, 2, 2);
293 if (length($pv) == 5) {
296 $pretty .= uc(substr($pv, 4, 1));
299 $piece = substr($pv, 4, 1);
301 $piece = uc(substr($pv, 4, 1));
306 $pretty = uc($piece);
308 # see how many of these pieces could go here, in all
312 next unless (substr($board->[$row], $col, 1) eq $piece);
313 ++$num_total if (can_reach($board, $piece, $row, $col, $to_row, $to_col));
317 # see how many of these pieces from the given row could go here
320 next unless (substr($board->[$from_row], $col, 1) eq $piece);
321 ++$num_row if (can_reach($board, $piece, $from_row, $col, $to_row, $to_col));
324 # and same for columns
327 next unless (substr($board->[$row], $from_col, 1) eq $piece);
328 ++$num_col if (can_reach($board, $piece, $row, $from_col, $to_row, $to_col));
331 # see if we need to disambiguate
332 if ($num_total > 1) {
334 $pretty .= substr($pv, 0, 1);
335 } elsif ($num_row == 1) {
336 $pretty .= substr($pv, 1, 1);
338 $pretty .= substr($pv, 0, 2);
343 if (substr($board->[$to_row], $to_col, 1) ne '-') {
347 $pretty .= substr($pv, 2, 2);
352 substr($nb[$from_row], $from_col, 1, '-');
353 substr($nb[$to_row], $to_col, 1, $piece);
357 } elsif (in_check(\@nb) ne 'none') {
361 return ($pretty, prettyprint_pv(\@nb, @pvs));
367 print "
\ecAnalysis:\n";
368 if (defined($uciinfo{'score_mate'})) {
369 printf " Mate in %d\n", $uciinfo{'score_mate'};
371 my $score = $uciinfo{'score_cp'} * 0.01;
372 if ($ficsinfo{'toplay'} eq 'B') {
375 printf " Score: %+5.2f\n", $score;
377 print " PV: ", join(', ', prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv'}}));
379 printf " %u nodes, %7u nodes/sec, depth %u ply",
380 $uciinfo{'nodes'}, $uciinfo{'nps'}, $uciinfo{'depth'};
381 if (exists($uciinfo{'tbhits'})) {
382 printf ", %u Nalimov hits", $uciinfo{'tbhits'};
384 if (exists($uciinfo{'seldepth'})) {
385 printf " (%u selective)", $uciinfo{'seldepth'};
392 my ($wkr, $wkc, $bkr, $bkc);
396 my $piece = substr($board->[$row], $col, 1);
398 ($wkr, $wkc) = ($row, $col);
399 } elsif ($piece eq 'k') {
400 ($bkr, $bkc) = ($row, $col);
405 return ($wkr, $wkc, $bkr, $bkc);
410 my $check = in_check($board);
411 return 0 if ($check eq 'none');
413 # try all possible moves for the side in check
416 my $piece = substr($board->[$row], $col, 1);
417 next if ($piece eq '-');
419 if ($check eq 'white') {
420 next if ($piece eq lc($piece));
422 next if ($piece eq uc($piece));
425 for my $dest_row (0..7) {
426 for my $dest_col (0..7) {
427 next if ($row == $dest_row && $col == $dest_col);
428 next unless (can_reach($board, $piece, $row, $col, $dest_row, $dest_col));
431 substr($nb[$row], $col, 1, '-');
432 substr($nb[$dest_row], $dest_col, 1, $piece);
434 my $new_check = in_check(\@nb);
435 return 0 if ($new_check ne $check && $new_check ne 'both');
441 # nothing to do; mate
447 my ($black_check, $white_check) = (0, 0);
449 my ($wkr, $wkc, $bkr, $bkc) = find_kings($board);
451 # check all pieces for the possibility of threatening the two kings
454 my $piece = substr($board->[$row], $col, 1);
455 next if ($piece eq '-' || lc($piece) eq 'k');
457 if (uc($piece) eq $piece) {
459 $black_check = 1 if (can_reach($board, $piece, $row, $col, $bkr, $bkc));
462 $white_check = 1 if (can_reach($board, $piece, $row, $col, $wkr, $wkc));
467 if ($black_check && $white_check) {
469 } elsif ($black_check) {
471 } elsif ($white_check) {
479 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
481 # can't eat your own piece
482 my $dest_piece = substr($board->[$to_row], $to_col, 1);
483 if ($dest_piece ne '-') {
484 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
487 if (lc($piece) eq 'k') {
488 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
490 if (lc($piece) eq 'r') {
491 return 0 unless ($from_row == $to_row || $from_col == $to_col);
493 # check that there's a clear passage
494 if ($from_row == $to_row) {
495 if ($from_col > $to_col) {
496 ($to_col, $from_col) = ($from_col, $to_col);
499 for my $c (($from_col+1)..($to_col-1)) {
500 my $middle_piece = substr($board->[$to_row], $c, 1);
501 return 0 if ($middle_piece ne '-');
506 if ($from_row > $to_row) {
507 ($to_row, $from_row) = ($from_row, $to_row);
510 for my $r (($from_row+1)..($to_row-1)) {
511 my $middle_piece = substr($board->[$r], $to_col, 1);
512 return 0 if ($middle_piece ne '-');
518 if (lc($piece) eq 'b') {
519 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
521 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
522 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
524 my $r = $from_row + $dr;
525 my $c = $from_col + $dc;
527 while ($r != $to_row) {
528 my $middle_piece = substr($board->[$r], $c, 1);
529 return 0 if ($middle_piece ne '-');
537 if (lc($piece) eq 'n') {
538 my $diff_r = abs($from_row - $to_row);
539 my $diff_c = abs($from_col - $to_col);
540 return 1 if ($diff_r == 2 && $diff_c == 1);
541 return 1 if ($diff_r == 1 && $diff_c == 2);
545 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
546 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
549 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
550 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
554 if ($to_col == $from_col && $to_row == $from_row + 1) {
555 return ($dest_piece eq '-');
557 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
558 return ($dest_piece ne '-');
564 if ($to_col == $from_col && $to_row == $from_row - 1) {
565 return ($dest_piece eq '-');
567 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
568 return ($dest_piece ne '-');