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.
20 my $server = "freechess.org";
21 my $target = "GMCarlsen";
22 # my $engine = "/usr/games/toga2";
23 #my $engine = "wine Rybkav2.3.2a.mp.w32.exe";
24 my $engine = "~/microwine-0.5/microwine Rybkav2.3.2a.mp.x64.exe";
25 #my $engine = "ssh -t sesse\@84.48.204.209 ./microwine-0.5/microwine ./Rybkav2.3.2a.mp.x64.exe";
26 #my $engine = "ssh -t sesse\@cirkus.samfundet.no nice -n 19 ./microwine-0.5/microwine ./microwine-0.5/Rybkav2.3.2a.mp.x64.exe";
27 my $telltarget = undef; # undef to be silent
28 my @tell_intervals = (5, 20, 60, 120, 240, 480, 960); # after each move
29 my $uci_assume_full_compliance = 0; # dangerous :-)
39 $SIG{ALRM} = sub { output_screen(); };
43 open(FICSLOG, ">ficslog.txt")
44 or die "ficslog.txt: $!";
45 print FICSLOG "Log starting.\n";
49 open(UCILOG, ">ucilog.txt")
50 or die "ucilog.txt: $!";
51 print UCILOG "Log starting.\n";
56 # open the chess engine
57 my $pid = IPC::Open2::open2(*UCIREAD, *UCIWRITE, $engine);
60 my ($last_move, $last_tell);
62 my $last_told_text = '';
63 my ($pos_waiting, $pos_calculating);
73 uciprint("setoption name UCI_AnalyseMode value true");
74 # uciprint("setoption name Preserve Analysis value true");
75 # uciprint("setoption name NalimovPath value /srv/tablebase");
76 uciprint("setoption name NalimovUsage value Rarely");
77 uciprint("setoption name Hash value 1024");
78 uciprint("setoption name MultiPV value 2");
79 # uciprint("setoption name Contempt value 1000");
80 # uciprint("setoption name Outlook value Ultra Optimistic");
81 uciprint("ucinewgame");
83 print "Chess engine ready.\n";
86 my $t = Net::Telnet->new(Timeout => 10, Prompt => '/fics% /');
87 $t->input_log(\*FICSLOG);
89 $t->print("SesseBOT");
90 $t->waitfor('/Press return to enter the server/');
94 $t->cmd("set shout 0");
95 $t->cmd("set seek 0");
96 $t->cmd("set style 12");
97 $t->cmd("observe $target");
100 print "FICS ready.\n";
104 vec($rin, fileno(UCIREAD), 1) = 1;
105 vec($rin, fileno($t), 1) = 1;
107 my ($nfound, $timeleft) = select($rout=$rin, undef, undef, 5.0);
111 my $line = $t->getline(Timeout => 0, errmode => 'return');
112 last if (!defined($line));
116 if ($line =~ /^<12> /) {
117 my $pos = style12_to_fen($line);
119 # if this is already in the queue, ignore it
120 next if (defined($pos_waiting) && $pos->{'fen'} eq $pos_waiting->{'fen'});
122 # if we're already chewing on this and there's nothing else in the queue,
124 next if (!defined($pos_waiting) && defined($pos_calculating) &&
125 $pos->{'fen'} eq $pos_calculating->{'fen'});
127 # if we're already thinking on something, stop and wait for the engine
129 if (defined($pos_calculating)) {
130 if (!defined($pos_waiting)) {
133 if ($uci_assume_full_compliance) {
136 uciprint("position fen " . $pos->{'fen'});
137 uciprint("go infinite");
138 $pos_calculating = $pos;
141 # it's wrong just to give the FEN (the move history is useful,
142 # and per the UCI spec, we should really have sent "ucinewgame"),
144 uciprint("position fen " . $pos->{'fen'});
145 uciprint("go infinite");
146 $pos_calculating = $pos;
153 # Output a command every move to note that we're
154 # still paying attention -- this is a good tradeoff,
155 # since if no move has happened in the last half
156 # hour, the analysis/relay has most likely stopped
157 # and we should stop hogging server resources.
161 if ($line =~ /^([A-Za-z]+)(?:\([A-Z]+\))* tells you: (.*)$/) {
162 my ($who, $msg) = ($1, $2);
164 next if (grep { $_ eq $who } (@masters) == 0);
166 if ($msg =~ /^fics (.*?)$/) {
167 $t->cmd("tell $who Executing '$1' on FICS.");
169 } elsif ($msg =~ /^uci (.*?)$/) {
170 $t->cmd("tell $who Sending '$1' to the engine.");
171 print UCIWRITE "$1\n";
173 $t->cmd("tell $who Couldn't understand '$msg', sorry.");
176 #print "FICS: [$line]\n";
180 # any fun on the UCI channel?
181 if ($nfound > 0 && vec($rout, fileno(UCIREAD), 1) == 1) {
183 # Read until we've got a full line -- if the engine sends part of
184 # a line and then stops we're pretty much hosed, but that should
188 while ($line !~ /\n/) {
190 my $ret = sysread UCIREAD, $tmp, 1;
192 if (!defined($ret)) {
194 die "error in reading from the UCI engine: $!";
195 } elsif ($ret == 0) {
196 die "EOF from UCI engine";
206 # don't update too often
207 Time::HiRes::alarm(0.2);
218 $line =~ s/ / /g; # Sometimes needed for Zappa Mexico
219 print UCILOG localtime() . " <= $line\n";
220 if ($line =~ /^info/) {
221 my (@infos) = split / /, $line;
226 if ($line =~ /^id/) {
227 my (@ids) = split / /, $line;
232 if ($line =~ /^bestmove/ && $uci_assume_full_compliance) {
233 if (defined($pos_waiting)) {
234 uciprint("position fen " . $pos_waiting->{'fen'});
235 uciprint("go infinite");
237 $pos_calculating = $pos_waiting;
238 $pos_waiting = undef;
247 while (scalar @x > 0) {
248 if ($x[0] =~ 'multipv') {
253 if ($x[0] =~ /^(currmove|currmovenumber|cpuload)$/) {
255 my $value = shift @x;
256 $uciinfo{$key} = $value;
259 if ($x[0] =~ /^(depth|seldepth|hashfull|time|nodes|nps|tbhits)$/) {
261 my $value = shift @x;
262 $uciinfo{$key . $mpv} = $value;
265 if ($x[0] eq 'score') {
268 delete $uciinfo{'score_cp' . $mpv};
269 delete $uciinfo{'score_mate' . $mpv};
271 while ($x[0] =~ /^(cp|mate|lowerbound|upperbound)$/) {
274 $uciinfo{'score_cp' . $mpv} = shift @x;
275 } elsif ($x[0] eq 'mate') {
277 $uciinfo{'score_mate' . $mpv} = shift @x;
285 $uciinfo{'pv' . $mpv} = [ @x[1..$#x] ];
288 if ($x[0] eq 'string' || $x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting' || $x[0] eq 'contempt') {
292 #print "unknown info '$x[0]', trying to recover...\n";
294 die "Unknown info '" . join(',', @x) . "'";
302 while (scalar @x > 0) {
303 if ($x[0] =~ /^(name|author)$/) {
305 my $value = join(' ', @x);
306 $uciid{$key} = $value;
318 my (@x) = split / /, $str;
320 $pos{'board'} = [ @x[1..8] ];
321 $pos{'toplay'} = $x[9];
324 my (@board) = @x[1..8];
325 for my $rank (0..7) {
326 $board[$rank] =~ s/(-+)/length($1)/ge;
328 my $fen = join('/', @board);
330 # white/black to move
336 $castling .= "K" if ($x[11] == 1);
337 $castling .= "Q" if ($x[12] == 1);
338 $castling .= "k" if ($x[13] == 1);
339 $castling .= "q" if ($x[14] == 1);
340 $castling = "-" if ($castling eq "");
341 # $castling = "-"; # chess960
349 my $nep = (qw(a b c d e f g h))[$col];
358 # Showing the en passant square when actually no capture can be made
359 # seems to confuse at least Rybka. Thus, check if there's actually
360 # a pawn of the opposite side that can do the en passant move, and if
361 # not, just lie -- it doesn't matter anyway. I'm unsure what's the
362 # "right" thing as per the standard, though.
365 $ep = $nep if ($col > 0 && substr($pos{'board'}[4], $col-1, 1) eq 'p');
366 $ep = $nep if ($col < 7 && substr($pos{'board'}[4], $col+1, 1) eq 'p');
368 $ep = $nep if ($col > 0 && substr($pos{'board'}[3], $col-1, 1) eq 'P');
369 $ep = $nep if ($col < 7 && substr($pos{'board'}[3], $col+1, 1) eq 'P');
384 $pos{'move_num'} = $x[26];
385 $pos{'last_move'} = $x[29];
391 my ($board, @pvs) = @_;
393 if (scalar @pvs == 0 || !defined($pvs[0])) {
400 my $from_col = ord(substr($pv, 0, 1)) - ord('a');
401 my $from_row = 7 - (ord(substr($pv, 1, 1)) - ord('1'));
402 my $to_col = ord(substr($pv, 2, 1)) - ord('a');
403 my $to_row = 7 - (ord(substr($pv, 3, 1)) - ord('1'));
406 my $piece = substr($board->[$from_row], $from_col, 1);
409 die "Invalid move $pv";
412 # white short castling
413 if ($pv eq 'e1g1' && $piece eq 'K') {
415 substr($nb[7], 4, 1, '-');
416 substr($nb[7], 6, 1, $piece);
419 substr($nb[7], 7, 1, '-');
420 substr($nb[7], 5, 1, 'R');
422 return ('0-0', prettyprint_pv(\@nb, @pvs));
425 # white long castling
426 if ($pv eq 'e1c1' && $piece eq 'K') {
428 substr($nb[7], 4, 1, '-');
429 substr($nb[7], 2, 1, $piece);
432 substr($nb[7], 0, 1, '-');
433 substr($nb[7], 3, 1, 'R');
435 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
438 # black short castling
439 if ($pv eq 'e8g8' && $piece eq 'k') {
441 substr($nb[0], 4, 1, '-');
442 substr($nb[0], 6, 1, $piece);
445 substr($nb[0], 7, 1, '-');
446 substr($nb[0], 5, 1, 'r');
448 return ('0-0', prettyprint_pv(\@nb, @pvs));
451 # black long castling
452 if ($pv eq 'e8c8' && $piece eq 'k') {
454 substr($nb[0], 4, 1, '-');
455 substr($nb[0], 2, 1, $piece);
458 substr($nb[0], 0, 1, '-');
459 substr($nb[0], 3, 1, 'r');
461 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
464 # check if the from-piece is a pawn
465 if (lc($piece) eq 'p') {
467 if ($from_col != $to_col) {
468 $pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
471 if (substr($board->[$to_row], $to_col, 1) eq '-') {
473 substr($nb[$to_row + 1], $to_col, 1, '-');
475 substr($nb[$to_row - 1], $to_col, 1, '-');
479 $pretty = substr($pv, 2, 2);
481 if (length($pv) == 5) {
484 $pretty .= uc(substr($pv, 4, 1));
487 $piece = substr($pv, 4, 1);
489 $piece = uc(substr($pv, 4, 1));
494 $pretty = uc($piece);
496 # see how many of these pieces could go here, in all
500 next unless (substr($board->[$row], $col, 1) eq $piece);
501 ++$num_total if (can_reach($board, $piece, $row, $col, $to_row, $to_col));
505 # see how many of these pieces from the given row could go here
508 next unless (substr($board->[$from_row], $col, 1) eq $piece);
509 ++$num_row if (can_reach($board, $piece, $from_row, $col, $to_row, $to_col));
512 # and same for columns
515 next unless (substr($board->[$row], $from_col, 1) eq $piece);
516 ++$num_col if (can_reach($board, $piece, $row, $from_col, $to_row, $to_col));
519 # see if we need to disambiguate
520 if ($num_total > 1) {
522 $pretty .= substr($pv, 0, 1);
523 } elsif ($num_row == 1) {
524 $pretty .= substr($pv, 1, 1);
526 $pretty .= substr($pv, 0, 2);
531 if (substr($board->[$to_row], $to_col, 1) ne '-') {
535 $pretty .= substr($pv, 2, 2);
539 substr($nb[$from_row], $from_col, 1, '-');
540 substr($nb[$to_row], $to_col, 1, $piece);
544 } elsif (in_check(\@nb) ne 'none') {
548 return ($pretty, prettyprint_pv(\@nb, @pvs));
554 return if (!defined($pos_calculating));
557 # Check the PVs first. if they're invalid, just wait, as our data
558 # is most likely out of sync. This isn't a very good solution, as
559 # it can frequently miss stuff, but it's good enough for most users.
563 if (exists($uciinfo{'pv'})) {
564 $dummy = prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv'}});
568 while (exists($uciinfo{'pv' . $mpv})) {
569 $dummy = prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv' . $mpv}});
578 my $text = 'Analysis';
579 if ($pos_calculating->{'last_move'} ne 'none') {
580 if ($pos_calculating->{'toplay'} eq 'W') {
581 $text .= sprintf ' after %u. ... %s', ($pos_calculating->{'move_num'}-1), $pos_calculating->{'last_move'};
583 $text .= sprintf ' after %u. %s', $pos_calculating->{'move_num'}, $pos_calculating->{'last_move'};
585 if (exists($uciid{'name'})) {
590 if (exists($uciid{'name'})) {
591 $text .= " by $uciid{'name'}:\n\n";
596 return unless (exists($pos_calculating->{'board'}));
599 # Some programs _always_ report MultiPV, even with only one PV.
600 # In this case, we simply use that data as if MultiPV was never
603 if (exists($uciinfo{'pv1'}) && !exists($uciinfo{'pv2'})) {
604 for my $key qw(pv score_cp score_mate nodes nps depth seldepth tbhits) {
605 if (exists($uciinfo{$key . '1'}) && !exists($uciinfo{$key})) {
606 $uciinfo{$key} = $uciinfo{$key . '1'};
611 if (exists($uciinfo{'pv1'}) && exists($uciinfo{'pv2'})) {
614 while (exists($uciinfo{'pv' . $mpv})) {
615 $text .= sprintf " PV%2u", $mpv;
616 my $score = short_score(\%uciinfo, $pos_calculating, $mpv);
617 $text .= " ($score)" if (defined($score));
620 if (exists($uciinfo{'tbhits' . $mpv}) && $uciinfo{'tbhits' . $mpv} > 0) {
621 if ($uciinfo{'tbhits' . $mpv} == 1) {
622 $tbhits = ", 1 tbhit";
624 $tbhits = sprintf ", %u tbhits", $uciinfo{'tbhits' . $mpv};
628 if (exists($uciinfo{'nodes' . $mpv}) && exists($uciinfo{'nps' . $mpv}) && exists($uciinfo{'depth' . $mpv})) {
629 $text .= sprintf " (%5u kn, %3u kn/s, %2u ply$tbhits)",
630 $uciinfo{'nodes' . $mpv} / 1000, $uciinfo{'nps' . $mpv} / 1000, $uciinfo{'depth' . $mpv};
634 $text .= " " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv' . $mpv}})) . "\n";
640 my $score = long_score(\%uciinfo, $pos_calculating, '');
641 $text .= " $score\n" if defined($score);
642 $text .= " PV: " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv'}}));
645 if (exists($uciinfo{'nodes'}) && exists($uciinfo{'nps'}) && exists($uciinfo{'depth'})) {
646 $text .= sprintf " %u nodes, %7u nodes/sec, depth %u ply",
647 $uciinfo{'nodes'}, $uciinfo{'nps'}, $uciinfo{'depth'};
649 if (exists($uciinfo{'tbhits'}) && $uciinfo{'tbhits'} > 0) {
650 if ($uciinfo{'tbhits'} == 1) {
651 $text .= ", one Nalimov hit";
653 $text .= sprintf ", %u Nalimov hits", $uciinfo{'tbhits'};
656 if (exists($uciinfo{'seldepth'})) {
657 $text .= sprintf " (%u selective)", $uciinfo{'seldepth'};
662 #$text .= book_info($pos_calculating->{'fen'}, $pos_calculating->{'board'}, $pos_calculating->{'toplay'});
664 if ($last_text ne $text) {
665 print "
\e[H
\e[2J"; # clear the screen
670 # Now construct the tell text, if any
671 return if (!defined($telltarget));
675 if (exists($uciid{'name'})) {
676 $tell_text .= "Analysis by $uciid{'name'} -- see http://analysis.sesse.net/ for more information\n";
678 $tell_text .= "Computer analysis -- http://analysis.sesse.net/ for more information\n";
681 if (exists($uciinfo{'pv1'}) && exists($uciinfo{'pv2'})) {
684 while (exists($uciinfo{'pv' . $mpv})) {
685 $tell_text .= sprintf " PV%2u", $mpv;
686 my $score = short_score(\%uciinfo, $pos_calculating, $mpv);
687 $tell_text .= " ($score)" if (defined($score));
689 if (exists($uciinfo{'depth' . $mpv})) {
690 $tell_text .= sprintf " (%2u ply)", $uciinfo{'depth' . $mpv};
694 $tell_text .= join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv' . $mpv}}));
700 my $score = long_score(\%uciinfo, $pos_calculating, '');
701 $tell_text .= " $score\n" if defined($score);
702 $tell_text .= " PV: " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv'}}));
703 if (exists($uciinfo{'depth'})) {
704 $tell_text .= sprintf " (depth %u ply)", $uciinfo{'depth'};
709 # see if a new tell is called for -- it is if the delay has expired _and_
710 # this is not simply a repetition of the last one
711 if ($last_told_text ne $tell_text) {
713 for my $iv (@tell_intervals) {
714 last if ($now - $last_move < $iv);
715 next if ($last_tell - $last_move >= $iv);
717 for my $line (split /\n/, $tell_text) {
718 $t->print("tell $telltarget [$target] $line");
721 $last_told_text = $text;
731 my ($wkr, $wkc, $bkr, $bkc);
735 my $piece = substr($board->[$row], $col, 1);
737 ($wkr, $wkc) = ($row, $col);
738 } elsif ($piece eq 'k') {
739 ($bkr, $bkc) = ($row, $col);
744 return ($wkr, $wkc, $bkr, $bkc);
749 my $check = in_check($board);
750 return 0 if ($check eq 'none');
752 # try all possible moves for the side in check
755 my $piece = substr($board->[$row], $col, 1);
756 next if ($piece eq '-');
758 if ($check eq 'white') {
759 next if ($piece eq lc($piece));
761 next if ($piece eq uc($piece));
764 for my $dest_row (0..7) {
765 for my $dest_col (0..7) {
766 next if ($row == $dest_row && $col == $dest_col);
767 next unless (can_reach($board, $piece, $row, $col, $dest_row, $dest_col));
770 substr($nb[$row], $col, 1, '-');
771 substr($nb[$dest_row], $dest_col, 1, $piece);
773 my $new_check = in_check(\@nb);
774 return 0 if ($new_check ne $check && $new_check ne 'both');
780 # nothing to do; mate
786 my ($black_check, $white_check) = (0, 0);
788 my ($wkr, $wkc, $bkr, $bkc) = find_kings($board);
790 # check all pieces for the possibility of threatening the two kings
793 my $piece = substr($board->[$row], $col, 1);
794 next if ($piece eq '-');
796 if (uc($piece) eq $piece) {
798 $black_check = 1 if (can_reach($board, $piece, $row, $col, $bkr, $bkc));
801 $white_check = 1 if (can_reach($board, $piece, $row, $col, $wkr, $wkc));
806 if ($black_check && $white_check) {
808 } elsif ($black_check) {
810 } elsif ($white_check) {
818 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
820 # can't eat your own piece
821 my $dest_piece = substr($board->[$to_row], $to_col, 1);
822 if ($dest_piece ne '-') {
823 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
826 if (lc($piece) eq 'k') {
827 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
829 if (lc($piece) eq 'r') {
830 return 0 unless ($from_row == $to_row || $from_col == $to_col);
832 # check that there's a clear passage
833 if ($from_row == $to_row) {
834 if ($from_col > $to_col) {
835 ($to_col, $from_col) = ($from_col, $to_col);
838 for my $c (($from_col+1)..($to_col-1)) {
839 my $middle_piece = substr($board->[$to_row], $c, 1);
840 return 0 if ($middle_piece ne '-');
845 if ($from_row > $to_row) {
846 ($to_row, $from_row) = ($from_row, $to_row);
849 for my $r (($from_row+1)..($to_row-1)) {
850 my $middle_piece = substr($board->[$r], $to_col, 1);
851 return 0 if ($middle_piece ne '-');
857 if (lc($piece) eq 'b') {
858 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
860 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
861 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
863 my $r = $from_row + $dr;
864 my $c = $from_col + $dc;
866 while ($r != $to_row) {
867 my $middle_piece = substr($board->[$r], $c, 1);
868 return 0 if ($middle_piece ne '-');
876 if (lc($piece) eq 'n') {
877 my $diff_r = abs($from_row - $to_row);
878 my $diff_c = abs($from_col - $to_col);
879 return 1 if ($diff_r == 2 && $diff_c == 1);
880 return 1 if ($diff_r == 1 && $diff_c == 2);
884 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
885 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
888 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
889 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
893 if ($to_col == $from_col && $to_row == $from_row + 1) {
894 return ($dest_piece eq '-');
896 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
897 return ($dest_piece ne '-');
903 if ($to_col == $from_col && $to_row == $from_row - 1) {
904 return ($dest_piece eq '-');
906 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
907 return ($dest_piece ne '-');
918 print UCIWRITE "$msg\n";
919 print UCILOG localtime() . " => $msg\n";
923 my ($uciinfo, $pos, $mpv) = @_;
925 if (defined($uciinfo{'score_mate' . $mpv})) {
926 return sprintf "M%3d", $uciinfo{'score_mate' . $mpv};
928 if (exists($uciinfo{'score_cp' . $mpv})) {
929 my $score = $uciinfo{'score_cp' . $mpv} * 0.01;
930 if ($pos->{'toplay'} eq 'B') {
933 return sprintf "%+5.2f", $score;
941 my ($uciinfo, $pos, $mpv) = @_;
943 if (defined($uciinfo{'score_mate' . $mpv})) {
944 my $mate = $uciinfo{'score_mate' . $mpv};
945 if ($pos->{'toplay'} eq 'B') {
949 return sprintf "White mates in %u", $mate;
951 return sprintf "Black mates in %u", -$mate;
954 if (exists($uciinfo{'score_cp' . $mpv})) {
955 my $score = $uciinfo{'score_cp' . $mpv} * 0.01;
956 if ($pos->{'toplay'} eq 'B') {
959 return sprintf "Score: %+5.2f", $score;
968 my ($fen, $board, $toplay) = @_;
970 if (exists($book_cache{$fen})) {
971 return $book_cache{$fen};
974 my $ret = `./booklook $fen`;
975 return "" if ($ret =~ /Not found/ || $ret eq '');
979 for my $m (split /\n/, $ret) {
980 my ($move, $annotation, $win, $draw, $lose, $rating, $rating_div) = split /,/, $m;
984 $pmove = '(current)';
986 ($pmove) = prettyprint_pv($board, $move);
987 $pmove .= $annotation;
991 if ($toplay eq 'W') {
992 $score = 1.0 * $win + 0.5 * $draw + 0.0 * $lose;
994 $score = 0.0 * $win + 0.5 * $draw + 1.0 * $lose;
996 my $n = $win + $draw + $lose;
1002 $percent = sprintf "%4u%%", int(100.0 * $score / $n + 0.5);
1005 push @moves, [ $pmove, $n, $percent, $rating ];
1008 @moves[1..$#moves] = sort { $b->[2] cmp $a->[2] } @moves[1..$#moves];
1010 my $text = "Book moves:\n\n Perf. N Rating\n\n";
1011 for my $m (@moves) {
1012 $text .= sprintf " %-10s %s %6u %4s\n", $m->[0], $m->[2], $m->[1], $m->[3]