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";
66 vec($rin, fileno(UCIREAD), 1) = 1;
67 vec($rin, fileno($t), 1) = 1;
69 my ($nfound, $timeleft) = select($rin, undef, undef, 5.0);
73 my $line = $t->getline(Timeout => 0, errmode => 'return');
74 last if (!defined($line));
78 if ($line =~ /^<12> /) {
79 my $fen = style12_to_fen($line);
80 print UCIWRITE "stop\n";
81 print UCIWRITE "position fen $fen\n";
82 print UCIWRITE "go infinite\n";
84 #print "position fen $fen\n";
85 #print "go infinite\n";
87 #print "FICS: [$line]\n";
91 # any fun on the UCI channel?
92 if (vec($rin, fileno(UCIREAD), 1)) {
96 #print "UCI: $line\n";
97 if ($line =~ /^info/) {
98 my (@infos) = split / /, $line;
105 # don't update too often
106 Time::HiRes::alarm(0.2);
115 while (scalar @x > 0) {
116 if ($x[0] =~ /^(currmove|currmovenumber|time|nodes|nps|cpuload|hashfull|depth|seldepth|multipv|time|tbhits)$/) {
118 my $value = shift @x;
119 $uciinfo{$key} = $value;
122 if ($x[0] eq 'score') {
125 delete $uciinfo{'score_cp'};
126 delete $uciinfo{'score_mate'};
128 while ($x[0] =~ /^(cp|mate|lowerbound|upperbound)$/) {
131 $uciinfo{'score_cp'} = shift @x;
132 } elsif ($x[0] eq 'mate') {
134 $uciinfo{'score_mate'} = shift @x;
142 $uciinfo{'pv'} = [ @x[1..$#x] ];
145 if ($x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting' || $x[0] eq 'contempt') {
149 #print "unknown info '$x[0]', trying to recover...\n";
151 die "Unknown info '" . join(',', @x) . "'";
158 my (@x) = split / /, $str;
160 $ficsinfo{'board'} = [ @x[1..8] ];
161 $ficsinfo{'toplay'} = $x[9];
164 my (@board) = @x[1..8];
165 for my $rank (0..7) {
166 $board[$rank] =~ s/(-+)/length($1)/ge;
168 my $fen = join('/', @board);
170 # white/black to move
176 $castling .= "K" if ($x[11] == 1);
177 $castling .= "Q" if ($x[12] == 1);
178 $castling .= "k" if ($x[13] == 1);
179 $castling .= "q" if ($x[14] == 1);
180 $castling = "-" if ($castling eq "");
187 $ep = (qw(a b c d e f g h))[$x[10]];
209 my ($board, @pvs) = @_;
211 if (scalar @pvs == 0 || !defined($pvs[0])) {
216 my $from_col = ord(substr($pv, 0, 1)) - ord('a');
217 my $from_row = 7 - (ord(substr($pv, 1, 1)) - ord('1'));
218 my $to_col = ord(substr($pv, 2, 1)) - ord('a');
219 my $to_row = 7 - (ord(substr($pv, 3, 1)) - ord('1'));
222 my $piece = substr($board->[$from_row], $from_col, 1);
224 # white short castling
225 if ($pv eq 'e1g1' && $piece eq 'K') {
229 substr($nb[7], 4, 1, '-');
230 substr($nb[7], 6, 1, $piece);
233 substr($nb[7], 7, 1, '-');
234 substr($nb[7], 5, 1, 'R');
236 return ('0-0', prettyprint_pv(\@nb, @pvs));
239 # white long castling
240 if ($pv eq 'e1b1' && $piece eq 'K') {
244 substr($nb[7], 4, 1, '-');
245 substr($nb[7], 2, 1, $piece);
248 substr($nb[7], 0, 1, '-');
249 substr($nb[7], 2, 1, 'R');
251 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
254 # black short castling
255 if ($pv eq 'e8g8' && $piece eq 'k') {
259 substr($nb[0], 4, 1, '-');
260 substr($nb[0], 6, 1, $piece);
263 substr($nb[0], 7, 1, '-');
264 substr($nb[0], 5, 1, 'R');
266 return ('0-0', prettyprint_pv(\@nb, @pvs));
269 # black long castling
270 if ($pv eq 'e8b8' && $piece eq 'k') {
274 substr($nb[0], 4, 1, '-');
275 substr($nb[0], 2, 1, $piece);
278 substr($nb[0], 0, 1, '-');
279 substr($nb[0], 2, 1, 'R');
281 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
284 # check if the from-piece is a pawn
285 if (lc($piece) eq 'p') {
287 if (substr($board->[$to_row], $to_col, 1) ne '-') {
288 $pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
290 $pretty = substr($pv, 2, 2);
292 if (length($pv) == 5) {
295 $pretty .= uc(substr($pv, 4, 1));
298 $piece = substr($pv, 4, 1);
300 $piece = uc(substr($pv, 4, 1));
305 $pretty = uc($piece);
307 # see how many of these pieces could go here, in all
311 next unless (substr($board->[$row], $col, 1) eq $piece);
312 ++$num_total if (can_reach($board, $piece, $row, $col, $to_row, $to_col));
316 # see how many of these pieces from the given row could go here
319 next unless (substr($board->[$from_row], $col, 1) eq $piece);
320 ++$num_row if (can_reach($board, $piece, $from_row, $col, $to_row, $to_col));
323 # and same for columns
326 next unless (substr($board->[$row], $from_col, 1) eq $piece);
327 ++$num_col if (can_reach($board, $piece, $row, $from_col, $to_row, $to_col));
330 # see if we need to disambiguate
331 if ($num_total > 1) {
333 $pretty .= substr($pv, 0, 1);
334 } elsif ($num_row == 1) {
335 $pretty .= substr($pv, 1, 1);
337 $pretty .= substr($pv, 0, 2);
342 if (substr($board->[$to_row], $to_col, 1) ne '-') {
346 $pretty .= substr($pv, 2, 2);
351 substr($nb[$from_row], $from_col, 1, '-');
352 substr($nb[$to_row], $to_col, 1, $piece);
356 } elsif (in_check(\@nb) ne 'none') {
360 return ($pretty, prettyprint_pv(\@nb, @pvs));
366 print "
\ecAnalysis:\n";
367 if (defined($uciinfo{'score_mate'})) {
368 printf " Mate in %d\n", $uciinfo{'score_mate'};
370 my $score = $uciinfo{'score_cp'} * 0.01;
371 if ($ficsinfo{'toplay'} eq 'B') {
374 printf " Score: %+5.2f\n", $score;
376 print " PV: ", join(', ', prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv'}}));
378 printf " %u nodes, %7u nodes/sec, depth %u ply",
379 $uciinfo{'nodes'}, $uciinfo{'nps'}, $uciinfo{'depth'};
380 if (exists($uciinfo{'tbhits'})) {
381 printf ", %u Nalimov hits", $uciinfo{'tbhits'};
383 if (exists($uciinfo{'seldepth'})) {
384 printf " (%u selective)", $uciinfo{'seldepth'};
391 my ($wkr, $wkc, $bkr, $bkc);
395 my $piece = substr($board->[$row], $col, 1);
397 ($wkr, $wkc) = ($row, $col);
398 } elsif ($piece eq 'k') {
399 ($bkr, $bkc) = ($row, $col);
404 return ($wkr, $wkc, $bkr, $bkc);
409 my $check = in_check($board);
410 return 0 if ($check eq 'none');
412 # try all possible moves for the side in check
415 my $piece = substr($board->[$row], $col, 1);
416 next if ($piece eq '-');
418 if ($check eq 'white') {
419 next if ($piece eq lc($piece));
421 next if ($piece eq uc($piece));
424 for my $dest_row (0..7) {
425 for my $dest_col (0..7) {
426 next if ($row == $dest_row && $col == $dest_col);
427 next unless (can_reach($board, $piece, $row, $col, $dest_row, $dest_col));
430 substr($nb[$row], $col, 1, '-');
431 substr($nb[$dest_row], $dest_col, 1, $piece);
433 my $new_check = in_check(\@nb);
434 return 0 if ($new_check ne $check && $new_check ne 'both');
440 # nothing to do; mate
446 my ($black_check, $white_check) = (0, 0);
448 my ($wkr, $wkc, $bkr, $bkc) = find_kings($board);
450 # check all pieces for the possibility of threatening the two kings
453 my $piece = substr($board->[$row], $col, 1);
454 next if ($piece eq '-' || lc($piece) eq 'k');
456 if (uc($piece) eq $piece) {
458 $black_check = 1 if (can_reach($board, $piece, $row, $col, $bkr, $bkc));
461 $white_check = 1 if (can_reach($board, $piece, $row, $col, $wkr, $wkc));
466 if ($black_check && $white_check) {
468 } elsif ($black_check) {
470 } elsif ($white_check) {
478 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
480 # can't eat your own piece
481 my $dest_piece = substr($board->[$to_row], $to_col, 1);
482 if ($dest_piece ne '-') {
483 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
486 if (lc($piece) eq 'k') {
487 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
489 if (lc($piece) eq 'r') {
490 return 0 unless ($from_row == $to_row || $from_col == $to_col);
492 # check that there's a clear passage
493 if ($from_row == $to_row) {
494 if ($from_col > $to_col) {
495 ($to_col, $from_col) = ($from_col, $to_col);
498 for my $c (($from_col+1)..($to_col-1)) {
499 my $middle_piece = substr($board->[$to_row], $c, 1);
500 return 0 if ($middle_piece ne '-');
505 if ($from_row > $to_row) {
506 ($to_row, $from_row) = ($from_row, $to_row);
509 for my $r (($from_row+1)..($to_row-1)) {
510 my $middle_piece = substr($board->[$r], $to_col, 1);
511 return 0 if ($middle_piece ne '-');
517 if (lc($piece) eq 'b') {
518 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
520 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
521 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
523 my $r = $from_row + $dr;
524 my $c = $from_col + $dc;
526 while ($r != $to_row) {
527 my $middle_piece = substr($board->[$r], $c, 1);
528 return 0 if ($middle_piece ne '-');
536 if (lc($piece) eq 'n') {
537 my $diff_r = abs($from_row - $to_row);
538 my $diff_c = abs($from_col - $to_col);
539 return 1 if ($diff_r == 2 && $diff_c == 1);
540 return 1 if ($diff_r == 1 && $diff_c == 2);
544 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
545 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
548 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
549 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
553 if ($to_col == $from_col && $to_row == $from_row + 1) {
554 return ($dest_piece eq '-');
556 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
557 return ($dest_piece ne '-');
563 if ($to_col == $from_col && $to_row == $from_row - 1) {
564 return ($dest_piece eq '-');
566 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
567 return ($dest_piece ne '-');