-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfen2deck.py
123 lines (102 loc) · 2.46 KB
/
fen2deck.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
# Convert FEN notation into a deck that initializes the board
# Reads and writes from stdio
#
import sys
# side, piece representation for this test code.
WHITE = 0
BLACK = 1
# piece encoding for add_piece
PAWN = 1
KNIGHT = 2
BISHOP = 3
QUEEN = 4
ROOK = 5
KING = 6
# piece encoding in memory
OTHER = 1
WPAWN = 2
WKNIGHT = 3
WBISHOP = 4
WQUEEN = 5
BPAWN = 6
BKNIGHT = 7
BBISHOP = 8
BQUEEN = 9
# addresses of separately encoded pieces in memory_layout.asm
WKING = 32
BKING = 33
WROOK = 34
WROOK2 = 35
# fromp in memory_layout.asm, used to indicate current player
FROMP = 36
# material score difference between white and black (50 is no difference)
MSCORE = 37
# we print the memory when we are done
memory = [0]*75
# player = WHITE,BLACK, piece=PAWN,KNIGHT,..., rank and file are 0..7
def add_piece(player, piece, rank, file):
global memory
loc0 = rank*8 + file
loc = loc0 // 2
if piece<=QUEEN:
# pawn, knight, bishop, queen are stored directly
digit = piece+WPAWN-PAWN if player==WHITE else piece+BPAWN-PAWN
else:
# king and rook encoded as other, their positions stored after board
digit = OTHER
square = (1+rank)*10+(1+file)
if piece==KING:
memory[WKING + player]=square
else:
# piece == ROOK, but we only store positions of white rooks
if player==WHITE:
if memory[WROOK]==0:
memory[WROOK]=square
else:
memory[WROOK2]=square
if loc0%2:
memory[loc] += digit
else:
memory[loc] += digit*10
def read_fen(fen):
global memory
fields = fen.split()
ranks = fields[0].split('/')
for rank in range(0,8):
s = ranks[7-rank] # fen reverses rank order
# print(s)
file = 0
while s != '':
c = s[0]
s = s[1:]
if c.isdigit():
file += int(c)
else:
piece = 'PNBQRKpnbqrk'.index(c)
if piece >= 6:
player = BLACK
piece -= 6
else:
player = WHITE
add_piece(player, piece+PAWN, rank, file)
file += 1
if fields[1] == 'b':
memory[FROMP] = BLACK*10
memory[45] = 99 # best_score
# write one card for each nonzero word in memory, just the way load_board.asm likes it
def print_deck():
global memory
out = ''
for (a,d) in enumerate(memory):
if d!=0:
out += f'{a:02}{d:02}0' + 75*' ' + '\n'
out += '99000' + 75*' ' + '\n'
return out.strip()
read_fen(sys.stdin.read().rstrip())
# initialize starting material score to balanced
memory[MSCORE] = 50
memory[38] = 1 # initial stack depth
memory[69] = 99 # beta
# set up appropriately for black or white play
print(print_deck())