]> git.sesse.net Git - remoteglot/blobdiff - Board.pm
Make parse_info a bit less regex-happy; speeds it up somewhat.
[remoteglot] / Board.pm
index b925a388952ce6c5cc6e5ae1d35d00a5898d6171..ff2828af82b03c60f6e2fcef4dd341e8d2d6ed0e 100644 (file)
--- a/Board.pm
+++ b/Board.pm
@@ -36,7 +36,7 @@ sub clone {
 # Returns a new board.
 sub make_move {
        my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
-       my $move = _move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
+       my $move = move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
        my $piece = $board->[$from_row][$from_col];
        my $nb = $board->clone();
 
@@ -103,20 +103,19 @@ sub make_move {
                        # en passant?
                        if ($board->[$to_row][$to_col] eq '-') {
                                if ($piece eq 'p') {
-                                       $nb->[$to_row + 1][$to_col] = '-';
-                               } else {
                                        $nb->[$to_row - 1][$to_col] = '-';
-                               }
-                       }
-               } else {
-                       if (defined($promo) && $promo ne '') {
-                               if ($piece eq 'p') {
-                                       $piece = $promo;
                                } else {
-                                       $piece = uc($promo);
+                                       $nb->[$to_row + 1][$to_col] = '-';
                                }
                        }
                }
+               if (defined($promo) && $promo ne '') {
+                       if ($piece eq 'p') {
+                               $piece = lc($promo);
+                       } else {
+                               $piece = uc($promo);
+                       }
+               }
        }
 
        # update the board
@@ -145,7 +144,7 @@ sub _square_to_pos {
        return (_row_letter_to_num($2), _col_letter_to_num($1));
 }
 
-sub _move_to_uci_notation {
+sub move_to_uci_notation {
        my ($from_row, $from_col, $to_row, $to_col, $promo) = @_;
        $promo //= "";
        return _pos_to_square($from_row, $from_col) . _pos_to_square($to_row, $to_col) . $promo;
@@ -194,6 +193,14 @@ sub parse_pretty_move {
                        next if (defined($from_col) && $from_col != $col);
                        next if ($board->[$row][$col] ne $piece);
                        next if (!$board->can_reach($piece, $row, $col, $to_row, $to_col));
+
+                       # See if doing this move would put us in check
+                       # (yes, there are clients that expect us to do this).
+                       my $check = $board->make_move($row, $col, $to_row, $to_col, $promo)->in_check();
+                       next if ($check eq 'both' ||
+                                ($toplay eq 'W' && $check eq 'white') ||
+                                ($toplay eq 'B' && $check eq 'black'));
+
                        push @squares, [ $row, $col ];
                }
        }
@@ -290,7 +297,6 @@ sub can_reach {
                        can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
        }
 
-       # TODO: en passant
        if ($piece eq 'p') {
                # black pawn
                if ($to_col == $from_col && $to_row == $from_row + 1) {
@@ -301,7 +307,12 @@ sub can_reach {
                        return ($dest_piece eq '-' && $middle_piece eq '-');
                }
                if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
-                       return ($dest_piece ne '-');
+                       if ($dest_piece eq '-') {
+                               # En passant. TODO: check that the last move was indeed an EP move
+                               return ($to_row == 5 && $board->[4][$to_col] eq 'P');
+                       } else {
+                               return 1;
+                       }
                }
                return 0;
        }
@@ -315,7 +326,12 @@ sub can_reach {
                        return ($dest_piece eq '-' && $middle_piece eq '-');
                }
                if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
-                       return ($dest_piece ne '-');
+                       if ($dest_piece eq '-') {
+                               # En passant. TODO: check that the last move was indeed an EP move
+                               return ($to_row == 2 && $board->[3][$to_col] eq 'p');
+                       } else {
+                               return 1;
+                       }
                }
                return 0;
        }
@@ -431,7 +447,7 @@ sub prettyprint_move {
 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);
+       my $move = move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
 
        if ($piece eq '-') {
                die "Invalid move $move";