bhlangonijr / chesslib

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

Queen was able to jump over a pawn #12

Closed Aegel5 closed 5 years ago

Aegel5 commented 5 years ago

Found a bug, the Queen was able to jump over a pawn Board board = new Board(); Move mv = new Move(Square.D1, Square.D3); boolean res = board.doMove(mv, true); // return true

bhlangonijr commented 5 years ago

Hi @alexzh2, It may sound a bit counter-intuitive but the API is actually behaving as expected. Board#isLegalMove() is only checking if that move is not gonna leave the current chess Board in an illegal state after the move is played. You could, for example, design a chess UI where you can drag and drop chess pieces for setting up an arbitrary chess position where you are free to shuffle around all the pieces. It's not checking for a legal move within the current chessboard position. If you want to play only legal moves for the current chess position you should instead generate the moves using the method MoveGenerator.generateLegalMoves(board): you pick one of the moves generated by this method to play it on the board. You can otherwise play any move as long as it is not leaving the board in an illegal state. You can certify if the moves generated by this method are really legal and there is no bug by testing the position with a perft function. It's explained here: https://github.com/bhlangonijr/chesslib#sanity-checking-of-chesslib-move-generation-with-perft