bhlangonijr / chesslib

chess library for legal move generation, FEN/PGN parsing and more
Apache License 2.0
229 stars 80 forks source link

How to validate the move? #81

Closed imanishpr closed 2 years ago

bhlangonijr commented 2 years ago

In order to validate if selected_move is a legal move in standard chess rules you can do something like:

if (board.legalMoves().contains(selected_move)) {
    board.doMove(selected_move);
}

if you just want to validate if shuffling around some piece within the chessboard - which is not necessarily a legal move - leaves it in a valid state (i.e.: own king is not attacked after the move):

if (board.isMoveLegal(arbitrary_move, true)) {
  // chessboard is in a valid state
}

this is particularly useful when for example you are implementing a GUI with a drag & drop feature for editing a chess position.