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 Rybkav2.3.2a.mp.w32.exe";
28 open(UCILOG, ">ucilog.txt")
29 or die "ucilog.txt: $!";
30 print UCILOG "Log starting.\n";
35 # open the chess engine
36 my $pid = IPC::Open2::open2(*UCIREAD, *UCIWRITE, $engine);
47 uciprint("setoption name UCI_AnalyseMode value true");
48 uciprint("setoption name NalimovPath value c:\\nalimov");
49 uciprint("setoption name NalimovUsage value Normally");
50 # uciprint("setoption name Contempt value 1000");
51 # uciprint("setoption name Outlook value Ultra Optimistic");
52 uciprint("ucinewgame");
54 print "Chess engine ready.\n";
57 my $t = Net::Telnet->new(Timeout => 10, Prompt => '/fics% /');
58 #$t->input_log(\*STDOUT);
61 $t->waitfor('/Press return to enter the server/');
65 $t->cmd("set shout 0");
66 $t->cmd("set seek 0");
67 $t->cmd("set style 12");
68 $t->cmd("observe $target");
71 print "FICS ready.\n";
75 vec($rin, fileno(UCIREAD), 1) = 1;
76 vec($rin, fileno($t), 1) = 1;
78 my ($nfound, $timeleft) = select($rout=$rin, undef, undef, 5.0);
82 my $line = $t->getline(Timeout => 0, errmode => 'return');
83 last if (!defined($line));
87 if ($line =~ /^<12> /) {
88 my $fen = style12_to_fen($line);
90 uciprint("position fen $fen");
91 uciprint("go infinite");
93 #print "FICS: [$line]\n";
97 # any fun on the UCI channel?
98 if ($nfound > 0 && vec($rout, fileno(UCIREAD), 1) == 1) {
102 print UCILOG "<= $line\n";
103 if ($line =~ /^info/) {
104 my (@infos) = split / /, $line;
111 # don't update too often
112 Time::HiRes::alarm(0.2);
121 while (scalar @x > 0) {
122 if ($x[0] =~ /^(currmove|currmovenumber|time|nodes|nps|cpuload|hashfull|depth|seldepth|multipv|time|tbhits)$/) {
124 my $value = shift @x;
125 $uciinfo{$key} = $value;
128 if ($x[0] eq 'score') {
131 delete $uciinfo{'score_cp'};
132 delete $uciinfo{'score_mate'};
134 while ($x[0] =~ /^(cp|mate|lowerbound|upperbound)$/) {
137 $uciinfo{'score_cp'} = shift @x;
138 } elsif ($x[0] eq 'mate') {
140 $uciinfo{'score_mate'} = shift @x;
148 $uciinfo{'pv'} = [ @x[1..$#x] ];
151 if ($x[0] eq 'string' || $x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting' || $x[0] eq 'contempt') {
155 #print "unknown info '$x[0]', trying to recover...\n";
157 die "Unknown info '" . join(',', @x) . "'";
164 my (@x) = split / /, $str;
166 $ficsinfo{'board'} = [ @x[1..8] ];
167 $ficsinfo{'toplay'} = $x[9];
170 my (@board) = @x[1..8];
171 for my $rank (0..7) {
172 $board[$rank] =~ s/(-+)/length($1)/ge;
174 my $fen = join('/', @board);
176 # white/black to move
182 $castling .= "K" if ($x[11] == 1);
183 $castling .= "Q" if ($x[12] == 1);
184 $castling .= "k" if ($x[13] == 1);
185 $castling .= "q" if ($x[14] == 1);
186 $castling = "-" if ($castling eq "");
194 my $nep = (qw(a b c d e f g h))[$col];
203 # Showing the en passant square when actually no capture can be made
204 # seems to confuse at least Rybka. Thus, check if there's actually
205 # a pawn of the opposite side that can do the en passant move, and if
206 # not, just lie -- it doesn't matter anyway. I'm unsure what's the
207 # "right" thing as per the standard, though.
210 $ep = $nep if ($col > 0 && substr($board[4], $col-1, 1) eq 'p');
211 $ep = $nep if ($col < 7 && substr($board[4], $col+1, 1) eq 'p');
213 $ep = $nep if ($col > 0 && substr($board[3], $col-1, 1) eq 'P');
214 $ep = $nep if ($col < 7 && substr($board[3], $col+1, 1) eq 'P');
232 my ($board, @pvs) = @_;
234 if (scalar @pvs == 0 || !defined($pvs[0])) {
239 my $from_col = ord(substr($pv, 0, 1)) - ord('a');
240 my $from_row = 7 - (ord(substr($pv, 1, 1)) - ord('1'));
241 my $to_col = ord(substr($pv, 2, 1)) - ord('a');
242 my $to_row = 7 - (ord(substr($pv, 3, 1)) - ord('1'));
245 my $piece = substr($board->[$from_row], $from_col, 1);
247 # white short castling
248 if ($pv eq 'e1g1' && $piece eq 'K') {
252 substr($nb[7], 4, 1, '-');
253 substr($nb[7], 6, 1, $piece);
256 substr($nb[7], 7, 1, '-');
257 substr($nb[7], 5, 1, 'R');
259 return ('0-0', prettyprint_pv(\@nb, @pvs));
262 # white long castling
263 if ($pv eq 'e1b1' && $piece eq 'K') {
267 substr($nb[7], 4, 1, '-');
268 substr($nb[7], 2, 1, $piece);
271 substr($nb[7], 0, 1, '-');
272 substr($nb[7], 2, 1, 'R');
274 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
277 # black short castling
278 if ($pv eq 'e8g8' && $piece eq 'k') {
282 substr($nb[0], 4, 1, '-');
283 substr($nb[0], 6, 1, $piece);
286 substr($nb[0], 7, 1, '-');
287 substr($nb[0], 5, 1, 'R');
289 return ('0-0', prettyprint_pv(\@nb, @pvs));
292 # black long castling
293 if ($pv eq 'e8b8' && $piece eq 'k') {
297 substr($nb[0], 4, 1, '-');
298 substr($nb[0], 2, 1, $piece);
301 substr($nb[0], 0, 1, '-');
302 substr($nb[0], 2, 1, 'R');
304 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
307 # check if the from-piece is a pawn
308 if (lc($piece) eq 'p') {
310 if (substr($board->[$to_row], $to_col, 1) ne '-') {
311 $pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
313 $pretty = substr($pv, 2, 2);
315 if (length($pv) == 5) {
318 $pretty .= uc(substr($pv, 4, 1));
321 $piece = substr($pv, 4, 1);
323 $piece = uc(substr($pv, 4, 1));
328 $pretty = uc($piece);
330 # see how many of these pieces could go here, in all
334 next unless (substr($board->[$row], $col, 1) eq $piece);
335 ++$num_total if (can_reach($board, $piece, $row, $col, $to_row, $to_col));
339 # see how many of these pieces from the given row could go here
342 next unless (substr($board->[$from_row], $col, 1) eq $piece);
343 ++$num_row if (can_reach($board, $piece, $from_row, $col, $to_row, $to_col));
346 # and same for columns
349 next unless (substr($board->[$row], $from_col, 1) eq $piece);
350 ++$num_col if (can_reach($board, $piece, $row, $from_col, $to_row, $to_col));
353 # see if we need to disambiguate
354 if ($num_total > 1) {
356 $pretty .= substr($pv, 0, 1);
357 } elsif ($num_row == 1) {
358 $pretty .= substr($pv, 1, 1);
360 $pretty .= substr($pv, 0, 2);
365 if (substr($board->[$to_row], $to_col, 1) ne '-') {
369 $pretty .= substr($pv, 2, 2);
374 substr($nb[$from_row], $from_col, 1, '-');
375 substr($nb[$to_row], $to_col, 1, $piece);
379 } elsif (in_check(\@nb) ne 'none') {
383 return ($pretty, prettyprint_pv(\@nb, @pvs));
389 print "
\ecAnalysis:\n";
390 if (defined($uciinfo{'score_mate'})) {
391 printf " Mate in %d\n", $uciinfo{'score_mate'};
393 my $score = $uciinfo{'score_cp'} * 0.01;
394 if ($ficsinfo{'toplay'} eq 'B') {
397 printf " Score: %+5.2f\n", $score;
399 print " PV: ", join(', ', prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv'}}));
401 printf " %u nodes, %7u nodes/sec, depth %u ply",
402 $uciinfo{'nodes'}, $uciinfo{'nps'}, $uciinfo{'depth'};
403 if (exists($uciinfo{'tbhits'})) {
404 printf ", %u Nalimov hits", $uciinfo{'tbhits'};
406 if (exists($uciinfo{'seldepth'})) {
407 printf " (%u selective)", $uciinfo{'seldepth'};
414 my ($wkr, $wkc, $bkr, $bkc);
418 my $piece = substr($board->[$row], $col, 1);
420 ($wkr, $wkc) = ($row, $col);
421 } elsif ($piece eq 'k') {
422 ($bkr, $bkc) = ($row, $col);
427 return ($wkr, $wkc, $bkr, $bkc);
432 my $check = in_check($board);
433 return 0 if ($check eq 'none');
435 # try all possible moves for the side in check
438 my $piece = substr($board->[$row], $col, 1);
439 next if ($piece eq '-');
441 if ($check eq 'white') {
442 next if ($piece eq lc($piece));
444 next if ($piece eq uc($piece));
447 for my $dest_row (0..7) {
448 for my $dest_col (0..7) {
449 next if ($row == $dest_row && $col == $dest_col);
450 next unless (can_reach($board, $piece, $row, $col, $dest_row, $dest_col));
453 substr($nb[$row], $col, 1, '-');
454 substr($nb[$dest_row], $dest_col, 1, $piece);
456 my $new_check = in_check(\@nb);
457 return 0 if ($new_check ne $check && $new_check ne 'both');
463 # nothing to do; mate
469 my ($black_check, $white_check) = (0, 0);
471 my ($wkr, $wkc, $bkr, $bkc) = find_kings($board);
473 # check all pieces for the possibility of threatening the two kings
476 my $piece = substr($board->[$row], $col, 1);
477 next if ($piece eq '-' || lc($piece) eq 'k');
479 if (uc($piece) eq $piece) {
481 $black_check = 1 if (can_reach($board, $piece, $row, $col, $bkr, $bkc));
484 $white_check = 1 if (can_reach($board, $piece, $row, $col, $wkr, $wkc));
489 if ($black_check && $white_check) {
491 } elsif ($black_check) {
493 } elsif ($white_check) {
501 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
503 # can't eat your own piece
504 my $dest_piece = substr($board->[$to_row], $to_col, 1);
505 if ($dest_piece ne '-') {
506 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
509 if (lc($piece) eq 'k') {
510 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
512 if (lc($piece) eq 'r') {
513 return 0 unless ($from_row == $to_row || $from_col == $to_col);
515 # check that there's a clear passage
516 if ($from_row == $to_row) {
517 if ($from_col > $to_col) {
518 ($to_col, $from_col) = ($from_col, $to_col);
521 for my $c (($from_col+1)..($to_col-1)) {
522 my $middle_piece = substr($board->[$to_row], $c, 1);
523 return 0 if ($middle_piece ne '-');
528 if ($from_row > $to_row) {
529 ($to_row, $from_row) = ($from_row, $to_row);
532 for my $r (($from_row+1)..($to_row-1)) {
533 my $middle_piece = substr($board->[$r], $to_col, 1);
534 return 0 if ($middle_piece ne '-');
540 if (lc($piece) eq 'b') {
541 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
543 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
544 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
546 my $r = $from_row + $dr;
547 my $c = $from_col + $dc;
549 while ($r != $to_row) {
550 my $middle_piece = substr($board->[$r], $c, 1);
551 return 0 if ($middle_piece ne '-');
559 if (lc($piece) eq 'n') {
560 my $diff_r = abs($from_row - $to_row);
561 my $diff_c = abs($from_col - $to_col);
562 return 1 if ($diff_r == 2 && $diff_c == 1);
563 return 1 if ($diff_r == 1 && $diff_c == 2);
567 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
568 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
571 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
572 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
576 if ($to_col == $from_col && $to_row == $from_row + 1) {
577 return ($dest_piece eq '-');
579 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
580 return ($dest_piece ne '-');
586 if ($to_col == $from_col && $to_row == $from_row - 1) {
587 return ($dest_piece eq '-');
589 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
590 return ($dest_piece ne '-');
601 print UCIWRITE "$msg\n";
602 print UCILOG "=> $msg\n";