bhlangonijr / chesslib

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

Add more info to Move #26

Closed EmmanuelMess closed 4 years ago

EmmanuelMess commented 4 years ago

Add:

bhlangonijr commented 4 years ago

Hello @EmmanuelMess,

You can use the method Move#getPromotion() in order to check if that move lead to a promotion in case it is not of type Piece.NONE. You could also check for under promotions as the promotion field is of type Piece.

Unlike the promotion case - which I think it's important to specify the promotion piece in the Move object itself - the other state information are not stored in the Move object as these are dependent on the board situation. You could potentially use the same MoveList on different Board objects which accept the same moves, for example. This is of course a design decision which I personally think makes more sense if we consider all the other different usages of the API. That said, you can have what you want by looking at the Board#getBackup(). This is a linked list with all the moves and state information generated by playing them. Examples:

// returns the captured piece of last move, or `Piece.NONE` if it wasn't a capture
Piece capturedPiece = board.getBackup().getLast().getCapturedPiece();
// whether last move is a castle
board.getBackup().getLast().isCastleMove()
// whether last move is en passant
board.getBackup().getLast().isEnPassantMove()
// returns the moving piece of last move
Piece movingPiece = board.getBackup().getLast().getMovingPiece() 
// returns the last move
Move lastMove = board.getBackup().getLast().getMove()
// much more...

Obs.: The backup linked list is used to undo moves played so you better not trying to manipulate this list (Ideally it'd an immutable list when called outside the Board object).

Let me know if this is fulfilling the needs of your use case and we can close this ticket.

Best regards, Ben