-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.py
More file actions
executable file
·65 lines (50 loc) · 1.77 KB
/
state.py
File metadata and controls
executable file
·65 lines (50 loc) · 1.77 KB
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
#!/usr/bin/env python3
import chess
import numpy as np
class State(object):
def __init__(self, board=None):
if board is None:
self.board = chess.Board()
else:
self.board = board
def edges(self):
return list(self.board.legal_moves)
def serialize(self):
assert self.board.is_valid()
bstate = np.zeros(64, np.uint8)
for i in range(64):
pp = self.board.piece_at(i)
if pp is not None:
bstate[i] = {'P':1, 'N':2, 'B':3, 'R':4, 'Q':5, 'K':6, \
'p':9, 'n':10, 'b':11, 'r':12, 'q':13, 'k':14}[pp.symbol()]
if self.board.has_queenside_castling_rights(chess.WHITE):
assert bstate[0] == 4
bstate[0] = 7
if self.board.has_kingside_castling_rights(chess.WHITE):
assert bstate[7] == 4
bstate[7] = 7
if self.board.has_queenside_castling_rights(chess.BLACK):
assert bstate[56] == 8+4
bstate[56] = 8+7
if self.board.has_kingside_castling_rights(chess.BLACK):
assert bstate[63] == 8+4
bstate[63] = 8+7
if self.board.ep_square is not None:
assert bstate[self.board.ep_square] == 0
bstate[self.board.ep_square] = 8
bstate = bstate.reshape(8, 8)
#binary state
state = np.zeros((5,8,8), np.uint8)
# 0-3 cols to binary
state[0] = (bstate>>3)&1
state[1] = (bstate>>2)&1
state[2] = (bstate>>1)&1
state[3] = (bstate>>0)&1
# 4th col is who's turn is it
state[4] = (self.board.turn*1.0)
return state
def shredder_fen_to_vec(x):
pass
if __name__=='__main__':
s = State()
###print(s.edges())