This repository has been archived on 2021-02-18. You can view files and clone it, but cannot push or open issues or pull requests.
chess/src/dev/kske/chess/pgn/PGNGame.java

115 lines
3.5 KiB
Java

package dev.kske.chess.pgn;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import dev.kske.chess.board.Board;
import dev.kske.chess.board.FENString;
import dev.kske.chess.board.Move;
import dev.kske.chess.exception.ChessException;
/**
* Project: <strong>Chess</strong><br>
* File: <strong>PGNGame.java</strong><br>
* Created: <strong>22 Sep 2019</strong><br>
*
* @since Chess v0.5-alpha
* @author Kai S. K. Engelbart
*/
public class PGNGame {
private final Map<String, String> tagPairs = new HashMap<>(7);
private final Board board;
public PGNGame() { board = new Board(); }
public PGNGame(Board board) { this.board = board; }
public static PGNGame parse(Scanner sc) throws ChessException {
PGNGame game = new PGNGame();
MatchResult matchResult;
Pattern tagPairPattern = Pattern.compile("\\[(\\w+) \"(.*)\"]"),
movePattern = Pattern.compile("\\d+\\.\\s+(?:(?:(\\S+)\\s+(\\S+))|(?:O-O-O)|(?:O-O))(?:\\+{0,2}|\\#)"),
nagPattern = Pattern.compile("(\\$\\d{1,3})*"), terminationMarkerPattern = Pattern.compile("1-0|0-1|1\\/2-1\\/2|\\*");
// Parse tag pairs
while (sc.findInLine(tagPairPattern) != null) {
matchResult = sc.match();
if (matchResult.groupCount() == 2) game.setTag(matchResult.group(1), matchResult.group(2));
else break;
sc.nextLine();
}
// Parse movetext
while (true) {
// Skip NAG (Numeric Annotation Glyph)
sc.skip(nagPattern);
// TODO: Parse RAV (Recursive Annotation Variation)
if (sc.findWithinHorizon(movePattern, 20) != null) {
matchResult = sc.match();
if (matchResult.groupCount() > 0) for (int i = 1; i < matchResult.groupCount() + 1; i++) {
game.board.move(matchResult.group(i));
System.out.println(game.getBoard().getLog().getLast().move.toLAN() + ": " + new FENString(game.board).toString());
}
else break;
} else break;
}
// Parse game termination marker
if (sc.findWithinHorizon(terminationMarkerPattern, 20) == null) System.err.println("Termination marker expected");
return game;
}
public void writePGN(PrintWriter pw) {
// Set the unknown result tag if no result tag is specified
tagPairs.putIfAbsent("Result", "*");
// Write tag pairs
tagPairs.forEach((k, v) -> pw.printf("[%s \"%s\"]%n", k, v));
// Insert newline if tags were printed
if (!tagPairs.isEmpty()) pw.println();
// Collect SAN moves
Board clone = new Board(board, true);
List<String> sanMoves = new ArrayList<>();
while (clone.getLog().hasParent()) {
Move move = clone.getLog().getLast().move;
clone.revert();
sanMoves.add(move.toSAN(clone));
}
// Write movetext
for (int i = sanMoves.size() - 1; i >= 0; i--)
pw.printf("%s ", sanMoves.get(i));
// Write movetext
// board.getLog().forEach(m -> {
// if (m.activeColor == Color.BLACK) pw.printf("%d. ", m.fullmoveCounter);
// pw.printf("%s ", m.move.toSAN(board));
// });
// Write game termination marker
pw.print(tagPairs.get("Result"));
}
public String getTag(String tagName) { return tagPairs.get(tagName); }
public boolean hasTag(String tagName) { return tagPairs.containsKey(tagName); }
public void setTag(String tagName, String tagValue) { tagPairs.put(tagName, tagValue); }
public Board getBoard() { return board; }
}