-
Notifications
You must be signed in to change notification settings - Fork 0
/
moves.py
472 lines (352 loc) · 18.3 KB
/
moves.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import sys
from attack_tables import is_square_attacked, get_attacks, pawn_attacks
from constants import *
from bb_operations import *
from position import Position, generate_hash_key
"""
Binary move bits Meaning Hexadecimal
0000 0000 0000 0000 0011 1111 source square 0x3f
0000 0000 0000 1111 1100 0000 target square 0xfc0
0000 0000 0111 0000 0000 0000 piece 0x7000
0000 0000 1000 0000 0000 0000 side 0x8000
0000 1111 0000 0000 0000 0000 promoted piece 0xf0000
0001 0000 0000 0000 0000 0000 capture flag 0x100000
0010 0000 0000 0000 0000 0000 double push flag 0x200000
0100 0000 0000 0000 0000 0000 enpassant flag 0x400000
1000 0000 0000 0000 0000 0000 castling flag 0x800000
"""
@njit(cache=True)
def encode_move(source, target, piece, side, promote_to, capture, double, enpas, castling):
return source | target << 6 | piece << 12 | side << 15 | promote_to << 16 | capture << 20 | \
double << 21 | enpas << 22 | castling << 23
@njit(nb.uint8(nb.uint64), cache=True)
def get_move_source(move):
return move & 0x3f
@njit(nb.uint8(nb.uint64), cache=True)
def get_move_target(move):
return (move & 0xfc0) >> 6
@njit(nb.uint8(nb.uint64), cache=True)
def get_move_piece(move):
return (move & 0x7000) >> 12
@njit(nb.uint8(nb.uint64), cache=True)
def get_move_side(move):
return bool(move & 0x8000)
@njit(nb.uint8(nb.uint64), cache=True)
def get_move_promote_to(move):
return (move & 0xf0000) >> 16
@njit(nb.b1(nb.uint64), cache=True)
def get_move_capture(move):
return bool(move & 0x100000)
@njit(nb.b1(nb.uint64), cache=True)
def get_move_double(move):
return bool(move & 0x200000)
@njit(nb.b1(nb.uint64), cache=True)
def get_move_enpas(move):
return bool(move & 0x400000)
@njit(nb.b1(nb.uint64), cache=True)
def get_move_castling(move):
return bool(move & 0x800000)
@njit(cache=True)
def get_move_uci(move):
"""get the uci string of a move"""
return str(square_to_coordinates[get_move_source(move)]) + str(square_to_coordinates[get_move_target(move)]) + \
(piece_to_letter[black][get_move_promote_to(move)] if get_move_promote_to(move) else '')
def print_move(move):
"""print a move in UCI format"""
print(f"{square_to_coordinates[get_move_source(move)]}{square_to_coordinates[get_move_target(move)]}"
f"{piece_to_letter[black][get_move_promote_to(move)] if get_move_promote_to(move) else ''}")
def print_move_list(move_list):
"""print a nice move list"""
if not move_list:
print("Empty move_list")
print()
print(" move piece capture double enpas castling")
for move in move_list:
print(f" {square_to_coordinates[get_move_source(move)]}{square_to_coordinates[get_move_target(move)]}"
f"{piece_to_letter[black][get_move_promote_to(move)] if get_move_promote_to(move) else ''} "
f"{piece_to_letter[get_move_side(move)][get_move_piece(move)]} "
f"{get_move_capture(move)} {get_move_double(move)} "
f"{get_move_enpas(move)} "
f"{get_move_castling(move)}")
print("Total number of moves:", len(move_list))
def print_attacked_square(pos, side):
"""print a bitboard of all squares attacked by a given side"""
attacked = EMPTY
for sq in squares:
if is_square_attacked(pos, sq, side):
attacked = set_bit(attacked, sq)
print_bb(attacked)
@njit
def generate_moves(pos):
"""return a list of pseudo legal moves from a given Position"""
# TODO: integrate the constants to be able to compile AOT
move_list = []
for piece in range(6):
bb = pos.pieces[pos.side][piece]
opp = pos.side ^ 1
# white pawns & king castling moves
if pos.side == white:
if piece == pawn:
while bb:
# pawn move
source = get_ls1b_index(bb)
target = source - 8
# quiet pawn move
if not target < a8 and not get_bit(pos.occupancy[both], target):
# promotion
if a7 <= source <= h7:
move_list.append(encode_move(source, target, piece, pos.side, queen, 0, 0, 0, 0))
move_list.append(encode_move(source, target, piece, pos.side, rook, 0, 0, 0, 0))
move_list.append(encode_move(source, target, piece, pos.side, bishop, 0, 0, 0, 0))
move_list.append(encode_move(source, target, piece, pos.side, knight, 0, 0, 0, 0))
else:
# push
move_list.append(encode_move(source, target, piece, pos.side, 0, 0, 0, 0, 0))
# push push
if a2 <= source <= h2 and not get_bit(pos.occupancy[both], target - 8):
move_list.append(
encode_move(source, target - 8, piece, pos.side, 0, 0, 1, 0, 0))
# pawn attack tables
attacks = pawn_attacks[white][source] & pos.occupancy[black]
while attacks:
target = get_ls1b_index(attacks)
# promotion capture
if a7 <= source <= h7:
move_list.append(encode_move(source, target, piece, pos.side, queen, 1, 0, 0, 0))
move_list.append(encode_move(source, target, piece, pos.side, rook, 1, 0, 0, 0))
move_list.append(encode_move(source, target, piece, pos.side, bishop, 1, 0, 0, 0))
move_list.append(encode_move(source, target, piece, pos.side, knight, 1, 0, 0, 0))
# capture
else:
move_list.append(encode_move(source, target, piece, pos.side, 0, 1, 0, 0, 0))
attacks = pop_bit(attacks, target)
# en-passant
if pos.enpas != no_sq:
enpas_attacks = pawn_attacks[white][source] & (BIT << pos.enpas)
if enpas_attacks:
target_enpas = get_ls1b_index(enpas_attacks)
move_list.append(encode_move(source, target_enpas, piece, pos.side, 0, 1, 0, 1, 0))
bb = pop_bit(bb, source)
if piece == king:
if pos.castle & wk:
# are squares empty
if not get_bit(pos.occupancy[both], f1) and not get_bit(pos.occupancy[both], g1):
# are squares safe
if not is_square_attacked(pos, e1, black) and not is_square_attacked(pos, f1, black):
move_list.append(encode_move(e1, g1, piece, pos.side, 0, 0, 0, 0, 1))
if pos.castle & wq:
# squares are empty
if not get_bit(pos.occupancy[both], d1) and not get_bit(pos.occupancy[both], c1) and not get_bit(
pos.occupancy[both], b1):
# squares are not attacked by black
if not is_square_attacked(pos, e1, black) and not is_square_attacked(pos, d1, black):
move_list.append(encode_move(e1, c1, piece, pos.side, 0, 0, 0, 0, 1))
# black pawns & king castling moves
if pos.side == black:
if piece == pawn:
while bb:
source = get_ls1b_index(bb)
target = source + 8
# quiet pawn move
if not target > h1 and not get_bit(pos.occupancy[both], target):
# Promotion
if a2 <= source <= h2:
move_list.append(encode_move(source, target, piece, pos.side, queen, 0, 0, 0, 0))
move_list.append(encode_move(source, target, piece, pos.side, rook, 0, 0, 0, 0))
move_list.append(encode_move(source, target, piece, pos.side, bishop, 0, 0, 0, 0))
move_list.append(encode_move(source, target, piece, pos.side, knight, 0, 0, 0, 0))
else:
# push
move_list.append(encode_move(source, target, piece, pos.side, 0, 0, 0, 0, 0))
# push push
if a7 <= source <= h7 and not get_bit(pos.occupancy[both], target + 8):
move_list.append(
encode_move(source, target + 8, piece, pos.side, 0, 0, 1, 0, 0))
# pawn attack tables
attacks = pawn_attacks[black][source] & pos.occupancy[white]
while attacks:
target = get_ls1b_index(attacks)
# promotion capture
if a2 <= source <= h2:
move_list.append(encode_move(source, target, piece, pos.side, queen, 1, 0, 0, 0))
move_list.append(encode_move(source, target, piece, pos.side, rook, 1, 0, 0, 0))
move_list.append(encode_move(source, target, piece, pos.side, bishop, 1, 0, 0, 0))
move_list.append(encode_move(source, target, piece, pos.side, knight, 1, 0, 0, 0))
# capture
else:
move_list.append(encode_move(source, target, piece, pos.side, 0, 1, 0, 0, 0))
attacks = pop_bit(attacks, target)
# en-passant
if pos.enpas != no_sq:
enpas_attacks = pawn_attacks[black][source] & (BIT << pos.enpas)
if enpas_attacks:
target_enpas = get_ls1b_index(enpas_attacks)
move_list.append(encode_move(source, target_enpas, piece, pos.side, 0, 1, 0, 1, 0))
bb = pop_bit(bb, source)
if piece == king: # target square will be checked later with legality
if pos.castle & bk:
# squares are empty
if not get_bit(pos.occupancy[both], f8) and not get_bit(pos.occupancy[both], g8):
# squares are not attacked by black
if not is_square_attacked(pos, e8, white) and not is_square_attacked(pos, f8, white):
move_list.append(encode_move(e8, g8, piece, pos.side, 0, 0, 0, 0, 1))
if pos.castle & bq:
# squares are empty
if not get_bit(pos.occupancy[both], d8) and not get_bit(pos.occupancy[both], c8) and not get_bit(
pos.occupancy[both], b8):
# squares are not attacked by white
if not is_square_attacked(pos, e8, white) and not is_square_attacked(pos, d8, white):
move_list.append(encode_move(e8, c8, piece, pos.side, 0, 0, 0, 0, 1))
if piece in range(1, 6):
while bb:
source = get_ls1b_index(bb)
attacks = get_attacks(piece, source, pos)
while attacks != EMPTY:
target = get_ls1b_index(attacks)
# quiet
if not get_bit(pos.occupancy[opp], target):
move_list.append(encode_move(source, target, piece, pos.side, 0, 0, 0, 0, 0))
# capture
else:
move_list.append(encode_move(source, target, piece, pos.side, 0, 1, 0, 0, 0))
attacks = pop_bit(attacks, target)
bb = pop_bit(bb, source)
return move_list
def generate_legal_moves(pos):
"""very inefficient, use only to debug"""
return [move for move in generate_moves(pos) if make_move(pos, move)]
# (Position.class_type.instance_type(Position.class_type.instance_type, nb.uint64, nb.b1))
@njit
def make_move(pos_orig, move, only_captures=False):
"""return new updated position if (move is legal) else None"""
# TODO: integrate the constants to be able to compile AOT
# create a copy of the position
pos = Position()
pos.pieces = pos_orig.pieces.copy()
pos.side = pos_orig.side
pos.enpas = pos_orig.enpas
pos.castle = pos_orig.castle
pos.hash_key = pos_orig.hash_key
# quiet moves
if not only_captures:
# parse move
source_square = get_move_source(move)
target_square = get_move_target(move)
piece = get_move_piece(move)
side = get_move_side(move)
opp = side ^ 1
promote_to = get_move_promote_to(move)
capture = get_move_capture(move)
double_push = get_move_double(move)
enpas = get_move_enpas(move)
castling = get_move_castling(move)
# Actual Move
# update bitboards
pos.pieces[side][piece] = pop_bit(pos.pieces[side][piece], source_square)
pos.pieces[side][piece] = set_bit(pos.pieces[side][piece], target_square)
# update hash key
pos.hash_key ^= piece_keys[side][piece][source_square]
pos.hash_key ^= piece_keys[side][piece][target_square]
if capture: # find what we captured and erase it
for opp_piece in range(6):
if get_bit(pos.pieces[opp][opp_piece], target_square):
# update bitboards
pos.pieces[opp][opp_piece] = pop_bit(pos.pieces[opp][opp_piece], target_square)
# update hash key
pos.hash_key ^= piece_keys[opp][opp_piece][target_square]
break
if promote_to: # erase pawn and place promoted piece
pos.pieces[side][piece] = pop_bit(pos.pieces[side][piece], target_square)
pos.hash_key ^= piece_keys[side][piece][target_square]
pos.pieces[side][promote_to] = set_bit(pos.pieces[side][promote_to], target_square)
pos.hash_key ^= piece_keys[side][promote_to][target_square]
if enpas: # erase the opp pawn
if side: # black just moved
pos.pieces[opp][piece] = pop_bit(pos.pieces[opp][piece], target_square - 8)
pos.hash_key ^= piece_keys[opp][piece][target_square - 8]
else: # white just moved
pos.pieces[opp][piece] = pop_bit(pos.pieces[opp][piece], target_square + 8)
pos.hash_key ^= piece_keys[opp][piece][target_square + 8]
if pos.enpas != no_sq:
pos.hash_key ^= en_passant_keys[pos.enpas]
# reset enpas
pos.enpas = no_sq
if double_push: # set en-passant square
if side: # black just moved
pos.enpas = target_square - 8
pos.hash_key ^= en_passant_keys[target_square - 8]
else: # white just moved
pos.enpas = target_square + 8
pos.hash_key ^= en_passant_keys[target_square + 8]
if castling: # move rook accordingly
if target_square == g1:
pos.pieces[side][rook] = pop_bit(pos.pieces[side][rook], h1)
pos.pieces[side][rook] = set_bit(pos.pieces[side][rook], f1)
pos.hash_key ^= piece_keys[side][rook][h1]
pos.hash_key ^= piece_keys[side][rook][f1]
elif target_square == c1:
pos.pieces[side][rook] = pop_bit(pos.pieces[side][rook], a1)
pos.pieces[side][rook] = set_bit(pos.pieces[side][rook], d1)
pos.hash_key ^= piece_keys[side][rook][a1]
pos.hash_key ^= piece_keys[side][rook][d1]
elif target_square == g8:
pos.pieces[side][rook] = pop_bit(pos.pieces[side][rook], h8)
pos.pieces[side][rook] = set_bit(pos.pieces[side][rook], f8)
pos.hash_key ^= piece_keys[side][rook][h8]
pos.hash_key ^= piece_keys[side][rook][f8]
elif target_square == c8:
pos.pieces[side][rook] = pop_bit(pos.pieces[side][rook], a8)
pos.pieces[side][rook] = set_bit(pos.pieces[side][rook], d8)
pos.hash_key ^= piece_keys[side][rook][a8]
pos.hash_key ^= piece_keys[side][rook][d8]
# reset castling hash
pos.hash_key ^= castle_keys[pos.castle]
# update castling rights
pos.castle &= castling_rights[source_square]
pos.castle &= castling_rights[target_square]
# update castling hash
pos.hash_key ^= castle_keys[pos.castle]
# update occupancy
for color in range(2):
for bb in pos.pieces[color]:
pos.occupancy[color] |= bb
pos.occupancy[both] |= pos.occupancy[color]
pos.side = opp
pos.hash_key ^= side_key
if not is_square_attacked(pos, get_ls1b_index(pos.pieces[side][king]), opp):
return pos
# Capturing moves
else:
if get_move_capture(move):
return make_move(pos, move, only_captures=False)
return None
return None
@njit
def make_null_move(pos_orig):
"""return a position with no enpas sq and flipped sides"""
pos = Position()
pos.pieces = pos_orig.pieces.copy()
pos.occupancy = pos_orig.occupancy.copy()
pos.castle = pos_orig.castle
pos.side = pos_orig.side ^ 1
pos.enpas = no_sq
pos.hash_key = pos_orig.hash_key
# update hash table
if pos_orig.enpas != no_sq:
pos.hash_key ^= en_passant_keys[pos_orig.enpas]
pos.hash_key ^= side_key
return pos
def parse_move(pos, uci_move: str) -> int:
"""encode a uci move"""
source = (ord(uci_move[0]) - ord('a')) + ((8 - int(uci_move[1])) * 8)
target = (ord(uci_move[2]) - ord('a')) + ((8 - int(uci_move[3])) * 8)
for move in generate_moves(pos):
if get_move_source(move) == source and get_move_target(move) == target:
promoted_piece = get_move_promote_to(move)
if promoted_piece:
for p, s in enumerate(('n', 'b', 'r', 'q'), 1):
if promoted_piece == p and uci_move[4] == s:
return move
return 0 # in case of illegal promotion (e.g. e7d8f)
return move
return 0