+# Returns the short algebraic form of the move, as well as the new position.
+sub prettyprint_move {
+ my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
+ my $pretty = $board->_prettyprint_move_no_check_or_mate($from_row, $from_col, $to_row, $to_col, $promo);
+
+ my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col, $promo);
+
+ my $piece = $board->[$from_row][$from_col];
+ my $other_side = (uc($piece) eq $piece) ? 'k' : 'K';
+ my $in_check = $nb->in_check($other_side);
+ if ($nb->in_mate($other_side, $in_check)) {
+ $pretty .= '#';
+ } elsif ($in_check) {
+ $pretty .= '+';
+ }
+ return ($pretty, $nb);
+}
+
+sub num_pieces {
+ my ($board) = @_;
+
+ my $num = 0;
+ for my $row (0..7) {
+ for my $col (0..7) {
+ my $piece = $board->[$row][$col];
+ ++$num if ($piece ne '-');
+ }
+ }
+ return $num;
+}
+
+sub _prettyprint_move_no_check_or_mate {
+ my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
+ my $piece = $board->[$from_row][$from_col];
+ my $move = move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
+
+ if ($piece eq '-') {
+ die "Invalid move $move";
+ }
+
+ # white short castling
+ if ($move eq 'e1g1' && $piece eq 'K') {
+ return 'O-O';
+ }
+
+ # white long castling
+ if ($move eq 'e1c1' && $piece eq 'K') {
+ return 'O-O-O';
+ }
+
+ # black short castling
+ if ($move eq 'e8g8' && $piece eq 'k') {
+ return 'O-O';
+ }
+
+ # black long castling
+ if ($move eq 'e8c8' && $piece eq 'k') {
+ return 'O-O-O';
+ }
+
+ my $pretty;
+
+ # check if the from-piece is a pawn
+ if (lc($piece) eq 'p') {
+ # attack?
+ if ($from_col != $to_col) {
+ $pretty = substr($move, 0, 1) . 'x' . _pos_to_square($to_row, $to_col);
+ } else {
+ $pretty = _pos_to_square($to_row, $to_col);
+ }
+
+ if (defined($promo) && $promo ne '') {
+ # promotion
+ $pretty .= "=";
+ $pretty .= uc($promo);
+ }
+ return $pretty;
+ }
+
+ $pretty = uc($piece);
+
+ # see how many of these pieces could go here, in all
+ my $num_total = 0;
+ for my $col (0..7) {
+ for my $row (0..7) {
+ next unless ($board->[$row][$col] eq $piece);
+ ++$num_total if ($board->can_legally_reach($piece, $row, $col, $to_row, $to_col));
+ }
+ }
+
+ # see how many of these pieces from the given row could go here
+ my $num_row = 0;
+ for my $col (0..7) {
+ next unless ($board->[$from_row][$col] eq $piece);
+ ++$num_row if ($board->can_legally_reach($piece, $from_row, $col, $to_row, $to_col));
+ }
+
+ # and same for columns
+ my $num_col = 0;
+ for my $row (0..7) {
+ next unless ($board->[$row][$from_col] eq $piece);
+ ++$num_col if ($board->can_legally_reach($piece, $row, $from_col, $to_row, $to_col));
+ }
+
+ # see if we need to disambiguate
+ if ($num_total > 1) {
+ if ($num_col == 1) {
+ $pretty .= substr($move, 0, 1);
+ } elsif ($num_row == 1) {
+ $pretty .= substr($move, 1, 1);
+ } else {
+ $pretty .= substr($move, 0, 2);
+ }
+ }
+
+ # attack?
+ if ($board->[$to_row][$to_col] ne '-') {
+ $pretty .= 'x';
+ }
+
+ $pretty .= _pos_to_square($to_row, $to_col);
+ return $pretty;
+}
+