Skip to content

If there is no piece on the source square inside Board.isMoveLegal(move, fullValidation) method, then return false instead of throwing exception #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/github/bhlangonijr/chesslib/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ public boolean isMoveLegal(Move move, boolean fullValidation) {

if (fullValidation) {
if (Piece.NONE.equals(fromPiece)) {
throw new RuntimeException("From piece cannot be null");
return false;
}

if (fromPiece.getPieceSide().equals(capturedPiece.getPieceSide())) {
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/com/github/bhlangonijr/chesslib/BoardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -829,5 +829,14 @@ public void testDoSanMove2() {
assertEquals("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1", board.getFen());
}

@Test
public void testIsMoveLegalWithFullValidationFailureCase() {
Board board = new Board();
board.doMove(new Move(Square.E2, Square.E4));
board.doMove(new Move(Square.E7, Square.E5));

//* now there is no piece on E2 square
assertFalse(board.isMoveLegal(new Move(Square.E2, Square.E3), true));
}

}