-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
188 lines (151 loc) · 3.65 KB
/
Copy pathfunctions.py
File metadata and controls
188 lines (151 loc) · 3.65 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from constants import *
import pygame as pg
import numpy as np
from functools import reduce
from operator import or_
'''
Performs bit shifting on a bitboard
-------------------------------------
bb (int) : Bitboard to shift
amount (int) : How many places to shift by
Positive shifts right
Negative shifts left
'''
def shift(bb, amount):
if amount > 0:
return bb << amount
else:
return bb >> -amount
'''
Prints a bitboard as a chess board in terminal
------------------------------------------------
'''
def bbprint(bb):
# Creates string (length 64) from binary
binary_str = str(bin(bb))[2:]
binary_str = binary_str.zfill(64)
print('\nBitboard: {}\n'.format(binary_str))
# Resize the array into 8x8
char_array = np.array(list(binary_str))
char_array = np.reshape(char_array, (8, 8))
char_array = np.fliplr(char_array)
for i in range(8):
rank = '{}| '.format(8-i)
for j in range(8):
char = char_array[i,j]
if char == '0':
rank += '. '
else:
rank += '# '
print(rank)
# print(' | ')
print('-+-----------------')
print(' | a b c d e f g h ')
print('<=================>')
'''
Prints binary representation of number
with </> showing if positive or negative
------------------------------------------
'''
def binprint(bb):
if bb < 0:
print('<', format(((1<<64)-1) & bb,'#066b'))
else:
print('>', format(bb, '#066b'))
'''
Return Least/Most Significant Bit
-----------------------------------
'''
def find_LSB(n):
return int(np.log2(n & -n))
def find_MSB(n):
return int(np.log2(n))
'''
Return number of bits in Bitboard
-----------------------------------
'''
def count_bits(bb):
count = 0
while bb:
count += 1
bb &= bb-1
return count
'''
Finds nearest bit in a direction
----------------------------------
'''
def find_nth_piece(pieces, direction, n=1):
if pieces:
# Initialise no nearest bit
closest_bit = EMPTY
# Find smallest bit index if moving in positive direction
if direction in POSITIVE_DIRECTIONS:
find_closest_bit = find_LSB
# Otherwise fine largest bit index
else:
find_closest_bit = find_MSB
# While diving to depth 'n'
while n > 0:
# Get the closest bit
closest_bit = 1 << find_closest_bit(pieces)
# Remove from bitboard of pieces
pieces ^= closest_bit
# Iterate
n -= 1
return closest_bit
else:
return 0
'''
Import image as pygame image object
-------------------------------------
'''
def import_image(name):
filename = 'img/{}.png'.format(name)
img = pg.image.load(filename).convert_alpha()
img = pg.transform.smoothscale(img, (SQ_SIZE, SQ_SIZE))
return img
'''
Convert between 3 representations of position: bb, cn and xy
--------------------------------------------------------------
'''
def cn2bb(cn):
col = ord(cn[0]) - ord('a')
row = ord(cn[1]) - ord('1')
bb = shift(START, row*8 + col)
return bb
def cn2xy(cn):
col = ord(cn[0]) - ord('a')
row = ord(cn[1]) - ord('1')
x = col * SQ_SIZE
y = BOARD_HEIGHT - (row*SQ_SIZE) - SQ_SIZE
return(x,y)
def bb2cn(bb):
# Find the bit location, assuming it's a single bit bb
bb_bit = find_LSB(bb)
col = bb_bit % 8
row = bb_bit // 8
col += ord('a')
row += ord('1')
cn = chr(col)+chr(row)
return cn
def bb2xy(bb):
return cn2xy(bb2cn(bb))
def xy2cn(x, y):
col = x // SQ_SIZE
row = (BOARD_HEIGHT - y) // SQ_SIZE
col += ord('a')
row += ord('1')
cn = chr(col)+chr(row)
return cn
def xy2bb(x, y):
return cn2bb(xy2cn(x, y))
'''
Take in a list of single-bit bitboards and return a combined bitboard
-----------------------------------------------------------------------
'''
def bblist2bb(bblist):
# bb = EMPTY
# for bit in bblist:
# bb |= int(bit)
# return bb
return reduce(or_, bblist)