From b7f030b1748affa4d36bfcb3c8dedca7ee0b653b Mon Sep 17 00:00:00 2001 From: yzyray <348533481@qq.com> Date: Mon, 13 Sep 2021 10:46:50 +0800 Subject: [PATCH 1/2] Avoid use recursion --- .../featurecat/lizzie/rules/SGFParser.java | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/main/java/featurecat/lizzie/rules/SGFParser.java b/src/main/java/featurecat/lizzie/rules/SGFParser.java index 3cc146e50..89a82e3e5 100644 --- a/src/main/java/featurecat/lizzie/rules/SGFParser.java +++ b/src/main/java/featurecat/lizzie/rules/SGFParser.java @@ -19,6 +19,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; +import java.util.Stack; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -652,8 +653,16 @@ private static void saveToStream(Board board, Writer writer) throws IOException // * with 'xy' = coordinates ; or 'tt' for pass. // Write variation tree - builder.append(generateNode(board, history.getCurrentHistoryNode())); - + Stack stack = new Stack<>(); + stack.push(history.getCurrentHistoryNode()); + while (!stack.isEmpty()) { + BoardHistoryNode cur = stack.pop(); + builder.append(generateNode(board, cur)); + if (cur.numberOfChildren() >= 1) { + for (int i = cur.numberOfChildren() - 1; i >= 0; i--) + stack.push(cur.getVariations().get(i)); + } + } // close file builder.append(')'); writer.append(builder.toString()); @@ -698,19 +707,6 @@ private static String generateNode(Board board, BoardHistoryNode node) throws IO builder.append(String.format("LZ[%s]", formatNodeData(node))); } } - - if (node.numberOfChildren() > 1) { - // Variation - for (BoardHistoryNode sub : node.getVariations()) { - builder.append("("); - builder.append(generateNode(board, sub)); - builder.append(")"); - } - } else if (node.numberOfChildren() == 1) { - builder.append(generateNode(board, node.next().orElse(null))); - } else { - return builder.toString(); - } } return builder.toString(); From 2bb8f9e431681ee86cca4c0cd3178b6692a4a3bb Mon Sep 17 00:00:00 2001 From: yzyray <348533481@qq.com> Date: Tue, 21 Sep 2021 11:07:31 +0800 Subject: [PATCH 2/2] Fix missing '(' and ')' --- .../java/featurecat/lizzie/rules/SGFParser.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/featurecat/lizzie/rules/SGFParser.java b/src/main/java/featurecat/lizzie/rules/SGFParser.java index 89a82e3e5..41070ccd7 100644 --- a/src/main/java/featurecat/lizzie/rules/SGFParser.java +++ b/src/main/java/featurecat/lizzie/rules/SGFParser.java @@ -653,14 +653,28 @@ private static void saveToStream(Board board, Writer writer) throws IOException // * with 'xy' = coordinates ; or 'tt' for pass. // Write variation tree + BoardHistoryNode markerBeg = new BoardHistoryNode(null); + BoardHistoryNode markerEnd = new BoardHistoryNode(null); Stack stack = new Stack<>(); stack.push(history.getCurrentHistoryNode()); while (!stack.isEmpty()) { BoardHistoryNode cur = stack.pop(); + if (cur == markerBeg) { + builder.append('('); + continue; + } + if (cur == markerEnd) { + builder.append(')'); + continue; + } builder.append(generateNode(board, cur)); + boolean hasBrothers = (cur.numberOfChildren() > 1); if (cur.numberOfChildren() >= 1) { - for (int i = cur.numberOfChildren() - 1; i >= 0; i--) + for (int i = cur.numberOfChildren() - 1; i >= 0; i--) { + if (hasBrothers) stack.push(markerEnd); stack.push(cur.getVariations().get(i)); + if (hasBrothers) stack.push(markerBeg); + } } } // close file