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.
15 use Chess::PGN::Parse;
23 require 'Position.pm';
31 $SIG{ALRM} = sub { output(); };
32 my $latest_update = undef;
33 my $http_timer = undef;
34 my $tb_retry_timer = undef;
36 my $tb_lookup_running = 0;
40 open(FICSLOG, ">ficslog.txt")
41 or die "ficslog.txt: $!";
42 print FICSLOG "Log starting.\n";
46 open(UCILOG, ">ucilog.txt")
47 or die "ucilog.txt: $!";
48 print UCILOG "Log starting.\n";
52 open(TBLOG, ">tblog.txt")
53 or die "tblog.txt: $!";
54 print TBLOG "Log starting.\n";
60 # open the chess engine
61 my $engine = open_engine($remoteglotconf::engine_cmdline, 'E1', sub { handle_uci(@_, 1); });
62 my $engine2 = open_engine($remoteglotconf::engine2_cmdline, 'E2', sub { handle_uci(@_, 0); });
65 my ($pos_waiting, $pos_calculating, $pos_calculating_second_engine);
67 uciprint($engine, "setoption name UCI_AnalyseMode value true");
68 while (my ($key, $value) = each %remoteglotconf::engine_config) {
69 uciprint($engine, "setoption name $key value $value");
71 uciprint($engine, "ucinewgame");
73 if (defined($engine2)) {
74 uciprint($engine2, "setoption name UCI_AnalyseMode value true");
75 while (my ($key, $value) = each %remoteglotconf::engine2_config) {
76 uciprint($engine2, "setoption name $key value $value");
78 uciprint($engine2, "setoption name MultiPV value 500");
79 uciprint($engine2, "ucinewgame");
82 print "Chess engine ready.\n";
85 my $t = Net::Telnet->new(Timeout => 10, Prompt => '/fics% /');
86 $t->input_log(\*FICSLOG);
87 $t->open($remoteglotconf::server);
88 $t->print($remoteglotconf::nick);
89 $t->waitfor('/Press return to enter the server/');
93 $t->cmd("set shout 0");
94 $t->cmd("set seek 0");
95 $t->cmd("set style 12");
96 $t->cmd("observe $remoteglotconf::target");
97 print "FICS ready.\n";
99 my $ev1 = AnyEvent->io(
102 cb => sub { # what callback to execute
104 my $line = $t->getline(Timeout => 0, errmode => 'return');
105 return if (!defined($line));
113 # Engine events have already been set up by Engine.pm.
117 my ($engine, $line, $primary) = @_;
119 return if $line =~ /(upper|lower)bound/;
121 $line =~ s/ / /g; # Sometimes needed for Zappa Mexico
122 print UCILOG localtime() . " $engine->{'tag'} <= $line\n";
123 if ($line =~ /^info/) {
124 my (@infos) = split / /, $line;
127 parse_infos($engine, @infos);
129 if ($line =~ /^id/) {
130 my (@ids) = split / /, $line;
133 parse_ids($engine, @ids);
135 if ($line =~ /^bestmove/) {
137 return if (!$remoteglotconf::uci_assume_full_compliance);
138 if (defined($pos_waiting)) {
139 uciprint($engine, "position fen " . $pos_waiting->fen());
140 uciprint($engine, "go infinite");
142 $pos_calculating = $pos_waiting;
143 $pos_waiting = undef;
146 $engine2->{'info'} = {};
147 my $pos = $pos_waiting // $pos_calculating;
148 uciprint($engine2, "position fen " . $pos->fen());
149 uciprint($engine2, "go infinite");
150 $pos_calculating_second_engine = $pos;
158 if ($line =~ /^<12> /) {
159 handle_position(Position->new($line));
161 if ($line =~ /^([A-Za-z]+)(?:\([A-Z]+\))* tells you: (.*)$/) {
162 my ($who, $msg) = ($1, $2);
164 next if (grep { $_ eq $who } (@remoteglotconf::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 { $engine->{'write'} } "$1\n";
172 } elsif ($msg =~ /^pgn (.*?)$/) {
174 $t->cmd("tell $who Starting to poll '$url'.");
175 AnyEvent::HTTP::http_get($url, sub {
176 handle_pgn(@_, $url);
178 } elsif ($msg =~ /^stoppgn$/) {
179 $t->cmd("tell $who Stopping poll.");
181 } elsif ($msg =~ /^quit$/) {
182 $t->cmd("tell $who Bye bye.");
185 $t->cmd("tell $who Couldn't understand '$msg', sorry.");
188 #print "FICS: [$line]\n";
192 my ($body, $header, $url) = @_;
193 my $pgn = Chess::PGN::Parse->new(undef, $body);
194 if (!defined($pgn) || !$pgn->read_game()) {
195 warn "Error in parsing PGN from $url\n";
197 $pgn->quick_parse_game;
198 my $pos = Position->start_pos($pgn->white, $pgn->black);
199 my $moves = $pgn->moves;
201 for my $move (@$moves) {
202 my ($from_row, $from_col, $to_row, $to_col, $promo) = $pos->parse_pretty_move($move);
203 push @uci_moves, Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
204 $pos = $pos->make_move($from_row, $from_col, $to_row, $to_col, $promo);
206 $pos->{'history'} = \@uci_moves;
207 $pos->{'pretty_history'} = $moves;
208 handle_position($pos);
211 $http_timer = AnyEvent->timer(after => 1.0, cb => sub {
212 AnyEvent::HTTP::http_get($url, sub {
213 handle_pgn(@_, $url);
218 sub handle_position {
221 # if this is already in the queue, ignore it
222 return if (defined($pos_waiting) && $pos->fen() eq $pos_waiting->fen());
224 # if we're already chewing on this and there's nothing else in the queue,
226 return if (!defined($pos_waiting) && defined($pos_calculating) &&
227 $pos->fen() eq $pos_calculating->fen());
229 # if we're already thinking on something, stop and wait for the engine
231 if (defined($pos_calculating)) {
232 if (!defined($pos_waiting)) {
233 uciprint($engine, "stop");
235 if ($remoteglotconf::uci_assume_full_compliance) {
238 uciprint($engine, "position fen " . $pos->fen());
239 uciprint($engine, "go infinite");
240 $pos_calculating = $pos;
243 # it's wrong just to give the FEN (the move history is useful,
244 # and per the UCI spec, we should really have sent "ucinewgame"),
246 uciprint($engine, "position fen " . $pos->fen());
247 uciprint($engine, "go infinite");
248 $pos_calculating = $pos;
251 if (defined($engine2)) {
252 if (defined($pos_calculating_second_engine)) {
253 uciprint($engine2, "stop");
255 uciprint($engine2, "position fen " . $pos->fen());
256 uciprint($engine2, "go infinite");
257 $pos_calculating_second_engine = $pos;
259 $engine2->{'info'} = {};
262 $engine->{'info'} = {};
265 schedule_tb_lookup();
268 # Output a command every move to note that we're
269 # still paying attention -- this is a good tradeoff,
270 # since if no move has happened in the last half
271 # hour, the analysis/relay has most likely stopped
272 # and we should stop hogging server resources.
278 my ($engine, @x) = @_;
281 my $info = $engine->{'info'};
283 # Search for "multipv" first of all, since e.g. Stockfish doesn't put it first.
284 for my $i (0..$#x - 1) {
285 if ($x[$i] eq 'multipv') {
291 while (scalar @x > 0) {
292 if ($x[0] eq 'multipv') {
298 if ($x[0] eq 'currmove' || $x[0] eq 'currmovenumber' || $x[0] eq 'cpuload') {
300 my $value = shift @x;
301 $info->{$key} = $value;
304 if ($x[0] eq 'depth' || $x[0] eq 'seldepth' || $x[0] eq 'hashfull' ||
305 $x[0] eq 'time' || $x[0] eq 'nodes' || $x[0] eq 'nps' ||
308 my $value = shift @x;
309 $info->{$key . $mpv} = $value;
312 if ($x[0] eq 'score') {
315 delete $info->{'score_cp' . $mpv};
316 delete $info->{'score_mate' . $mpv};
318 while ($x[0] eq 'cp' || $x[0] eq 'mate') {
321 $info->{'score_cp' . $mpv} = shift @x;
322 } elsif ($x[0] eq 'mate') {
324 $info->{'score_mate' . $mpv} = shift @x;
332 $info->{'pv' . $mpv} = [ @x[1..$#x] ];
335 if ($x[0] eq 'string' || $x[0] eq 'UCI_AnalyseMode' || $x[0] eq 'setting' || $x[0] eq 'contempt') {
339 #print "unknown info '$x[0]', trying to recover...\n";
341 die "Unknown info '" . join(',', @x) . "'";
347 my ($engine, @x) = @_;
349 while (scalar @x > 0) {
350 if ($x[0] =~ /^(name|author)$/) {
352 my $value = join(' ', @x);
353 $engine->{'id'}{$key} = $value;
362 sub prettyprint_pv_no_cache {
363 my ($board, @pvs) = @_;
365 if (scalar @pvs == 0 || !defined($pvs[0])) {
370 my ($from_col, $from_row, $to_col, $to_row, $promo) = parse_uci_move($pv);
371 my ($pretty, $nb) = $board->prettyprint_move($from_row, $from_col, $to_row, $to_col, $promo);
372 return ( $pretty, prettyprint_pv_no_cache($nb, @pvs) );
376 my ($pos, @pvs) = @_;
378 my $cachekey = join('', @pvs);
379 if (exists($pos->{'prettyprint_cache'}{$cachekey})) {
380 return @{$pos->{'prettyprint_cache'}{$cachekey}};
382 my @res = prettyprint_pv_no_cache($pos->{'board'}, @pvs);
383 $pos->{'prettyprint_cache'}{$cachekey} = \@res;
391 return if (!defined($pos_calculating));
393 # Don't update too often.
394 my $age = Time::HiRes::tv_interval($latest_update);
395 if ($age < $remoteglotconf::update_max_interval) {
396 Time::HiRes::alarm($remoteglotconf::update_max_interval + 0.01 - $age);
400 my $info = $engine->{'info'};
403 # If we have tablebase data from a previous lookup, replace the
404 # engine data with the data from the tablebase.
406 my $fen = $pos_calculating->fen();
407 if (exists($tb_cache{$fen})) {
408 for my $key (qw(pv score_cp score_mate nodes nps depth seldepth tbhits)) {
409 delete $info->{$key . '1'};
410 delete $info->{$key};
412 $info->{'nodes'} = 0;
414 $info->{'depth'} = 0;
415 $info->{'seldepth'} = 0;
416 $info->{'tbhits'} = 0;
418 my $t = $tb_cache{$fen};
420 my $matelen = int((1 + scalar @$pv) / 2);
421 if ($t->{'result'} eq '1/2-1/2') {
422 $info->{'score_cp'} = 0;
423 } elsif ($t->{'result'} eq '1-0') {
424 if ($pos_calculating->{'toplay'} eq 'B') {
425 $info->{'score_mate'} = -$matelen;
427 $info->{'score_mate'} = $matelen;
430 if ($pos_calculating->{'toplay'} eq 'B') {
431 $info->{'score_mate'} = $matelen;
433 $info->{'score_mate'} = -$matelen;
437 $info->{'tablebase'} = 1;
439 $info->{'tablebase'} = 0;
443 # Some programs _always_ report MultiPV, even with only one PV.
444 # In this case, we simply use that data as if MultiPV was never
447 if (exists($info->{'pv1'}) && !exists($info->{'pv2'})) {
448 for my $key (qw(pv score_cp score_mate nodes nps depth seldepth tbhits)) {
449 if (exists($info->{$key . '1'})) {
450 $info->{$key} = $info->{$key . '1'};
456 # Check the PVs first. if they're invalid, just wait, as our data
457 # is most likely out of sync. This isn't a very good solution, as
458 # it can frequently miss stuff, but it's good enough for most users.
462 if (exists($info->{'pv'})) {
463 $dummy = prettyprint_pv($pos_calculating, @{$info->{'pv'}});
467 while (exists($info->{'pv' . $mpv})) {
468 $dummy = prettyprint_pv($pos_calculating, @{$info->{'pv' . $mpv}});
473 $engine->{'info'} = {};
479 $latest_update = [Time::HiRes::gettimeofday];
483 my $info = $engine->{'info'};
484 my $id = $engine->{'id'};
486 my $text = 'Analysis';
487 if ($pos_calculating->{'last_move'} ne 'none') {
488 if ($pos_calculating->{'toplay'} eq 'W') {
489 $text .= sprintf ' after %u. ... %s', ($pos_calculating->{'move_num'}-1), $pos_calculating->{'last_move'};
491 $text .= sprintf ' after %u. %s', $pos_calculating->{'move_num'}, $pos_calculating->{'last_move'};
493 if (exists($id->{'name'})) {
498 if (exists($id->{'name'})) {
499 $text .= " by $id->{'name'}:\n\n";
504 return unless (exists($pos_calculating->{'board'}));
506 if (exists($info->{'pv1'}) && exists($info->{'pv2'})) {
509 while (exists($info->{'pv' . $mpv})) {
510 $text .= sprintf " PV%2u", $mpv;
511 my $score = short_score($info, $pos_calculating, $mpv);
512 $text .= " ($score)" if (defined($score));
515 if (exists($info->{'tbhits' . $mpv}) && $info->{'tbhits' . $mpv} > 0) {
516 if ($info->{'tbhits' . $mpv} == 1) {
517 $tbhits = ", 1 tbhit";
519 $tbhits = sprintf ", %u tbhits", $info->{'tbhits' . $mpv};
523 if (exists($info->{'nodes' . $mpv}) && exists($info->{'nps' . $mpv}) && exists($info->{'depth' . $mpv})) {
524 $text .= sprintf " (%5u kn, %3u kn/s, %2u ply$tbhits)",
525 $info->{'nodes' . $mpv} / 1000, $info->{'nps' . $mpv} / 1000, $info->{'depth' . $mpv};
529 $text .= " " . join(', ', prettyprint_pv($pos_calculating, @{$info->{'pv' . $mpv}})) . "\n";
535 my $score = long_score($info, $pos_calculating, '');
536 $text .= " $score\n" if defined($score);
537 $text .= " PV: " . join(', ', prettyprint_pv($pos_calculating, @{$info->{'pv'}}));
540 if (exists($info->{'nodes'}) && exists($info->{'nps'}) && exists($info->{'depth'})) {
541 $text .= sprintf " %u nodes, %7u nodes/sec, depth %u ply",
542 $info->{'nodes'}, $info->{'nps'}, $info->{'depth'};
544 if (exists($info->{'seldepth'})) {
545 $text .= sprintf " (%u selective)", $info->{'seldepth'};
547 if (exists($info->{'tbhits'}) && $info->{'tbhits'} > 0) {
548 if ($info->{'tbhits'} == 1) {
549 $text .= ", one Syzygy hit";
551 $text .= sprintf ", %u Syzygy hits", $info->{'tbhits'};
557 #$text .= book_info($pos_calculating->fen(), $pos_calculating->{'board'}, $pos_calculating->{'toplay'});
559 my @refutation_lines = ();
560 if (defined($engine2)) {
561 for (my $mpv = 1; $mpv < 500; ++$mpv) {
562 my $info = $engine2->{'info'};
563 last if (!exists($info->{'pv' . $mpv}));
565 my $pv = $info->{'pv' . $mpv};
567 my $pretty_move = join('', prettyprint_pv($pos_calculating_second_engine, $pv->[0]));
568 my @pretty_pv = prettyprint_pv($pos_calculating_second_engine, @$pv);
569 if (scalar @pretty_pv > 5) {
570 @pretty_pv = @pretty_pv[0..4];
571 push @pretty_pv, "...";
573 my $key = $pretty_move;
574 my $line = sprintf(" %-6s %6s %3s %s",
576 short_score($info, $pos_calculating_second_engine, $mpv, 0),
577 "d" . $info->{'depth' . $mpv},
578 join(', ', @pretty_pv));
579 push @refutation_lines, [ $key, $line ];
584 if ($#refutation_lines >= 0) {
585 $text .= "Shallow search of all legal moves:\n\n";
586 for my $line (sort { $a->[0] cmp $b->[0] } @refutation_lines) {
587 $text .= $line->[1] . "\n";
592 if ($last_text ne $text) {
593 print "
\e[H
\e[2J"; # clear the screen
600 my $info = $engine->{'info'};
603 $json->{'position'} = $pos_calculating->to_json_hash();
604 $json->{'id'} = $engine->{'id'};
605 $json->{'score'} = long_score($info, $pos_calculating, '');
607 $json->{'nodes'} = $info->{'nodes'};
608 $json->{'nps'} = $info->{'nps'};
609 $json->{'depth'} = $info->{'depth'};
610 $json->{'tbhits'} = $info->{'tbhits'};
611 $json->{'seldepth'} = $info->{'seldepth'};
612 $json->{'tablebase'} = $info->{'tablebase'};
614 # single-PV only for now
615 $json->{'pv_uci'} = $info->{'pv'};
616 $json->{'pv_pretty'} = [ prettyprint_pv($pos_calculating, @{$info->{'pv'}}) ];
618 my %refutation_lines = ();
619 my @refutation_lines = ();
620 if (defined($engine2)) {
621 for (my $mpv = 1; $mpv < 500; ++$mpv) {
622 my $info = $engine2->{'info'};
623 my $pretty_move = "";
625 last if (!exists($info->{'pv' . $mpv}));
628 my $pv = $info->{'pv' . $mpv};
629 my $pretty_move = join('', prettyprint_pv($pos_calculating, $pv->[0]));
630 my @pretty_pv = prettyprint_pv($pos_calculating, @$pv);
631 $refutation_lines{$pv->[0]} = {
632 sort_key => $pretty_move,
633 depth => $info->{'depth' . $mpv},
634 score_sort_key => score_sort_key($info, $pos_calculating, $mpv, 0),
635 pretty_score => short_score($info, $pos_calculating, $mpv, 0),
636 pretty_move => $pretty_move,
637 pv_pretty => \@pretty_pv,
639 $refutation_lines{$pv->[0]}->{'pv_uci'} = $pv;
643 $json->{'refutation_lines'} = \%refutation_lines;
645 open my $fh, ">", $remoteglotconf::json_output . ".tmp"
647 print $fh JSON::XS::encode_json($json);
649 rename($remoteglotconf::json_output . ".tmp", $remoteglotconf::json_output);
653 my ($engine, $msg) = @_;
654 $engine->print($msg);
655 print UCILOG localtime() . " $engine->{'tag'} => $msg\n";
659 my ($info, $pos, $mpv, $invert) = @_;
662 if ($pos->{'toplay'} eq 'B') {
666 if (defined($info->{'score_mate' . $mpv})) {
668 return sprintf "M%3d", -$info->{'score_mate' . $mpv};
670 return sprintf "M%3d", $info->{'score_mate' . $mpv};
673 if (exists($info->{'score_cp' . $mpv})) {
674 my $score = $info->{'score_cp' . $mpv} * 0.01;
681 return sprintf "%+5.2f", $score;
689 my ($info, $pos, $mpv, $invert) = @_;
691 if (defined($info->{'score_mate' . $mpv})) {
692 my $mate = $info->{'score_mate' . $mpv};
696 $mate = 99999 - $mate;
698 # Side to move is getting mated (note the double negative for $mate)
699 $mate = -99999 - $mate;
706 if (exists($info->{'score_cp' . $mpv})) {
707 my $score = $info->{'score_cp' . $mpv};
719 my ($info, $pos, $mpv) = @_;
721 if (defined($info->{'score_mate' . $mpv})) {
722 my $mate = $info->{'score_mate' . $mpv};
723 if ($pos->{'toplay'} eq 'B') {
727 return sprintf "White mates in %u", $mate;
729 return sprintf "Black mates in %u", -$mate;
732 if (exists($info->{'score_cp' . $mpv})) {
733 my $score = $info->{'score_cp' . $mpv} * 0.01;
735 if ($info->{'tablebase'}) {
736 return "Theoretical draw";
738 return "Score: 0.00";
741 if ($pos->{'toplay'} eq 'B') {
744 return sprintf "Score: %+5.2f", $score;
753 my ($fen, $board, $toplay) = @_;
755 if (exists($book_cache{$fen})) {
756 return $book_cache{$fen};
759 my $ret = `./booklook $fen`;
760 return "" if ($ret =~ /Not found/ || $ret eq '');
764 for my $m (split /\n/, $ret) {
765 my ($move, $annotation, $win, $draw, $lose, $rating, $rating_div) = split /,/, $m;
769 $pmove = '(current)';
771 ($pmove) = prettyprint_pv_no_cache($board, $move);
772 $pmove .= $annotation;
776 if ($toplay eq 'W') {
777 $score = 1.0 * $win + 0.5 * $draw + 0.0 * $lose;
779 $score = 0.0 * $win + 0.5 * $draw + 1.0 * $lose;
781 my $n = $win + $draw + $lose;
787 $percent = sprintf "%4u%%", int(100.0 * $score / $n + 0.5);
790 push @moves, [ $pmove, $n, $percent, $rating ];
793 @moves[1..$#moves] = sort { $b->[2] cmp $a->[2] } @moves[1..$#moves];
795 my $text = "Book moves:\n\n Perf. N Rating\n\n";
797 $text .= sprintf " %-10s %s %6u %4s\n", $m->[0], $m->[2], $m->[1], $m->[3]
803 sub schedule_tb_lookup {
804 return if (!defined($remoteglotconf::tb_serial_key));
805 my $pos = $pos_waiting // $pos_calculating;
806 return if (exists($tb_cache{$pos->fen()}));
808 # If there's more than seven pieces, there's not going to be an answer,
810 return if ($pos->num_pieces() > 7);
812 # Max one at a time. If it's still relevant when it returns,
813 # schedule_tb_lookup() will be called again.
814 return if ($tb_lookup_running);
816 $tb_lookup_running = 1;
817 my $url = 'http://158.250.18.203:6904/tasks/addtask?auth.login=' .
818 $remoteglotconf::tb_serial_key .
819 '&auth.password=aquarium&type=0&fen=' .
820 URI::Escape::uri_escape($pos->fen());
821 print TBLOG "Downloading $url...\n";
822 AnyEvent::HTTP::http_get($url, sub {
823 handle_tb_lookup_return(@_, $pos, $pos->fen());
827 sub handle_tb_lookup_return {
828 my ($body, $header, $pos, $fen) = @_;
829 print TBLOG "Response for [$fen]:\n";
830 print TBLOG $header . "\n\n";
831 print TBLOG $body . "\n\n";
833 my $response = JSON::XS::decode_json($body);
834 if ($response->{'ErrorCode'} != 0) {
835 die "Unknown tablebase server error: " . $response->{'ErrorDesc'};
837 my $state = $response->{'Response'}{'StateString'};
838 if ($state eq 'COMPLETE') {
839 my $pgn = Chess::PGN::Parse->new(undef, $response->{'Response'}{'Moves'});
840 if (!defined($pgn) || !$pgn->read_game()) {
841 warn "Error in parsing PGN\n";
843 $pgn->quick_parse_game;
845 my $moves = $pgn->moves;
847 for my $move (@$moves) {
848 my ($from_row, $from_col, $to_row, $to_col, $promo) = $pvpos->parse_pretty_move($move);
849 push @uci_moves, Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
850 $pvpos = $pvpos->make_move($from_row, $from_col, $to_row, $to_col, $promo);
853 result => $pgn->result,
858 } elsif ($state =~ /QUEUED/ || $state =~ /PROCESSING/) {
859 # Try again in a second. Note that if we have changed
860 # position in the meantime, we might query a completely
861 # different position! But that's fine.
863 die "Unknown response state state " . $response->{'Response'}{'StateString'};
866 # Wait a second before we schedule another one.
867 $tb_retry_timer = AnyEvent->timer(after => 1.0, cb => sub {
868 $tb_lookup_running = 0;
869 schedule_tb_lookup();
873 warn "Error in tablebase lookup: $@";
875 # Don't try this one again, but don't block new lookups either.
876 $tb_lookup_running = 0;
881 my ($cmdline, $tag, $cb) = @_;
882 return undef if (!defined($cmdline));
883 return Engine->open($cmdline, $tag, $cb);
886 sub col_letter_to_num {
887 return ord(shift) - ord('a');
890 sub row_letter_to_num {
891 return 7 - (ord(shift) - ord('1'));
896 my $from_col = col_letter_to_num(substr($move, 0, 1));
897 my $from_row = row_letter_to_num(substr($move, 1, 1));
898 my $to_col = col_letter_to_num(substr($move, 2, 1));
899 my $to_row = row_letter_to_num(substr($move, 3, 1));
900 my $promo = substr($move, 4, 1);
901 return ($from_col, $from_row, $to_col, $to_row, $promo);