-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathshatranj.py
executable file
·4577 lines (4101 loc) · 175 KB
/
shatranj.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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import string,time,random,cPickle,bisect,profile
import sys,select,types,math
try:
import signal
except:
pass
VERSION = "1.18"
'''
Last Modified: May 12, 2014
Shatranj is a bitboard based chess programming toolkit and engine.
Copyright (C) 2006-2014 Sam Tannous
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
This program may not be used in whole, nor in part, to enter
any computer chess competition without written permission from
the author. Such permission will include the requirement that
the program be entered under the name "Shatranj" so that
the program ancestry will be known.
Personal use does not allow anyone to enter this into a chess
tournament where other program authors are invited to participate. You
can do your own local tournament, with Shatranj and other programs, since
this is for your personal enjoyment. But you may not enter Shatranj into an
event where it will be in competition with other programs/programmers
without permission from the author.
version date description
---------------------------------------------------------------------------
1.00 12/10/2006 First OO version of the bit-board program to play
a legitimate game
1.10 04/06/2007 Some optimizations, and some other minor changes.
1.11 04/06/2007 Fixed crash in en passant move generation, implemented
move take back.
1.12 05/02/2007 Fixed bug in move generation.
1.13 06/20/2007 Added ICGA Journal Article test code.
1.15 11/28/2007 Added xboard compatibility mode.
1.16 05/01/2008 Added support for Windows by catching lack of
signal support (thanks to Eber Ramirez).
1.17 05/08/2008 Added some command-line features to set board position,
1.18 05/12/2014 Remove psyco module since it is dead.
'''
class Move:
def __init__(self,from_square,to_square,move_name="",
captured_piece="",promoted_piece=""):
self.from_square = from_square
self.to_square = to_square
self.move_name = move_name
self.captured_piece = captured_piece
self.promoted_piece = promoted_piece
def __repr__(self):
print "Move: from square = %s to square = %s name = %s" % \
(bin2alg[self.from_square],bin2alg[self.to_square],self.move_name)
print " captured piece = %s promoted piece = %s" % \
(str(self.captured_piece),str(self.promoted_piece))
return("")
class Position:
def __init__(self,fen=None):
if (not fen):
fen = INIT_FEN
self.piece_bb = dict(w_occupied=0,
b_occupied=0,
P=0,N=0,B=0,R=0,Q=0,K=0,
p=0,n=0,b=0,r=0,q=0,k=0)
self.piece_count = dict(P=0,N=0,B=0,R=0,Q=0,K=0,
p=0,n=0,b=0,r=0,q=0,k=0)
self.piece_name = {}
self.moves = {}
self.attacks_from = {}
self.attacks_to = {}
for i in range(64):
self.attacks_from[1L<<i] = 0
self.attacks_to[1L<<i] = 0
self.attacks_to[0] = 0
self.attacks_from[0] = 0
self.move_history = []
self.best_moves = []
self.move_count = 0
self.winner = None
self.in_check = 0
# castling checks
self.w_king_move_count = 0
self.b_king_move_count = 0
self.w_rook_a1_move_count = 0
self.w_rook_a1_location = a1
self.w_rook_h1_move_count = 0
self.w_rook_h1_location = h1
self.b_rook_h8_move_count = 0
self.b_rook_h8_location = h8
self.b_rook_a8_move_count = 0
self.b_rook_a8_location = a8
# for en passant checks
# last double pawn move is eligable for
# capture if we are on the 5th rank (for white)
# but it has to be captured on very next move
# save the end square of the last double move
self.w_pawn_last_double_move = 0
self.b_pawn_last_double_move = 0
# for 50 move rule
# someone must have moved a pawn or made a capture
# within the last 50 moves or the game is a draw
self.half_move_counter = 0
# Hash Index
# ==========
# The hash index is a FEN-like list made up of
# 1. 64 square content items (piece symbol or empty square '-',
# 2. 1 side to move item ('wtm' for white, 'btm' for black to move),
# 3. 1 item for castling ability ('-' for none, KQkq for
# respective sides,
# 4. En Passant target square ('-' for none or the square behind
# the last two square pawn move),
# 5. the half move counter: the number of half moves since the last
# pawn advance or capture -- for the 50 move rule.
# 6. fullmove counter: the number of full moves
#
# This has to be a list since arrays are not order dependent.
# It is 69 items long. The following variables are used to access
# these locations:
# MOVER_HASH_INDEX = 64
# CASTLING_HASH_INDEX = 65
# ENPASSANT_HASH_INDEX = 66
# HALF_MOVE_HASH_INDEX = 67
# FULL_MOVE_HASH_INDEX = 68
self.hash_index = ['-']*64 + ['wtm'] + ['KQkq'] + ['-'] + [0] + [0]
# we also track the half moves in a list so we can backtrack if needed.
# this needs to be a list because in a search, we might have to take
# back a move where the half move counter was 0 and we need to know
# where the counter was before it was reset to 0 because of a pawn
# move or a capture.
# This is just a running counter of half moves that gets reset
# when there is a pawn move or capture.
# [0, 1, 2, 3, 4, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7]
# ^ ^------ pawn moves or captures
self.half_move_counter_list = [0]
# we need to keep track of positions we've seen so we can detect
# repititions. we track this with an array that uses the hash_index
# of the position and keeps track of how many times we have seen
# this position.
# This is simply a list of the state of the position counters for each move
# [{position_counters move 0}, {position_counters move 1}, ...]
self.position_counter_array_list = [{}]
row = fen.split("/")
index = 63
self.fen = fen
for i in row:
for j in i:
if (j in "12345678"): # we have a space, move the index
index -= int(j)
else: # we have a piece
self.piece_bb[j] |= 1L<<index
self.piece_name[1L<<index] = j
self.piece_count[j] += 1
index -= 1
# Initialize the hash index for the transition tables
for i in range(64):
if (self.piece_name.has_key(1L<<i)):
self.hash_index[i] = self.piece_name[1L<<i]
else:
self.hash_index[i] = '-'
self.piece_bb['w_occupied'] = self.piece_bb['P'] | self.piece_bb['R'] | \
self.piece_bb['N'] | self.piece_bb['B'] | \
self.piece_bb['Q'] | self.piece_bb['K']
self.piece_bb['b_occupied'] = self.piece_bb['p'] | self.piece_bb['r'] | \
self.piece_bb['n'] | self.piece_bb['b'] | \
self.piece_bb['q'] | self.piece_bb['k']
def __repr__(self):
white = 1
for r in range(8,0,-1):
print "\n +---+---+---+---+---+---+---+---+"
print " ",r, "|",
for f in "abcdefgh":
name = "%s%s" % (f,r)
bb = alg2bin[name]
if (self.piece_name.has_key(bb)):
print self.piece_name[bb],"|",
else:
if (white):
print " ","|",
else:
print ".","|",
white ^= 1
white ^= 1
print "\n +---+---+---+---+---+---+---+---+"
print " a b c d e f g h \n"
return("")
def generate_moves (self,wtm):
"""
This generates all the legal moves. First, we check
to see if the king is in check.
"""
piece_bb = self.piece_bb
piece_name = self.piece_name
move_list = []
self.winner = None
self.in_check = 0
self.side_in_check = None
# later on we need to update attacks in efficiently in
# move() and unmove() but for now, we'll do it manually
# for each move
self.generate_attacks()
attacks_from = self.attacks_from
attacks_to = self.attacks_to
if (wtm):
other_pieces = piece_bb['b_occupied']
else:
other_pieces = piece_bb['w_occupied']
all_pieces = piece_bb['b_occupied'] | piece_bb['w_occupied']
if (wtm):
king = piece_bb['K']
else:
king = piece_bb['k']
# check to make sure we have a king.
# If we don't, we shouldn't be here.
if (king == 0):
print "Error: one of the kings is missing."
print self #display(position)
sys.exit()
return(([],[]))
# Are we in check?! Check!!
if (attacks_to[king] & other_pieces):
# we are in check, find check evasions to get out
#print "generate_moves: In CHECK!"
#print self
self.in_check = 1
self.side_in_check = wtm
move_list = self.generate_check_evasions(wtm)
if (not len(move_list)):
#print "generate_moves: no moves!"
#print self
# no moves...game is over :-(
if (wtm):
self.winner = "black"
else:
self.winner = "white"
#print "winner is",self.winner
return([])
else:
# only return moves that get us out of check
return(move_list)
# we're not in check, but we need to make sure
# we don't cause a discovered check
#
# at this point we already know the attacks_from for all
# pieces...we just need to remove our own pieces from the attack.
if (wtm):
pieces = piece_bb['w_occupied'] & ~piece_bb['P']
else:
pieces = piece_bb['b_occupied'] & ~piece_bb['p']
kings = piece_bb['k'] | piece_bb['K']
while (pieces):
from_square = ((pieces) & -(pieces))
mask = self.pinned(from_square,wtm)
moves = (attacks_from[from_square] & (other_pieces | ~all_pieces)) & mask
while (moves):
to_square = ((moves) & -(moves))
# need to make sure king doesn't move into check
if ((from_square & kings) and \
(attacks_to[to_square] & other_pieces)):
pass
elif (to_square & other_pieces): # capture
move_list.insert(0,Move(from_square,to_square,"",
piece_name[to_square],""))
else: # empty square
move_list.append(Move(from_square,to_square,"","",""))
moves = ((moves) & ((moves) - 1L))
pieces = ((pieces) & ((pieces) - 1L))
# try to castle. If the king was in check, the moves are generated
# in the check_evasions routine above.
w_occupied_ks = (g1 | f1) & all_pieces
w_occupied_qs = (b1 | c1 | d1) & all_pieces
b_occupied_ks = (g8 | f8) & all_pieces
b_occupied_qs = (b8 | c8 | d8) & all_pieces
if (wtm and piece_bb['K'] == e1 and \
self.w_king_move_count == 0 and \
(not w_occupied_ks or not w_occupied_qs)):
piece = piece_bb['K']
attacked_ks = (attacks_to[g1] | \
attacks_to[f1]) & other_pieces
attacked_qs = (attacks_to[c1] | \
attacks_to[d1]) & other_pieces
if (self.w_rook_h1_move_count == 0 and \
piece_name.has_key(h1) and piece_name[h1] == 'R' and \
not attacked_ks and not w_occupied_ks):
move_list.append(Move(piece,g1,"castle","",""))
if (self.w_rook_a1_move_count == 0 and \
piece_name.has_key(a1) and piece_name[a1] == 'R' and \
not attacked_qs and not w_occupied_qs):
move_list.append(Move(piece,c1,"castle","",""))
elif (not wtm and piece_bb['k'] == e8 and \
self.b_king_move_count == 0 and \
(not b_occupied_ks or not b_occupied_qs)):
piece = piece_bb['k']
attacked_ks = (attacks_to[g8] | \
attacks_to[f8]) & other_pieces
attacked_qs = (attacks_to[c8] | \
attacks_to[d8]) & other_pieces
if (self.b_rook_h8_move_count == 0 and \
piece_name.has_key(h8) and piece_name[h8] == 'r' and \
not attacked_ks and not b_occupied_ks):
move_list.append(Move(piece,g8,"castle","",""))
if (self.b_rook_a8_move_count == 0 and \
piece_name.has_key(a8) and piece_name[a8] == 'r' and \
not attacked_qs and not b_occupied_qs):
move_list.append(Move(piece,c8,"castle","",""))
# produce pawn captures
# these are not handled in generate_attacks
# since pawns can only attack diagonally (or enpassant) and pawns attack
# empty square but moves can only take place if a piece exists on that square
if (wtm):
piece = piece_bb['P']
left_captures = piece<<9 & other_pieces & ~file_mask[h1] & ALL_ONES
right_captures = piece<<7 & other_pieces & ~file_mask[a1] & ALL_ONES
else:
piece = piece_bb['p']
left_captures = piece>>7 & other_pieces & ~file_mask[h1]
right_captures = piece>>9 & other_pieces & ~file_mask[a1]
while (left_captures):
to_square = ((left_captures) & -(left_captures))
if (wtm):
from_square = to_square>>9
else:
from_square = to_square<<7
mask = self.pinned(from_square,wtm)
if (mask & to_square):
if (rank[to_square] == 8 or rank[to_square] == 1):
# promotion and capture
move_list.insert(0,Move(from_square,to_square,"promotion",
piece_name[to_square],"Q"))
else:
move_list.insert(0,Move(from_square,to_square,"",
piece_name[to_square],""))
left_captures = ((left_captures) & ((left_captures) - 1L))
while (right_captures):
to_square = ((right_captures) & -(right_captures))
if (wtm):
from_square = to_square>>7
else:
from_square = to_square<<9
mask = self.pinned(from_square,wtm)
if (mask & to_square):
if (rank[to_square] == 8 or rank[to_square] == 1):
# promotion and capture
move_list.insert(0,Move(from_square,to_square,"promotion",
piece_name[to_square],"Q"))
else:
move_list.insert(0,Move(from_square,to_square,"",
piece_name[to_square],""))
right_captures = ((right_captures) & ((right_captures) - 1L))
# produce en passant captures
if (wtm and self.b_pawn_last_double_move):
last_double = self.b_pawn_last_double_move
pawns = piece_bb['P']
# handle the right side capture
if (file[last_double] < 8):
right_file = file_mask[last_double] >> 1
rank5_pawn = pawns & rank_mask[a5] & right_file
# there is a chance the pawn could be pinned along
# a diagonal and could still capture en passant
if (rank5_pawn):
mask = self.pinned(rank5_pawn,wtm)
to_square = file_mask[last_double] & rank_mask[a6]
if (mask & to_square) and not self.ep_pinned(rank5_pawn,to_square):
move_list.insert(0,Move(rank5_pawn,to_square,"enpassant","p",""))
# handle the left side capture
if (file[last_double] > 1):
left_file = file_mask[last_double] << 1
rank5_pawn = pawns & rank_mask[a5] & left_file
# there is a chance the pawn could be pinned along
# a diagonal and could still capture en passant
if (rank5_pawn):
mask = self.pinned(rank5_pawn,wtm)
to_square = file_mask[last_double] & rank_mask[a6]
if (mask & to_square) and not self.ep_pinned(rank5_pawn,to_square):
move_list.insert(0,Move(rank5_pawn,to_square,"enpassant","p",""))
elif (not wtm and self.w_pawn_last_double_move):
last_double = self.w_pawn_last_double_move
pawns = piece_bb['p']
# handle the right side capture
if (file[last_double] < 8):
right_file = file_mask[last_double] >> 1
rank4_pawn = pawns & rank_mask[a4] & right_file
# there is a chance the pawn could be pinned along
# a diagonal and could still capture en passant
if (rank4_pawn):
mask = self.pinned(rank4_pawn,wtm)
to_square = file_mask[last_double] & rank_mask[a3]
if (mask & to_square) and not self.ep_pinned(rank4_pawn,to_square):
move_list.insert(0,Move(rank4_pawn,to_square,"enpassant","P",""))
# handle the left side capture
if (file[last_double] > 1):
left_file = file_mask[last_double] << 1
rank4_pawn = pawns & rank_mask[a4] & left_file
# there is a chance the pawn could be pinned along
# a diagonal and could still capture en passant
if (rank4_pawn):
mask = self.pinned(rank4_pawn,wtm)
to_square = file_mask[last_double] & rank_mask[a3]
if (mask & to_square) and not self.ep_pinned(rank4_pawn,to_square):
move_list.insert(0,Move(rank4_pawn,to_square,"enpassant","P",""))
# produce single pawn moves...these are not handled in generate_attacks
# since pawns can only attack diagonally (or enpassant) and pawns attack
# empty square but moves can only take place if a piece exists on that square
if (wtm):
piece = piece_bb['P']
single_moves = piece<<8 & ~all_pieces
double_moves = (single_moves<<8) & ~all_pieces & rank_mask[a4]
else:
piece = piece_bb['p']
single_moves = piece>>8 & ~all_pieces
double_moves = (single_moves>>8) & ~all_pieces & rank_mask[a5]
while (single_moves):
to_square = ((single_moves) & -(single_moves))
if (wtm):
from_square = to_square>>8
else:
from_square = to_square<<8
mask = self.pinned(from_square,wtm)
if (mask & to_square):
if (rank[to_square] == 8 or rank[to_square] == 1):
move_list.append(Move(from_square,to_square,"promotion","","Q"))
else:
move_list.append(Move(from_square,to_square,"","",""))
single_moves = ((single_moves) & ((single_moves) - 1L))
# produce double pawn moves
while (double_moves):
to_square = ((double_moves) & -(double_moves))
if (wtm):
from_square = to_square>>16
else:
from_square = to_square<<16
mask = self.pinned(from_square,wtm)
if (mask & to_square):
#print "WE ARE HERE",bin2alg[from_square]
move_list.append(Move(from_square,to_square,"pawn double move","",""))
double_moves = ((double_moves) & ((double_moves) - 1L))
return(move_list)
def show_moves (self,wtm):
move_list = self.generate_moves(wtm)
moves,san_moves = self.get_move_list(move_list)
return(san_moves.values())
def show_moves90 (self,wtm):
move_list = self.generate_moves_rot(wtm)
moves,san_moves = self.get_move_list(move_list)
return(san_moves.values())
def generate_moves_rot (self,wtm):
"""
This generates all the legal moves. First, we check
to see if the king is in check.
"""
piece_bb = self.piece_bb
piece_name = self.piece_name
move_list = []
self.winner = None
self.in_check = None
self.side_in_check = None
# later on we need to update attacks in efficiently in
# move() and unmove() but for now, we'll do it manually
# for each move
self.generate_attacks()
attacks_from = self.attacks_from
attacks_to = self.attacks_to
if (wtm):
other_pieces = piece_bb['b_occupied']
else:
other_pieces = piece_bb['w_occupied']
all_pieces = piece_bb['b_occupied'] | piece_bb['w_occupied']
all_pieces90 = piece_bb['all_pieces90']
all_pieces45ne = piece_bb['all_pieces45ne']
all_pieces45nw = piece_bb['all_pieces45nw']
if (wtm):
king = piece_bb['K']
else:
king = piece_bb['k']
# check to make sure we have a king.
# If we don't, we shouldn't be here.
if (king == 0):
print "Error: one of the kings is missing."
print self #display(position)
sys.exit()
return(([],[]))
# Are we in check?! Check!!
if (attacks_to[king] & other_pieces):
# we are in check, find check evasions to get out
#print "In CHECK!"
#display(position)
self.in_check = 1
self.in_check = wtm
move_list = self.generate_check_evasions(wtm)
if (not len(move_list)):
# no moves...game is over :-(
if (wtm):
self.winner = "black"
else:
self.winner = "white"
return([])
else:
# only return moves that get us out of check
return(move_list)
# we're not in check, but we need to make sure
# we don't cause a discovered check
#
# produce king and knight moves
# we're not in check, but we need to make sure
# we don't cause a discovered check
#
if (wtm):
piece = piece_bb['K'] | piece_bb['N']
else:
piece = piece_bb['k'] | piece_bb['n']
knights = piece_bb['n'] | piece_bb['N']
kings = piece_bb['k'] | piece_bb['K']
while (piece):
from_square = lsb(piece)
mask = self.pinned(from_square,wtm)
moves = ((king_moves[from_square & kings] & other_pieces) | \
(king_moves[from_square & kings] & ~all_pieces) | \
(knight_moves[from_square & knights] & other_pieces) | \
(knight_moves[from_square & knights] & ~all_pieces)) & mask
while (moves):
to_square = lsb(moves)
# need to make sure king doesn't move into check
if ((from_square & kings) and \
(attacks_to[to_square] & other_pieces)):
pass
elif (to_square & other_pieces):
move_list.insert(0,Move(from_square,to_square,"",piece_name[to_square],""))
else:
move_list.append(Move(from_square,to_square,"","",""))
moves = clear_lsb(moves)
piece = clear_lsb(piece)
#################################################################
# produce rook and queen moves
if (wtm):
piece = piece_bb['R'] | piece_bb['Q']
else:
piece = piece_bb['r'] | piece_bb['q']
while (piece):
moves = 0
from_square = lsb(piece)
mask = self.pinned(from_square,wtm)
# handle the rank attacks
rank_shift = 8 * (rank[from_square] - 1)
first_rank_pieces = (all_pieces >> rank_shift) & 255
file_shift = 8 * (rank[rot90[from_square]] - 1)
first_file_pieces = (all_pieces90 >> file_shift) & 255
# this produces all moves including our own piece attacks
moves |= (RankAttacks[from_square][first_rank_pieces])
# ((other_pieces >> rank_shift) & 255) | \
## (RankAttacks[from_square][first_rank_pieces] & \
# ((~all_pieces >> rank_shift) & 255))) & mask
moves |= (FileAttacks[from_square][first_file_pieces]) # & \
# ((other_pieces90 >> file_shift) & 255) | \
# (FileAttacks[from_square][first_file_pieces] & \
# ((~all_pieces90 >> file_shift) & 255))) & mask
while (moves):
to_square = lsb(moves)
if (to_square & other_pieces):
move_list.insert(0,Move(from_square,to_square,"",piece_name[to_square],""))
else:
move_list.append(Move(from_square,to_square,"","",""))
moves = clear_lsb(moves)
piece = clear_lsb(piece)
# produce bishop and queen moves
if (wtm):
piece = piece_bb['B'] | piece_bb['Q']
else:
piece = piece_bb['b'] | piece_bb['q']
while (piece):
moves = 0
from_square = lsb(piece)
mask = self.pinned(from_square,wtm)
# handle the rank attacks
ne_shift = 8 * (rank[rot45ne[from_square]] - 1)
ne_pieces = (all_pieces45ne >> ne_shift) & 255
nw_shift = 8 * (rank[rot45nw[from_square]] - 1)
nw_pieces = (all_pieces45nw >> nw_shift) & 255
# this produces all moves including our own piece attacks
moves |= (BishopAttacksNE[from_square][ne_pieces])
# ((other_pieces >> rank_shift) & 255) | \
## (RankAttacks[from_square][first_rank_pieces] & \
# ((~all_pieces >> rank_shift) & 255))) & mask
moves |= (BishopAttacksNW[from_square][nw_pieces]) # & \
# ((other_pieces90 >> file_shift) & 255) | \
# (FileAttacks[from_square][first_file_pieces] & \
# ((~all_pieces90 >> file_shift) & 255))) & mask
while (moves):
to_square = lsb(moves)
if (to_square & other_pieces):
move_list.insert(0,Move(from_square,to_square,"",piece_name[to_square],""))
else:
move_list.append(Move(from_square,to_square,"","",""))
moves = clear_lsb(moves)
piece = clear_lsb(piece)
# try to castle. If the king was in check, the moves are generated
# in the check_evasions routine.
w_occupied_ks = (g1 | f1) & all_pieces
w_occupied_qs = (b1 | c1 | d1) & all_pieces
b_occupied_ks = (g8 | f8) & all_pieces
b_occupied_qs = (b8 | c8 | d8) & all_pieces
if (wtm and piece_bb['K'] == e1 and \
self.w_king_move_count == 0 and \
(not w_occupied_ks or not w_occupied_qs)):
piece = piece_bb['K']
attacked_ks = (attacks_to[g1] | \
attacks_to[f1]) & other_pieces
attacked_qs = (attacks_to[b1] | \
attacks_to[c1] |
attacks_to[d1]) & other_pieces
if (self.w_rook_h1_move_count == 0 and \
piece_name.has_key(h1) and piece_name[h1] == 'R' and \
not attacked_ks and not w_occupied_ks):
move_list.append(Move(piece,g1,"castle","",""))
if (self.w_rook_a1_move_count == 0 and \
piece_name.has_key(a1) and piece_name[a1] == 'R' and \
not attacked_qs and not w_occupied_qs):
move_list.append(Move(piece,c1,"castle","",""))
elif (not wtm and piece_bb['k'] == e8 and \
self.b_king_move_count == 0 and \
(not b_occupied_ks or not b_occupied_qs)):
piece = piece_bb['k']
attacked_ks = (attacks_to[g8] | \
attacks_to[f8]) & other_pieces
attacked_qs = (attacks_to[b8] | \
attacks_to[c8] |
attacks_to[d8]) & other_pieces
if (self.b_rook_h8_move_count == 0 and \
piece_name.has_key(h8) and piece_name[h8] == 'r' and \
not attacked_ks and not b_occupied_ks):
move_list.append(Move(piece,g8,"castle","",""))
if (self.b_rook_a8_move_count == 0 and \
piece_name.has_key(a8) and piece_name[a8] == 'r' and \
not attacked_qs and not b_occupied_qs):
move_list.append(Move(piece,c8,"castle","",""))
# produce pawn captures
if (wtm):
piece = piece_bb['P']
left_captures = piece<<9 & other_pieces & ~file_mask[h1] & ALL_ONES
right_captures = piece<<7 & other_pieces & ~file_mask[a1] & ALL_ONES
else:
piece = piece_bb['p']
left_captures = piece>>7 & other_pieces & ~file_mask[h1]
right_captures = piece>>9 & other_pieces & ~file_mask[a1]
while (left_captures):
to_square = ((left_captures) & -(left_captures))
if (wtm):
from_square = to_square>>9
else:
from_square = to_square<<7
mask = self.pinned(from_square,wtm)
if (mask & to_square):
if (rank[to_square] == 8 or rank[to_square] == 1):
# promotion and capture
move_list.insert(0,Move(from_square,to_square,"promotion",
piece_name[to_square],"Q"))
else:
move_list.insert(0,Move(from_square,to_square,"",
piece_name[to_square],""))
left_captures = ((left_captures) & ((left_captures) - 1L))
while (right_captures):
to_square = ((right_captures) & -(right_captures))
if (wtm):
from_square = to_square>>7
else:
from_square = to_square<<9 & ALL_ONES
mask = self.pinned(from_square,wtm)
if (mask & to_square):
if (rank[to_square] == 8 or rank[to_square] == 1):
# promotion and capture
move_list.insert(0,Move(from_square,to_square,"promotion",
piece_name[to_square],"Q"))
else:
move_list.insert(0,Move(from_square,to_square,"",
piece_name[to_square],""))
right_captures = ((right_captures) & ((right_captures) - 1L))
# produce en passant captures
if (wtm and self.b_pawn_last_double_move):
last_double = self.b_pawn_last_double_move
pawns = piece_bb['P']
# handle the right side capture
if (file[last_double] < 8):
right_file = file_mask[last_double] >> 1
rank5_pawn = pawns & rank_mask[a5] & right_file
# there is a chance the pawn could be pinned along
# a diagonal and could still capture en passant
if (rank5_pawn):
mask = self.pinned(rank5_pawn,wtm)
to_square = file_mask[last_double] & rank_mask[a6]
if (mask & to_square):
move_list.insert(0,Move(rank5_pawn,to_square,"enpassant","p",""))
# handle the left side capture
if (file[last_double] > 1):
left_file = file_mask[last_double] << 1
rank5_pawn = pawns & rank_mask[a5] & left_file
# there is a chance the pawn could be pinned along
# a diagonal and could still capture en passant
if (rank5_pawn):
mask = self.pinned(rank5_pawn,wtm)
to_square = file_mask[last_double] & rank_mask[a6]
if (mask & to_square):
move_list.insert(0,Move(rank5_pawn,to_square,"enpassant","p",""))
elif (not wtm and self.w_pawn_last_double_move):
last_double = self.w_pawn_last_double_move
pawns = piece_bb['p']
# handle the right side capture
if (file[last_double] < 8):
right_file = file_mask[last_double] >> 1
rank4_pawn = pawns & rank_mask[a4] & right_file
# there is a chance the pawn could be pinned along
# a diagonal and could still capture en passant
if (rank4_pawn and (mask & to_square)):
mask = self.pinned(rank4_pawn,wtm)
to_square = file_mask[last_double] & rank_mask[a3]
if (mask & to_square):
move_list.insert(0,Move(rank4_pawn,to_square,"enpassant","P",""))
# handle the left side capture
if (file[last_double] > 1):
left_file = file_mask[last_double] << 1
rank4_pawn = pawns & rank_mask[a4] & left_file
# there is a chance the pawn could be pinned along
# a diagonal and could still capture en passant
if (rank4_pawn):
mask = self.pinned(rank4_pawn,wtm)
to_square = file_mask[last_double] & rank_mask[a3]
if (mask & to_square):
move_list.insert(0,Move(rank4_pawn,to_square,"enpassant","P",""))
# produce single pawn moves
if (wtm):
piece = piece_bb['P']
single_moves = piece<<8 & ~all_pieces
double_moves = (single_moves<<8) & ~all_pieces & rank_mask[a4]
else:
piece = piece_bb['p']
single_moves = piece>>8 & ~all_pieces
double_moves = (single_moves>>8) & ~all_pieces & rank_mask[a5]
while (single_moves):
to_square = ((single_moves) & -(single_moves))
if (wtm):
from_square = to_square>>8
else:
from_square = to_square<<8
mask = self.pinned(from_square,wtm)
if (mask & to_square):
if (rank[to_square] == 8 or rank[to_square] == 1):
move_list.append(Move(from_square,to_square,"promotion","","Q"))
else:
move_list.append(Move(from_square,to_square,"","",""))
single_moves = ((single_moves) & ((single_moves) - 1L))
# produce double pawn moves
while (double_moves):
to_square = ((double_moves) & -(double_moves))
if (wtm):
from_square = to_square>>16
else:
from_square = to_square<<16
mask = self.pinned(from_square,wtm)
if (mask & to_square):
#print "WE ARE HERE",bin2alg[from_square]
move_list.append(Move(from_square,to_square,"pawn double move","",""))
double_moves = ((double_moves) & ((double_moves) - 1L))
return(move_list)
def generate_check_evasions (self,wtm):
# this should only get called if we're in check
piece_bb = self.piece_bb
piece_name = self.piece_name
attacks_from = self.attacks_from
attacks_to = self.attacks_to
move_list = []
all_pieces = piece_bb['b_occupied'] | piece_bb['w_occupied']
last_double = 0
rank45_pawns = 0
if (wtm):
other_pieces = piece_bb['b_occupied']
our_pieces = piece_bb['w_occupied']
king = piece_bb['K']
pawns = piece_bb['P']
forward_pawns = (pawns << 8) & ALL_ONES
double_forward_pawns = ((pawns & rank_mask[a2]) << 16) & ALL_ONES
last_double = self.b_pawn_last_double_move
# capture toward the left
if (last_double and file[last_double] < 8):
rank45_pawns = piece_bb['P'] & \
(rank_mask[a5] & (file_mask[last_double]>>1))
# capture toward the right
if (last_double and file[last_double] > 1):
rank45_pawns = rank45_pawns | \
(piece_bb['P'] & \
(rank_mask[a5] & (file_mask[last_double]<<1)))
else:
other_pieces = piece_bb['w_occupied']
our_pieces = piece_bb['b_occupied']
king = piece_bb['k']
pawns = piece_bb['p']
forward_pawns = pawns >> 8
double_forward_pawns = (pawns & rank_mask[a7]) >> 16
last_double = self.w_pawn_last_double_move
if (last_double and file[last_double] < 8):
rank45_pawns = piece_bb['p'] & \
(rank_mask[a4] & (file_mask[last_double]>>1))
if (last_double and file[last_double] > 1):
rank45_pawns = rank45_pawns | \
(piece_bb['p'] & \
(rank_mask[a4] & (file_mask[last_double]<<1)))
all_pieces = piece_bb['b_occupied'] | piece_bb['w_occupied']
# check to make sure we're in check
king_attackers = attacks_to[king] & other_pieces
if (not king_attackers):
print "Error: we're not in check. We shouldn't be here."
return
# Yikes! We're in check!
# we either
# 1. capture the attacking piece
# 2. move out of the way or
# 3. block its path
num_attackers = ipc(king_attackers)
############################################################################
# option 1 Capture
############################################################################
if (num_attackers == 1):
# one attacker so we can try to capture it
# with a piece other then the king.
# if there are two attackers, no point in capturing
# one of then unless the king captures it (and it's
# unprotected)...king captures are handled in part 2
# pawns can capture the attacking piece in a normal fashion
# and this is handled here. In the rare case that a pawn
# can capture en passant, this is also handled here because
# attacks_to should see this.
attacker_attackers = attacks_to[king_attackers] & our_pieces & ~king
while (attacker_attackers):
attacker = ((attacker_attackers) & -(attacker_attackers) )
# for en passant moves, we mask against the diag not the attacker
# since that's where we end up.
# a rank45 pawn can capture the threatening pawn en passant
# first we determine the mask if we can capture en passant.
# we check to make sure the attacker is the last pawn to move
# a double move.
mask = self.pinned(attacker,wtm)
# make sure the attacker isn't pinned
if (king_attackers & mask):
# we handle the king attacks later
# handle the en passant move first
# the attacker must be the one doing en passant capture
# and the king attacker must be the one being captured
if (wtm):
if ((king_attackers & last_double) and (attacker & rank45_pawns)):
# wtm and enpassant
move_list.insert(0,Move(attacker,last_double<<8,
"enpassant","p",""))
elif (piece_name[attacker] == 'P' and rank[king_attackers] == 8):
# wtm and promotion along with the capture
move_list.insert(0,Move(attacker,king_attackers,"promotion",
piece_name[king_attackers],"Q"))
else:
move_list.insert(0,Move(attacker,king_attackers,"",
piece_name[king_attackers],""))
else:
if ((king_attackers & last_double) and (attacker & rank45_pawns)):
# not wtm and enpassant
move_list.insert(0,Move(attacker,last_double>>8,
"enpassant","P",""))
elif (piece_name[attacker] == 'p' and rank[king_attackers] == 1):
# not wtm and promotion along with the capture
move_list.insert(0,Move(attacker,king_attackers,"promotion",
piece_name[king_attackers],"Q"))
else:
move_list.insert(0,Move(attacker,king_attackers,"",
piece_name[king_attackers],""))
attacker_attackers = ((attacker_attackers) & ((attacker_attackers) - 1L))
############################################################################
# option 2 Move out of the way
############################################################################
# move the king out of the way...ok to capture other pieces
# or even the attacker (all the better)
# captures and empty squares are found on the next line.
moves = (king_moves[king] & other_pieces) | \
(king_moves[king] & ~all_pieces)
# eliminate sliding moves where we're still in check
# we want to make sure we're not sliding
# the king away from the attackers on the same
# file, rank or diagonal....since we'd still be in
# check....unless we take the piece attacking the king
attackers = king_attackers
king_rank_mask = rank_mask[king]
king_file_mask = file_mask[king]
king_diag_ne = diag_mask_ne[king]
king_diag_nw = diag_mask_nw[king]
attacker_masks = 0
while (attackers):
attacker = ((attackers) & -(attackers))
name = piece_name[attacker]
if (name in "QqRrBb"):
# here, we keep a mask of all the attackers ranks
# files and diagonals
if (king_rank_mask == rank_mask[attacker]):
attacker_masks |= king_rank_mask
elif (king_file_mask == file_mask[attacker]):
attacker_masks |= king_file_mask
elif (king_diag_ne == diag_mask_ne[attacker]):
attacker_masks |= king_diag_ne
elif (king_diag_nw == diag_mask_nw[attacker]):
attacker_masks |= king_diag_nw