chesslib
chesslib copied to clipboard
Using toSanArray fails when board is loaded from FEN
The method toSanArray
allows getting the SAN for a played move. BTW, it would also be nice if there is a more convenient way to achieve this.
But the method only works if the board is created from the initial position. If created from FEN it throws a NullPointerException.
So as such, I see no way to get the SAN for a played move when the board was constructed with a FEN, which is a major use case.
@Test
void test() throws Exception {
final Board board = new Board();
board.doMove(new Move(Square.E2, Square.E4));
assertEquals("e4", calculateSan(board)); // works fine
final var fen = "8/8/3KP3/5P2/8/3p4/3kp3/8 w - - 0 100";
board.loadFromFen(fen);
board.doMove(new Move(Square.E6, Square.E5));
assertEquals("e5", calculateSan(board)); // throws NPE
}
private static String calculateSan(Board board) {
final MoveList moveList = new MoveList();
moveList.addAll(calculateMoveList(board));
final var sanArray = moveList.toSanArray();
return sanArray[sanArray.length - 1];
}
private static List<Move> calculateMoveList(Board board) {
final List<Move> result = new ArrayList<>();
for (final MoveBackup moveBackup : board.getBackup()) {
result.add(moveBackup.getMove());
}
return result;
}