Skip to content

Fix Board.moveStack() returning chess960 castling notation in non-chess960 mode#913

Draft
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-911
Draft

Fix Board.moveStack() returning chess960 castling notation in non-chess960 mode#913
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-911

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Sep 5, 2025

The Board.moveStack() method in ffish.js was incorrectly returning chess960-style UCI castling moves (e.g., e8h8) instead of standard castling notation (e.g., e8g8) when playing in non-chess960 mode. This occurred when the board position changed significantly after a castling move was played.

Problem

Consider this sequence:

let board = new ffish.Board("seirawan", "4kbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR[EH] w KQBCDFGk - 0 1", false);
board.pushMoves("e2e4 g8f6 e4e5 f6d5 c2c4 d5b4 a2a3 b4c6 d2d4 e7e6 b1c3h f8e7 g1f3e e7d8 b2b4 c6e7 b1e4 a7a6 h2h4 h7h6 e4b7 e8g8 g2g4 a6a5 b4a5 c7c5 d4c5 g8h7 d1d7 h7h8 a5a6 e7g6 a6a7 d8a5 b7a5 h8h7 g4g5 h6h5 g1g3 g6f4 c1f4 g7g6 a1b1 h7g8 b1b8 g8g7 b8f8 g7f8 a7a8q f8g7 a8e8");

console.log(board.moveStack()); // Contains "e8g8" (correct)
board.push("g7h7");
console.log(board.moveStack()); // Contains "e8h8" (incorrect - changed to chess960 notation!)
board.pop();
console.log(board.moveStack()); // Back to "e8g8" (correct again)

Root Cause

The move_stack() method was calling UCI::move(pos, *it) to convert each stored Move object back to UCI format using the current position. When the position changed after castling, the UCI formatting logic detected that the standard castling move (e8g8) would be ambiguous with a normal king move in the new position, causing it to fall back to chess960 notation (e8h8).

Solution

Store the original UCI strings when moves are played instead of converting Move objects back to UCI format later:

  1. Added moveStackUCI member variable to preserve input UCI strings
  2. Modified push() and push_san() to store original UCI strings alongside Move objects
  3. Updated move_stack() to return stored UCI strings instead of converting Move objects
  4. Updated pop() and set_fen() to maintain consistency between both stacks

This ensures moveStack() always returns the exact UCI strings as they were originally input, eliminating the position-dependent conversion issue.

Testing

Added a comprehensive test case that reproduces the exact issue and verifies the fix works correctly for both push/pop operations and position changes.

Fixes #911.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…mode

Co-authored-by: ianfab <12969303+ianfab@users.noreply.github.com>
Copilot AI changed the title [WIP] ffish.js Board.moveStack() method returns chess960 style UCI castling move in non-chess960 mode Fix Board.moveStack() returning chess960 castling notation in non-chess960 mode Sep 5, 2025
Copilot AI requested a review from ianfab September 5, 2025 22:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ffish.js Board.moveStack() method returns chess960 style UCI castling move in non-chess960 mode

2 participants