]> git.sesse.net Git - remoteglot/blobdiff - www/js/chess.js
Chess.js: In move_from_san(), only bother generating relevant candidate moves.
[remoteglot] / www / js / chess.js
index af275589f65e1924bf880d37ba480c1c6b763f49..3c97b646e03c07565556ed54209c78b7dc99ee85 100644 (file)
@@ -1120,7 +1120,14 @@ var Chess = function(fen) {
        return '';
     }
 
-    let moves = find_attacking_moves(move.to, piece, move.color, sloppy);
+    let moves = find_attacking_moves(move.to, piece, move.color);
+    if (moves.length <= 1) {
+       // There can be no ambiguity, so don't bother checking legality
+       // (we assume the move has already been found legal).
+       return '';
+    }
+
+    moves = possibly_filter_moves(moves, move.color, !sloppy);
 
     var ambiguities = 0;
     var same_rank = 0;
@@ -1169,11 +1176,12 @@ var Chess = function(fen) {
     return '';
   }
 
-  // Find all moves featuring the given piece attacking the given square
-  // (using symmetry of all non-pawn-or-castle moves, we simply generate
-  // moves backwards). Does not support kings or pawns. Assumes there's
-  // not already a piece of our own color on the destination square.
-  function find_attacking_moves(to, piece, us, sloppy) {
+  // Find all pseudolegal moves featuring the given piece moving to
+  // the given square (using symmetry of all non-pawn-or-castle moves,
+  // we simply generate moves backwards). Does not support pawns.
+  // Assumes there's not already a piece of our own color
+  // on the destination square.
+  function find_attacking_moves(to, piece, us) {
     let moves = [];
 
     function add_move(board, moves, from, to, flags, rook_sq) {
@@ -1187,7 +1195,7 @@ var Chess = function(fen) {
         if (square & 0x88) break;
 
         if (board[square] != null) {
-          if (board[square].color !== us) break;
+          if (board[square].color !== us || board[square].type !== piece) break;
           if (board[to] == null) {
             add_move(board, moves, square, to, BITS.NORMAL);
           } else {
@@ -1196,12 +1204,12 @@ var Chess = function(fen) {
           break;
         }
 
-        /* break if knight */
-        if (piece === 'n') break;
+        /* break if knight or king */
+        if (piece === 'n' || piece === 'k') break;
       }
     }
 
-    return possibly_filter_moves(moves, us, !sloppy);
+    return moves;
   }
 
   function ascii() {
@@ -1251,7 +1259,23 @@ var Chess = function(fen) {
       }
     }
 
-    var moves = generate_moves();
+    let moves;
+    let piece_matches = clean_move.match(/^([NBRQK])x?([a-h][1-8])$/);
+    if (piece_matches) {
+      // Only look for moves by the given piece to the given square.
+      let to = SQUARES[piece_matches[2]];
+      if (board[to] != null && board[to].color === turn) {
+        // Cannot capture our own piece.
+        return null;
+      }
+      moves = find_attacking_moves(to, piece_matches[1].toLowerCase(), turn);
+      // Legal moves only.
+      moves = possibly_filter_moves(moves, turn, true);
+    } else {
+      // Fallback (also used for pawns): Any (legal) moves.
+      moves = generate_moves();
+    }
+
     for (var i = 0, len = moves.length; i < len; i++) {
       // try the strict parser first, then the sloppy parser if requested
       // by the user