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_cmdline = "'./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 $engine = open_engine($engine_cmdline);
56 my ($last_move, $last_tell);
58 my $last_told_text = '';
59 my ($pos_waiting, $pos_calculating);
61 uciprint($engine, "setoption name UCI_AnalyseMode value true");
62 # uciprint($engine, "setoption name NalimovPath value /srv/tablebase");
63 uciprint($engine, "setoption name NalimovUsage value Rarely");
64 uciprint($engine, "setoption name Hash value 1024");
65 # uciprint($engine, "setoption name MultiPV value 2");
66 uciprint($engine, "ucinewgame");
68 print "Chess engine ready.\n";
71 my $t = Net::Telnet->new(Timeout => 10, Prompt => '/fics% /');
72 $t->input_log(\*FICSLOG);
74 $t->print("SesseBOT");
75 $t->waitfor('/Press return to enter the server/');
79 $t->cmd("set shout 0");
80 $t->cmd("set seek 0");
81 $t->cmd("set style 12");
82 $t->cmd("observe $target");
85 print "FICS ready.\n";
89 vec($rin, fileno($engine->{'read'}), 1) = 1;
90 vec($rin, fileno($t), 1) = 1;
92 my ($nfound, $timeleft) = select($rout=$rin, undef, undef, 5.0);
96 my $line = $t->getline(Timeout => 0, errmode => 'return');
97 last if (!defined($line));
101 if ($line =~ /^<12> /) {
102 my $pos = style12_to_fen($line);
104 # if this is already in the queue, ignore it
105 next if (defined($pos_waiting) && $pos->{'fen'} eq $pos_waiting->{'fen'});
107 # if we're already chewing on this and there's nothing else in the queue,
109 next if (!defined($pos_waiting) && defined($pos_calculating) &&
110 $pos->{'fen'} eq $pos_calculating->{'fen'});
112 # if we're already thinking on something, stop and wait for the engine
114 if (defined($pos_calculating)) {
115 if (!defined($pos_waiting)) {
116 uciprint($engine, "stop");
118 if ($uci_assume_full_compliance) {
121 uciprint($engine, "position fen " . $pos->{'fen'});
122 uciprint($engine, "go infinite");
123 $pos_calculating = $pos;
126 # it's wrong just to give the FEN (the move history is useful,
127 # and per the UCI spec, we should really have sent "ucinewgame"),
129 uciprint($engine, "position fen " . $pos->{'fen'});
130 uciprint($engine, "go infinite");
131 $pos_calculating = $pos;
138 # Output a command every move to note that we're
139 # still paying attention -- this is a good tradeoff,
140 # since if no move has happened in the last half
141 # hour, the analysis/relay has most likely stopped
142 # and we should stop hogging server resources.
146 if ($line =~ /^([A-Za-z]+)(?:\([A-Z]+\))* tells you: (.*)$/) {
147 my ($who, $msg) = ($1, $2);
149 next if (grep { $_ eq $who } (@masters) == 0);
151 if ($msg =~ /^fics (.*?)$/) {
152 $t->cmd("tell $who Executing '$1' on FICS.");
154 } elsif ($msg =~ /^uci (.*?)$/) {
155 $t->cmd("tell $who Sending '$1' to the engine.");
156 print { $engine->{'write'} } "$1\n";
158 $t->cmd("tell $who Couldn't understand '$msg', sorry.");
161 #print "FICS: [$line]\n";
165 # any fun on the UCI channel?
166 if ($nfound > 0 && vec($rout, fileno($engine->{'read'}), 1) == 1) {
168 # Read until we've got a full line -- if the engine sends part of
169 # a line and then stops we're pretty much hosed, but that should
173 while ($line !~ /\n/) {
175 my $ret = sysread $engine->{'read'}, $tmp, 1;
177 if (!defined($ret)) {
179 die "error in reading from the UCI engine: $!";
180 } elsif ($ret == 0) {
181 die "EOF from UCI engine";
191 # don't update too often
192 Time::HiRes::alarm(0.2);
203 $line =~ s/ / /g; # Sometimes needed for Zappa Mexico
204 print UCILOG localtime() . " <= $line\n";
205 if ($line =~ /^info/) {
206 my (@infos) = split / /, $line;
211 if ($line =~ /^id/) {
212 my (@ids) = split / /, $line;
217 if ($line =~ /^bestmove/ && $uci_assume_full_compliance) {
218 if (defined($pos_waiting)) {
219 uciprint($engine, "position fen " . $pos_waiting->{'fen'});
220 uciprint($engine, "go infinite");
222 $pos_calculating = $pos_waiting;
223 $pos_waiting = undef;
232 while (scalar @x > 0) {
233 if ($x[0] =~ 'multipv') {
238 if ($x[0] =~ /^(currmove|currmovenumber|cpuload)$/) {
240 my $value = shift @x;
241 $uciinfo{$key} = $value;
244 if ($x[0] =~ /^(depth|seldepth|hashfull|time|nodes|nps|tbhits)$/) {
246 my $value = shift @x;
247 $uciinfo{$key . $mpv} = $value;
250 if ($x[0] eq 'score') {
253 delete $uciinfo{'score_cp' . $mpv};
254 delete $uciinfo{'score_mate' . $mpv};
256 while ($x[0] =~ /^(cp|mate|lowerbound|upperbound)$/) {
259 $uciinfo{'score_cp' . $mpv} = shift @x;
260 } elsif ($x[0] eq 'mate') {
262 $uciinfo{'score_mate' . $mpv} = shift @x;
270 $uciinfo{'pv' . $mpv} = [ @x[1..$#x] ];
273 if ($x[0] eq 'string' || $x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting' || $x[0] eq 'contempt') {
277 #print "unknown info '$x[0]', trying to recover...\n";
279 die "Unknown info '" . join(',', @x) . "'";
287 while (scalar @x > 0) {
288 if ($x[0] =~ /^(name|author)$/) {
290 my $value = join(' ', @x);
291 $uciid{$key} = $value;
303 my (@x) = split / /, $str;
305 $pos{'board'} = [ @x[1..8] ];
306 $pos{'toplay'} = $x[9];
309 my (@board) = @x[1..8];
310 for my $rank (0..7) {
311 $board[$rank] =~ s/(-+)/length($1)/ge;
313 my $fen = join('/', @board);
315 # white/black to move
321 $castling .= "K" if ($x[11] == 1);
322 $castling .= "Q" if ($x[12] == 1);
323 $castling .= "k" if ($x[13] == 1);
324 $castling .= "q" if ($x[14] == 1);
325 $castling = "-" if ($castling eq "");
326 # $castling = "-"; # chess960
334 my $nep = (qw(a b c d e f g h))[$col];
343 # Showing the en passant square when actually no capture can be made
344 # seems to confuse at least Rybka. Thus, check if there's actually
345 # a pawn of the opposite side that can do the en passant move, and if
346 # not, just lie -- it doesn't matter anyway. I'm unsure what's the
347 # "right" thing as per the standard, though.
350 $ep = $nep if ($col > 0 && substr($pos{'board'}[4], $col-1, 1) eq 'p');
351 $ep = $nep if ($col < 7 && substr($pos{'board'}[4], $col+1, 1) eq 'p');
353 $ep = $nep if ($col > 0 && substr($pos{'board'}[3], $col-1, 1) eq 'P');
354 $ep = $nep if ($col < 7 && substr($pos{'board'}[3], $col+1, 1) eq 'P');
369 $pos{'move_num'} = $x[26];
370 $pos{'last_move'} = $x[29];
376 my ($board, @pvs) = @_;
378 if (scalar @pvs == 0 || !defined($pvs[0])) {
385 my $from_col = ord(substr($pv, 0, 1)) - ord('a');
386 my $from_row = 7 - (ord(substr($pv, 1, 1)) - ord('1'));
387 my $to_col = ord(substr($pv, 2, 1)) - ord('a');
388 my $to_row = 7 - (ord(substr($pv, 3, 1)) - ord('1'));
391 my $piece = substr($board->[$from_row], $from_col, 1);
394 die "Invalid move $pv";
397 # white short castling
398 if ($pv eq 'e1g1' && $piece eq 'K') {
400 substr($nb[7], 4, 1, '-');
401 substr($nb[7], 6, 1, $piece);
404 substr($nb[7], 7, 1, '-');
405 substr($nb[7], 5, 1, 'R');
407 return ('0-0', prettyprint_pv(\@nb, @pvs));
410 # white long castling
411 if ($pv eq 'e1c1' && $piece eq 'K') {
413 substr($nb[7], 4, 1, '-');
414 substr($nb[7], 2, 1, $piece);
417 substr($nb[7], 0, 1, '-');
418 substr($nb[7], 3, 1, 'R');
420 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
423 # black short castling
424 if ($pv eq 'e8g8' && $piece eq 'k') {
426 substr($nb[0], 4, 1, '-');
427 substr($nb[0], 6, 1, $piece);
430 substr($nb[0], 7, 1, '-');
431 substr($nb[0], 5, 1, 'r');
433 return ('0-0', prettyprint_pv(\@nb, @pvs));
436 # black long castling
437 if ($pv eq 'e8c8' && $piece eq 'k') {
439 substr($nb[0], 4, 1, '-');
440 substr($nb[0], 2, 1, $piece);
443 substr($nb[0], 0, 1, '-');
444 substr($nb[0], 3, 1, 'r');
446 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
449 # check if the from-piece is a pawn
450 if (lc($piece) eq 'p') {
452 if ($from_col != $to_col) {
453 $pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
456 if (substr($board->[$to_row], $to_col, 1) eq '-') {
458 substr($nb[$to_row + 1], $to_col, 1, '-');
460 substr($nb[$to_row - 1], $to_col, 1, '-');
464 $pretty = substr($pv, 2, 2);
466 if (length($pv) == 5) {
469 $pretty .= uc(substr($pv, 4, 1));
472 $piece = substr($pv, 4, 1);
474 $piece = uc(substr($pv, 4, 1));
479 $pretty = uc($piece);
481 # see how many of these pieces could go here, in all
485 next unless (substr($board->[$row], $col, 1) eq $piece);
486 ++$num_total if (can_reach($board, $piece, $row, $col, $to_row, $to_col));
490 # see how many of these pieces from the given row could go here
493 next unless (substr($board->[$from_row], $col, 1) eq $piece);
494 ++$num_row if (can_reach($board, $piece, $from_row, $col, $to_row, $to_col));
497 # and same for columns
500 next unless (substr($board->[$row], $from_col, 1) eq $piece);
501 ++$num_col if (can_reach($board, $piece, $row, $from_col, $to_row, $to_col));
504 # see if we need to disambiguate
505 if ($num_total > 1) {
507 $pretty .= substr($pv, 0, 1);
508 } elsif ($num_row == 1) {
509 $pretty .= substr($pv, 1, 1);
511 $pretty .= substr($pv, 0, 2);
516 if (substr($board->[$to_row], $to_col, 1) ne '-') {
520 $pretty .= substr($pv, 2, 2);
524 substr($nb[$from_row], $from_col, 1, '-');
525 substr($nb[$to_row], $to_col, 1, $piece);
529 } elsif (in_check(\@nb) ne 'none') {
533 return ($pretty, prettyprint_pv(\@nb, @pvs));
539 return if (!defined($pos_calculating));
542 # Check the PVs first. if they're invalid, just wait, as our data
543 # is most likely out of sync. This isn't a very good solution, as
544 # it can frequently miss stuff, but it's good enough for most users.
548 if (exists($uciinfo{'pv'})) {
549 $dummy = prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv'}});
553 while (exists($uciinfo{'pv' . $mpv})) {
554 $dummy = prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv' . $mpv}});
563 my $text = 'Analysis';
564 if ($pos_calculating->{'last_move'} ne 'none') {
565 if ($pos_calculating->{'toplay'} eq 'W') {
566 $text .= sprintf ' after %u. ... %s', ($pos_calculating->{'move_num'}-1), $pos_calculating->{'last_move'};
568 $text .= sprintf ' after %u. %s', $pos_calculating->{'move_num'}, $pos_calculating->{'last_move'};
570 if (exists($uciid{'name'})) {
575 if (exists($uciid{'name'})) {
576 $text .= " by $uciid{'name'}:\n\n";
581 return unless (exists($pos_calculating->{'board'}));
584 # Some programs _always_ report MultiPV, even with only one PV.
585 # In this case, we simply use that data as if MultiPV was never
588 if (exists($uciinfo{'pv1'}) && !exists($uciinfo{'pv2'})) {
589 for my $key (qw(pv score_cp score_mate nodes nps depth seldepth tbhits)) {
590 if (exists($uciinfo{$key . '1'}) && !exists($uciinfo{$key})) {
591 $uciinfo{$key} = $uciinfo{$key . '1'};
596 if (exists($uciinfo{'pv1'}) && exists($uciinfo{'pv2'})) {
599 while (exists($uciinfo{'pv' . $mpv})) {
600 $text .= sprintf " PV%2u", $mpv;
601 my $score = short_score(\%uciinfo, $pos_calculating, $mpv);
602 $text .= " ($score)" if (defined($score));
605 if (exists($uciinfo{'tbhits' . $mpv}) && $uciinfo{'tbhits' . $mpv} > 0) {
606 if ($uciinfo{'tbhits' . $mpv} == 1) {
607 $tbhits = ", 1 tbhit";
609 $tbhits = sprintf ", %u tbhits", $uciinfo{'tbhits' . $mpv};
613 if (exists($uciinfo{'nodes' . $mpv}) && exists($uciinfo{'nps' . $mpv}) && exists($uciinfo{'depth' . $mpv})) {
614 $text .= sprintf " (%5u kn, %3u kn/s, %2u ply$tbhits)",
615 $uciinfo{'nodes' . $mpv} / 1000, $uciinfo{'nps' . $mpv} / 1000, $uciinfo{'depth' . $mpv};
619 $text .= " " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv' . $mpv}})) . "\n";
625 my $score = long_score(\%uciinfo, $pos_calculating, '');
626 $text .= " $score\n" if defined($score);
627 $text .= " PV: " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv'}}));
630 if (exists($uciinfo{'nodes'}) && exists($uciinfo{'nps'}) && exists($uciinfo{'depth'})) {
631 $text .= sprintf " %u nodes, %7u nodes/sec, depth %u ply",
632 $uciinfo{'nodes'}, $uciinfo{'nps'}, $uciinfo{'depth'};
634 if (exists($uciinfo{'tbhits'}) && $uciinfo{'tbhits'} > 0) {
635 if ($uciinfo{'tbhits'} == 1) {
636 $text .= ", one Nalimov hit";
638 $text .= sprintf ", %u Nalimov hits", $uciinfo{'tbhits'};
641 if (exists($uciinfo{'seldepth'})) {
642 $text .= sprintf " (%u selective)", $uciinfo{'seldepth'};
647 #$text .= book_info($pos_calculating->{'fen'}, $pos_calculating->{'board'}, $pos_calculating->{'toplay'});
649 if ($last_text ne $text) {
650 print "
\e[H
\e[2J"; # clear the screen
655 # Now construct the tell text, if any
656 return if (!defined($telltarget));
660 if (exists($uciid{'name'})) {
661 $tell_text .= "Analysis by $uciid{'name'} -- see http://analysis.sesse.net/ for more information\n";
663 $tell_text .= "Computer analysis -- http://analysis.sesse.net/ for more information\n";
666 if (exists($uciinfo{'pv1'}) && exists($uciinfo{'pv2'})) {
669 while (exists($uciinfo{'pv' . $mpv})) {
670 $tell_text .= sprintf " PV%2u", $mpv;
671 my $score = short_score(\%uciinfo, $pos_calculating, $mpv);
672 $tell_text .= " ($score)" if (defined($score));
674 if (exists($uciinfo{'depth' . $mpv})) {
675 $tell_text .= sprintf " (%2u ply)", $uciinfo{'depth' . $mpv};
679 $tell_text .= join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv' . $mpv}}));
685 my $score = long_score(\%uciinfo, $pos_calculating, '');
686 $tell_text .= " $score\n" if defined($score);
687 $tell_text .= " PV: " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv'}}));
688 if (exists($uciinfo{'depth'})) {
689 $tell_text .= sprintf " (depth %u ply)", $uciinfo{'depth'};
694 # see if a new tell is called for -- it is if the delay has expired _and_
695 # this is not simply a repetition of the last one
696 if ($last_told_text ne $tell_text) {
698 for my $iv (@tell_intervals) {
699 last if ($now - $last_move < $iv);
700 next if ($last_tell - $last_move >= $iv);
702 for my $line (split /\n/, $tell_text) {
703 $t->print("tell $telltarget [$target] $line");
706 $last_told_text = $text;
716 my ($wkr, $wkc, $bkr, $bkc);
720 my $piece = substr($board->[$row], $col, 1);
722 ($wkr, $wkc) = ($row, $col);
723 } elsif ($piece eq 'k') {
724 ($bkr, $bkc) = ($row, $col);
729 return ($wkr, $wkc, $bkr, $bkc);
734 my $check = in_check($board);
735 return 0 if ($check eq 'none');
737 # try all possible moves for the side in check
740 my $piece = substr($board->[$row], $col, 1);
741 next if ($piece eq '-');
743 if ($check eq 'white') {
744 next if ($piece eq lc($piece));
746 next if ($piece eq uc($piece));
749 for my $dest_row (0..7) {
750 for my $dest_col (0..7) {
751 next if ($row == $dest_row && $col == $dest_col);
752 next unless (can_reach($board, $piece, $row, $col, $dest_row, $dest_col));
755 substr($nb[$row], $col, 1, '-');
756 substr($nb[$dest_row], $dest_col, 1, $piece);
758 my $new_check = in_check(\@nb);
759 return 0 if ($new_check ne $check && $new_check ne 'both');
765 # nothing to do; mate
771 my ($black_check, $white_check) = (0, 0);
773 my ($wkr, $wkc, $bkr, $bkc) = find_kings($board);
775 # check all pieces for the possibility of threatening the two kings
778 my $piece = substr($board->[$row], $col, 1);
779 next if ($piece eq '-');
781 if (uc($piece) eq $piece) {
783 $black_check = 1 if (can_reach($board, $piece, $row, $col, $bkr, $bkc));
786 $white_check = 1 if (can_reach($board, $piece, $row, $col, $wkr, $wkc));
791 if ($black_check && $white_check) {
793 } elsif ($black_check) {
795 } elsif ($white_check) {
803 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
805 # can't eat your own piece
806 my $dest_piece = substr($board->[$to_row], $to_col, 1);
807 if ($dest_piece ne '-') {
808 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
811 if (lc($piece) eq 'k') {
812 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
814 if (lc($piece) eq 'r') {
815 return 0 unless ($from_row == $to_row || $from_col == $to_col);
817 # check that there's a clear passage
818 if ($from_row == $to_row) {
819 if ($from_col > $to_col) {
820 ($to_col, $from_col) = ($from_col, $to_col);
823 for my $c (($from_col+1)..($to_col-1)) {
824 my $middle_piece = substr($board->[$to_row], $c, 1);
825 return 0 if ($middle_piece ne '-');
830 if ($from_row > $to_row) {
831 ($to_row, $from_row) = ($from_row, $to_row);
834 for my $r (($from_row+1)..($to_row-1)) {
835 my $middle_piece = substr($board->[$r], $to_col, 1);
836 return 0 if ($middle_piece ne '-');
842 if (lc($piece) eq 'b') {
843 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
845 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
846 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
848 my $r = $from_row + $dr;
849 my $c = $from_col + $dc;
851 while ($r != $to_row) {
852 my $middle_piece = substr($board->[$r], $c, 1);
853 return 0 if ($middle_piece ne '-');
861 if (lc($piece) eq 'n') {
862 my $diff_r = abs($from_row - $to_row);
863 my $diff_c = abs($from_col - $to_col);
864 return 1 if ($diff_r == 2 && $diff_c == 1);
865 return 1 if ($diff_r == 1 && $diff_c == 2);
869 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
870 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
873 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
874 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
878 if ($to_col == $from_col && $to_row == $from_row + 1) {
879 return ($dest_piece eq '-');
881 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
882 return ($dest_piece ne '-');
888 if ($to_col == $from_col && $to_row == $from_row - 1) {
889 return ($dest_piece eq '-');
891 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
892 return ($dest_piece ne '-');
902 my ($engine, $msg) = @_;
903 print { $engine->{'write'} } "$msg\n";
904 print UCILOG localtime() . " => $msg\n";
908 my ($uciinfo, $pos, $mpv) = @_;
910 if (defined($uciinfo{'score_mate' . $mpv})) {
911 return sprintf "M%3d", $uciinfo{'score_mate' . $mpv};
913 if (exists($uciinfo{'score_cp' . $mpv})) {
914 my $score = $uciinfo{'score_cp' . $mpv} * 0.01;
915 if ($pos->{'toplay'} eq 'B') {
918 return sprintf "%+5.2f", $score;
926 my ($uciinfo, $pos, $mpv) = @_;
928 if (defined($uciinfo{'score_mate' . $mpv})) {
929 my $mate = $uciinfo{'score_mate' . $mpv};
930 if ($pos->{'toplay'} eq 'B') {
934 return sprintf "White mates in %u", $mate;
936 return sprintf "Black mates in %u", -$mate;
939 if (exists($uciinfo{'score_cp' . $mpv})) {
940 my $score = $uciinfo{'score_cp' . $mpv} * 0.01;
941 if ($pos->{'toplay'} eq 'B') {
944 return sprintf "Score: %+5.2f", $score;
953 my ($fen, $board, $toplay) = @_;
955 if (exists($book_cache{$fen})) {
956 return $book_cache{$fen};
959 my $ret = `./booklook $fen`;
960 return "" if ($ret =~ /Not found/ || $ret eq '');
964 for my $m (split /\n/, $ret) {
965 my ($move, $annotation, $win, $draw, $lose, $rating, $rating_div) = split /,/, $m;
969 $pmove = '(current)';
971 ($pmove) = prettyprint_pv($board, $move);
972 $pmove .= $annotation;
976 if ($toplay eq 'W') {
977 $score = 1.0 * $win + 0.5 * $draw + 0.0 * $lose;
979 $score = 0.0 * $win + 0.5 * $draw + 1.0 * $lose;
981 my $n = $win + $draw + $lose;
987 $percent = sprintf "%4u%%", int(100.0 * $score / $n + 0.5);
990 push @moves, [ $pmove, $n, $percent, $rating ];
993 @moves[1..$#moves] = sort { $b->[2] cmp $a->[2] } @moves[1..$#moves];
995 my $text = "Book moves:\n\n Perf. N Rating\n\n";
997 $text .= sprintf " %-10s %s %6u %4s\n", $m->[0], $m->[2], $m->[1], $m->[3]
1004 my $cmdline = shift;
1005 my ($uciread, $uciwrite);
1006 my $pid = IPC::Open2::open2($uciread, $uciwrite, $cmdline);
1014 uciprint($engine, "uci");
1016 # gobble the options
1017 while (<$uciread>) {