bhlangonijr / chesslib

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

Is possible to check special moves? #119

Closed 9acca9 closed 1 month ago

9acca9 commented 1 month ago

Hi. Sorry the ignorance. I made a little app in python using python-chess (a module to chess validation move), with this module you can ask if certain moves are "en passant", "castling", "promotion". For example:

if chess_board.is_castling(move): 
      do...

if move is a move that is castling the if will be true

Now im trying to replicate my app but with Java... (im trying to learn java...) It is possible to know if this moves are happening with your library?

Thanks!

bhlangonijr commented 1 month ago

Hello @9acca9 , yes absolutely. You can definitely achieve similar functionality, though it might not be as intuitive as in your Python library: Castling:

// check castle move
board.getContext().isCastleMove(move);
board.getContext().isKingSideCastle(move);
board.getContext().isQueenSideCastle(move);
...
// check for castle rights
board.getContext().hasCastleRight(move, QUEEN_SIDE);
board.getContext().hasCastleRight(move, KING_SIDE);
board.getContext().hasCastleRight(move, KING_AND_QUEEN_SIDE);

Check for promotion:

move.getPromotion() != NONE // Something different from NONE is either a promotion to queen or an underpromotion 

In order to check whether move is en passant, you can verify if the moving piece is a pawn and destination square is en passant:

// given that:
board.getEnPassant() // the destination square of an en passant capture, if any
board.getEnPassantTarget() // the target square of an en passant capture, if any

// checks whether the move is en passant depending on the board state info

boolean movingPieceIsPawn = board.getPiece(move.getFrom()).getPieceType() == PieceType.PAWN;
boolean isMoveEnPassant = movingPieceIsPawn && board.getEnPassant() == move.getTo()
9acca9 commented 1 month ago

Thanks! Excellent! Thanks for the developed answer!