ChessPresso extract FEN

May 9, 2018 chess programming

Chesspresso is a great library for processing chess games stored in PGN format. One of the greatest uses I have found for chesspresso is using it to extract FENs of the current position on the board. I’ve used this for example to create a candidate move selector in a chess database application. Here is a an example usage to extract the FENs from a game PGN given as a String:

ByteArrayInputStream bis = new ByteArrayInputStream(singleGame.getBytes());
PGNReader pgnReader = new PGNReader(bis, "");
Game game;
game = pgnReader.parseGame();
if (game == null) return null;
game.gotoStart();
Move[] mainLine = game.getMainLine();
for (int i = 0; i < mainLine.length; i++) {
    Move move = mainLine[i];
    String fen = game.getPosition().getFEN();
    System.out.println(fen);
    game.getPosition().doMove(move);
}