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 = "'./Deep Rybka 4 SSE42 x64'";
23 my $telltarget = undef; # undef to be silent
24 my @tell_intervals = (5, 20, 60, 120, 240, 480, 960); # after each move
25 my $uci_assume_full_compliance = 0; # 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 NalimovPath value /srv/tablebase");
71 uciprint("setoption name NalimovUsage value Rarely");
72 uciprint("setoption name Hash value 1024");
73 # uciprint("setoption name MultiPV value 2");
74 uciprint("ucinewgame");
76 print "Chess engine ready.\n";
79 my $t = Net::Telnet->new(Timeout => 10, Prompt => '/fics% /');
80 $t->input_log(\*FICSLOG);
82 $t->print("SesseBOT");
83 $t->waitfor('/Press return to enter the server/');
87 $t->cmd("set shout 0");
88 $t->cmd("set seek 0");
89 $t->cmd("set style 12");
90 $t->cmd("observe $target");
93 print "FICS ready.\n";
97 vec($rin, fileno(UCIREAD), 1) = 1;
98 vec($rin, fileno($t), 1) = 1;
100 my ($nfound, $timeleft) = select($rout=$rin, undef, undef, 5.0);
104 my $line = $t->getline(Timeout => 0, errmode => 'return');
105 last if (!defined($line));
109 if ($line =~ /^<12> /) {
110 my $pos = style12_to_fen($line);
112 # if this is already in the queue, ignore it
113 next if (defined($pos_waiting) && $pos->{'fen'} eq $pos_waiting->{'fen'});
115 # if we're already chewing on this and there's nothing else in the queue,
117 next if (!defined($pos_waiting) && defined($pos_calculating) &&
118 $pos->{'fen'} eq $pos_calculating->{'fen'});
120 # if we're already thinking on something, stop and wait for the engine
122 if (defined($pos_calculating)) {
123 if (!defined($pos_waiting)) {
126 if ($uci_assume_full_compliance) {
129 uciprint("position fen " . $pos->{'fen'});
130 uciprint("go infinite");
131 $pos_calculating = $pos;
134 # it's wrong just to give the FEN (the move history is useful,
135 # and per the UCI spec, we should really have sent "ucinewgame"),
137 uciprint("position fen " . $pos->{'fen'});
138 uciprint("go infinite");
139 $pos_calculating = $pos;
146 # Output a command every move to note that we're
147 # still paying attention -- this is a good tradeoff,
148 # since if no move has happened in the last half
149 # hour, the analysis/relay has most likely stopped
150 # and we should stop hogging server resources.
154 if ($line =~ /^([A-Za-z]+)(?:\([A-Z]+\))* tells you: (.*)$/) {
155 my ($who, $msg) = ($1, $2);
157 next if (grep { $_ eq $who } (@masters) == 0);
159 if ($msg =~ /^fics (.*?)$/) {
160 $t->cmd("tell $who Executing '$1' on FICS.");
162 } elsif ($msg =~ /^uci (.*?)$/) {
163 $t->cmd("tell $who Sending '$1' to the engine.");
164 print UCIWRITE "$1\n";
166 $t->cmd("tell $who Couldn't understand '$msg', sorry.");
169 #print "FICS: [$line]\n";
173 # any fun on the UCI channel?
174 if ($nfound > 0 && vec($rout, fileno(UCIREAD), 1) == 1) {
176 # Read until we've got a full line -- if the engine sends part of
177 # a line and then stops we're pretty much hosed, but that should
181 while ($line !~ /\n/) {
183 my $ret = sysread UCIREAD, $tmp, 1;
185 if (!defined($ret)) {
187 die "error in reading from the UCI engine: $!";
188 } elsif ($ret == 0) {
189 die "EOF from UCI engine";
199 # don't update too often
200 Time::HiRes::alarm(0.2);
211 $line =~ s/ / /g; # Sometimes needed for Zappa Mexico
212 print UCILOG localtime() . " <= $line\n";
213 if ($line =~ /^info/) {
214 my (@infos) = split / /, $line;
219 if ($line =~ /^id/) {
220 my (@ids) = split / /, $line;
225 if ($line =~ /^bestmove/ && $uci_assume_full_compliance) {
226 if (defined($pos_waiting)) {
227 uciprint("position fen " . $pos_waiting->{'fen'});
228 uciprint("go infinite");
230 $pos_calculating = $pos_waiting;
231 $pos_waiting = undef;
240 while (scalar @x > 0) {
241 if ($x[0] =~ 'multipv') {
246 if ($x[0] =~ /^(currmove|currmovenumber|cpuload)$/) {
248 my $value = shift @x;
249 $uciinfo{$key} = $value;
252 if ($x[0] =~ /^(depth|seldepth|hashfull|time|nodes|nps|tbhits)$/) {
254 my $value = shift @x;
255 $uciinfo{$key . $mpv} = $value;
258 if ($x[0] eq 'score') {
261 delete $uciinfo{'score_cp' . $mpv};
262 delete $uciinfo{'score_mate' . $mpv};
264 while ($x[0] =~ /^(cp|mate|lowerbound|upperbound)$/) {
267 $uciinfo{'score_cp' . $mpv} = shift @x;
268 } elsif ($x[0] eq 'mate') {
270 $uciinfo{'score_mate' . $mpv} = shift @x;
278 $uciinfo{'pv' . $mpv} = [ @x[1..$#x] ];
281 if ($x[0] eq 'string' || $x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting' || $x[0] eq 'contempt') {
285 #print "unknown info '$x[0]', trying to recover...\n";
287 die "Unknown info '" . join(',', @x) . "'";
295 while (scalar @x > 0) {
296 if ($x[0] =~ /^(name|author)$/) {
298 my $value = join(' ', @x);
299 $uciid{$key} = $value;
311 my (@x) = split / /, $str;
313 $pos{'board'} = [ @x[1..8] ];
314 $pos{'toplay'} = $x[9];
317 my (@board) = @x[1..8];
318 for my $rank (0..7) {
319 $board[$rank] =~ s/(-+)/length($1)/ge;
321 my $fen = join('/', @board);
323 # white/black to move
329 $castling .= "K" if ($x[11] == 1);
330 $castling .= "Q" if ($x[12] == 1);
331 $castling .= "k" if ($x[13] == 1);
332 $castling .= "q" if ($x[14] == 1);
333 $castling = "-" if ($castling eq "");
334 # $castling = "-"; # chess960
342 my $nep = (qw(a b c d e f g h))[$col];
351 # Showing the en passant square when actually no capture can be made
352 # seems to confuse at least Rybka. Thus, check if there's actually
353 # a pawn of the opposite side that can do the en passant move, and if
354 # not, just lie -- it doesn't matter anyway. I'm unsure what's the
355 # "right" thing as per the standard, though.
358 $ep = $nep if ($col > 0 && substr($pos{'board'}[4], $col-1, 1) eq 'p');
359 $ep = $nep if ($col < 7 && substr($pos{'board'}[4], $col+1, 1) eq 'p');
361 $ep = $nep if ($col > 0 && substr($pos{'board'}[3], $col-1, 1) eq 'P');
362 $ep = $nep if ($col < 7 && substr($pos{'board'}[3], $col+1, 1) eq 'P');
377 $pos{'move_num'} = $x[26];
378 $pos{'last_move'} = $x[29];
384 my ($board, @pvs) = @_;
386 if (scalar @pvs == 0 || !defined($pvs[0])) {
393 my $from_col = ord(substr($pv, 0, 1)) - ord('a');
394 my $from_row = 7 - (ord(substr($pv, 1, 1)) - ord('1'));
395 my $to_col = ord(substr($pv, 2, 1)) - ord('a');
396 my $to_row = 7 - (ord(substr($pv, 3, 1)) - ord('1'));
399 my $piece = substr($board->[$from_row], $from_col, 1);
402 die "Invalid move $pv";
405 # white short castling
406 if ($pv eq 'e1g1' && $piece eq 'K') {
408 substr($nb[7], 4, 1, '-');
409 substr($nb[7], 6, 1, $piece);
412 substr($nb[7], 7, 1, '-');
413 substr($nb[7], 5, 1, 'R');
415 return ('0-0', prettyprint_pv(\@nb, @pvs));
418 # white long castling
419 if ($pv eq 'e1c1' && $piece eq 'K') {
421 substr($nb[7], 4, 1, '-');
422 substr($nb[7], 2, 1, $piece);
425 substr($nb[7], 0, 1, '-');
426 substr($nb[7], 3, 1, 'R');
428 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
431 # black short castling
432 if ($pv eq 'e8g8' && $piece eq 'k') {
434 substr($nb[0], 4, 1, '-');
435 substr($nb[0], 6, 1, $piece);
438 substr($nb[0], 7, 1, '-');
439 substr($nb[0], 5, 1, 'r');
441 return ('0-0', prettyprint_pv(\@nb, @pvs));
444 # black long castling
445 if ($pv eq 'e8c8' && $piece eq 'k') {
447 substr($nb[0], 4, 1, '-');
448 substr($nb[0], 2, 1, $piece);
451 substr($nb[0], 0, 1, '-');
452 substr($nb[0], 3, 1, 'r');
454 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
457 # check if the from-piece is a pawn
458 if (lc($piece) eq 'p') {
460 if ($from_col != $to_col) {
461 $pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
464 if (substr($board->[$to_row], $to_col, 1) eq '-') {
466 substr($nb[$to_row + 1], $to_col, 1, '-');
468 substr($nb[$to_row - 1], $to_col, 1, '-');
472 $pretty = substr($pv, 2, 2);
474 if (length($pv) == 5) {
477 $pretty .= uc(substr($pv, 4, 1));
480 $piece = substr($pv, 4, 1);
482 $piece = uc(substr($pv, 4, 1));
487 $pretty = uc($piece);
489 # see how many of these pieces could go here, in all
493 next unless (substr($board->[$row], $col, 1) eq $piece);
494 ++$num_total if (can_reach($board, $piece, $row, $col, $to_row, $to_col));
498 # see how many of these pieces from the given row could go here
501 next unless (substr($board->[$from_row], $col, 1) eq $piece);
502 ++$num_row if (can_reach($board, $piece, $from_row, $col, $to_row, $to_col));
505 # and same for columns
508 next unless (substr($board->[$row], $from_col, 1) eq $piece);
509 ++$num_col if (can_reach($board, $piece, $row, $from_col, $to_row, $to_col));
512 # see if we need to disambiguate
513 if ($num_total > 1) {
515 $pretty .= substr($pv, 0, 1);
516 } elsif ($num_row == 1) {
517 $pretty .= substr($pv, 1, 1);
519 $pretty .= substr($pv, 0, 2);
524 if (substr($board->[$to_row], $to_col, 1) ne '-') {
528 $pretty .= substr($pv, 2, 2);
532 substr($nb[$from_row], $from_col, 1, '-');
533 substr($nb[$to_row], $to_col, 1, $piece);
537 } elsif (in_check(\@nb) ne 'none') {
541 return ($pretty, prettyprint_pv(\@nb, @pvs));
547 return if (!defined($pos_calculating));
550 # Check the PVs first. if they're invalid, just wait, as our data
551 # is most likely out of sync. This isn't a very good solution, as
552 # it can frequently miss stuff, but it's good enough for most users.
556 if (exists($uciinfo{'pv'})) {
557 $dummy = prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv'}});
561 while (exists($uciinfo{'pv' . $mpv})) {
562 $dummy = prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv' . $mpv}});
571 my $text = 'Analysis';
572 if ($pos_calculating->{'last_move'} ne 'none') {
573 if ($pos_calculating->{'toplay'} eq 'W') {
574 $text .= sprintf ' after %u. ... %s', ($pos_calculating->{'move_num'}-1), $pos_calculating->{'last_move'};
576 $text .= sprintf ' after %u. %s', $pos_calculating->{'move_num'}, $pos_calculating->{'last_move'};
578 if (exists($uciid{'name'})) {
583 if (exists($uciid{'name'})) {
584 $text .= " by $uciid{'name'}:\n\n";
589 return unless (exists($pos_calculating->{'board'}));
592 # Some programs _always_ report MultiPV, even with only one PV.
593 # In this case, we simply use that data as if MultiPV was never
596 if (exists($uciinfo{'pv1'}) && !exists($uciinfo{'pv2'})) {
597 for my $key qw(pv score_cp score_mate nodes nps depth seldepth tbhits) {
598 if (exists($uciinfo{$key . '1'}) && !exists($uciinfo{$key})) {
599 $uciinfo{$key} = $uciinfo{$key . '1'};
604 if (exists($uciinfo{'pv1'}) && exists($uciinfo{'pv2'})) {
607 while (exists($uciinfo{'pv' . $mpv})) {
608 $text .= sprintf " PV%2u", $mpv;
609 my $score = short_score(\%uciinfo, $pos_calculating, $mpv);
610 $text .= " ($score)" if (defined($score));
613 if (exists($uciinfo{'tbhits' . $mpv}) && $uciinfo{'tbhits' . $mpv} > 0) {
614 if ($uciinfo{'tbhits' . $mpv} == 1) {
615 $tbhits = ", 1 tbhit";
617 $tbhits = sprintf ", %u tbhits", $uciinfo{'tbhits' . $mpv};
621 if (exists($uciinfo{'nodes' . $mpv}) && exists($uciinfo{'nps' . $mpv}) && exists($uciinfo{'depth' . $mpv})) {
622 $text .= sprintf " (%5u kn, %3u kn/s, %2u ply$tbhits)",
623 $uciinfo{'nodes' . $mpv} / 1000, $uciinfo{'nps' . $mpv} / 1000, $uciinfo{'depth' . $mpv};
627 $text .= " " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv' . $mpv}})) . "\n";
633 my $score = long_score(\%uciinfo, $pos_calculating, '');
634 $text .= " $score\n" if defined($score);
635 $text .= " PV: " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv'}}));
638 if (exists($uciinfo{'nodes'}) && exists($uciinfo{'nps'}) && exists($uciinfo{'depth'})) {
639 $text .= sprintf " %u nodes, %7u nodes/sec, depth %u ply",
640 $uciinfo{'nodes'}, $uciinfo{'nps'}, $uciinfo{'depth'};
642 if (exists($uciinfo{'tbhits'}) && $uciinfo{'tbhits'} > 0) {
643 if ($uciinfo{'tbhits'} == 1) {
644 $text .= ", one Nalimov hit";
646 $text .= sprintf ", %u Nalimov hits", $uciinfo{'tbhits'};
649 if (exists($uciinfo{'seldepth'})) {
650 $text .= sprintf " (%u selective)", $uciinfo{'seldepth'};
655 #$text .= book_info($pos_calculating->{'fen'}, $pos_calculating->{'board'}, $pos_calculating->{'toplay'});
657 if ($last_text ne $text) {
658 print "
\e[H
\e[2J"; # clear the screen
663 # Now construct the tell text, if any
664 return if (!defined($telltarget));
668 if (exists($uciid{'name'})) {
669 $tell_text .= "Analysis by $uciid{'name'} -- see http://analysis.sesse.net/ for more information\n";
671 $tell_text .= "Computer analysis -- http://analysis.sesse.net/ for more information\n";
674 if (exists($uciinfo{'pv1'}) && exists($uciinfo{'pv2'})) {
677 while (exists($uciinfo{'pv' . $mpv})) {
678 $tell_text .= sprintf " PV%2u", $mpv;
679 my $score = short_score(\%uciinfo, $pos_calculating, $mpv);
680 $tell_text .= " ($score)" if (defined($score));
682 if (exists($uciinfo{'depth' . $mpv})) {
683 $tell_text .= sprintf " (%2u ply)", $uciinfo{'depth' . $mpv};
687 $tell_text .= join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv' . $mpv}}));
693 my $score = long_score(\%uciinfo, $pos_calculating, '');
694 $tell_text .= " $score\n" if defined($score);
695 $tell_text .= " PV: " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv'}}));
696 if (exists($uciinfo{'depth'})) {
697 $tell_text .= sprintf " (depth %u ply)", $uciinfo{'depth'};
702 # see if a new tell is called for -- it is if the delay has expired _and_
703 # this is not simply a repetition of the last one
704 if ($last_told_text ne $tell_text) {
706 for my $iv (@tell_intervals) {
707 last if ($now - $last_move < $iv);
708 next if ($last_tell - $last_move >= $iv);
710 for my $line (split /\n/, $tell_text) {
711 $t->print("tell $telltarget [$target] $line");
714 $last_told_text = $text;
724 my ($wkr, $wkc, $bkr, $bkc);
728 my $piece = substr($board->[$row], $col, 1);
730 ($wkr, $wkc) = ($row, $col);
731 } elsif ($piece eq 'k') {
732 ($bkr, $bkc) = ($row, $col);
737 return ($wkr, $wkc, $bkr, $bkc);
742 my $check = in_check($board);
743 return 0 if ($check eq 'none');
745 # try all possible moves for the side in check
748 my $piece = substr($board->[$row], $col, 1);
749 next if ($piece eq '-');
751 if ($check eq 'white') {
752 next if ($piece eq lc($piece));
754 next if ($piece eq uc($piece));
757 for my $dest_row (0..7) {
758 for my $dest_col (0..7) {
759 next if ($row == $dest_row && $col == $dest_col);
760 next unless (can_reach($board, $piece, $row, $col, $dest_row, $dest_col));
763 substr($nb[$row], $col, 1, '-');
764 substr($nb[$dest_row], $dest_col, 1, $piece);
766 my $new_check = in_check(\@nb);
767 return 0 if ($new_check ne $check && $new_check ne 'both');
773 # nothing to do; mate
779 my ($black_check, $white_check) = (0, 0);
781 my ($wkr, $wkc, $bkr, $bkc) = find_kings($board);
783 # check all pieces for the possibility of threatening the two kings
786 my $piece = substr($board->[$row], $col, 1);
787 next if ($piece eq '-');
789 if (uc($piece) eq $piece) {
791 $black_check = 1 if (can_reach($board, $piece, $row, $col, $bkr, $bkc));
794 $white_check = 1 if (can_reach($board, $piece, $row, $col, $wkr, $wkc));
799 if ($black_check && $white_check) {
801 } elsif ($black_check) {
803 } elsif ($white_check) {
811 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
813 # can't eat your own piece
814 my $dest_piece = substr($board->[$to_row], $to_col, 1);
815 if ($dest_piece ne '-') {
816 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
819 if (lc($piece) eq 'k') {
820 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
822 if (lc($piece) eq 'r') {
823 return 0 unless ($from_row == $to_row || $from_col == $to_col);
825 # check that there's a clear passage
826 if ($from_row == $to_row) {
827 if ($from_col > $to_col) {
828 ($to_col, $from_col) = ($from_col, $to_col);
831 for my $c (($from_col+1)..($to_col-1)) {
832 my $middle_piece = substr($board->[$to_row], $c, 1);
833 return 0 if ($middle_piece ne '-');
838 if ($from_row > $to_row) {
839 ($to_row, $from_row) = ($from_row, $to_row);
842 for my $r (($from_row+1)..($to_row-1)) {
843 my $middle_piece = substr($board->[$r], $to_col, 1);
844 return 0 if ($middle_piece ne '-');
850 if (lc($piece) eq 'b') {
851 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
853 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
854 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
856 my $r = $from_row + $dr;
857 my $c = $from_col + $dc;
859 while ($r != $to_row) {
860 my $middle_piece = substr($board->[$r], $c, 1);
861 return 0 if ($middle_piece ne '-');
869 if (lc($piece) eq 'n') {
870 my $diff_r = abs($from_row - $to_row);
871 my $diff_c = abs($from_col - $to_col);
872 return 1 if ($diff_r == 2 && $diff_c == 1);
873 return 1 if ($diff_r == 1 && $diff_c == 2);
877 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
878 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
881 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
882 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
886 if ($to_col == $from_col && $to_row == $from_row + 1) {
887 return ($dest_piece eq '-');
889 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
890 return ($dest_piece ne '-');
896 if ($to_col == $from_col && $to_row == $from_row - 1) {
897 return ($dest_piece eq '-');
899 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
900 return ($dest_piece ne '-');
911 print UCIWRITE "$msg\n";
912 print UCILOG localtime() . " => $msg\n";
916 my ($uciinfo, $pos, $mpv) = @_;
918 if (defined($uciinfo{'score_mate' . $mpv})) {
919 return sprintf "M%3d", $uciinfo{'score_mate' . $mpv};
921 if (exists($uciinfo{'score_cp' . $mpv})) {
922 my $score = $uciinfo{'score_cp' . $mpv} * 0.01;
923 if ($pos->{'toplay'} eq 'B') {
926 return sprintf "%+5.2f", $score;
934 my ($uciinfo, $pos, $mpv) = @_;
936 if (defined($uciinfo{'score_mate' . $mpv})) {
937 my $mate = $uciinfo{'score_mate' . $mpv};
938 if ($pos->{'toplay'} eq 'B') {
942 return sprintf "White mates in %u", $mate;
944 return sprintf "Black mates in %u", -$mate;
947 if (exists($uciinfo{'score_cp' . $mpv})) {
948 my $score = $uciinfo{'score_cp' . $mpv} * 0.01;
949 if ($pos->{'toplay'} eq 'B') {
952 return sprintf "Score: %+5.2f", $score;
961 my ($fen, $board, $toplay) = @_;
963 if (exists($book_cache{$fen})) {
964 return $book_cache{$fen};
967 my $ret = `./booklook $fen`;
968 return "" if ($ret =~ /Not found/ || $ret eq '');
972 for my $m (split /\n/, $ret) {
973 my ($move, $annotation, $win, $draw, $lose, $rating, $rating_div) = split /,/, $m;
977 $pmove = '(current)';
979 ($pmove) = prettyprint_pv($board, $move);
980 $pmove .= $annotation;
984 if ($toplay eq 'W') {
985 $score = 1.0 * $win + 0.5 * $draw + 0.0 * $lose;
987 $score = 0.0 * $win + 0.5 * $draw + 1.0 * $lose;
989 my $n = $win + $draw + $lose;
995 $percent = sprintf "%4u%%", int(100.0 * $score / $n + 0.5);
998 push @moves, [ $pmove, $n, $percent, $rating ];
1001 @moves[1..$#moves] = sort { $b->[2] cmp $a->[2] } @moves[1..$#moves];
1003 my $text = "Book moves:\n\n Perf. N Rating\n\n";
1004 for my $m (@moves) {
1005 $text .= sprintf " %-10s %s %6u %4s\n", $m->[0], $m->[2], $m->[1], $m->[3]