-
Notifications
You must be signed in to change notification settings - Fork 5
/
json2epd.py
81 lines (65 loc) · 2.47 KB
/
json2epd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
""" Generates EPD positions from JSON games file saved from pychess.org """
import argparse
import os
import sys
import json
from tqdm import tqdm
import pyffish as sf
GRANDS = ("xiangqi", "manchu", "grand", "grandhouse", "shako", "janggi")
def zero2grand(move):
if move[1] == "@":
return "%s@%s%s" % (move[0], move[2], int(move[3:]) + 1)
return "%s%s%s%s%s" % (
move[0],
int(move[1]) + 1,
move[2],
int(move[3]) + 1,
move[4] if len(move) == 5 else "",
)
def generate_fens(json_file, stream, variant, count):
if variant not in sf.variants():
raise Exception("Unsupported variant: {}".format(variant))
show_promoted = variant in ("makruk", "makpong", "cambodian")
sfen = False
with open(json_file, "r") as f:
games = json.load(f)
cnt = 0
for game in tqdm(games):
if game["variant"] != variant:
continue
try:
moves = game["moves"]
if variant in GRANDS:
moves = list(map(zero2grand, moves))
_id = game["id"]
is960 = game["is960"] == 1
if game["fen"]:
start_fen = game["fen"]
else:
start_fen = sf.start_fen(variant)
for i in range(1, len(moves)):
fen = sf.get_fen(variant, start_fen, moves[:i], is960, sfen, show_promoted)
stream.write(
"{};variant {};site https://www.pychess.org/{}{}".format(
fen, variant, _id, os.linesep
)
)
cnt += 1
if cnt >= count:
break
except SystemError:
# Possible an old game saved in USI format
continue
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input-file", help="json file containing pychess games")
parser.add_argument(
"-v", "--variant", default="chess", help="variant to generate positions for"
)
parser.add_argument(
"-p", "--variant-path", default="", help="custom variants definition file path"
)
parser.add_argument("-c", "--count", type=int, default=1000, help="number of games")
args = parser.parse_args()
sf.set_option("VariantPath", args.variant_path)
generate_fens(args.input_file, sys.stdout, args.variant, args.count)