-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathepd_inverter.py
49 lines (36 loc) · 1.44 KB
/
epd_inverter.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 1 10:01:47 2023
@author: Shahin M. Shahin (peregrine)
Usage : python epd_inverter.py <INPUT_FILE.epd> <OUTPUT_FILE.epd>
Example : python epd_inverter.py endgames.epd endgames_black.epd
"""
import sys
def invert_case(char):
return char.upper() if char == char.lower() else char.lower()
def flip_FEN(FEN):
position, turn, castle, ep, *rest = FEN.split()
position = '/'.join(''.join(invert_case(char) for char in row) for row in reversed(position.split('/')))
turn = 'b' if turn == 'w' else 'w'
if castle != '-':
castle = ''.join(invert_case(char) for char in sorted(castle))
if ep != '-':
ep = list(ep)
ep[1] = '3' if ep[1] == '6' else '6'
ep = ''.join(ep)
return ' '.join([position, turn, castle, ep] + rest)
def convert_epd(input_file, output_file):
with open(input_file, 'r') as input_f:
fen_lines = input_f.readlines()
converted_fens = [flip_FEN(fen.strip()) for fen in fen_lines]
with open(output_file, 'w') as output_f:
for fen in converted_fens:
output_f.write(fen + '\n')
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python epd_inverter.py <INPUT_FILE.epd> <OUTPUT_FILE.epd>")
else:
input_file = sys.argv[1]
output_file = sys.argv[2]
convert_epd(input_file, output_file)