From d6b5442ee91c8595c66b91c56c86718cbfaf4f26 Mon Sep 17 00:00:00 2001 From: kske Date: Thu, 7 Nov 2019 05:53:28 +0100 Subject: [PATCH] Added empty save methods to PGNDatabase and PGNGame --- src/dev/kske/chess/board/Log.java | 6 ++++++ src/dev/kske/chess/pgn/PGNDatabase.java | 19 ++++++++++++------- src/dev/kske/chess/pgn/PGNGame.java | 16 ++++++++++++++++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/dev/kske/chess/board/Log.java b/src/dev/kske/chess/board/Log.java index 9d60002..d31f5de 100644 --- a/src/dev/kske/chess/board/Log.java +++ b/src/dev/kske/chess/board/Log.java @@ -109,8 +109,14 @@ public class Log implements Iterable { } else reset(); } + /** + * @return {@code true} if the root node exists + */ public boolean isEmpty() { return root == null; } + /** + * @return {@code true} if the current node has a parent node + */ public boolean hasParent() { return !isEmpty() && current.hasParent(); } /** diff --git a/src/dev/kske/chess/pgn/PGNDatabase.java b/src/dev/kske/chess/pgn/PGNDatabase.java index c29598e..e00a4ad 100644 --- a/src/dev/kske/chess/pgn/PGNDatabase.java +++ b/src/dev/kske/chess/pgn/PGNDatabase.java @@ -2,6 +2,7 @@ package dev.kske.chess.pgn; import java.io.File; import java.io.FileNotFoundException; +import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Scanner; @@ -18,15 +19,19 @@ import dev.kske.chess.exception.ChessException; */ public class PGNDatabase { - private final List games = new ArrayList<>(); + private final List games = new ArrayList<>(); public void load(File pgnFile) throws FileNotFoundException, ChessException { - try (Scanner sc = new Scanner(pgnFile)) { - while (sc.hasNext()) - games.add(PGNGame.parse(sc)); - } catch (FileNotFoundException | ChessException e) { - throw e; - } + Scanner sc = new Scanner(pgnFile); + while (sc.hasNext()) + games.add(PGNGame.parse(sc)); + sc.close(); + } + + public void save(File pgnFile) throws FileNotFoundException { + PrintWriter pw = new PrintWriter(pgnFile); + games.forEach(g -> g.writePGN(pw)); + pw.close(); } public List getGames() { return games; } diff --git a/src/dev/kske/chess/pgn/PGNGame.java b/src/dev/kske/chess/pgn/PGNGame.java index 575fb75..e5c35f0 100644 --- a/src/dev/kske/chess/pgn/PGNGame.java +++ b/src/dev/kske/chess/pgn/PGNGame.java @@ -1,5 +1,6 @@ package dev.kske.chess.pgn; +import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import java.util.Scanner; @@ -8,6 +9,7 @@ import java.util.regex.Pattern; import dev.kske.chess.board.Board; import dev.kske.chess.board.FENString; +import dev.kske.chess.board.Piece.Color; import dev.kske.chess.exception.ChessException; /** @@ -62,6 +64,20 @@ public class PGNGame { return game; } + public void writePGN(PrintWriter pw) { + // Write tag pairs + tagPairs.forEach((k, v) -> pw.printf("[%s \"%s\"]%n", k, v)); + + // Write movetext + board.getLog().forEach(m -> { + if (m.activeColor == Color.BLACK) pw.printf("%d. ", m.fullmoveCounter); + pw.print(m.move); // TODO: Convert to SAN + }); + + // TODO: Write game termination marker + // TODO: Check if the game has ended + } + public String getTag(String tagName) { return tagPairs.get(tagName); } public boolean hasTag(String tagName) { return tagPairs.containsKey(tagName); }