Implemented game serialization to the PGN format #16
@ -82,29 +82,31 @@ public class PGNGame {
|
|||||||
// Insert newline if tags were printed
|
// Insert newline if tags were printed
|
||||||
if (!tagPairs.isEmpty()) pw.println();
|
if (!tagPairs.isEmpty()) pw.println();
|
||||||
|
|
||||||
// Collect SAN moves
|
if (!board.getLog().isEmpty()) {
|
||||||
Board clone = new Board(board, true);
|
// Collect SAN moves
|
||||||
List<String> chunks = new ArrayList<>();
|
Board clone = new Board(board, true);
|
||||||
while (clone.getLog().hasParent()) {
|
List<String> chunks = new ArrayList<>();
|
||||||
Move move = clone.getLog().getLast().move;
|
boolean flag = true;
|
||||||
clone.revert();
|
while (flag) {
|
||||||
String chunk = clone.getLog().getLast().activeColor == Color.WHITE ? String.format(" %d. ", clone.getLog().getLast().fullmoveCounter)
|
Move move = clone.getLog().getLast().move;
|
||||||
: " ";
|
flag = clone.getLog().hasParent();
|
||||||
chunk += move.toSAN(clone);
|
clone.revert();
|
||||||
chunks.add(chunk);
|
String chunk = clone.getLog().getActiveColor() == Color.WHITE ? String.format(" %d. ", clone.getLog().getFullmoveNumber()) : " ";
|
||||||
}
|
chunk += move.toSAN(clone);
|
||||||
Collections.reverse(chunks);
|
chunks.add(chunk);
|
||||||
|
|
||||||
// Write movetext
|
|
||||||
String line = "";
|
|
||||||
for (String chunk : chunks)
|
|
||||||
if (line.length() + chunk.length() <= 80) line += chunk;
|
|
||||||
else {
|
|
||||||
// Flush line
|
|
||||||
pw.println(line);
|
|
||||||
line = "";
|
|
||||||
}
|
}
|
||||||
|
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
|
// Write game termination marker
|
||||||
pw.print(tagPairs.get("Result"));
|
pw.print(tagPairs.get("Result"));
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user