]> git.sesse.net Git - remoteglot/blobdiff - Board.pm
Fix some outdated documentation.
[remoteglot] / Board.pm
index b925a388952ce6c5cc6e5ae1d35d00a5898d6171..d6a0f5524869360b4f171f226aa17086fb34c8da 100644 (file)
--- a/Board.pm
+++ b/Board.pm
@@ -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
@@ -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;
        }