2019-10-09 21:03:39 +02:00
|
|
|
package dev.kske.chess.pgn;
|
|
|
|
|
2019-11-07 05:53:28 +01:00
|
|
|
import java.io.PrintWriter;
|
2019-10-09 21:03:39 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Scanner;
|
|
|
|
import java.util.regex.MatchResult;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
import dev.kske.chess.board.Board;
|
2019-10-25 11:52:48 +02:00
|
|
|
import dev.kske.chess.board.FENString;
|
2019-11-07 05:53:28 +01:00
|
|
|
import dev.kske.chess.board.Piece.Color;
|
2019-10-09 21:03:39 +02:00
|
|
|
import dev.kske.chess.exception.ChessException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Project: <strong>Chess</strong><br>
|
|
|
|
* File: <strong>PGNGame.java</strong><br>
|
|
|
|
* Created: <strong>22 Sep 2019</strong><br>
|
2019-10-26 07:55:21 +02:00
|
|
|
*
|
|
|
|
* @since Chess v0.5-alpha
|
|
|
|
* @author Kai S. K. Engelbart
|
2019-10-09 21:03:39 +02:00
|
|
|
*/
|
|
|
|
public class PGNGame {
|
|
|
|
|
|
|
|
private final Map<String, String> tagPairs = new HashMap<>(7);
|
2019-11-08 15:22:12 +01:00
|
|
|
private final Board board;
|
|
|
|
|
|
|
|
public PGNGame() { board = new Board(); }
|
|
|
|
|
|
|
|
public PGNGame(Board board) { this.board = board; }
|
2019-10-09 21:03:39 +02:00
|
|
|
|
|
|
|
public static PGNGame parse(Scanner sc) throws ChessException {
|
|
|
|
PGNGame game = new PGNGame();
|
|
|
|
|
|
|
|
MatchResult matchResult;
|
|
|
|
Pattern tagPairPattern = Pattern.compile("\\[(\\w+) \"(.*)\"]"),
|
2019-10-20 08:52:51 +02:00
|
|
|
movePattern = Pattern.compile("\\d+\\.\\s+(?:(?:(\\S+)\\s+(\\S+))|(?:O-O-O)|(?:O-O))(?:\\+{0,2}|\\#)"),
|
2019-10-25 11:52:48 +02:00
|
|
|
nagPattern = Pattern.compile("(\\$\\d{1,3})*"), terminationMarkerPattern = Pattern.compile("1-0|0-1|1\\/2-1\\/2|\\*");
|
2019-10-09 21:03:39 +02:00
|
|
|
|
|
|
|
// Parse tag pairs
|
2019-10-20 08:52:51 +02:00
|
|
|
while (sc.findInLine(tagPairPattern) != null) {
|
|
|
|
matchResult = sc.match();
|
|
|
|
if (matchResult.groupCount() == 2) game.setTag(matchResult.group(1), matchResult.group(2));
|
|
|
|
else break;
|
2019-10-09 21:03:39 +02:00
|
|
|
sc.nextLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse movetext
|
|
|
|
while (true) {
|
|
|
|
// Skip NAG (Numeric Annotation Glyph)
|
|
|
|
sc.skip(nagPattern);
|
|
|
|
|
|
|
|
// TODO: Parse RAV (Recursive Annotation Variation)
|
|
|
|
|
2019-10-20 08:52:51 +02:00
|
|
|
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));
|
2019-10-25 11:52:48 +02:00
|
|
|
System.out.println(game.getBoard().getLog().getLast().move.toLAN() + ": " + new FENString(game.board).toString());
|
2019-10-20 08:52:51 +02:00
|
|
|
}
|
|
|
|
else break;
|
|
|
|
} else break;
|
2019-10-09 21:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse game termination marker
|
2019-10-25 11:52:48 +02:00
|
|
|
if (sc.findWithinHorizon(terminationMarkerPattern, 20) == null) System.err.println("Termination marker expected");
|
2019-10-09 21:03:39 +02:00
|
|
|
|
|
|
|
return game;
|
|
|
|
}
|
|
|
|
|
2019-11-07 05:53:28 +01:00
|
|
|
public void writePGN(PrintWriter pw) {
|
2019-11-08 15:22:12 +01:00
|
|
|
// Set the unknown result tag if no result tag is specified
|
|
|
|
tagPairs.putIfAbsent("Result", "*");
|
|
|
|
|
2019-11-07 05:53:28 +01:00
|
|
|
// Write tag pairs
|
|
|
|
tagPairs.forEach((k, v) -> pw.printf("[%s \"%s\"]%n", k, v));
|
|
|
|
|
2019-11-08 15:22:12 +01:00
|
|
|
// Insert newline if tags were printed
|
|
|
|
if (!tagPairs.isEmpty()) pw.println();
|
|
|
|
|
2019-11-07 05:53:28 +01:00
|
|
|
// Write movetext
|
|
|
|
board.getLog().forEach(m -> {
|
|
|
|
if (m.activeColor == Color.BLACK) pw.printf("%d. ", m.fullmoveCounter);
|
2019-11-08 15:22:12 +01:00
|
|
|
pw.printf("%s ", m.move); // TODO: Convert to SAN
|
2019-11-07 05:53:28 +01:00
|
|
|
});
|
|
|
|
|
2019-11-08 15:22:12 +01:00
|
|
|
// Write game termination marker
|
|
|
|
pw.print(tagPairs.get("Result"));
|
2019-11-07 05:53:28 +01:00
|
|
|
}
|
|
|
|
|
2019-10-25 11:52:48 +02:00
|
|
|
public String getTag(String tagName) { return tagPairs.get(tagName); }
|
2019-10-09 21:03:39 +02:00
|
|
|
|
2019-10-25 11:52:48 +02:00
|
|
|
public boolean hasTag(String tagName) { return tagPairs.containsKey(tagName); }
|
2019-10-09 21:03:39 +02:00
|
|
|
|
2019-10-25 11:52:48 +02:00
|
|
|
public void setTag(String tagName, String tagValue) { tagPairs.put(tagName, tagValue); }
|
2019-10-14 06:31:03 +02:00
|
|
|
|
|
|
|
public Board getBoard() { return board; }
|
2019-10-09 21:03:39 +02:00
|
|
|
}
|