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) {
167 my $line = read_line($engine->{'read'});
171 # don't update too often
172 Time::HiRes::alarm(0.2);
183 $line =~ s/ / /g; # Sometimes needed for Zappa Mexico
184 print UCILOG localtime() . " <= $line\n";
185 if ($line =~ /^info/) {
186 my (@infos) = split / /, $line;
191 if ($line =~ /^id/) {
192 my (@ids) = split / /, $line;
197 if ($line =~ /^bestmove/ && $uci_assume_full_compliance) {
198 if (defined($pos_waiting)) {
199 uciprint($engine, "position fen " . $pos_waiting->{'fen'});
200 uciprint($engine, "go infinite");
202 $pos_calculating = $pos_waiting;
203 $pos_waiting = undef;
212 while (scalar @x > 0) {
213 if ($x[0] =~ 'multipv') {
218 if ($x[0] =~ /^(currmove|currmovenumber|cpuload)$/) {
220 my $value = shift @x;
221 $uciinfo{$key} = $value;
224 if ($x[0] =~ /^(depth|seldepth|hashfull|time|nodes|nps|tbhits)$/) {
226 my $value = shift @x;
227 $uciinfo{$key . $mpv} = $value;
230 if ($x[0] eq 'score') {
233 delete $uciinfo{'score_cp' . $mpv};
234 delete $uciinfo{'score_mate' . $mpv};
236 while ($x[0] =~ /^(cp|mate|lowerbound|upperbound)$/) {
239 $uciinfo{'score_cp' . $mpv} = shift @x;
240 } elsif ($x[0] eq 'mate') {
242 $uciinfo{'score_mate' . $mpv} = shift @x;
250 $uciinfo{'pv' . $mpv} = [ @x[1..$#x] ];
253 if ($x[0] eq 'string' || $x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting' || $x[0] eq 'contempt') {
257 #print "unknown info '$x[0]', trying to recover...\n";
259 die "Unknown info '" . join(',', @x) . "'";
267 while (scalar @x > 0) {
268 if ($x[0] =~ /^(name|author)$/) {
270 my $value = join(' ', @x);
271 $uciid{$key} = $value;
283 my (@x) = split / /, $str;
285 $pos{'board'} = [ @x[1..8] ];
286 $pos{'toplay'} = $x[9];
289 my (@board) = @x[1..8];
290 for my $rank (0..7) {
291 $board[$rank] =~ s/(-+)/length($1)/ge;
293 my $fen = join('/', @board);
295 # white/black to move
301 $castling .= "K" if ($x[11] == 1);
302 $castling .= "Q" if ($x[12] == 1);
303 $castling .= "k" if ($x[13] == 1);
304 $castling .= "q" if ($x[14] == 1);
305 $castling = "-" if ($castling eq "");
306 # $castling = "-"; # chess960
314 my $nep = (qw(a b c d e f g h))[$col];
323 # Showing the en passant square when actually no capture can be made
324 # seems to confuse at least Rybka. Thus, check if there's actually
325 # a pawn of the opposite side that can do the en passant move, and if
326 # not, just lie -- it doesn't matter anyway. I'm unsure what's the
327 # "right" thing as per the standard, though.
330 $ep = $nep if ($col > 0 && substr($pos{'board'}[4], $col-1, 1) eq 'p');
331 $ep = $nep if ($col < 7 && substr($pos{'board'}[4], $col+1, 1) eq 'p');
333 $ep = $nep if ($col > 0 && substr($pos{'board'}[3], $col-1, 1) eq 'P');
334 $ep = $nep if ($col < 7 && substr($pos{'board'}[3], $col+1, 1) eq 'P');
349 $pos{'move_num'} = $x[26];
350 $pos{'last_move'} = $x[29];
356 my ($board, @pvs) = @_;
358 if (scalar @pvs == 0 || !defined($pvs[0])) {
365 my $from_col = ord(substr($pv, 0, 1)) - ord('a');
366 my $from_row = 7 - (ord(substr($pv, 1, 1)) - ord('1'));
367 my $to_col = ord(substr($pv, 2, 1)) - ord('a');
368 my $to_row = 7 - (ord(substr($pv, 3, 1)) - ord('1'));
371 my $piece = substr($board->[$from_row], $from_col, 1);
374 die "Invalid move $pv";
377 # white short castling
378 if ($pv eq 'e1g1' && $piece eq 'K') {
380 substr($nb[7], 4, 1, '-');
381 substr($nb[7], 6, 1, $piece);
384 substr($nb[7], 7, 1, '-');
385 substr($nb[7], 5, 1, 'R');
387 return ('0-0', prettyprint_pv(\@nb, @pvs));
390 # white long castling
391 if ($pv eq 'e1c1' && $piece eq 'K') {
393 substr($nb[7], 4, 1, '-');
394 substr($nb[7], 2, 1, $piece);
397 substr($nb[7], 0, 1, '-');
398 substr($nb[7], 3, 1, 'R');
400 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
403 # black short castling
404 if ($pv eq 'e8g8' && $piece eq 'k') {
406 substr($nb[0], 4, 1, '-');
407 substr($nb[0], 6, 1, $piece);
410 substr($nb[0], 7, 1, '-');
411 substr($nb[0], 5, 1, 'r');
413 return ('0-0', prettyprint_pv(\@nb, @pvs));
416 # black long castling
417 if ($pv eq 'e8c8' && $piece eq 'k') {
419 substr($nb[0], 4, 1, '-');
420 substr($nb[0], 2, 1, $piece);
423 substr($nb[0], 0, 1, '-');
424 substr($nb[0], 3, 1, 'r');
426 return ('0-0-0', prettyprint_pv(\@nb, @pvs));
429 # check if the from-piece is a pawn
430 if (lc($piece) eq 'p') {
432 if ($from_col != $to_col) {
433 $pretty = substr($pv, 0, 1) . 'x' . substr($pv, 2, 2);
436 if (substr($board->[$to_row], $to_col, 1) eq '-') {
438 substr($nb[$to_row + 1], $to_col, 1, '-');
440 substr($nb[$to_row - 1], $to_col, 1, '-');
444 $pretty = substr($pv, 2, 2);
446 if (length($pv) == 5) {
449 $pretty .= uc(substr($pv, 4, 1));
452 $piece = substr($pv, 4, 1);
454 $piece = uc(substr($pv, 4, 1));
459 $pretty = uc($piece);
461 # see how many of these pieces could go here, in all
465 next unless (substr($board->[$row], $col, 1) eq $piece);
466 ++$num_total if (can_reach($board, $piece, $row, $col, $to_row, $to_col));
470 # see how many of these pieces from the given row could go here
473 next unless (substr($board->[$from_row], $col, 1) eq $piece);
474 ++$num_row if (can_reach($board, $piece, $from_row, $col, $to_row, $to_col));
477 # and same for columns
480 next unless (substr($board->[$row], $from_col, 1) eq $piece);
481 ++$num_col if (can_reach($board, $piece, $row, $from_col, $to_row, $to_col));
484 # see if we need to disambiguate
485 if ($num_total > 1) {
487 $pretty .= substr($pv, 0, 1);
488 } elsif ($num_row == 1) {
489 $pretty .= substr($pv, 1, 1);
491 $pretty .= substr($pv, 0, 2);
496 if (substr($board->[$to_row], $to_col, 1) ne '-') {
500 $pretty .= substr($pv, 2, 2);
504 substr($nb[$from_row], $from_col, 1, '-');
505 substr($nb[$to_row], $to_col, 1, $piece);
509 } elsif (in_check(\@nb) ne 'none') {
513 return ($pretty, prettyprint_pv(\@nb, @pvs));
519 return if (!defined($pos_calculating));
522 # Check the PVs first. if they're invalid, just wait, as our data
523 # is most likely out of sync. This isn't a very good solution, as
524 # it can frequently miss stuff, but it's good enough for most users.
528 if (exists($uciinfo{'pv'})) {
529 $dummy = prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv'}});
533 while (exists($uciinfo{'pv' . $mpv})) {
534 $dummy = prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv' . $mpv}});
543 my $text = 'Analysis';
544 if ($pos_calculating->{'last_move'} ne 'none') {
545 if ($pos_calculating->{'toplay'} eq 'W') {
546 $text .= sprintf ' after %u. ... %s', ($pos_calculating->{'move_num'}-1), $pos_calculating->{'last_move'};
548 $text .= sprintf ' after %u. %s', $pos_calculating->{'move_num'}, $pos_calculating->{'last_move'};
550 if (exists($uciid{'name'})) {
555 if (exists($uciid{'name'})) {
556 $text .= " by $uciid{'name'}:\n\n";
561 return unless (exists($pos_calculating->{'board'}));
564 # Some programs _always_ report MultiPV, even with only one PV.
565 # In this case, we simply use that data as if MultiPV was never
568 if (exists($uciinfo{'pv1'}) && !exists($uciinfo{'pv2'})) {
569 for my $key (qw(pv score_cp score_mate nodes nps depth seldepth tbhits)) {
570 if (exists($uciinfo{$key . '1'}) && !exists($uciinfo{$key})) {
571 $uciinfo{$key} = $uciinfo{$key . '1'};
576 if (exists($uciinfo{'pv1'}) && exists($uciinfo{'pv2'})) {
579 while (exists($uciinfo{'pv' . $mpv})) {
580 $text .= sprintf " PV%2u", $mpv;
581 my $score = short_score(\%uciinfo, $pos_calculating, $mpv);
582 $text .= " ($score)" if (defined($score));
585 if (exists($uciinfo{'tbhits' . $mpv}) && $uciinfo{'tbhits' . $mpv} > 0) {
586 if ($uciinfo{'tbhits' . $mpv} == 1) {
587 $tbhits = ", 1 tbhit";
589 $tbhits = sprintf ", %u tbhits", $uciinfo{'tbhits' . $mpv};
593 if (exists($uciinfo{'nodes' . $mpv}) && exists($uciinfo{'nps' . $mpv}) && exists($uciinfo{'depth' . $mpv})) {
594 $text .= sprintf " (%5u kn, %3u kn/s, %2u ply$tbhits)",
595 $uciinfo{'nodes' . $mpv} / 1000, $uciinfo{'nps' . $mpv} / 1000, $uciinfo{'depth' . $mpv};
599 $text .= " " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv' . $mpv}})) . "\n";
605 my $score = long_score(\%uciinfo, $pos_calculating, '');
606 $text .= " $score\n" if defined($score);
607 $text .= " PV: " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv'}}));
610 if (exists($uciinfo{'nodes'}) && exists($uciinfo{'nps'}) && exists($uciinfo{'depth'})) {
611 $text .= sprintf " %u nodes, %7u nodes/sec, depth %u ply",
612 $uciinfo{'nodes'}, $uciinfo{'nps'}, $uciinfo{'depth'};
614 if (exists($uciinfo{'tbhits'}) && $uciinfo{'tbhits'} > 0) {
615 if ($uciinfo{'tbhits'} == 1) {
616 $text .= ", one Nalimov hit";
618 $text .= sprintf ", %u Nalimov hits", $uciinfo{'tbhits'};
621 if (exists($uciinfo{'seldepth'})) {
622 $text .= sprintf " (%u selective)", $uciinfo{'seldepth'};
627 #$text .= book_info($pos_calculating->{'fen'}, $pos_calculating->{'board'}, $pos_calculating->{'toplay'});
629 if ($last_text ne $text) {
630 print "
\e[H
\e[2J"; # clear the screen
635 # Now construct the tell text, if any
636 return if (!defined($telltarget));
640 if (exists($uciid{'name'})) {
641 $tell_text .= "Analysis by $uciid{'name'} -- see http://analysis.sesse.net/ for more information\n";
643 $tell_text .= "Computer analysis -- http://analysis.sesse.net/ for more information\n";
646 if (exists($uciinfo{'pv1'}) && exists($uciinfo{'pv2'})) {
649 while (exists($uciinfo{'pv' . $mpv})) {
650 $tell_text .= sprintf " PV%2u", $mpv;
651 my $score = short_score(\%uciinfo, $pos_calculating, $mpv);
652 $tell_text .= " ($score)" if (defined($score));
654 if (exists($uciinfo{'depth' . $mpv})) {
655 $tell_text .= sprintf " (%2u ply)", $uciinfo{'depth' . $mpv};
659 $tell_text .= join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv' . $mpv}}));
665 my $score = long_score(\%uciinfo, $pos_calculating, '');
666 $tell_text .= " $score\n" if defined($score);
667 $tell_text .= " PV: " . join(', ', prettyprint_pv($pos_calculating->{'board'}, @{$uciinfo{'pv'}}));
668 if (exists($uciinfo{'depth'})) {
669 $tell_text .= sprintf " (depth %u ply)", $uciinfo{'depth'};
674 # see if a new tell is called for -- it is if the delay has expired _and_
675 # this is not simply a repetition of the last one
676 if ($last_told_text ne $tell_text) {
678 for my $iv (@tell_intervals) {
679 last if ($now - $last_move < $iv);
680 next if ($last_tell - $last_move >= $iv);
682 for my $line (split /\n/, $tell_text) {
683 $t->print("tell $telltarget [$target] $line");
686 $last_told_text = $text;
696 my ($wkr, $wkc, $bkr, $bkc);
700 my $piece = substr($board->[$row], $col, 1);
702 ($wkr, $wkc) = ($row, $col);
703 } elsif ($piece eq 'k') {
704 ($bkr, $bkc) = ($row, $col);
709 return ($wkr, $wkc, $bkr, $bkc);
714 my $check = in_check($board);
715 return 0 if ($check eq 'none');
717 # try all possible moves for the side in check
720 my $piece = substr($board->[$row], $col, 1);
721 next if ($piece eq '-');
723 if ($check eq 'white') {
724 next if ($piece eq lc($piece));
726 next if ($piece eq uc($piece));
729 for my $dest_row (0..7) {
730 for my $dest_col (0..7) {
731 next if ($row == $dest_row && $col == $dest_col);
732 next unless (can_reach($board, $piece, $row, $col, $dest_row, $dest_col));
735 substr($nb[$row], $col, 1, '-');
736 substr($nb[$dest_row], $dest_col, 1, $piece);
738 my $new_check = in_check(\@nb);
739 return 0 if ($new_check ne $check && $new_check ne 'both');
745 # nothing to do; mate
751 my ($black_check, $white_check) = (0, 0);
753 my ($wkr, $wkc, $bkr, $bkc) = find_kings($board);
755 # check all pieces for the possibility of threatening the two kings
758 my $piece = substr($board->[$row], $col, 1);
759 next if ($piece eq '-');
761 if (uc($piece) eq $piece) {
763 $black_check = 1 if (can_reach($board, $piece, $row, $col, $bkr, $bkc));
766 $white_check = 1 if (can_reach($board, $piece, $row, $col, $wkr, $wkc));
771 if ($black_check && $white_check) {
773 } elsif ($black_check) {
775 } elsif ($white_check) {
783 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
785 # can't eat your own piece
786 my $dest_piece = substr($board->[$to_row], $to_col, 1);
787 if ($dest_piece ne '-') {
788 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
791 if (lc($piece) eq 'k') {
792 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
794 if (lc($piece) eq 'r') {
795 return 0 unless ($from_row == $to_row || $from_col == $to_col);
797 # check that there's a clear passage
798 if ($from_row == $to_row) {
799 if ($from_col > $to_col) {
800 ($to_col, $from_col) = ($from_col, $to_col);
803 for my $c (($from_col+1)..($to_col-1)) {
804 my $middle_piece = substr($board->[$to_row], $c, 1);
805 return 0 if ($middle_piece ne '-');
810 if ($from_row > $to_row) {
811 ($to_row, $from_row) = ($from_row, $to_row);
814 for my $r (($from_row+1)..($to_row-1)) {
815 my $middle_piece = substr($board->[$r], $to_col, 1);
816 return 0 if ($middle_piece ne '-');
822 if (lc($piece) eq 'b') {
823 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
825 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
826 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
828 my $r = $from_row + $dr;
829 my $c = $from_col + $dc;
831 while ($r != $to_row) {
832 my $middle_piece = substr($board->[$r], $c, 1);
833 return 0 if ($middle_piece ne '-');
841 if (lc($piece) eq 'n') {
842 my $diff_r = abs($from_row - $to_row);
843 my $diff_c = abs($from_col - $to_col);
844 return 1 if ($diff_r == 2 && $diff_c == 1);
845 return 1 if ($diff_r == 1 && $diff_c == 2);
849 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
850 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
853 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
854 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
860 if ($to_col == $from_col && $to_row == $from_row + 1) {
861 return ($dest_piece eq '-');
863 if ($to_col == $from_col && $from_row == 1 && $to_row == 3) {
864 my $middle_piece = substr($board->[2], $to_col, 1);
865 return ($dest_piece eq '-' && $middle_piece eq '-');
867 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
868 return ($dest_piece ne '-');
874 if ($to_col == $from_col && $to_row == $from_row - 1) {
875 return ($dest_piece eq '-');
877 if ($to_col == $from_col && $from_row == 6 && $to_row == 4) {
878 my $middle_piece = substr($board->[5], $to_col, 1);
879 return ($dest_piece eq '-' && $middle_piece eq '-');
881 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
882 return ($dest_piece ne '-');
892 my ($engine, $msg) = @_;
893 print { $engine->{'write'} } "$msg\n";
894 print UCILOG localtime() . " => $msg\n";
898 my ($uciinfo, $pos, $mpv) = @_;
900 if (defined($uciinfo{'score_mate' . $mpv})) {
901 return sprintf "M%3d", $uciinfo{'score_mate' . $mpv};
903 if (exists($uciinfo{'score_cp' . $mpv})) {
904 my $score = $uciinfo{'score_cp' . $mpv} * 0.01;
905 if ($pos->{'toplay'} eq 'B') {
908 return sprintf "%+5.2f", $score;
916 my ($uciinfo, $pos, $mpv) = @_;
918 if (defined($uciinfo{'score_mate' . $mpv})) {
919 my $mate = $uciinfo{'score_mate' . $mpv};
920 if ($pos->{'toplay'} eq 'B') {
924 return sprintf "White mates in %u", $mate;
926 return sprintf "Black mates in %u", -$mate;
929 if (exists($uciinfo{'score_cp' . $mpv})) {
930 my $score = $uciinfo{'score_cp' . $mpv} * 0.01;
931 if ($pos->{'toplay'} eq 'B') {
934 return sprintf "Score: %+5.2f", $score;
943 my ($fen, $board, $toplay) = @_;
945 if (exists($book_cache{$fen})) {
946 return $book_cache{$fen};
949 my $ret = `./booklook $fen`;
950 return "" if ($ret =~ /Not found/ || $ret eq '');
954 for my $m (split /\n/, $ret) {
955 my ($move, $annotation, $win, $draw, $lose, $rating, $rating_div) = split /,/, $m;
959 $pmove = '(current)';
961 ($pmove) = prettyprint_pv($board, $move);
962 $pmove .= $annotation;
966 if ($toplay eq 'W') {
967 $score = 1.0 * $win + 0.5 * $draw + 0.0 * $lose;
969 $score = 0.0 * $win + 0.5 * $draw + 1.0 * $lose;
971 my $n = $win + $draw + $lose;
977 $percent = sprintf "%4u%%", int(100.0 * $score / $n + 0.5);
980 push @moves, [ $pmove, $n, $percent, $rating ];
983 @moves[1..$#moves] = sort { $b->[2] cmp $a->[2] } @moves[1..$#moves];
985 my $text = "Book moves:\n\n Perf. N Rating\n\n";
987 $text .= sprintf " %-10s %s %6u %4s\n", $m->[0], $m->[2], $m->[1], $m->[3]
995 my ($uciread, $uciwrite);
996 my $pid = IPC::Open2::open2($uciread, $uciwrite, $cmdline);
1004 uciprint($engine, "uci");
1006 # gobble the options
1007 while (<$uciread>) {
1019 # Read until we've got a full line -- if the engine sends part of
1020 # a line and then stops we're pretty much hosed, but that should
1024 while ($line !~ /\n/) {
1026 my $ret = sysread $engine->{'read'}, $tmp, 1;
1028 if (!defined($ret)) {
1029 next if ($!{EINTR});
1030 die "error in reading from the UCI engine: $!";
1031 } elsif ($ret == 0) {
1032 die "EOF from UCI engine";
1038 $line =~ tr/\r\n//d;