"""Videodaki tahta sahneleri için FEN + hamle (uci) kaydı; PGN'den üretilir -> tahtalar.json (render bu dosyayı okur)."""
import json, chess, chess.pgn
gs = {}
f = open('kasparov-deepblue-1997.pgn')
while (g := chess.pgn.read_game(f)):
    gs[int(g.headers['Round'])] = g
def before(g, no, color):
    b = g.board()
    for m in g.mainline_moves():
        if b.fullmove_number == no and b.turn == color:
            return b, m
        b.push(m)
out = {}
for key, (r, no, col, alts) in {'oyun1_44': (1, 44, chess.BLACK, []), 'oyun2_36': (2, 36, chess.WHITE, ['Qb6']), 'oyun2_45': (2, 45, chess.WHITE, [])}.items():
    b, m = before(gs[r], no, col)
    out[key] = {'fen': b.fen(), 'oynanan_san': b.san(m), 'oynanan_uci': m.uci(), 'alternatif': {a: b.parse_san(a).uci() for a in alts}}
b2 = gs[2].end().board(); out['oyun2_son'] = {'fen': b2.fen(), 'son_hamle': gs[2].end().move.uci(), 'Qe3': b2.parse_san('Qe3').uci()}
b6 = gs[6].end().board(); out['oyun6_son'] = {'fen': b6.fen(), 'son_hamle': gs[6].end().move.uci(), 'son_san': gs[6].end().san(), 'yarim_hamle': len(list(gs[6].mainline_moves()))}
json.dump(out, open('tahtalar.json', 'w'), ensure_ascii=False, indent=1); print(json.dumps(out, indent=1))
