From ac1c8577955e22726946c82e97a6efc5d8bebee1 Mon Sep 17 00:00:00 2001 From: kske Date: Tue, 10 Dec 2019 21:27:06 +0100 Subject: [PATCH] Fixed fullmove counters in PGN export --- src/dev/kske/chess/pgn/PGNGame.java | 44 +++++++++++++++-------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/dev/kske/chess/pgn/PGNGame.java b/src/dev/kske/chess/pgn/PGNGame.java index 8a86f04..2d91930 100644 --- a/src/dev/kske/chess/pgn/PGNGame.java +++ b/src/dev/kske/chess/pgn/PGNGame.java @@ -82,29 +82,31 @@ public class PGNGame { // Insert newline if tags were printed if (!tagPairs.isEmpty()) pw.println(); - // Collect SAN moves - Board clone = new Board(board, true); - List chunks = new ArrayList<>(); - while (clone.getLog().hasParent()) { - Move move = clone.getLog().getLast().move; - clone.revert(); - String chunk = clone.getLog().getLast().activeColor == Color.WHITE ? String.format(" %d. ", clone.getLog().getLast().fullmoveCounter) - : " "; - chunk += move.toSAN(clone); - chunks.add(chunk); - } - Collections.reverse(chunks); - - // Write movetext - String line = ""; - for (String chunk : chunks) - if (line.length() + chunk.length() <= 80) line += chunk; - else { - // Flush line - pw.println(line); - line = ""; + if (!board.getLog().isEmpty()) { + // Collect SAN moves + Board clone = new Board(board, true); + List chunks = new ArrayList<>(); + boolean flag = true; + while (flag) { + Move move = clone.getLog().getLast().move; + flag = clone.getLog().hasParent(); + clone.revert(); + String chunk = clone.getLog().getActiveColor() == Color.WHITE ? String.format(" %d. ", clone.getLog().getFullmoveNumber()) : " "; + chunk += move.toSAN(clone); + chunks.add(chunk); } + Collections.reverse(chunks); + // Write movetext + String line = ""; + for (String chunk : chunks) + if (line.length() + chunk.length() <= 80) line += chunk; + else { + pw.println(line); + line = ""; + } + if (!line.isEmpty()) pw.println(line); + } // Write game termination marker pw.print(tagPairs.get("Result")); }