oakmac / chessboard2

chessboard.js v2
ISC License
81 stars 7 forks source link

Setting fen of a chessboard does not work sometimes #47

Closed gdm3 closed 1 year ago

gdm3 commented 1 year ago

It seems like using let x = board.fen() to get the chessboard and then setting it back with board.fen(x) does not work if two pieces have moved already, and only if two pieces have moved already.

I have code like this to send a request to my server to see if the move is valid or not, and if it isn't then don't show the move:

function onDrop (source, target) {
  // Get current board to snap back to if move is invalid
  var cur_board = board.fen()

  axios.post('/', {
    source: source.source,
    target: source.target
  })
  .then(function (response) {
    // If move is invalid, set board back to the original board
    if(response.data.processed == false){
      board.fen(cur_board)
    }
  })
  .catch(function (error) {
    console.log(error);
  });
}

The funny thing is this works totally fine up until two pieces are changed on the board. Then it doesn't work at all. Am I overlooking something? If you console.log the fen before and after doing the changes, they're not any different, but if you log cur_board it is different.

Adding onto that, an example of a fen string that board.fen() does not work on is rnbqkbnr/pppppppp/3P5/8/4P3/8/PPP3PPP/RNBQKBNR, but that fen string was generated with board.fen() itself, so it should work.

oakmac commented 1 year ago

I cannot tell from your issue exactly what the bug is here. My hunch is that this is a timing / state bug with your program. Can you recreate the minimal code that shows a bug with chessboard2?

Adding onto that, an example of a fen string that board.fen() does not work on is rnbqkbnr/pppppppp/3P5/8/4P3/8/PPP3PPP/RNBQKBNR, but that fen string was generated with board.fen() itself, so it should work.

rnbqkbnr/pppppppp/3P5/8/4P3/8/PPP3PPP/RNBQKBNR is not a legal FEN string (3P5 is 9 squares, PPP3PPP is also 9 squares). Can you reproduce the code that caused chessboard2 to create it?

gdm3 commented 1 year ago

I will recreate an example later, but that fen string was returned by board.fen() so it should be a legal fen string.

gdm3 commented 1 year ago

It seems like just making some certain illegal moves causes board.fen() to return an invalid fen string, such as playing e4, d4, and then moving the d4 pawn to d7, 'capturing' the pawn. (not an actual legal move, obviously). This specific sequence returns rnbqkbnr/pppppppp/8/8/3PP3/8/PPP3PPP/RNBQKBNR as the fen string when checking board.fen().

Quick example where you can try this: https://chessboard-test.noelb33.repl.co/

oakmac commented 1 year ago

Thank you for following up on this :+1:

I found a one-character bug with the position->fen function that I am pretty sure is the root cause here :upside_down_face:

Sorry for the trouble! I can imagine this would be quite frustrating to debug. This should be fixed with v0.5.0.