oakmac / chessboardjs

JavaScript chessboard
https://chessboardjs.com
MIT License
2.01k stars 408 forks source link

Pawn Promotions not Updating Board #227

Open sethbassetti opened 2 years ago

sethbassetti commented 2 years ago

I am currently using chessboard.js as a frontend to process a game whose logic is controlled with an engine I've written in C++. Currently, whenever I make a move (with the onDrop event and draggable set to true), it determines whether it is a valid move, makes the move on my backend C++ board, queries it for its FEN string, and updates the chessboard display accordingly. This is working fine so far, but I am running into issues when I attempt to promote a pawn.

My onDrop function looks like this:

function onDrop(source, target, piece, newPos, oldPos, orientation) {

  // Gets the start and end squares of the move
  var source_index = squareIndexes.indexOf(source);
  var target_index = squareIndexes.indexOf(target);

  // Returns true if this is a valid move
  if (Module.MakeMove(source_index, target_index)) {
    UpdateBoard();
    return true;
  }
  return 'snapback';
}

My Update Board function looks like this:

/* Gets the FEN string from the chess engine and updates the display */
function UpdateBoard() {

    // boardPosition contains an FEN string of the true board representation
  var boardPosition = Module.GetBoardPosition();

  // Sets the board position to the correct FEN string
    board.position(boardPosition, false);
  }

Through debugging, I've found that the chess engine is processing the move correctly, and the FEN string Module.GetBoardPosition() returns shows a queen where the pawn previously was. However, it seems that chessboard.js in this case is updating the board twice, the first time it correctly updates the pawn to a queen. This is the expected behavior, however, it somehow updates the board position a second time, reverting the queen to a pawn. (This was verified by logging with the onChange event). Upon the next move for the other side, chessboard.js gets correctly updated and a queen appears, but it only shows correctly upon the move following the promotion.

I have no idea why the board position is being updated twice in the event of a pawn promotion and even weirder, why it is reverting the promoted queen back to a pawn, even though the FEN string the board position is being set to contains a queen there.

Any help would be super appreciated as I have been battling this issue for a while.

samboy commented 2 years ago

Here’s the PGN of a game to test pawn promotion:

1. e4 Nf6 2. Nc3?!  d5  3. e5 d4 4. exf6 dxc3 5. d4?! cxb2 6. fxg7 bxa1=Q 
7. gxh8=Q Qxa2?  8. Qxh7?!  a5??   9. h4??  Nc6?!   10. Nf3 a4??   11. h5??   a3??   
12. Qg8??   Qe6+??   13. Be2 a2??   14. h6 a1=Q?!   15. h7 Qdd5?!   16. h8=Q Qda5+ 
17. Kf1 Kd7 18. Qxf8 b5?!   19. Qe8+ Kd6 20. Bf4+ Kd5 21. Rh5+ f5 22. Rxf5+ Qxf5
23. Qhg8+ e6 24. c4+ Ke4?   25. Qxc6+ Kxf4 26. g3#

Source: https://lichess.org/jBC2lZJt (Rosen vs. NN 2019)

For the record, it works for me, but I use chessboardjs as part of my toolkit to put interactive chess diagrams on my webpage.