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 = "Sessse";
22 # my $engine = "/usr/games/toga2";
23 # my $engine = "wine Rybkav2.3.2a.mp.w32.exe";
24 my $engine = "~/microwine-0.2/microwine Rybkav2.3.2a.mp.x64.exe";
25 my $telltarget = undef; # undef to be silent
26 my @tell_intervals = (5, 20, 60, 120, 240, 480, 960); # after each move
27 my $uci_assume_full_compliance = 1; # dangerous :-)
35 $SIG{ALRM} = sub { output_screen(); };
39 open(FICSLOG, ">ficslog.txt")
40 or die "ficslog.txt: $!";
41 print FICSLOG "Log starting.\n";
45 open(UCILOG, ">ucilog.txt")
46 or die "ucilog.txt: $!";
47 print UCILOG "Log starting.\n";
52 # open the chess engine
53 my $pid = IPC::Open2::open2(*UCIREAD, *UCIWRITE, $engine);
56 my ($last_move, $last_tell);
58 my $last_told_text = '';
59 my ($pos_waiting, $pos_calculating);
69 uciprint("setoption name UCI_AnalyseMode value true");
70 # uciprint("setoption name Preserve Analysis value true");
71 uciprint("setoption name NalimovPath value /srv/tablebase");
72 uciprint("setoption name NalimovUsage value Rarely");
73 uciprint("setoption name Hash value 1024");
74 uciprint("setoption name MultiPV value 2");
75 # uciprint("setoption name Contempt value 1000");
76 # uciprint("setoption name Outlook value Ultra Optimistic");
77 uciprint("ucinewgame");
79 print "Chess engine ready.\n";
82 my $t = Net::Telnet->new(Timeout => 10, Prompt => '/fics% /');
83 $t->input_log(\*FICSLOG);
86 $t->waitfor('/Press return to enter the server/');
90 $t->cmd("set shout 0");
91 $t->cmd("set seek 0");
92 $t->cmd("set style 12");
93 $t->cmd("observe $target");
96 print "FICS ready.\n";
100 vec($rin, fileno(UCIREAD), 1) = 1;
101 vec($rin, fileno($t), 1) = 1;
103 my ($nfound, $timeleft) = select($rout=$rin, undef, undef, 5.0);
107 my $line = $t->getline(Timeout => 0, errmode => 'return');
108 last if (!defined($line));
112 if ($line =~ /^<12> /) {
113 my $pos = style12_to_fen($line);
115 # if this is already in the queue, ignore it
116 next if (defined($pos_waiting) && $pos->{'fen'} eq $pos_waiting->{'fen'});
118 # if we're already chewing on this and there's nothing else in the queue,
120 next if (!defined($pos_waiting) && defined($pos_calculating) &&
121 $pos->{'fen'} eq $pos_calculating->{'fen'});
123 # if we're already thinking on something, stop and wait for the engine
125 if (defined($pos_calculating)) {
126 if (!defined($pos_waiting)) {
129 if ($uci_assume_full_compliance) {
132 uciprint("position fen " . $pos->{'fen'});
133 uciprint("go infinite");
134 $pos_calculating = $pos;
137 # it's wrong just to give the FEN (the move history is useful,
138 # and per the UCI spec, we should really have sent "ucinewgame"),
140 uciprint("position fen " . $pos->{'fen'});
141 uciprint("go infinite");
142 $pos_calculating = $pos;
149 # Output a command every move to note that we're
150 # still paying attention -- this is a good tradeoff,
151 # since if no move has happened in the last half
152 # hour, the analysis/relay has most likely stopped
153 # and we should stop hogging server resources.
157 if ($line =~ /^([A-Za-z]+)(?:\([A-Z]+\))* tells you: (.*)$/) {
158 my ($who, $msg) = ($1, $2);
160 next if (grep { $_ eq $who } (@masters) == 0);
162 if ($msg =~ /^fics (.*?)$/) {
163 $t->cmd("tell $who Executing '$1' on FICS.");
165 } elsif ($msg =~ /^uci (.*?)$/) {
166 $t->cmd("tell $who Sending '$1' to the engine.");
167 print UCIWRITE "$1\n";
169 $t->cmd("tell $who Couldn't understand '$msg', sorry.");
172 #print "FICS: [$line]\n";
176 # any fun on the UCI channel?
177 if ($nfound > 0 && vec($rout, fileno(UCIREAD), 1) == 1) {
179 # Read until we've got a full line -- if the engine sends part of
180 # a line and then stops we're pretty much hosed, but that should
184 while ($line !~ /\n/) {
186 my $ret = sysread UCIREAD, $tmp, 1;
188 if (!defined($ret)) {
190 die "error in reading from the UCI engine: $!";
191 } elsif ($ret == 0) {
192 die "EOF from UCI engine";
202 # don't update too often
203 Time::HiRes::alarm(0.2);
214 print UCILOG localtime() . " <= $line\n";
215 if ($line =~ /^info/) {
216 my (@infos) = split / /, $line;
221 if ($line =~ /^id/) {
222 my (@ids) = split / /, $line;
227 if ($line =~ /^bestmove/ && $uci_assume_full_compliance) {
228 if (defined($pos_waiting)) {
229 uciprint("position fen " . $pos_waiting->{'fen'});
230 uciprint("go infinite");
232 $pos_calculating = $pos_waiting;
233 $pos_waiting = undef;
242 while (scalar @x > 0) {
243 if ($x[0] =~ 'multipv') {
248 if ($x[0] =~ /^(currmove|currmovenumber|cpuload)$/) {
250 my $value = shift @x;
251 $uciinfo{$key} = $value;
254 if ($x[0] =~ /^(depth|seldepth|hashfull|time|nodes|nps|tbhits)$/) {
256 my $value = shift @x;
257 $uciinfo{$key . $mpv} = $value;
260 if ($x[0] eq 'score') {
263 delete $uciinfo{'score_cp' . $mpv};
264 delete $uciinfo{'score_mate' . $mpv};
266 while ($x[0] =~ /^(cp|mate|lowerbound|upperbound)$/) {
269 $uciinfo{'score_cp' . $mpv} = shift @x;
270 } elsif ($x[0] eq 'mate') {
272 $uciinfo{'score_mate' . $mpv} = shift @x;
280 $uciinfo{'pv' . $mpv} = [ @x[1..$#x] ];
283 if ($x[0] eq 'string' || $x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting' || $x[0] eq 'contempt') {
287 #print "unknown info '$x[0]', trying to recover...\n";
289 die "Unknown info '" . join(',', @x) . "'";
297 while (scalar @x > 0) {
298 if ($x[0] =~ /^(name|author)$/) {
300 my $value = join(' ', @x);
301 $uciid{$key} = $value;
313 my (@x) = split / /, $str;
315 $pos{'board'} = [ @x[1..8] ];
316 $pos{'toplay'} = $x[9];
319 my (@board) = @x[1..8];
320 for my $rank (0..7) {
321 $board[$rank] =~ s/(-+)/length($1)/ge;
323 my $fen = join('/', @board);
325 # white/black to move
331 $castling .= "K" if ($x[11] == 1);
332 $castling .= "Q" if ($x[12] == 1);
333 $castling .= "k" if ($x[13] == 1);
334 $castling .= "q" if ($x[14] == 1);
335 $castling = "-" if ($castling eq "");
343 my $nep = (qw(a b c d e f g h))[$col];
352 # Showing the en passant square when actually no capture can be made
353 # seems to confuse at least Rybka. Thus, check if there's actually
354 # a pawn of the opposite side that can do the en passant move, and if
355 # not, just lie -- it doesn't matter anyway. I'm unsure what's the
356 # "right" thing as per the standard, though.
359 $ep = $nep if ($col > 0 && substr($pos{'board'}[4], $col-1, 1) eq 'p');
360 $ep = $nep if ($col < 7 && substr($pos{'board'}[4], $col+1, 1) eq 'p');
362 $ep = $nep if ($col > 0 && substr($pos{'board'}[3], $col-1, 1) eq 'P');
363 $ep = $nep if ($col < 7 && substr($pos{'board'}[3], $col+1, 1) eq 'P');
378 $pos{'move_num'} = $x[26];
379 $pos{'last_move'} = $x[29];
385 my ($board, @pvs) = @_;
387 if (scalar @pvs == 0 || !defined($pvs[0])) {
394 my $from_col = ord(substr($pv, 0, 1)) - ord('a');
395 my $from_row = 7 - (ord(substr($pv, 1, 1)) - ord('1'));
396 my $to_col = ord(substr($pv, 2, 1)) - ord('a');
397 my $to_row = 7 - (ord(substr($pv, 3, 1)) - ord('1'));
400 my $piece = substr($board->[$from_row], $from_col, 1);
406 # white short castling
407 if ($pv eq 'e1g1' && $piece eq 'K') {
409 substr($nb[7], 4, 1, '-');
410 substr($nb[7], 6, 1, $piece);
413 substr($nb[7], 7, 1, '-');
414 substr($nb[7], 5, 1, 'R');
416 return ('0-0', prettyprint_pv(\@nb, @pvs));
419 # white long castling
420 if ($pv eq 'e1c1' && $piece eq 'K') {
422 substr($nb[7], 4, 1, '-');
423 substr($nb[7], 2, 1, $piece);
426 substr($nb[7], 0, 1, '-');
427 substr($nb[7], 3, 1, 'R');
429 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
432 # black short castling
433 if ($pv eq 'e8g8' && $piece eq 'k') {
435 substr($nb[0], 4, 1, '-');
436 substr($nb[0], 6, 1, $piece);
439 substr($nb[0], 7, 1, '-');
440 substr($nb[0], 5, 1, 'r');
442 return ('0-0', prettyprint_pv(\@nb, @pvs));
445 # black long castling
446 if ($pv eq 'e8c8' && $piece eq 'k') {
448 substr($nb[0], 4, 1, '-');
449 substr($nb[0], 2, 1, $piece);
452 substr($nb[0], 0, 1, '-');
453 substr($nb[0], 3, 1, 'r');
455 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
458 # check if the from-piece is a pawn
459 if (lc($piece) eq 'p') {
461 if ($from_col != $to_col) {
462 $pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
465 if (substr($board->[$to_row], $to_col, 1) eq '-') {
467 substr($nb[$to_row + 1], $to_col, 1, '-');
469 substr($nb[$to_row - 1], $to_col, 1, '-');
473 $pretty = substr($pv, 2, 2);
475 if (length($pv) == 5) {
478 $pretty .= uc(substr($pv, 4, 1));
481 $piece = substr($pv, 4, 1);
483 $piece = uc(substr($pv, 4, 1));
488 $pretty = uc($piece);
490 # see how many of these pieces could go here, in all
494 next unless (substr($board->[$row], $col, 1) eq $piece);
495 ++$num_total if (can_reach($board, $piece, $row, $col, $to_row, $to_col));
499 # see how many of these pieces from the given row could go here
502 next unless (substr($board->[$from_row], $col, 1) eq $piece);
503 ++$num_row if (can_reach($board, $piece, $from_row, $col, $to_row, $to_col));
506 # and same for columns
509 next unless (substr($board->[$row], $from_col, 1) eq $piece);
510 ++$num_col if (can_reach($board, $piece, $row, $from_col, $to_row, $to_col));
513 # see if we need to disambiguate
514 if ($num_total > 1) {
516 $pretty .= substr($pv, 0, 1);
517 } elsif ($num_row == 1) {
518 $pretty .= substr($pv, 1, 1);
520 $pretty .= substr($pv, 0, 2);
525 if (substr($board->[$to_row], $to_col, 1) ne '-') {
529 $pretty .= substr($pv, 2, 2);
533 substr($nb[$from_row], $from_col, 1, '-');
534 substr($nb[$to_row], $to_col, 1, $piece);
538 } elsif (in_check(\@nb) ne 'none') {
542 return ($pretty, prettyprint_pv(\@nb, @pvs));
548 return if (!defined($pos_calculating));
551 # Check the PVs first. if they're invalid, just wait, as our data
552 # is most likely out of sync. This isn't a very good solution, as
553 # it can frequently miss stuff, but it's good enough for most users.
557 if (exists($uciinfo{'pv'})) {
558 $dummy = prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv'}});
562 while (exists($uciinfo{'pv' . $mpv})) {
563 $dummy = prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv' . $mpv}});
572 my $text = 'Analysis';
573 if ($pos_calculating->{'last_move'} ne 'none') {
574 if ($pos_calculating->{'toplay'} eq 'W') {
575 $text .= sprintf ' after %u. ... %s', ($pos_calculating->{'move_num'}-1), $pos_calculating->{'last_move'};
577 $text .= sprintf ' after %u. %s', $pos_calculating->{'move_num'}, $pos_calculating->{'last_move'};
579 if (exists($uciid{'name'})) {
584 if (exists($uciid{'name'})) {
585 $text .= " by $uciid{'name'}:\n\n";
590 return unless (exists($pos_calculating->{'board'}));
593 # Some programs _always_ report MultiPV, even with only one PV.
594 # In this case, we simply use that data as if MultiPV was never
597 if (exists($uciinfo{'pv1'}) && !exists($uciinfo{'pv2'})) {
598 for my $key qw(pv score_cp score_mate nodes nps depth seldepth tbhits) {
599 if (exists($uciinfo{$key . '1'}) && !exists($uciinfo{$key})) {
600 $uciinfo{$key} = $uciinfo{$key . '1'};
605 if (exists($uciinfo{'pv1'}) && exists($uciinfo{'pv2'})) {
608 while (exists($uciinfo{'pv' . $mpv})) {
609 $text .= sprintf " PV%2u", $mpv;
610 my $score = short_score(\%uciinfo, $pos_calculating, $mpv);
611 $text .= " ($score)" if (defined($score));
614 if (exists($uciinfo{'tbhits' . $mpv}) && $uciinfo{'tbhits' . $mpv} > 0) {
615 if ($uciinfo{'tbhits' . $mpv} == 1) {
616 $tbhits = ", 1 tbhit";
618 $tbhits = sprintf ", %u tbhits", $uciinfo{'tbhits' . $mpv};
622 if (exists($uciinfo{'nodes' . $mpv}) && exists($uciinfo{'nps' . $mpv}) && exists($uciinfo{'depth' . $mpv})) {
623 $text .= sprintf " (%5u kn, %3u kn/s, %2u ply$tbhits)",
624 $uciinfo{'nodes' . $mpv} / 1000, $uciinfo{'nps' . $mpv} / 1000, $uciinfo{'depth' . $mpv};
628 $text .= " " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv' . $mpv}})) . "\n";
634 my $score = long_score(\%uciinfo, $pos_calculating, '');
635 $text .= " $score\n" if defined($score);
636 $text .= " PV: " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv'}}));
639 if (exists($uciinfo{'nodes'}) && exists($uciinfo{'nps'}) && exists($uciinfo{'depth'})) {
640 $text .= sprintf " %u nodes, %7u nodes/sec, depth %u ply",
641 $uciinfo{'nodes'}, $uciinfo{'nps'}, $uciinfo{'depth'};
643 if (exists($uciinfo{'tbhits'}) && $uciinfo{'tbhits'} > 0) {
644 if ($uciinfo{'tbhits'} == 1) {
645 $text .= ", one Nalimov hit";
647 $text .= sprintf ", %u Nalimov hits", $uciinfo{'tbhits'};
650 if (exists($uciinfo{'seldepth'})) {
651 $text .= sprintf " (%u selective)", $uciinfo{'seldepth'};
656 $text .= book_info($pos_calculating->{'fen'}, $pos_calculating->{'board'}, $pos_calculating->{'toplay'});
658 if ($last_text ne $text) {
659 print "
\e[H
\e[2J"; # clear the screen
664 # Now construct the tell text, if any
665 return if (!defined($telltarget));
669 if (exists($uciid{'name'})) {
670 $tell_text .= "Analysis by $uciid{'name'} -- see http://analysis.sesse.net/ for more information\n";
672 $tell_text .= "Computer analysis -- http://analysis.sesse.net/ for more information\n";
675 if (exists($uciinfo{'pv1'}) && exists($uciinfo{'pv2'})) {
678 while (exists($uciinfo{'pv' . $mpv})) {
679 $tell_text .= sprintf " PV%2u", $mpv;
680 my $score = short_score(\%uciinfo, $pos_calculating, $mpv);
681 $tell_text .= " ($score)" if (defined($score));
683 if (exists($uciinfo{'depth' . $mpv})) {
684 $tell_text .= sprintf " (%2u ply)", $uciinfo{'depth' . $mpv};
688 $tell_text .= join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv' . $mpv}}));
694 my $score = long_score(\%uciinfo, $pos_calculating, '');
695 $tell_text .= " $score\n" if defined($score);
696 $tell_text .= " PV: " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv'}}));
697 if (exists($uciinfo{'depth'})) {
698 $tell_text .= sprintf " (depth %u ply)", $uciinfo{'depth'};
703 # see if a new tell is called for -- it is if the delay has expired _and_
704 # this is not simply a repetition of the last one
705 if ($last_told_text ne $tell_text) {
707 for my $iv (@tell_intervals) {
708 last if ($now - $last_move < $iv);
709 next if ($last_tell - $last_move >= $iv);
711 for my $line (split /\n/, $tell_text) {
712 $t->print("tell $telltarget [$target] $line");
715 $last_told_text = $text;
725 my ($wkr, $wkc, $bkr, $bkc);
729 my $piece = substr($board->[$row], $col, 1);
731 ($wkr, $wkc) = ($row, $col);
732 } elsif ($piece eq 'k') {
733 ($bkr, $bkc) = ($row, $col);
738 return ($wkr, $wkc, $bkr, $bkc);
743 my $check = in_check($board);
744 return 0 if ($check eq 'none');
746 # try all possible moves for the side in check
749 my $piece = substr($board->[$row], $col, 1);
750 next if ($piece eq '-');
752 if ($check eq 'white') {
753 next if ($piece eq lc($piece));
755 next if ($piece eq uc($piece));
758 for my $dest_row (0..7) {
759 for my $dest_col (0..7) {
760 next if ($row == $dest_row && $col == $dest_col);
761 next unless (can_reach($board, $piece, $row, $col, $dest_row, $dest_col));
764 substr($nb[$row], $col, 1, '-');
765 substr($nb[$dest_row], $dest_col, 1, $piece);
767 my $new_check = in_check(\@nb);
768 return 0 if ($new_check ne $check && $new_check ne 'both');
774 # nothing to do; mate
780 my ($black_check, $white_check) = (0, 0);
782 my ($wkr, $wkc, $bkr, $bkc) = find_kings($board);
784 # check all pieces for the possibility of threatening the two kings
787 my $piece = substr($board->[$row], $col, 1);
788 next if ($piece eq '-');
790 if (uc($piece) eq $piece) {
792 $black_check = 1 if (can_reach($board, $piece, $row, $col, $bkr, $bkc));
795 $white_check = 1 if (can_reach($board, $piece, $row, $col, $wkr, $wkc));
800 if ($black_check && $white_check) {
802 } elsif ($black_check) {
804 } elsif ($white_check) {
812 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
814 # can't eat your own piece
815 my $dest_piece = substr($board->[$to_row], $to_col, 1);
816 if ($dest_piece ne '-') {
817 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
820 if (lc($piece) eq 'k') {
821 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
823 if (lc($piece) eq 'r') {
824 return 0 unless ($from_row == $to_row || $from_col == $to_col);
826 # check that there's a clear passage
827 if ($from_row == $to_row) {
828 if ($from_col > $to_col) {
829 ($to_col, $from_col) = ($from_col, $to_col);
832 for my $c (($from_col+1)..($to_col-1)) {
833 my $middle_piece = substr($board->[$to_row], $c, 1);
834 return 0 if ($middle_piece ne '-');
839 if ($from_row > $to_row) {
840 ($to_row, $from_row) = ($from_row, $to_row);
843 for my $r (($from_row+1)..($to_row-1)) {
844 my $middle_piece = substr($board->[$r], $to_col, 1);
845 return 0 if ($middle_piece ne '-');
851 if (lc($piece) eq 'b') {
852 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
854 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
855 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
857 my $r = $from_row + $dr;
858 my $c = $from_col + $dc;
860 while ($r != $to_row) {
861 my $middle_piece = substr($board->[$r], $c, 1);
862 return 0 if ($middle_piece ne '-');
870 if (lc($piece) eq 'n') {
871 my $diff_r = abs($from_row - $to_row);
872 my $diff_c = abs($from_col - $to_col);
873 return 1 if ($diff_r == 2 && $diff_c == 1);
874 return 1 if ($diff_r == 1 && $diff_c == 2);
878 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
879 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
882 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
883 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
887 if ($to_col == $from_col && $to_row == $from_row + 1) {
888 return ($dest_piece eq '-');
890 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
891 return ($dest_piece ne '-');
897 if ($to_col == $from_col && $to_row == $from_row - 1) {
898 return ($dest_piece eq '-');
900 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
901 return ($dest_piece ne '-');
912 print UCIWRITE "$msg\n";
913 print UCILOG localtime() . " => $msg\n";
917 my ($uciinfo, $pos, $mpv) = @_;
919 if (defined($uciinfo{'score_mate' . $mpv})) {
920 return sprintf "M%3d", $uciinfo{'score_mate' . $mpv};
922 if (exists($uciinfo{'score_cp' . $mpv})) {
923 my $score = $uciinfo{'score_cp' . $mpv} * 0.01;
924 if ($pos->{'toplay'} eq 'B') {
927 return sprintf "%+5.2f", $score;
935 my ($uciinfo, $pos, $mpv) = @_;
937 if (defined($uciinfo{'score_mate' . $mpv})) {
938 my $mate = $uciinfo{'score_mate' . $mpv};
939 if ($pos->{'toplay'} eq 'B') {
943 return sprintf "White mates in %u", $mate;
945 return sprintf "Black mates in %u", -$mate;
948 if (exists($uciinfo{'score_cp' . $mpv})) {
949 my $score = $uciinfo{'score_cp' . $mpv} * 0.01;
950 if ($pos->{'toplay'} eq 'B') {
953 return sprintf "Score: %+5.2f", $score;
962 my ($fen, $board, $toplay) = @_;
964 if (exists($book_cache{$fen})) {
965 return $book_cache{$fen};
968 my $ret = `./booklook $fen`;
969 return "[$fen]" if ($ret =~ /Not found/ || $ret eq '');
973 for my $m (split /\n/, $ret) {
974 my ($move, $annotation, $win, $draw, $lose, $rating, $rating_div) = split /,/, $m;
978 $pmove = '(current)';
980 ($pmove) = prettyprint_pv($board, $move);
981 $pmove .= $annotation;
985 if ($toplay eq 'W') {
986 $score = 1.0 * $win + 0.5 * $draw + 0.0 * $lose;
988 $score = 0.0 * $win + 0.5 * $draw + 1.0 * $lose;
990 my $n = $win + $draw + $lose;
996 $percent = sprintf "%4u%%", int(100.0 * $score / $n + 0.5);
999 push @moves, [ $pmove, $n, $percent, $rating ];
1002 @moves[1..$#moves] = sort { $b->[2] cmp $a->[2] } @moves[1..$#moves];
1004 my $text = "Book moves:\n\n Perf. N Rating\n\n";
1005 for my $m (@moves) {
1006 $text .= sprintf " %-10s %s %6u %4s\n", $m->[0], $m->[2], $m->[1], $m->[3]