bhlangonijr / chesslib

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

Board.legalMoves() gives moveGeneratorException #74

Closed Smartie1242 closed 3 years ago

Smartie1242 commented 3 years ago

Given the board configuration "r2k3r/p1q1b1p1/4b2p/nB1pp3/8/P2PB3/1PP2PPP/R3K2R b KQkq - 0 2". The following error is given: com.github.bhlangonijr.chesslib.move.MoveGeneratorException: Couldn't generate Legal moves: at com.github.bhlangonijr.chesslib.move.MoveGenerator.generateLegalMoves (MoveGenerator.java:352) at com.github.bhlangonijr.chesslib.Board.legalMoves (Board.java:1399) at nl.ndl.automatedchessboard.Main.lambda$main$0 (Main.java:27) at java.lang.Thread.run (Thread.java:833) Caused by: java.lang.NullPointerException: Cannot invoke "com.github.bhlangonijr.chesslib.PieceType.equals(Object)" because "fromType" is null at com.github.bhlangonijr.chesslib.Board.isMoveLegal (Board.java:1122) at com.github.bhlangonijr.chesslib.move.MoveGenerator.lambda$generateLegalMoves$0 (MoveGenerator.java:349) at java.util.Collection.removeIf (Collection.java:576) at com.github.bhlangonijr.chesslib.move.MoveGenerator.generateLegalMoves (MoveGenerator.java:349) at com.github.bhlangonijr.chesslib.Board.legalMoves (Board.java:1399) at nl.ndl.automatedchessboard.Main.lambda$main$0 (Main.java:27) at java.lang.Thread.run (Thread.java:833) There are still moves remaining obviously therefore there is likely a bug within the code.

bhlangonijr commented 3 years ago

There's an error in your FEN string that's why you got the exception. Since the KQkq castle rights are fully flagged the lib trust the black king is still on e8 while this is obviously not true. The correct FEN string should reflect the lost castle rights by black pieces, e.g.: r2k3r/p1q1b1p1/4b2p/nB1pp3/8/P2PB3/1PP2PPP/R3K2R b KQ - 0 2 After fixing this the issue should go away.