File: fen2ascii.py

package info (click to toggle)
lyx 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 138,444 kB
  • sloc: cpp: 244,268; ansic: 106,398; xml: 72,791; python: 39,384; sh: 7,666; makefile: 6,584; pascal: 2,143; perl: 2,101; objc: 1,084; tcl: 163; sed: 16
file content (39 lines) | stat: -rw-r--r-- 959 bytes parent folder | download
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
# file fen2ascii.py
# This file is part of LyX, the document processor.
# Licence details can be found in the file COPYING.

# author Kayvan A. Sylvan

# Full author contact details are available in file CREDITS.

# This script will convert a chess position in the FEN
# format to an ascii representation of the position.

import sys

fout = open(sys.argv[2], "w")

placement = open(sys.argv[1]).readline().split()[0]
row_codes = placement.split("/")

cont = 1
MARGIN = " " * 6
RULE = f"+{'-' * 15}+"

print(f"{MARGIN}   {RULE}", file=fout)
for i in range(8):
    cont += 1
    row = ""
    for p in row_codes[i]:
        if "1" <= p <= "8":
            for k in range(int(p)):
                cont += 1
                row += "| " if (cont % 2) else "|*"
        else:
            row += "|" + p
            cont += 1

    print(f"{MARGIN} {8 - i} {row}|", file=fout)

print(f"{MARGIN}   {RULE}", file=fout)
print(f"{MARGIN}    a b c d e f g h ", file=fout)