-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
executable file
·59 lines (42 loc) · 1.32 KB
/
util.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
# coding: utf-8
import functools
import operator
import numpy as np
from cchess import *
RED = 1
BLACK = -1
EMPTY = 0
X_SIZE, Y_SIZE, PIECE_SIZE = 9, 10, 7
BOARD_SIZE = Y_SIZE * X_SIZE
IMAGE_SIZE = (Y_SIZE, X_SIZE, PIECE_SIZE)
PIECE_TO_INDEX = {'P' : 0, 'R' : 1, 'N' : 2, 'B' : 3, 'A' : 4, 'K' : 5, 'C' : 6}
INDEX_TO_PIECE = {0 : 'P', 1 : 'R', 2 : 'N', 3 : 'B', 4 : 'A', 5 : 'K', 6 : 'C'}
def convert_bitboard_to_image(board):
im = np.zeros(IMAGE_SIZE)
for y in range(Y_SIZE):
for x in range(X_SIZE):
fench = board.get_fench(Pos(x, y))
if fench is None: continue
if fench.isupper():
im[y, x, PIECE_TO_INDEX[fench.upper()]] = RED
else:
im[y, x, PIECE_TO_INDEX[fench.upper()]] = BLACK
return im
def flip_image(im):
#return im[:, ::-1, :]
return im[::-1, :, :]
def flip_color(im):
indices_red = np.where(im == 1)
indices_black = np.where(im == -1)
im[indices_red] = -1
im[indices_black] = 1
return im
def flip_coord2d(pos):
return Pos(pos.x, Y_SIZE - 1 - pos.y)
def flatten_coord2d(pos):
return X_SIZE * pos.y + pos.x
#convenience functions for initializing weights and biases
def product(numbers):
return functools.reduce(operator.mul, numbers)
def score_to_coordinate(score):
return (score // X_SIZE, score % X_SIZE)