Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiraoka committed Oct 26, 2021
2 parents 62d5001 + 2bb8f9e commit b3c2f7d
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/main/java/featurecat/lizzie/rules/SGFParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -683,8 +684,30 @@ 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()));

BoardHistoryNode markerBeg = new BoardHistoryNode(null);
BoardHistoryNode markerEnd = new BoardHistoryNode(null);
Stack<BoardHistoryNode> 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--) {
if (hasBrothers) stack.push(markerEnd);
stack.push(cur.getVariations().get(i));
if (hasBrothers) stack.push(markerBeg);
}
}
}
// close file
builder.append(')');
writer.append(builder.toString());
Expand Down Expand Up @@ -729,19 +752,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();
Expand Down

0 comments on commit b3c2f7d

Please sign in to comment.