shaack / cm-chessboard

A JavaScript chessboard without dependencies. Rendered in SVG, coded in ES6. Views FEN, handles move input, animated, responsive, expandable
MIT License
209 stars 63 forks source link

How do people usually handle moving the rook post-castling? #118

Closed willdelphia closed 1 year ago

willdelphia commented 1 year ago

Sorry if this isn't an issue with the library per se. But I have installed a chess engine along with this cm-chessboard to accept legal moves and if they are legal update allow them on the cm-chessboard instance using the drag and drop validation method. The issue is that the the rook will remain in place after a legal castling move. Is there a simple way to make the rook jump the king after a successful castle? Or will I have to code my own solution to make this happen. Thanks.

shaack commented 1 year ago

Hi. This is solved with very few lines of code in the move validation example.

You can simply execute a "setPosition" after entering the move.

if (result) {
    this.chessboard.state.moveInputProcess.then(() => { // wait for the move input process has finished
        this.chessboard.setPosition(chess.fen(), true).then(() => { // update position, maybe castled and wait for animation has finished
            makeEngineMove(event.chessboard)
        })
    })
}

You could also take a look at the "Playfield" extension, where the move validation and more is implemented in a more sophisticated (but also a bit more complicated) way.

willdelphia commented 1 year ago

Thanks, I'll give that a try.