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 print UCILOG localtime() . " <= $line\n";
219 if ($line =~ /^info/) {
220 my (@infos) = split / /, $line;
225 if ($line =~ /^id/) {
226 my (@ids) = split / /, $line;
231 if ($line =~ /^bestmove/ && $uci_assume_full_compliance) {
232 if (defined($pos_waiting)) {
233 uciprint("position fen " . $pos_waiting->{'fen'});
234 uciprint("go infinite");
236 $pos_calculating = $pos_waiting;
237 $pos_waiting = undef;
246 while (scalar @x > 0) {
247 if ($x[0] =~ 'multipv') {
252 if ($x[0] =~ /^(currmove|currmovenumber|cpuload)$/) {
254 my $value = shift @x;
255 $uciinfo{$key} = $value;
258 if ($x[0] =~ /^(depth|seldepth|hashfull|time|nodes|nps|tbhits)$/) {
260 my $value = shift @x;
261 $uciinfo{$key . $mpv} = $value;
264 if ($x[0] eq 'score') {
267 delete $uciinfo{'score_cp' . $mpv};
268 delete $uciinfo{'score_mate' . $mpv};
270 while ($x[0] =~ /^(cp|mate|lowerbound|upperbound)$/) {
273 $uciinfo{'score_cp' . $mpv} = shift @x;
274 } elsif ($x[0] eq 'mate') {
276 $uciinfo{'score_mate' . $mpv} = shift @x;
284 $uciinfo{'pv' . $mpv} = [ @x[1..$#x] ];
287 if ($x[0] eq 'string' || $x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting' || $x[0] eq 'contempt') {
291 #print "unknown info '$x[0]', trying to recover...\n";
293 die "Unknown info '" . join(',', @x) . "'";
301 while (scalar @x > 0) {
302 if ($x[0] =~ /^(name|author)$/) {
304 my $value = join(' ', @x);
305 $uciid{$key} = $value;
317 my (@x) = split / /, $str;
319 $pos{'board'} = [ @x[1..8] ];
320 $pos{'toplay'} = $x[9];
323 my (@board) = @x[1..8];
324 for my $rank (0..7) {
325 $board[$rank] =~ s/(-+)/length($1)/ge;
327 my $fen = join('/', @board);
329 # white/black to move
335 $castling .= "K" if ($x[11] == 1);
336 $castling .= "Q" if ($x[12] == 1);
337 $castling .= "k" if ($x[13] == 1);
338 $castling .= "q" if ($x[14] == 1);
339 $castling = "-" if ($castling eq "");
340 # $castling = "-"; # chess960
348 my $nep = (qw(a b c d e f g h))[$col];
357 # Showing the en passant square when actually no capture can be made
358 # seems to confuse at least Rybka. Thus, check if there's actually
359 # a pawn of the opposite side that can do the en passant move, and if
360 # not, just lie -- it doesn't matter anyway. I'm unsure what's the
361 # "right" thing as per the standard, though.
364 $ep = $nep if ($col > 0 && substr($pos{'board'}[4], $col-1, 1) eq 'p');
365 $ep = $nep if ($col < 7 && substr($pos{'board'}[4], $col+1, 1) eq 'p');
367 $ep = $nep if ($col > 0 && substr($pos{'board'}[3], $col-1, 1) eq 'P');
368 $ep = $nep if ($col < 7 && substr($pos{'board'}[3], $col+1, 1) eq 'P');
383 $pos{'move_num'} = $x[26];
384 $pos{'last_move'} = $x[29];
390 my ($board, @pvs) = @_;
392 if (scalar @pvs == 0 || !defined($pvs[0])) {
399 my $from_col = ord(substr($pv, 0, 1)) - ord('a');
400 my $from_row = 7 - (ord(substr($pv, 1, 1)) - ord('1'));
401 my $to_col = ord(substr($pv, 2, 1)) - ord('a');
402 my $to_row = 7 - (ord(substr($pv, 3, 1)) - ord('1'));
405 my $piece = substr($board->[$from_row], $from_col, 1);
408 die "Invalid move $pv";
411 # white short castling
412 if ($pv eq 'e1g1' && $piece eq 'K') {
414 substr($nb[7], 4, 1, '-');
415 substr($nb[7], 6, 1, $piece);
418 substr($nb[7], 7, 1, '-');
419 substr($nb[7], 5, 1, 'R');
421 return ('0-0', prettyprint_pv(\@nb, @pvs));
424 # white long castling
425 if ($pv eq 'e1c1' && $piece eq 'K') {
427 substr($nb[7], 4, 1, '-');
428 substr($nb[7], 2, 1, $piece);
431 substr($nb[7], 0, 1, '-');
432 substr($nb[7], 3, 1, 'R');
434 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
437 # black short castling
438 if ($pv eq 'e8g8' && $piece eq 'k') {
440 substr($nb[0], 4, 1, '-');
441 substr($nb[0], 6, 1, $piece);
444 substr($nb[0], 7, 1, '-');
445 substr($nb[0], 5, 1, 'r');
447 return ('0-0', prettyprint_pv(\@nb, @pvs));
450 # black long castling
451 if ($pv eq 'e8c8' && $piece eq 'k') {
453 substr($nb[0], 4, 1, '-');
454 substr($nb[0], 2, 1, $piece);
457 substr($nb[0], 0, 1, '-');
458 substr($nb[0], 3, 1, 'r');
460 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
463 # check if the from-piece is a pawn
464 if (lc($piece) eq 'p') {
466 if ($from_col != $to_col) {
467 $pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
470 if (substr($board->[$to_row], $to_col, 1) eq '-') {
472 substr($nb[$to_row + 1], $to_col, 1, '-');
474 substr($nb[$to_row - 1], $to_col, 1, '-');
478 $pretty = substr($pv, 2, 2);
480 if (length($pv) == 5) {
483 $pretty .= uc(substr($pv, 4, 1));
486 $piece = substr($pv, 4, 1);
488 $piece = uc(substr($pv, 4, 1));
493 $pretty = uc($piece);
495 # see how many of these pieces could go here, in all
499 next unless (substr($board->[$row], $col, 1) eq $piece);
500 ++$num_total if (can_reach($board, $piece, $row, $col, $to_row, $to_col));
504 # see how many of these pieces from the given row could go here
507 next unless (substr($board->[$from_row], $col, 1) eq $piece);
508 ++$num_row if (can_reach($board, $piece, $from_row, $col, $to_row, $to_col));
511 # and same for columns
514 next unless (substr($board->[$row], $from_col, 1) eq $piece);
515 ++$num_col if (can_reach($board, $piece, $row, $from_col, $to_row, $to_col));
518 # see if we need to disambiguate
519 if ($num_total > 1) {
521 $pretty .= substr($pv, 0, 1);
522 } elsif ($num_row == 1) {
523 $pretty .= substr($pv, 1, 1);
525 $pretty .= substr($pv, 0, 2);
530 if (substr($board->[$to_row], $to_col, 1) ne '-') {
534 $pretty .= substr($pv, 2, 2);
538 substr($nb[$from_row], $from_col, 1, '-');
539 substr($nb[$to_row], $to_col, 1, $piece);
543 } elsif (in_check(\@nb) ne 'none') {
547 return ($pretty, prettyprint_pv(\@nb, @pvs));
553 return if (!defined($pos_calculating));
556 # Check the PVs first. if they're invalid, just wait, as our data
557 # is most likely out of sync. This isn't a very good solution, as
558 # it can frequently miss stuff, but it's good enough for most users.
562 if (exists($uciinfo{'pv'})) {
563 $dummy = prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv'}});
567 while (exists($uciinfo{'pv' . $mpv})) {
568 $dummy = prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv' . $mpv}});
577 my $text = 'Analysis';
578 if ($pos_calculating->{'last_move'} ne 'none') {
579 if ($pos_calculating->{'toplay'} eq 'W') {
580 $text .= sprintf ' after %u. ... %s', ($pos_calculating->{'move_num'}-1), $pos_calculating->{'last_move'};
582 $text .= sprintf ' after %u. %s', $pos_calculating->{'move_num'}, $pos_calculating->{'last_move'};
584 if (exists($uciid{'name'})) {
589 if (exists($uciid{'name'})) {
590 $text .= " by $uciid{'name'}:\n\n";
595 return unless (exists($pos_calculating->{'board'}));
598 # Some programs _always_ report MultiPV, even with only one PV.
599 # In this case, we simply use that data as if MultiPV was never
602 if (exists($uciinfo{'pv1'}) && !exists($uciinfo{'pv2'})) {
603 for my $key qw(pv score_cp score_mate nodes nps depth seldepth tbhits) {
604 if (exists($uciinfo{$key . '1'}) && !exists($uciinfo{$key})) {
605 $uciinfo{$key} = $uciinfo{$key . '1'};
610 if (exists($uciinfo{'pv1'}) && exists($uciinfo{'pv2'})) {
613 while (exists($uciinfo{'pv' . $mpv})) {
614 $text .= sprintf " PV%2u", $mpv;
615 my $score = short_score(\%uciinfo, $pos_calculating, $mpv);
616 $text .= " ($score)" if (defined($score));
619 if (exists($uciinfo{'tbhits' . $mpv}) && $uciinfo{'tbhits' . $mpv} > 0) {
620 if ($uciinfo{'tbhits' . $mpv} == 1) {
621 $tbhits = ", 1 tbhit";
623 $tbhits = sprintf ", %u tbhits", $uciinfo{'tbhits' . $mpv};
627 if (exists($uciinfo{'nodes' . $mpv}) && exists($uciinfo{'nps' . $mpv}) && exists($uciinfo{'depth' . $mpv})) {
628 $text .= sprintf " (%5u kn, %3u kn/s, %2u ply$tbhits)",
629 $uciinfo{'nodes' . $mpv} / 1000, $uciinfo{'nps' . $mpv} / 1000, $uciinfo{'depth' . $mpv};
633 $text .= " " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv' . $mpv}})) . "\n";
639 my $score = long_score(\%uciinfo, $pos_calculating, '');
640 $text .= " $score\n" if defined($score);
641 $text .= " PV: " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv'}}));
644 if (exists($uciinfo{'nodes'}) && exists($uciinfo{'nps'}) && exists($uciinfo{'depth'})) {
645 $text .= sprintf " %u nodes, %7u nodes/sec, depth %u ply",
646 $uciinfo{'nodes'}, $uciinfo{'nps'}, $uciinfo{'depth'};
648 if (exists($uciinfo{'tbhits'}) && $uciinfo{'tbhits'} > 0) {
649 if ($uciinfo{'tbhits'} == 1) {
650 $text .= ", one Nalimov hit";
652 $text .= sprintf ", %u Nalimov hits", $uciinfo{'tbhits'};
655 if (exists($uciinfo{'seldepth'})) {
656 $text .= sprintf " (%u selective)", $uciinfo{'seldepth'};
661 #$text .= book_info($pos_calculating->{'fen'}, $pos_calculating->{'board'}, $pos_calculating->{'toplay'});
663 if ($last_text ne $text) {
664 print "
\e[H
\e[2J"; # clear the screen
669 # Now construct the tell text, if any
670 return if (!defined($telltarget));
674 if (exists($uciid{'name'})) {
675 $tell_text .= "Analysis by $uciid{'name'} -- see http://analysis.sesse.net/ for more information\n";
677 $tell_text .= "Computer analysis -- http://analysis.sesse.net/ for more information\n";
680 if (exists($uciinfo{'pv1'}) && exists($uciinfo{'pv2'})) {
683 while (exists($uciinfo{'pv' . $mpv})) {
684 $tell_text .= sprintf " PV%2u", $mpv;
685 my $score = short_score(\%uciinfo, $pos_calculating, $mpv);
686 $tell_text .= " ($score)" if (defined($score));
688 if (exists($uciinfo{'depth' . $mpv})) {
689 $tell_text .= sprintf " (%2u ply)", $uciinfo{'depth' . $mpv};
693 $tell_text .= join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv' . $mpv}}));
699 my $score = long_score(\%uciinfo, $pos_calculating, '');
700 $tell_text .= " $score\n" if defined($score);
701 $tell_text .= " PV: " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv'}}));
702 if (exists($uciinfo{'depth'})) {
703 $tell_text .= sprintf " (depth %u ply)", $uciinfo{'depth'};
708 # see if a new tell is called for -- it is if the delay has expired _and_
709 # this is not simply a repetition of the last one
710 if ($last_told_text ne $tell_text) {
712 for my $iv (@tell_intervals) {
713 last if ($now - $last_move < $iv);
714 next if ($last_tell - $last_move >= $iv);
716 for my $line (split /\n/, $tell_text) {
717 $t->print("tell $telltarget [$target] $line");
720 $last_told_text = $text;
730 my ($wkr, $wkc, $bkr, $bkc);
734 my $piece = substr($board->[$row], $col, 1);
736 ($wkr, $wkc) = ($row, $col);
737 } elsif ($piece eq 'k') {
738 ($bkr, $bkc) = ($row, $col);
743 return ($wkr, $wkc, $bkr, $bkc);
748 my $check = in_check($board);
749 return 0 if ($check eq 'none');
751 # try all possible moves for the side in check
754 my $piece = substr($board->[$row], $col, 1);
755 next if ($piece eq '-');
757 if ($check eq 'white') {
758 next if ($piece eq lc($piece));
760 next if ($piece eq uc($piece));
763 for my $dest_row (0..7) {
764 for my $dest_col (0..7) {
765 next if ($row == $dest_row && $col == $dest_col);
766 next unless (can_reach($board, $piece, $row, $col, $dest_row, $dest_col));
769 substr($nb[$row], $col, 1, '-');
770 substr($nb[$dest_row], $dest_col, 1, $piece);
772 my $new_check = in_check(\@nb);
773 return 0 if ($new_check ne $check && $new_check ne 'both');
779 # nothing to do; mate
785 my ($black_check, $white_check) = (0, 0);
787 my ($wkr, $wkc, $bkr, $bkc) = find_kings($board);
789 # check all pieces for the possibility of threatening the two kings
792 my $piece = substr($board->[$row], $col, 1);
793 next if ($piece eq '-');
795 if (uc($piece) eq $piece) {
797 $black_check = 1 if (can_reach($board, $piece, $row, $col, $bkr, $bkc));
800 $white_check = 1 if (can_reach($board, $piece, $row, $col, $wkr, $wkc));
805 if ($black_check && $white_check) {
807 } elsif ($black_check) {
809 } elsif ($white_check) {
817 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
819 # can't eat your own piece
820 my $dest_piece = substr($board->[$to_row], $to_col, 1);
821 if ($dest_piece ne '-') {
822 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
825 if (lc($piece) eq 'k') {
826 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
828 if (lc($piece) eq 'r') {
829 return 0 unless ($from_row == $to_row || $from_col == $to_col);
831 # check that there's a clear passage
832 if ($from_row == $to_row) {
833 if ($from_col > $to_col) {
834 ($to_col, $from_col) = ($from_col, $to_col);
837 for my $c (($from_col+1)..($to_col-1)) {
838 my $middle_piece = substr($board->[$to_row], $c, 1);
839 return 0 if ($middle_piece ne '-');
844 if ($from_row > $to_row) {
845 ($to_row, $from_row) = ($from_row, $to_row);
848 for my $r (($from_row+1)..($to_row-1)) {
849 my $middle_piece = substr($board->[$r], $to_col, 1);
850 return 0 if ($middle_piece ne '-');
856 if (lc($piece) eq 'b') {
857 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
859 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
860 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
862 my $r = $from_row + $dr;
863 my $c = $from_col + $dc;
865 while ($r != $to_row) {
866 my $middle_piece = substr($board->[$r], $c, 1);
867 return 0 if ($middle_piece ne '-');
875 if (lc($piece) eq 'n') {
876 my $diff_r = abs($from_row - $to_row);
877 my $diff_c = abs($from_col - $to_col);
878 return 1 if ($diff_r == 2 && $diff_c == 1);
879 return 1 if ($diff_r == 1 && $diff_c == 2);
883 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
884 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
887 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
888 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
892 if ($to_col == $from_col && $to_row == $from_row + 1) {
893 return ($dest_piece eq '-');
895 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
896 return ($dest_piece ne '-');
902 if ($to_col == $from_col && $to_row == $from_row - 1) {
903 return ($dest_piece eq '-');
905 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
906 return ($dest_piece ne '-');
917 print UCIWRITE "$msg\n";
918 print UCILOG localtime() . " => $msg\n";
922 my ($uciinfo, $pos, $mpv) = @_;
924 if (defined($uciinfo{'score_mate' . $mpv})) {
925 return sprintf "M%3d", $uciinfo{'score_mate' . $mpv};
927 if (exists($uciinfo{'score_cp' . $mpv})) {
928 my $score = $uciinfo{'score_cp' . $mpv} * 0.01;
929 if ($pos->{'toplay'} eq 'B') {
932 return sprintf "%+5.2f", $score;
940 my ($uciinfo, $pos, $mpv) = @_;
942 if (defined($uciinfo{'score_mate' . $mpv})) {
943 my $mate = $uciinfo{'score_mate' . $mpv};
944 if ($pos->{'toplay'} eq 'B') {
948 return sprintf "White mates in %u", $mate;
950 return sprintf "Black mates in %u", -$mate;
953 if (exists($uciinfo{'score_cp' . $mpv})) {
954 my $score = $uciinfo{'score_cp' . $mpv} * 0.01;
955 if ($pos->{'toplay'} eq 'B') {
958 return sprintf "Score: %+5.2f", $score;
967 my ($fen, $board, $toplay) = @_;
969 if (exists($book_cache{$fen})) {
970 return $book_cache{$fen};
973 my $ret = `./booklook $fen`;
974 return "" if ($ret =~ /Not found/ || $ret eq '');
978 for my $m (split /\n/, $ret) {
979 my ($move, $annotation, $win, $draw, $lose, $rating, $rating_div) = split /,/, $m;
983 $pmove = '(current)';
985 ($pmove) = prettyprint_pv($board, $move);
986 $pmove .= $annotation;
990 if ($toplay eq 'W') {
991 $score = 1.0 * $win + 0.5 * $draw + 0.0 * $lose;
993 $score = 0.0 * $win + 0.5 * $draw + 1.0 * $lose;
995 my $n = $win + $draw + $lose;
1001 $percent = sprintf "%4u%%", int(100.0 * $score / $n + 0.5);
1004 push @moves, [ $pmove, $n, $percent, $rating ];
1007 @moves[1..$#moves] = sort { $b->[2] cmp $a->[2] } @moves[1..$#moves];
1009 my $text = "Book moves:\n\n Perf. N Rating\n\n";
1010 for my $m (@moves) {
1011 $text .= sprintf " %-10s %s %6u %4s\n", $m->[0], $m->[2], $m->[1], $m->[3]