-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_data.py
88 lines (73 loc) · 2.27 KB
/
parse_data.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
import os
import chess.pgn
import numpy as np
def get_bitboard(board):
'''
params
------
board : chess.pgn board object
board to get state from
returns
-------
bitboard representation of the state of the game
64 * 6 + 5 dim binary numpy vector
64 squares, 6 pieces, '1' indicates the piece is at a square
5 extra dimensions for castling rights queenside/kingside and whose turn
'''
bitboard = np.zeros(64*6*2 + 5)
'''
p: pawn
n: knight
b: bishop
r: rook
q: queen
k: king
'''
piece_idx = {'p': 0, 'n': 1, 'b': 2, 'r': 3, 'q': 4, 'k': 5}
for i in range(64):
if board.piece_at(i):
# White: return True
# Black: return False
color = int(board.piece_at(i).color) + 1
bitboard[(piece_idx[board.piece_at(i).symbol().lower()] + i * 6) * color] = 1
bitboard[-1] = int(board.turn)
bitboard[-2] = int(board.has_kingside_castling_rights(chess.WHITE))
bitboard[-3] = int(board.has_kingside_castling_rights(chess.BLACK))
bitboard[-4] = int(board.has_queenside_castling_rights(chess.WHITE))
bitboard[-5] = int(board.has_queenside_castling_rights(chess.BLACK))
return bitboard
def get_result(game):
result = game.headers['Result']
result = result.split('-')
if result[0] == '1':
return 1
elif result[0] == '0':
return -1
else:
return 0
games = open('./data/CCRL-4040.[1187224].pgn')
if not os.path.isdir('./data/bitboard'):
os.mkdir('./data/bitboard')
if not os.path.isdir('./data/label'):
os.mkdir('./data/label')
num_games = 0
while (num_games<40000):
bitboards = []
labels = []
for i in range(4000):
num_games += 1
game = chess.pgn.read_game(games)
result = get_result(game)
board = game.board()
for move in game.main_line():
# for move in game.mainline_moves():
board.push(move)
bitboard = get_bitboard(board)
bitboards.append(bitboard)
labels.append(result)
bitboards = np.array(bitboards)
labels = np.array(labels)
np.save('./data/bitboard/bitboards_' + str(num_games) + '.npy', bitboards)
np.save('./data/label/labels_' + str(num_games) + '.npy', labels)
print('Done parse!')
print('Done save data to numpy file!')