Skip to content

Commit 1970aa3

Browse files
committed
PGN
1 parent 50fa964 commit 1970aa3

3 files changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ru.base.game.server.dto.chess;
2+
3+
import java.io.IOException;
4+
5+
public abstract class Main {
6+
public static void main(String[] args) throws IOException {
7+
PortableGameNotation notation =
8+
new PortableGameNotation.Default(Main.class.getResourceAsStream("/chess/1992.11.04.pgn"));
9+
System.out.println(notation);
10+
}
11+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package ru.base.game.server.dto.chess;
2+
3+
import com.google.common.io.CharStreams;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.InputStreamReader;
8+
import java.io.Reader;
9+
import java.nio.charset.StandardCharsets;
10+
import java.util.ArrayList;
11+
import java.util.HashMap;
12+
import java.util.List;
13+
import java.util.Map;
14+
import java.util.Optional;
15+
import java.util.regex.Matcher;
16+
import java.util.regex.Pattern;
17+
18+
public interface PortableGameNotation {
19+
20+
enum Kind {
21+
KING, ROOK, BISHOP, KNIGHT, PAWN
22+
}
23+
24+
enum Color {
25+
WHITE, BLACK
26+
}
27+
28+
interface Element {
29+
}
30+
31+
sealed interface Metadata extends Element {
32+
record Value(String name, String text) implements Metadata {
33+
34+
}
35+
}
36+
37+
sealed interface Move extends Element {
38+
enum Type {
39+
SIMPLE, CASTLING
40+
}
41+
42+
record Line(int index, Step white, Step black) implements Move {
43+
44+
}
45+
46+
record Step(Kind kind,
47+
Color color,
48+
Optional<Position> from,
49+
Optional<Position> to,
50+
boolean capturing,
51+
boolean checking,
52+
Type type) {
53+
54+
}
55+
}
56+
57+
record Position(int x, int y) {
58+
59+
}
60+
61+
final class Default implements PortableGameNotation {
62+
private static final Pattern GRU = Pattern.compile(
63+
"(\\[(\\w+)\\s+\\\"(.*)\\\"\\]|\\d+\\.((([BQKNR]?([a-h1-8]?)x?[a-h][1-8]\\+?)|O-O|O-O-O)\\s(\\{(.*)\\}\\s)?){2})");
64+
private final Map<String, Metadata> metadata = new HashMap<>();
65+
private final List<Move> moves = new ArrayList<>();
66+
67+
private Default(Reader reader) throws IOException {
68+
this(CharStreams.toString(reader));
69+
}
70+
71+
private Default(String text) throws IOException {
72+
parse(text);
73+
}
74+
75+
public Default(InputStream stream) throws IOException {
76+
this(new InputStreamReader(stream, StandardCharsets.UTF_8));
77+
}
78+
79+
private void parse(String text) {
80+
metadata.clear();
81+
moves.clear();
82+
Matcher matcher = GRU.matcher(text);
83+
matcher.results().forEach(result -> {
84+
int grouped = result.groupCount();
85+
if (grouped > 0) {
86+
String value = result.group(4);
87+
if (value == null) {
88+
metadata.put(result.group(2), new Metadata.Value(result.group(2), result.group(3)));
89+
} else {
90+
System.out.println("Found group '" + grouped + "'");
91+
for (int i = 0; i < grouped; i++) {
92+
System.out.printf("%d. %s%n", i, result.group(i));
93+
}
94+
}
95+
}
96+
});
97+
}
98+
}
99+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[Event "F/S Return Match"]
2+
[Site "Belgrade, Serbia JUG"]
3+
[Date "1992.11.04"]
4+
[Round "29"]
5+
[White "Fischer, Robert J."]
6+
[Black "Spassky, Boris V."]
7+
[Result "1/2-1/2"]
8+
9+
1.e4 e5 2.Nf3 Nc6 3.Bb5 {This opening is called the Ruy Lopez.} a6
10+
4.Ba4 Nf6 5.O-O Be7 6.Re1 b5 7.Bb3 d6 8.c3 O-O 9.h3 Nb8 10.d4 Nbd7
11+
11.c4 c6 12.cxb5 axb5 13.Nc3 Bb7 14.Bg5 b4 15.Nb1 h6 16.Bh4 c5 17.dxe5
12+
Nxe4 18.Bxe7 Qxe7 19.exd6 Qf6 20.Nbd2 Nxd6 21.Nc4 Nxc4 22.Bxc4 Nb6
13+
23.Ne5 Rae8 24.Bxf7+ Rxf7 25.Nxf7 Rxe1+ 26.Qxe1 Kxf7 27.Qe3 Qg5 28.Qxg5
14+
hxg5 29.b3 Ke6 30.a3 Kd6 31.axb4 cxb4 32.Ra5 Nd5 33.f3 Bc8 34.Kf2 Bf5
15+
35.Ra7 g6 36.Ra6+ Kc5 37.Ke1 Nf4 38.g3 Nxh3 39.Kd2 Kb5 40.Rd6 Kc5 41.Ra6
16+
Nf2 42.g4 Bd3 43.Re6 1/2-1/2

0 commit comments

Comments
 (0)