Implemented game serialization to the PGN format #16
@ -82,14 +82,16 @@ 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();
|
||||||
|
|
||||||
|
if (!board.getLog().isEmpty()) {
|
||||||
// Collect SAN moves
|
// Collect SAN moves
|
||||||
Board clone = new Board(board, true);
|
Board clone = new Board(board, true);
|
||||||
List<String> chunks = new ArrayList<>();
|
List<String> chunks = new ArrayList<>();
|
||||||
while (clone.getLog().hasParent()) {
|
boolean flag = true;
|
||||||
|
while (flag) {
|
||||||
Move move = clone.getLog().getLast().move;
|
Move move = clone.getLog().getLast().move;
|
||||||
|
flag = clone.getLog().hasParent();
|
||||||
clone.revert();
|
clone.revert();
|
||||||
String chunk = clone.getLog().getLast().activeColor == Color.WHITE ? String.format(" %d. ", clone.getLog().getLast().fullmoveCounter)
|
String chunk = clone.getLog().getActiveColor() == Color.WHITE ? String.format(" %d. ", clone.getLog().getFullmoveNumber()) : " ";
|
||||||
: " ";
|
|
||||||
chunk += move.toSAN(clone);
|
chunk += move.toSAN(clone);
|
||||||
chunks.add(chunk);
|
chunks.add(chunk);
|
||||||
}
|
}
|
||||||
@ -100,11 +102,11 @@ public class PGNGame {
|
|||||||
for (String chunk : chunks)
|
for (String chunk : chunks)
|
||||||
if (line.length() + chunk.length() <= 80) line += chunk;
|
if (line.length() + chunk.length() <= 80) line += chunk;
|
||||||
else {
|
else {
|
||||||
// Flush line
|
|
||||||
pw.println(line);
|
pw.println(line);
|
||||||
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