bhlangonijr / chesslib

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

Using toSanArray fails when board is loaded from FEN #97

Closed dlbbld closed 2 years ago

dlbbld commented 2 years ago

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;
}
dcolazin commented 2 years ago

Hi, why did you close the issue?