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 "ucinewgame\n";
44 print "Chess engine ready.\n";
47 my $t = Net::Telnet->new(Timeout => 10, Prompt => '/fics% /');
48 #$t->input_log(\*STDOUT);
51 $t->waitfor('/Press return to enter the server/');
55 $t->cmd("set shout 0");
56 $t->cmd("set seek 0");
57 $t->cmd("set style 12");
58 $t->cmd("observe $target");
61 print "FICS ready.\n";
64 vec($rin, fileno(UCIREAD), 1) = 1;
65 vec($rin, fileno($t), 1) = 1;
67 my ($nfound, $timeleft) = select($rin, undef, undef, 5.0);
71 my $line = $t->getline(Timeout => 0, errmode => 'return');
72 last if (!defined($line));
76 if ($line =~ /^<12> /) {
77 my $fen = style12_to_fen($line);
78 print UCIWRITE "stop\n";
79 print UCIWRITE "position fen $fen\n";
80 print UCIWRITE "go infinite\n";
82 #print "position fen $fen\n";
83 #print "go infinite\n";
85 #print "FICS: [$line]\n";
89 # any fun on the UCI channel?
90 if (vec($rin, fileno(UCIREAD), 1)) {
94 #print "UCI: $line\n";
95 if ($line =~ /^info/) {
96 my (@infos) = split / /, $line;
103 # don't update too often
104 Time::HiRes::alarm(0.2);
113 while (scalar @x > 0) {
114 if ($x[0] =~ /^(currmove|currmovenumber|time|nodes|nps|cpuload|hashfull|depth|seldepth|multipv|time|tbhits)$/) {
116 my $value = shift @x;
117 $uciinfo{$key} = $value;
120 if ($x[0] eq 'score') {
123 delete $uciinfo{'score_cp'};
124 delete $uciinfo{'score_mate'};
126 while ($x[0] =~ /^(cp|mate|lowerbound|upperbound)$/) {
129 $uciinfo{'score_cp'} = shift @x;
130 } elsif ($x[0] eq 'mate') {
132 $uciinfo{'score_mate'} = shift @x;
140 $uciinfo{'pv'} = [ @x[1..$#x] ];
143 if ($x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting') {
147 #print "unknown info '$x[0]', trying to recover...\n";
149 die "Unknown info '" . join(',', @x) . "'";
156 my (@x) = split / /, $str;
158 $ficsinfo{'board'} = [ @x[1..8] ];
159 $ficsinfo{'toplay'} = $x[9];
162 my (@board) = @x[1..8];
163 for my $rank (0..7) {
164 $board[$rank] =~ s/(-+)/length($1)/ge;
166 my $fen = join('/', @board);
168 # white/black to move
174 $castling .= "K" if ($x[11] == 1);
175 $castling .= "Q" if ($x[12] == 1);
176 $castling .= "k" if ($x[13] == 1);
177 $castling .= "q" if ($x[14] == 1);
178 $castling = "-" if ($castling eq "");
185 $ep = (qw(a b c d e f g h))[$x[10]];
207 my ($board, @pvs) = @_;
209 if (scalar @pvs == 0 || !defined($pvs[0])) {
214 my $from_col = ord(substr($pv, 0, 1)) - ord('a');
215 my $from_row = 7 - (ord(substr($pv, 1, 1)) - ord('1'));
216 my $to_col = ord(substr($pv, 2, 1)) - ord('a');
217 my $to_row = 7 - (ord(substr($pv, 3, 1)) - ord('1'));
220 my $piece = substr($board->[$from_row], $from_col, 1);
222 # white short castling
223 if ($pv eq 'e1g1' && $piece eq 'K') {
227 substr($nb[7], 4, 1, '-');
228 substr($nb[7], 6, 1, $piece);
231 substr($nb[7], 7, 1, '-');
232 substr($nb[7], 5, 1, 'R');
234 return ('0-0', prettyprint_pv(\@nb, @pvs));
237 # white long castling
238 if ($pv eq 'e1b1' && $piece eq 'K') {
242 substr($nb[7], 4, 1, '-');
243 substr($nb[7], 2, 1, $piece);
246 substr($nb[7], 0, 1, '-');
247 substr($nb[7], 2, 1, 'R');
249 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
252 # black short castling
253 if ($pv eq 'e8g8' && $piece eq 'k') {
257 substr($nb[0], 4, 1, '-');
258 substr($nb[0], 6, 1, $piece);
261 substr($nb[0], 7, 1, '-');
262 substr($nb[0], 5, 1, 'R');
264 return ('0-0', prettyprint_pv(\@nb, @pvs));
267 # black long castling
268 if ($pv eq 'e8b8' && $piece eq 'k') {
272 substr($nb[0], 4, 1, '-');
273 substr($nb[0], 2, 1, $piece);
276 substr($nb[0], 0, 1, '-');
277 substr($nb[0], 2, 1, 'R');
279 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
282 # check if the from-piece is a pawn
283 if (lc($piece) eq 'p') {
285 if (substr($board->[$to_row], $to_col, 1) ne '-') {
286 $pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
288 $pretty = substr($pv, 2, 2);
290 if (length($pv) == 5) {
293 $pretty .= uc(substr($pv, 4, 1));
296 $piece = substr($pv, 4, 1);
298 $piece = uc(substr($pv, 4, 1));
303 $pretty = uc($piece);
305 # see how many of these pieces could go here, in all
309 next unless (substr($board->[$row], $col, 1) eq $piece);
310 ++$num_total if (can_reach($board, $piece, $row, $col, $to_row, $to_col));
314 # see how many of these pieces from the given row could go here
317 next unless (substr($board->[$from_row], $col, 1) eq $piece);
318 ++$num_row if (can_reach($board, $piece, $from_row, $col, $to_row, $to_col));
321 # and same for columns
324 next unless (substr($board->[$row], $from_col, 1) eq $piece);
325 ++$num_col if (can_reach($board, $piece, $row, $from_col, $to_row, $to_col));
328 # see if we need to disambiguate
329 if ($num_total > 1) {
331 $pretty .= substr($pv, 0, 1);
332 } elsif ($num_row == 1) {
333 $pretty .= substr($pv, 1, 1);
335 $pretty .= substr($pv, 0, 2);
340 if (substr($board->[$to_row], $to_col, 1) ne '-') {
344 $pretty .= substr($pv, 2, 2);
349 substr($nb[$from_row], $from_col, 1, '-');
350 substr($nb[$to_row], $to_col, 1, $piece);
354 } elsif (in_check(\@nb) ne 'none') {
358 return ($pretty, prettyprint_pv(\@nb, @pvs));
364 print "
\ecAnalysis:\n";
365 if (defined($uciinfo{'score_mate'})) {
366 printf " Mate in %d\n", $uciinfo{'score_mate'};
368 my $score = $uciinfo{'score_cp'} * 0.01;
369 if ($ficsinfo{'toplay'} eq 'B') {
372 printf " Score: %+5.2f\n", $score;
374 print " PV: ", join(', ', prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv'}}));
376 printf " %u nodes, %7u nodes/sec, depth %u ply",
377 $uciinfo{'nodes'}, $uciinfo{'nps'}, $uciinfo{'depth'};
378 if (exists($uciinfo{'tbhits'})) {
379 printf ", %u Nalimov hits", $uciinfo{'tbhits'};
381 if (exists($uciinfo{'seldepth'})) {
382 printf " (%u selective)", $uciinfo{'seldepth'};
389 my ($wkr, $wkc, $bkr, $bkc);
393 my $piece = substr($board->[$row], $col, 1);
395 ($wkr, $wkc) = ($row, $col);
396 } elsif ($piece eq 'k') {
397 ($bkr, $bkc) = ($row, $col);
402 return ($wkr, $wkc, $bkr, $bkc);
407 my $check = in_check($board);
408 return 0 if ($check eq 'none');
410 # try all possible moves for the side in check
413 my $piece = substr($board->[$row], $col, 1);
414 next if ($piece eq '-');
416 if ($check eq 'white') {
417 next if ($piece eq lc($piece));
419 next if ($piece eq uc($piece));
422 for my $dest_row (0..7) {
423 for my $dest_col (0..7) {
424 next if ($row == $dest_row && $col == $dest_col);
425 next unless (can_reach($board, $piece, $row, $col, $dest_row, $dest_col));
428 substr($nb[$row], $col, 1, '-');
429 substr($nb[$dest_row], $dest_col, 1, $piece);
431 my $new_check = in_check(\@nb);
432 return 0 if ($new_check ne $check && $new_check ne 'both');
438 # nothing to do; mate
444 my ($black_check, $white_check) = (0, 0);
446 my ($wkr, $wkc, $bkr, $bkc) = find_kings($board);
448 # check all pieces for the possibility of threatening the two kings
451 my $piece = substr($board->[$row], $col, 1);
452 next if ($piece eq '-' || lc($piece) eq 'k');
454 if (uc($piece) eq $piece) {
456 $black_check = 1 if (can_reach($board, $piece, $row, $col, $bkr, $bkc));
459 $white_check = 1 if (can_reach($board, $piece, $row, $col, $wkr, $wkc));
464 if ($black_check && $white_check) {
466 } elsif ($black_check) {
468 } elsif ($white_check) {
476 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
478 # can't eat your own piece
479 my $dest_piece = substr($board->[$to_row], $to_col, 1);
480 if ($dest_piece ne '-') {
481 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
484 if (lc($piece) eq 'k') {
485 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
487 if (lc($piece) eq 'r') {
488 return 0 unless ($from_row == $to_row || $from_col == $to_col);
490 # check that there's a clear passage
491 if ($from_row == $to_row) {
492 if ($from_col > $to_col) {
493 ($to_col, $from_col) = ($from_col, $to_col);
496 for my $c (($from_col+1)..($to_col-1)) {
497 my $middle_piece = substr($board->[$to_row], $c, 1);
498 return 0 if ($middle_piece ne '-');
503 if ($from_row > $to_row) {
504 ($to_row, $from_row) = ($from_row, $to_row);
507 for my $r (($from_row+1)..($to_row-1)) {
508 my $middle_piece = substr($board->[$r], $to_col, 1);
509 return 0 if ($middle_piece ne '-');
515 if (lc($piece) eq 'b') {
516 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
518 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
519 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
521 my $r = $from_row + $dr;
522 my $c = $from_col + $dc;
524 while ($r != $to_row) {
525 my $middle_piece = substr($board->[$r], $c, 1);
526 return 0 if ($middle_piece ne '-');
534 if (lc($piece) eq 'n') {
535 my $diff_r = abs($from_row - $to_row);
536 my $diff_c = abs($from_col - $to_col);
537 return 1 if ($diff_r == 2 && $diff_c == 1);
538 return 1 if ($diff_r == 1 && $diff_c == 2);
542 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
543 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
546 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
547 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
551 if ($to_col == $from_col && $to_row == $from_row + 1) {
552 return ($dest_piece eq '-');
554 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
555 return ($dest_piece ne '-');
561 if ($to_col == $from_col && $to_row == $from_row - 1) {
562 return ($dest_piece eq '-');
564 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
565 return ($dest_piece ne '-');