-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove_generator.py
More file actions
997 lines (786 loc) · 32.7 KB
/
Copy pathmove_generator.py
File metadata and controls
997 lines (786 loc) · 32.7 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
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
import pandas
import numpy
class Generator:
def __init__(self):
'''
Input ``board`` as a pandas dataframe
Input ``interval`` as the difference between each of the rows/columns
'''
self.move_rook = [False, False]
self.en_passant = (None, None)
self.pawn_promotion = False
def case_match(self, letter1, letter2):
'''
Checks if two strings' cases match
'''
if letter1.isupper() and letter2.isupper():
return True
elif letter1.islower() and letter2.islower():
return True
else:
return False
def find_piece(self, board, piece):
'''
Returns the coordinates of the piece on the board
'''
result = numpy.where(board == piece)
# If the entry was found, return the first pair of coordinates
if result[0].size > 0:
return (board.index[result[0][0]], board.columns[result[1][0]])
else:
return None
def check_move(self, board, piece, coords):
'''
Checks if the ``coords`` are in the board,
returns ``True`` if target is an empty space
returns ``True`` if target is hostile and the piece is not a pawn
``False`` if none of those match
'''
if coords[0] in board.index and coords[1] in board.columns:
target = board.loc[coords[0], coords[1]]
if target == '':
return True
elif self.case_match(target, piece) is False:
return True
else:
return False
return False
def get_letters_coords(self, board, case):
'''
Returns all lower or upper case letters in board EXCEPT THE KING with
coordinates in this format:
``('letter', (x, y))``
The ``case`` argument should be ``'upper'`` or ``'lower'``
'''
letters_list = []
# Loop over the rows
for coord, row in board.iterrows():
# Loop over the columns
for thing, entry in row.items():
if isinstance(entry, str) and ((case == 'lower' and entry.islower()) or (case == 'upper' and entry.isupper())) and not entry.lower() == 'k':
letters_list.append((entry, (coord, thing)))
return letters_list
def set_pawn_promotion(self, board, piece, coords):
'''
``coords`` - Where the pawn is currently
``piece`` - The pawn's name
Sets ``pawn_promotion`` to ``True`` if the pawn is on the edge of the
board
'''
if piece.lower() == 'p':
if piece.isupper():
if coords[1] == board.index[0]:
self.pawn_promotion = True
return
else:
if coords[1] == board.index[-1]:
self.pawn_promotion = True
return
self.pawn_promotion = False
def complete_pawn_promotion(self, board, piece, coords):
'''
Replaces the ``coords`` on the board with the chosen ``piece`` for
promotion
'''
board.loc[coords] = piece
self.pawn_promotion = False
return board
def check_target_coords(self, board, piece, target_coords):
'''
Currently only used to check diagonal attacks with pawns
'''
if target_coords[0] in board.index and target_coords[1] in board.columns:
target = board.loc[target_coords]
if not target == '' and self.case_match(target, piece) is False:
return target_coords
return None
def pawn(self, board, piece, coords):
'''
Returns legal moves of pawn
'''
def check_pawn_move(board, piece, coords):
'''
Checks if the ``coords`` are in the board,
``True`` if target is an empty space
``True`` if target is hostile
``False`` if none of those match
'''
if coords[0] in board.index and coords[1] in board.columns:
target = board.loc[coords[0], coords[1]]
if target == '':
return True
elif self.case_match(target, piece) is False and not piece.lower() == 'p':
return True
else:
return False
return False
legal_moves = []
if piece == 'P': # White
if coords[1] == board.index[-2]: # 2 spaces
if check_pawn_move(board, piece, (coords[0], coords[1] - 1)):
legal_moves.append((coords[0], coords[1] - 1))
if check_pawn_move(board, piece, (coords[0], coords[1] - 2)):
legal_moves.append((coords[0], coords[1] - 2))
elif check_pawn_move(board, piece, (coords[0], coords[1] - 1)):
legal_moves.append((coords[0], coords[1] - 1))
if piece == 'p': # Black
if coords[1] == board.index[1]: # 2 spaces
if check_pawn_move(board, piece, (coords[0], coords[1] + 1)):
legal_moves.append((coords[0], coords[1] + 1))
if check_pawn_move(board, piece, (coords[0], coords[1] + 2)):
legal_moves.append((coords[0], coords[1] + 2))
elif check_pawn_move(board, piece, (coords[0], coords[1] + 1)):
legal_moves.append((coords[0], coords[1] + 1))
# Diagonal moves
if piece.isupper():
target_coords = (coords[0] + 1, coords[1] - 1)
add = self.check_target_coords(board, piece, target_coords)
if add is not None:
legal_moves.append(add)
if target_coords == self.en_passant:
legal_moves.append(target_coords)
target_coords = (coords[0] - 1, coords[1] - 1)
add = self.check_target_coords(board, piece, target_coords)
if add is not None:
legal_moves.append(add)
if target_coords == self.en_passant:
legal_moves.append(target_coords)
else:
target_coords = (coords[0] + 1, coords[1] + 1)
add = self.check_target_coords(board, piece, target_coords)
if add is not None:
legal_moves.append(add)
if target_coords == self.en_passant:
legal_moves.append(target_coords)
target_coords = (coords[0] - 1, coords[1] + 1)
add = self.check_target_coords(board, piece, target_coords)
if add is not None:
legal_moves.append(add)
if target_coords == self.en_passant:
legal_moves.append(target_coords)
return legal_moves
def rook(self, board, piece, coords):
'''
Returns legal moves of rook
'''
legal_moves = []
# Going up
i = 1
while self.check_move(board, piece, (coords[0], coords[1] - i)):
target = (coords[0], coords[1] - i)
legal_moves.append(target)
i += 1
if not board.loc[target] == '':
break
# Going down
i = 1
while self.check_move(board, piece, (coords[0], coords[1] + i)):
target = (coords[0], coords[1] + i)
legal_moves.append(target)
i += 1
if not board.loc[target] == '':
break
# Going left
i = 1
while self.check_move(board, piece, (coords[0] - i, coords[1])):
target = (coords[0] - i, coords[1])
legal_moves.append(target)
i += 1
if not board.loc[target] == '':
break
# Going right
i = 1
while self.check_move(board, piece, (coords[0] + i, coords[1])):
target = (coords[0] + i, coords[1])
legal_moves.append(target)
i += 1
if not board.loc[target] == '':
break
return legal_moves
def bishop(self, board, piece, coords):
'''
Returns legal moves of bishop
'''
legal_moves = []
# Going left and up
i = 1
while self.check_move(board, piece, (coords[0] - i, coords[1] - i)):
target = (coords[0] - i, coords[1] - i)
legal_moves.append(target)
i += 1
if not board.loc[target] == '':
break
# Going right and up
i = 1
while self.check_move(board, piece, (coords[0] + i, coords[1] - i)):
target = (coords[0] + i, coords[1] - i)
legal_moves.append(target)
i += 1
if not board.loc[target] == '':
break
# Going left and down
i = 1
while self.check_move(board, piece, (coords[0] - i, coords[1] + i)):
target = (coords[0] - i, coords[1] + i)
legal_moves.append(target)
i += 1
if not board.loc[target] == '':
break
# Going right and down
i = 1
while self.check_move(board, piece, (coords[0] + i, coords[1] + i)):
target = (coords[0] + i, coords[1] + i)
legal_moves.append(target)
i += 1
if not board.loc[target] == '':
break
return legal_moves
def knight(self, board, piece, coords):
'''
Returns legal moves of knight
'''
legal_moves = []
# Going up and left
if self.check_move(board, piece, (coords[0] - 1, coords[1] - 2)):
legal_moves.append((coords[0] - 1, coords[1] - 2))
# Going up and right
if self.check_move(board, piece, (coords[0] + 1, coords[1] - 2)):
legal_moves.append((coords[0] + 1, coords[1] - 2))
# Going down and left
if self.check_move(board, piece, (coords[0] - 1, coords[1] + 2)):
legal_moves.append((coords[0] - 1, coords[1] + 2))
# Going down and right
if self.check_move(board, piece, (coords[0] + 1, coords[1] + 2)):
legal_moves.append((coords[0] + 1, coords[1] + 2))
# Going left and up
if self.check_move(board, piece, (coords[0] - 2, coords[1] - 1)):
legal_moves.append((coords[0] - 2, coords[1] - 1))
# Going left and down
if self.check_move(board, piece, (coords[0] - 2, coords[1] + 1)):
legal_moves.append((coords[0] - 2, coords[1] + 1))
# Going right and up
if self.check_move(board, piece, (coords[0] + 2, coords[1] - 1)):
legal_moves.append((coords[0] + 2, coords[1] - 1))
# Going right and down
if self.check_move(board, piece, (coords[0] + 2, coords[1] + 1)):
legal_moves.append((coords[0] + 2, coords[1] + 1))
return legal_moves
def basic_king(self, board, piece, coords):
'''
Returns the basic 8 moves a king can take allowing attacking a piece
and moving to an empty space
SHOULD NOT BE USED ON ITS OWN TO GET THE COMPLETE KING FUNCTIONS
'''
legal_moves = []
# Going up
if self.check_move(board, piece, (coords[0], coords[1] - 1)):
legal_moves.append((coords[0], coords[1] - 1))
# Going down
if self.check_move(board, piece, (coords[0], coords[1] + 1)):
legal_moves.append((coords[0], coords[1] + 1))
# Going left
if self.check_move(board, piece, (coords[0] - 1, coords[1])):
legal_moves.append((coords[0] - 1, coords[1]))
# Going right
if self.check_move(board, piece, (coords[0] + 1, coords[1])):
legal_moves.append((coords[0] + 1, coords[1]))
# Going up and left
if self.check_move(board, piece, (coords[0] - 1, coords[1] - 1)):
legal_moves.append((coords[0] - 1, coords[1] - 1))
# Going up and right
if self.check_move(board, piece, (coords[0] + 1, coords[1] - 1)):
legal_moves.append((coords[0] + 1, coords[1] - 1))
# Going down and left
if self.check_move(board, piece, (coords[0] - 1, coords[1] + 1)):
legal_moves.append((coords[0] - 1, coords[1] + 1))
# Going down and right
if self.check_move(board, piece, (coords[0] + 1, coords[1] + 1)):
legal_moves.append((coords[0] + 1, coords[1] + 1))
return legal_moves
def king(self, board, piece, coords, king_movement_tracker,
rook_movement_tracker):
'''
Returns legal moves of the king using the basic king function and then
adding king_cannot_move_into_check
'''
legal_moves = self.basic_king(board, piece, coords)
legal_moves = self.check_for_hostile_king(board, piece, legal_moves)
legal_moves = self.king_cannot_move_into_check(board, piece, coords,
legal_moves,
king_movement_tracker,
rook_movement_tracker)
legal_moves = self.is_king_defending_piece_to_attack(board, piece,
legal_moves)
return legal_moves
def queen(self, board, piece, coords):
'''
Returns legal moves of queen by combining the moves of a
rook and bishop
'''
legal_moves = self.rook(board, piece, coords)
more_moves = self.bishop(board, piece, coords)
return legal_moves + more_moves
def get_moves(self, board, coords, king_movement_tracker,
rook_movement_tracker):
'''
Returns the complete legal moves for a piece at given ``coords``
'''
self.move_rook = [False, False]
og_piece = board.loc[coords[0], coords[1]]
piece = og_piece.lower()
if og_piece == '':
return []
if piece == 'p':
legal_moves = self.pawn(board, og_piece, coords)
elif piece == 'r':
legal_moves = self.rook(board, og_piece, coords)
elif piece == 'n':
legal_moves = self.knight(board, og_piece, coords)
elif piece == 'b':
legal_moves = self.bishop(board, og_piece, coords)
elif piece == 'q':
legal_moves = self.queen(board, og_piece, coords)
if piece == 'k':
legal_moves = self.king(board, og_piece, coords,
king_movement_tracker,
rook_movement_tracker)
else:
legal_moves = self.no_ignore_check(board, og_piece, coords,
legal_moves)
return legal_moves
def get_neighbor_entries(self, board, coords, direction):
'''
Returns neighbor entires in the board provided a direction:
``'left'`` / ``'right'``
'''
x, y = coords
x_index = board.columns.tolist().index(x)
if direction == 'left':
if x_index - 1 < 0:
return ''
else:
return board.loc[board.columns[x_index - 1], y]
elif direction == 'right':
if x_index + 1 >= len(board.columns):
return ''
else:
return board.loc[board.columns[x_index + 1], y]
else:
return ''
def set_en_passant(self, board, piece, coords):
'''
The ``coords`` argument is for the pawn that can get killed
(The pawn that just moved 2 steps)
Does'nt return anything, sets ``en_passant`` to the coordinates of the
where the attacking pawn must go to kill a hostile pawn using En
Passant
'''
# Making sure the piece moved 2 steps
if (coords[1] == board.index[3] and piece.islower()) or (coords[1] == board.index[-4] and piece.isupper()):
pass
else:
return
left_piece = self.get_neighbor_entries(board, coords, 'left')
right_piece = self.get_neighbor_entries(board, coords, 'right')
if left_piece.lower() == 'p' and self.case_match(left_piece, piece) is False:
if piece.islower():
self.en_passant = (coords[0], coords[1] - 1)
else:
self.en_passant = (coords[0], coords[1] + 1)
if right_piece.lower() == 'p' and self.case_match(right_piece, piece) is False:
if piece.islower():
self.en_passant = (coords[0], coords[1] - 1)
else:
self.en_passant = (coords[0], coords[1] + 1)
def complete_en_passant(self, board, piece, coords):
'''
The argument ``coords`` are the same coordinates ``en_passant`` is
equal to
'''
if board.loc[coords] == piece:
if piece == 'P' and self.en_passant == coords:
next_coord = coords[1] + 1
if next_coord in board.columns:
board.loc[coords[0], next_coord] = ''
elif piece == 'p' and self.en_passant == coords:
next_coord = coords[1] - 1
if next_coord in board.columns:
board.loc[coords[0], next_coord] = ''
self.en_passant = (None, None)
return board
def complete_castling(self, board, king_coords, king_movement_tracker):
'''
Returns updated board with the rook moved. Use this function AFTER the
king has moved into the castle positions
'''
# If rooks can't be moved at all or the king moved, exit function
if self.move_rook == [False, False] or king_movement_tracker[board.loc[king_coords]] is True:
return board
# Passing that condition means that the rook can still move
king = board.loc[king_coords]
# If the king is not on end ranks, then exit function
if king.isupper():
if not king_coords[1] == 7:
return board
else:
if not king_coords[1] == 0:
return board
if not (king_coords[0] == 2 or king_coords[0] == 6):
return board
if king.islower():
rook = 'r'
else:
rook = 'R'
median_x = pandas.Series(board.columns.tolist()).median()
if king_coords[0] < median_x:
king_placement = -1 # King is on left side
elif king_coords[0] > median_x:
king_placement = 1 # King is on right side
corners = self.get_board_corners(board, None)
if king_placement == -1: # Queen side
if king.islower():
rook_coords = corners[0]
else:
rook_coords = corners[1]
elif king_placement == 1: # King side
if king.islower():
rook_coords = corners[2]
else:
rook_coords = corners[3]
board.loc[rook_coords] = ''
board.loc[king_coords[0] - (king_placement), king_coords[1]] = rook
self.move_rook = [False, False]
return board
def get_board_corners(self, board, side):
'''
Returns in this format:
``[``
``top left,``
``bottom left,``
``top right,``
``bottom right,``
``]``
``side`` inputs:
``'white'`` for bottom left and bottom right
``'black'`` for top left and top right
/ANYTHING ELSE/ for all corners
'''
first_row = board.index[0] # This is the first row label
last_row = board.index[-1] # This is the last row label
first_col = board.columns[0] # This is the first (left) column label
last_col = board.columns[-1] # This is the last (right) column label
if side == 'white':
corners = [(first_row, last_col), (last_row, last_col)]
elif side == 'black':
corners = [(first_row, first_col), (last_row, first_col)]
else:
corners = [(first_row, first_col), (first_row, last_col),
(last_row, first_col), (last_row, last_col)]
return corners
def get_castle_coords(self, board, coords):
'''
Returns 4 coordinates in the form of tuples
left 2, left, right, right 2
These are the coordinates the king can move to
'''
x, y = coords
x_list = list(board.columns)
index = x_list.index(x)
neighbours_coords = []
if index - 2 >= 0:
neighbours_coords.append((x_list[index - 2], y))
if index - 1 >= 0:
neighbours_coords.append((x_list[index - 1], y))
# Check that the right 1 and right 2 squares exist
if index + 1 < len(x_list):
neighbours_coords.append((x_list[index + 1], y))
if index + 2 < len(x_list):
neighbours_coords.append((x_list[index + 2], y))
return neighbours_coords
def check_path_clear(self, board, coords):
'''
Returns whether left or right is clear or not in the form of
left_clear = ``True`` / ``False``
right_clear = ``True`` / ``False``
'''
x, y = coords
x_list = list(board.columns)
index = x_list.index(x)
# Check path to the left
left_clear = False
for i in range(index - 1, -1, -1):
if board.loc[x_list[i], y].lower() == 'r':
left_clear = True
elif not board.loc[x_list[i], y].lower() == '':
break
# Check path to right
right_clear = False
for i in range(index + 1, len(x_list)):
if board.loc[x_list[i], y].lower() == 'r':
right_clear = True
break
elif not board.loc[x_list[i], y].lower() == '':
break
return left_clear, right_clear
def check_for_hostile_king(self, board, king, legal_moves):
'''
Returns list with items to remove from legal moves that could cause a
face off between the kings
'''
to_remove = []
if king == 'K':
H_king_coords = self.find_piece(board, 'k')
H_king_moves = self.basic_king(board, 'k', H_king_coords)
else:
H_king_coords = self.find_piece(board, 'K')
H_king_moves = self.basic_king(board, 'K', H_king_coords)
for move in legal_moves:
if move in H_king_moves:
to_remove.append(move)
to_remove = list(set(to_remove))
for i in range(len(to_remove)):
legal_moves.remove(to_remove[0])
to_remove.pop(0)
i -= 1
return legal_moves
def is_king_defending_piece_to_attack(self, board, king, legal_moves):
'''
Returns a new set of legal moves that make sure the king can't attack
a piece being defended by the opposite king
The ``king`` argument is the king that wants to attack the piece that
is defended
'''
to_remove = []
if king == 'K':
H_king_coords = self.find_piece(board, 'k')
else:
H_king_coords = self.find_piece(board, 'K')
for move in legal_moves:
# Checking if there is a hostile piece in the kings legal moves
piece = board.loc[move]
if self.case_match(king, piece) is False and not piece == '':
piece_raycast_moves = self.basic_king(board, board.loc[move].swapcase(), move)
if H_king_coords in piece_raycast_moves:
to_remove.append(move)
to_remove = list(set(to_remove))
for i in range(len(to_remove)):
legal_moves.remove(to_remove[0])
to_remove.pop(0)
i -= 1
return legal_moves
def king_cannot_move_into_check(self, board, piece, coords, legal_moves,
king_movement_tracker,
rook_movement_tracker):
'''
Returns a new set of legal moves that doesn't involve the king getting
captured
'''
to_remove = []
# last_long_castle_tile = None # 3rd tile in O-O-O castling
if king_movement_tracker[piece] is False and self.check_for_check(board, piece) is False:
castle_coords = self.get_castle_coords(board, coords)
else:
castle_coords = []
if piece.isupper():
king = 'K'
side = 'white'
else:
king = 'k'
side = 'black'
corners = self.get_board_corners(board, side)
left_clear, right_clear = self.check_path_clear(board, coords)
for move in legal_moves:
copy = board.copy()
copy.loc[move] = king
copy.loc[coords] = ''
if self.check_for_check(copy, king):
to_remove.append(move)
if move in castle_coords:
# Left of the king
if move == castle_coords[0] or move == castle_coords[1]:
left_clear = False
# Right of the king
elif move == castle_coords[2] or move == castle_coords[3]:
right_clear = False
elif move in castle_coords:
if castle_coords.index(move) == 1 and left_clear:
if (king == 'K' and rook_movement_tracker['left R'] is False) or (king == 'k' and rook_movement_tracker['left r'] is False):
legal_moves.append(castle_coords[0])
elif castle_coords.index(move) == 2 and right_clear:
if (king == 'K' and rook_movement_tracker['right R'] is False) or (king == 'k' and rook_movement_tracker['right r'] is False):
if rook_movement_tracker['right R'] is False:
legal_moves.append(castle_coords[3])
else:
if rook_movement_tracker['right r'] is False:
legal_moves.append(castle_coords[3])
corners = self.get_board_corners(board, side)
if board.loc[corners[1]].lower() == 'r':
self.move_rook[1] = True
if not castle_coords == []:
if king == 'K':
if rook_movement_tracker['left R'] is False:
self.move_rook[0] = True
else:
if rook_movement_tracker['left r'] is False:
self.move_rook[0] = True
to_remove = list(set(to_remove))
for i in range(len(to_remove)):
legal_moves.remove(to_remove[0])
to_remove.pop(0)
i -= 1
return legal_moves
def no_ignore_check(self, board, piece, coords, legal_moves):
'''
Returns a new set of legal moves that makes sure the piece
does'nt leave the king hanging while in check
'''
to_remove = []
if piece.isupper():
king = 'K'
else:
king = 'k'
for move in legal_moves:
copy = board.copy()
copy.loc[move] = piece
copy.loc[coords] = ''
if self.en_passant == move:
# Saving is nessesary because complete_en_passant can change
saved = self.en_passant
copy = self.complete_en_passant(copy, piece, move)
self.en_passant = saved
if self.check_for_check(copy, king):
to_remove.append(move)
to_remove = list(set(to_remove))
for i in range(len(to_remove)):
legal_moves.remove(to_remove[0])
to_remove.pop(0)
i -= 1
return legal_moves
def check_for_check(self, board, king):
'''
Checks if there is a check on the board
The ``king`` argument is the king you want to check the CHECK
against. Input in the form of ``'k'`` or ``'K'``
'''
if king.isupper():
pieces = self.get_letters_coords(board, 'lower')
else:
pieces = self.get_letters_coords(board, 'upper')
king_coords = self.find_piece(board, king)
for i in range(len(pieces)):
piece = pieces[i][0]
coords = pieces[i][1]
L_piece = piece.lower()
if L_piece == 'p':
legal_moves = self.pawn(board, piece, coords)
if L_piece == 'r':
legal_moves = self.rook(board, piece, coords)
if L_piece == 'n':
legal_moves = self.knight(board, piece, coords)
if L_piece == 'b':
legal_moves = self.bishop(board, piece, coords)
if L_piece == 'q':
legal_moves = self.queen(board, piece, coords)
if king_coords in legal_moves:
return True
return False
def check_for_checkmate(self, board, king):
'''
Checks if there is a checkmate on the board. Must check for check
before using this function. If not, it will check for stalemate.
'''
if king.isupper():
pieces = self.get_letters_coords(board, 'upper')
else:
pieces = self.get_letters_coords(board, 'lower')
pieces.append((king, self.find_piece(board, king)))
for i in range(len(pieces)):
coords = pieces[i][1]
legal_moves = self.get_moves(board, coords, {'K': False,
'k': False},
{'left R': False,
'right R': False,
'left r': False,
'right r': False})
# If the piece can't do anything to prevent the check, then the
# legal moves would be empty. If the legal moves are not empty,
# then the piece can do something to prevent the check and
# therefore it is not a checkmate.
if not legal_moves == []:
return False
return True
def check_for_draw(self, board):
'''
Checks if there is a draw on the board. These are the conditions:
King vs. King
King vs. King + Bishop
King vs. King + Knight
King vs. King + 2 Bishops
King vs. King + 2 Knights
King + Bishop vs. King + Knight
'''
upper_letters = self.get_letters_coords(board, 'upper')
lower_letters = self.get_letters_coords(board, 'lower')
N = 0
n = 0
B = 0
b = 0
upper_others = 0
lower_others = 0
for item in upper_letters:
if item[0] == 'N':
N += 1
elif item[0] == 'B':
B += 1
else:
upper_others += 1
for item in lower_letters:
if item[0] == 'n':
n += 1
elif item[0] == 'b':
b += 1
else:
lower_others += 1
# If you don't check for check before checking for checkmate,
# it becomes a check for stalemate
if self.check_for_checkmate(board, 'K'):
return True
if self.check_for_checkmate(board, 'k'):
return True
# If the list if empty, then there is only 1 piece for that
# side which is the king
elif upper_letters == [] and lower_letters == []:
return True
# B + K vs k
# B + B + K vs k
elif B >= 1 and N == 0 and upper_others == 0 and lower_others == 0 and b == 0 and n == 0:
return True
# b + k vs K
# b + b + k vs K
elif b >= 1 and n == 0 and lower_others == 0 and upper_others == 0 and B == 0 and N == 0:
return True
# N + K vs k
# N + N + K vs k
elif N >= 1 and B == 0 and upper_others == 0 and lower_others == 0 and b == 0 and n == 0:
return True
# n + k vs K
# n + n + k vs K
elif n >= 1 and b == 0 and lower_others == 0 and upper_others == 0 and B == 0 and N == 0:
return True
# B + K vs b + k
elif B == 1 and N == 0 and upper_others == 0 and lower_others == 0 and b == 1 and n == 0:
return True
# N + N + K vs k
elif N == 2 and B == 0 and upper_others == 0 and lower_others == 0 and b == 0 and n == 0:
return True
# N + K vs n + k
elif N == 1 and B == 0 and upper_others == 0 and lower_others == 0 and n == 1 and b == 0:
return True
# N + K vs B + K
elif N == 1 and B == 0 and upper_others == 0 and lower_others == 0 and b == 1 and n == 0:
return True
# B + K vs n + K
elif B == 1 and N == 0 and upper_others == 0 and lower_others == 0 and n == 1 and b == 0:
return True
else:
return False