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(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 '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 "");
193 $ep = (qw(a b c d e f g h))[$x[10]];
215 my ($board, @pvs) = @_;
217 if (scalar @pvs == 0 || !defined($pvs[0])) {
222 my $from_col = ord(substr($pv, 0, 1)) - ord('a');
223 my $from_row = 7 - (ord(substr($pv, 1, 1)) - ord('1'));
224 my $to_col = ord(substr($pv, 2, 1)) - ord('a');
225 my $to_row = 7 - (ord(substr($pv, 3, 1)) - ord('1'));
228 my $piece = substr($board->[$from_row], $from_col, 1);
230 # white short castling
231 if ($pv eq 'e1g1' && $piece eq 'K') {
235 substr($nb[7], 4, 1, '-');
236 substr($nb[7], 6, 1, $piece);
239 substr($nb[7], 7, 1, '-');
240 substr($nb[7], 5, 1, 'R');
242 return ('0-0', prettyprint_pv(\@nb, @pvs));
245 # white long castling
246 if ($pv eq 'e1b1' && $piece eq 'K') {
250 substr($nb[7], 4, 1, '-');
251 substr($nb[7], 2, 1, $piece);
254 substr($nb[7], 0, 1, '-');
255 substr($nb[7], 2, 1, 'R');
257 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
260 # black short castling
261 if ($pv eq 'e8g8' && $piece eq 'k') {
265 substr($nb[0], 4, 1, '-');
266 substr($nb[0], 6, 1, $piece);
269 substr($nb[0], 7, 1, '-');
270 substr($nb[0], 5, 1, 'R');
272 return ('0-0', prettyprint_pv(\@nb, @pvs));
275 # black long castling
276 if ($pv eq 'e8b8' && $piece eq 'k') {
280 substr($nb[0], 4, 1, '-');
281 substr($nb[0], 2, 1, $piece);
284 substr($nb[0], 0, 1, '-');
285 substr($nb[0], 2, 1, 'R');
287 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
290 # check if the from-piece is a pawn
291 if (lc($piece) eq 'p') {
293 if (substr($board->[$to_row], $to_col, 1) ne '-') {
294 $pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
296 $pretty = substr($pv, 2, 2);
298 if (length($pv) == 5) {
301 $pretty .= uc(substr($pv, 4, 1));
304 $piece = substr($pv, 4, 1);
306 $piece = uc(substr($pv, 4, 1));
311 $pretty = uc($piece);
313 # see how many of these pieces could go here, in all
317 next unless (substr($board->[$row], $col, 1) eq $piece);
318 ++$num_total if (can_reach($board, $piece, $row, $col, $to_row, $to_col));
322 # see how many of these pieces from the given row could go here
325 next unless (substr($board->[$from_row], $col, 1) eq $piece);
326 ++$num_row if (can_reach($board, $piece, $from_row, $col, $to_row, $to_col));
329 # and same for columns
332 next unless (substr($board->[$row], $from_col, 1) eq $piece);
333 ++$num_col if (can_reach($board, $piece, $row, $from_col, $to_row, $to_col));
336 # see if we need to disambiguate
337 if ($num_total > 1) {
339 $pretty .= substr($pv, 0, 1);
340 } elsif ($num_row == 1) {
341 $pretty .= substr($pv, 1, 1);
343 $pretty .= substr($pv, 0, 2);
348 if (substr($board->[$to_row], $to_col, 1) ne '-') {
352 $pretty .= substr($pv, 2, 2);
357 substr($nb[$from_row], $from_col, 1, '-');
358 substr($nb[$to_row], $to_col, 1, $piece);
362 } elsif (in_check(\@nb) ne 'none') {
366 return ($pretty, prettyprint_pv(\@nb, @pvs));
372 print "
\ecAnalysis:\n";
373 if (defined($uciinfo{'score_mate'})) {
374 printf " Mate in %d\n", $uciinfo{'score_mate'};
376 my $score = $uciinfo{'score_cp'} * 0.01;
377 if ($ficsinfo{'toplay'} eq 'B') {
380 printf " Score: %+5.2f\n", $score;
382 print " PV: ", join(', ', prettyprint_pv($ficsinfo{'board'}, @{$uciinfo{'pv'}}));
384 printf " %u nodes, %7u nodes/sec, depth %u ply",
385 $uciinfo{'nodes'}, $uciinfo{'nps'}, $uciinfo{'depth'};
386 if (exists($uciinfo{'tbhits'})) {
387 printf ", %u Nalimov hits", $uciinfo{'tbhits'};
389 if (exists($uciinfo{'seldepth'})) {
390 printf " (%u selective)", $uciinfo{'seldepth'};
397 my ($wkr, $wkc, $bkr, $bkc);
401 my $piece = substr($board->[$row], $col, 1);
403 ($wkr, $wkc) = ($row, $col);
404 } elsif ($piece eq 'k') {
405 ($bkr, $bkc) = ($row, $col);
410 return ($wkr, $wkc, $bkr, $bkc);
415 my $check = in_check($board);
416 return 0 if ($check eq 'none');
418 # try all possible moves for the side in check
421 my $piece = substr($board->[$row], $col, 1);
422 next if ($piece eq '-');
424 if ($check eq 'white') {
425 next if ($piece eq lc($piece));
427 next if ($piece eq uc($piece));
430 for my $dest_row (0..7) {
431 for my $dest_col (0..7) {
432 next if ($row == $dest_row && $col == $dest_col);
433 next unless (can_reach($board, $piece, $row, $col, $dest_row, $dest_col));
436 substr($nb[$row], $col, 1, '-');
437 substr($nb[$dest_row], $dest_col, 1, $piece);
439 my $new_check = in_check(\@nb);
440 return 0 if ($new_check ne $check && $new_check ne 'both');
446 # nothing to do; mate
452 my ($black_check, $white_check) = (0, 0);
454 my ($wkr, $wkc, $bkr, $bkc) = find_kings($board);
456 # check all pieces for the possibility of threatening the two kings
459 my $piece = substr($board->[$row], $col, 1);
460 next if ($piece eq '-' || lc($piece) eq 'k');
462 if (uc($piece) eq $piece) {
464 $black_check = 1 if (can_reach($board, $piece, $row, $col, $bkr, $bkc));
467 $white_check = 1 if (can_reach($board, $piece, $row, $col, $wkr, $wkc));
472 if ($black_check && $white_check) {
474 } elsif ($black_check) {
476 } elsif ($white_check) {
484 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
486 # can't eat your own piece
487 my $dest_piece = substr($board->[$to_row], $to_col, 1);
488 if ($dest_piece ne '-') {
489 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
492 if (lc($piece) eq 'k') {
493 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
495 if (lc($piece) eq 'r') {
496 return 0 unless ($from_row == $to_row || $from_col == $to_col);
498 # check that there's a clear passage
499 if ($from_row == $to_row) {
500 if ($from_col > $to_col) {
501 ($to_col, $from_col) = ($from_col, $to_col);
504 for my $c (($from_col+1)..($to_col-1)) {
505 my $middle_piece = substr($board->[$to_row], $c, 1);
506 return 0 if ($middle_piece ne '-');
511 if ($from_row > $to_row) {
512 ($to_row, $from_row) = ($from_row, $to_row);
515 for my $r (($from_row+1)..($to_row-1)) {
516 my $middle_piece = substr($board->[$r], $to_col, 1);
517 return 0 if ($middle_piece ne '-');
523 if (lc($piece) eq 'b') {
524 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
526 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
527 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
529 my $r = $from_row + $dr;
530 my $c = $from_col + $dc;
532 while ($r != $to_row) {
533 my $middle_piece = substr($board->[$r], $c, 1);
534 return 0 if ($middle_piece ne '-');
542 if (lc($piece) eq 'n') {
543 my $diff_r = abs($from_row - $to_row);
544 my $diff_c = abs($from_col - $to_col);
545 return 1 if ($diff_r == 2 && $diff_c == 1);
546 return 1 if ($diff_r == 1 && $diff_c == 2);
550 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
551 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
554 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
555 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
559 if ($to_col == $from_col && $to_row == $from_row + 1) {
560 return ($dest_piece eq '-');
562 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
563 return ($dest_piece ne '-');
569 if ($to_col == $from_col && $to_row == $from_row - 1) {
570 return ($dest_piece eq '-');
572 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
573 return ($dest_piece ne '-');
584 print UCIWRITE "$msg\n";
585 print UCILOG "=> $msg\n";