From 739c51f26569d7fc5fc8b8a1870b801ee9831901 Mon Sep 17 00:00:00 2001 From: kske Date: Sun, 8 Dec 2019 22:15:29 +0100 Subject: [PATCH] Including fullmove counters in PGN export --- src/dev/kske/chess/pgn/PGNGame.java | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/dev/kske/chess/pgn/PGNGame.java b/src/dev/kske/chess/pgn/PGNGame.java index 9308b47..8a86f04 100644 --- a/src/dev/kske/chess/pgn/PGNGame.java +++ b/src/dev/kske/chess/pgn/PGNGame.java @@ -2,6 +2,7 @@ package dev.kske.chess.pgn; import java.io.PrintWriter; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -12,6 +13,7 @@ import java.util.regex.Pattern; import dev.kske.chess.board.Board; import dev.kske.chess.board.FENString; import dev.kske.chess.board.Move; +import dev.kske.chess.board.Piece.Color; import dev.kske.chess.exception.ChessException; /** @@ -81,24 +83,27 @@ public class PGNGame { if (!tagPairs.isEmpty()) pw.println(); // Collect SAN moves - Board clone = new Board(board, true); - List sanMoves = new ArrayList<>(); - + Board clone = new Board(board, true); + List chunks = new ArrayList<>(); while (clone.getLog().hasParent()) { Move move = clone.getLog().getLast().move; clone.revert(); - sanMoves.add(move.toSAN(clone)); + 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 - for (int i = sanMoves.size() - 1; i >= 0; i--) - pw.printf("%s ", sanMoves.get(i)); - - // Write movetext - // board.getLog().forEach(m -> { - // if (m.activeColor == Color.BLACK) pw.printf("%d. ", m.fullmoveCounter); - // pw.printf("%s ", m.move.toSAN(board)); - // }); + String line = ""; + for (String chunk : chunks) + if (line.length() + chunk.length() <= 80) line += chunk; + else { + // Flush line + pw.println(line); + line = ""; + } // Write game termination marker pw.print(tagPairs.get("Result"));