forked from gkyriopoulos/Koifish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Engine.py
1073 lines (972 loc) · 45 KB
/
Engine.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
import itertools
import Utils
class Engine:
def __init__(self, board_choice):
self._boardNormal = [
["br", "bn", "bb", "bq", "bk", "bb", "bn", "br"],
["bp", "bp", "bp", "bp", "bp", "bp", "bp", "bp"],
["**", "**", "**", "**", "**", "**", "**", "**"],
["**", "**", "**", "**", "**", "**", "**", "**"],
["**", "**", "**", "**", "**", "**", "**", "**"],
["**", "**", "**", "**", "**", "**", "**", "**"],
["wp", "wp", "wp", "wp", "wp", "wp", "wp", "wp"],
["wr", "wn", "wb", "wq", "wk", "wb", "wn", "wr"]]
self._boardLosAlamos = [
["br", "bn", "bq", "bk", "bn", "br"],
["bp", "bp", "bp", "bp", "bp", "bp"],
["**", "**", "**", "**", "**", "**"],
["**", "**", "**", "**", "**", "**"],
["wp", "wp", "wp", "wp", "wp", "wp"],
["wr", "wn", "wq", "wk", "wn", "wr"]]
self._boardMicroChess = [
["bk", "bn", "bb", "br"],
["bp", "**", "**", "**"],
["**", "**", "**", "**"],
["**", "**", "**", "wp"],
["wr", "wb", "wn", "wk"]]
self._boardTest = [
["br", "**", "**", "**", "bk", "**", "**", "br"],
["bp", "bp", "bp", "bp", "bp", "bp", "bp", "bp"],
["br", "br", "bb", "bq", "bk", "bb", "bn", "br"],
["**", "**", "**", "**", "wp", "**", "**", "**"],
["**", "**", "**", "**", "**", "**", "**", "**"],
["wr", "wn", "wb", "wq", "wk", "wb", "wn", "wr"],
["wp", "wp", "wp", "wp", "wp", "wp", "wp", "wp"],
["wr", "**", "**", "**", "wk", "**", "**", "wr"]]
self._boardRKvsRK = [
["**", "bk", "**", "**"],
["**", "br", "**", "**"],
["**", "**", "**", "**"],
["**", "**", "wr", "**"],
["**", "**", "wk", "**"]]
self._boardRNKvsRK = [
["**", "bk", "**", "**"],
["br", "**", "**", "**"],
["**", "**", "**", "**"],
["**", "**", "wn", "**"],
["**", "**", "wk", "wr"]]
self._boardRKvsRNK = [
["br", "bk", "**", "**"],
["**", "bn", "**", "**"],
["**", "**", "**", "**"],
["**", "**", "**", "wr"],
["**", "**", "wk", "**"]]
self._boardPKvsK = [
["**", "bk", "**", "**"],
["**", "**", "**", "**"],
["**", "**", "**", "**"],
["**", "**", "wk", "wp"],
["**", "**", "**", "**"]]
self._boardKvsPK = [
["**", "**", "**", "**"],
["bp", "bk", "**", "**"],
["**", "**", "**", "**"],
["**", "**", "**", "**"],
["**", "**", "wk", "**"]]
if board_choice == "MicroChess":
self.board = self._boardMicroChess
self.dim_x = 4
self.dim_y = 5
self.king_pos_b = [0, 0]
self.king_pos_w = [4, 3]
self.rook_small_w = None
self.rook_big_w = (4, 0)
self.rook_small_b = None
self.rook_big_b = (0, 3)
self.king_b_moved = False
self.king_w_moved = False
self.rook_small_w_moved = False
self.rook_big_w_moved = False
self.rook_small_b_moved = False
self.rook_big_b_moved = False
elif board_choice == "LosAlamos":
self.board = self._boardLosAlamos
self.dim_x = 6
self.dim_y = 6
self.king_pos_b = [0, 3]
self.king_pos_w = [5, 3]
self.rook_small_w = (5, 5)
self.rook_big_w = (5, 0)
self.rook_small_b = (0, 5)
self.rook_big_b = (0, 0)
self.king_b_moved = False
self.king_w_moved = False
self.rook_small_w_moved = False
self.rook_big_w_moved = False
self.rook_small_b_moved = False
self.rook_big_b_moved = False
elif board_choice == "Normal":
self.board = self._boardNormal
self.dim_x = 8
self.dim_y = 8
self.king_pos_b = [0, 4]
self.king_pos_w = [7, 4]
self.rook_small_w = (7, 7)
self.rook_big_w = (7, 0)
self.rook_small_b = (0, 7)
self.rook_big_b = (0, 0)
self.king_b_moved = False
self.king_w_moved = False
self.rook_small_w_moved = False
self.rook_big_w_moved = False
self.rook_small_b_moved = False
self.rook_big_b_moved = False
elif board_choice == "RKvsRK":
self.board = self._boardRKvsRK
self.dim_x = 4
self.dim_y = 5
self.king_pos_b = [0, 1]
self.king_pos_w = [4, 2]
self.rook_small_w = None
self.rook_big_w = (3, 2)
self.rook_small_b = None
self.rook_big_b = (1, 1)
self.king_b_moved = False
self.king_w_moved = False
self.rook_small_w_moved = False
self.rook_big_w_moved = False
self.rook_small_b_moved = False
self.rook_big_b_moved = False
elif board_choice == "RKvsRNK":
self.board = self._boardRKvsRNK
self.dim_x = 4
self.dim_y = 5
self.king_pos_b = [0, 1]
self.king_pos_w = [4, 2]
self.rook_small_w = None
self.rook_big_w = (4, 3)
self.rook_small_b = None
self.rook_big_b = (1, 0)
self.king_b_moved = False
self.king_w_moved = False
self.rook_small_w_moved = False
self.rook_big_w_moved = False
self.rook_small_b_moved = False
self.rook_big_b_moved = False
elif board_choice == "RNKvsRK":
self.board = self._boardRNKvsRK
self.dim_x = 4
self.dim_y = 5
self.king_pos_b = [0, 1]
self.king_pos_w = [4, 2]
self.rook_small_w = None
self.rook_big_w = (3, 3)
self.rook_small_b = None
self.rook_big_b = (0, 0)
self.king_b_moved = False
self.king_w_moved = False
self.rook_small_w_moved = False
self.rook_big_w_moved = False
self.rook_small_b_moved = False
self.rook_big_b_moved = False
elif board_choice == "KvsPK":
self.board = self._boardKvsPK
self.dim_x = 4
self.dim_y = 5
self.king_pos_b = [0, 1]
self.king_pos_w = [3, 2]
self.rook_small_w = None
self.rook_big_w = False
self.rook_small_b = None
self.rook_big_b = None
self.king_b_moved = False
self.king_w_moved = False
self.rook_small_w_moved = False
self.rook_big_w_moved = False
self.rook_small_b_moved = False
self.rook_big_b_moved = False
elif board_choice == "PKvsK":
self.board = self._boardPKvsK
self.dim_x = 4
self.dim_y = 5
self.king_pos_b = [1, 1]
self.king_pos_w = [4, 2]
self.rook_small_w = None
self.rook_big_w = False
self.rook_small_b = None
self.rook_big_b = None
self.king_b_moved = False
self.king_w_moved = False
self.rook_small_w_moved = False
self.rook_big_w_moved = False
self.rook_small_b_moved = False
self.rook_big_b_moved = False
self.moves = 0
self.pieces = ["r", "n", "b", "q", "k", "p"]
self.board_choice = board_choice
self.repetition_counter = 0
self.score = 0
self.winner = "None"
self.turn_player = "w"
self.previous_move = []
self.pseudolegal_moves = []
self.legal_moves = []
self.threatmap = []
self.pinrays = []
self.checkers = []
self.previous_positions = []
self.generate_legal_moves(self.turn_player)
# I use this to player swap in player vs player mode it's probably not need.
self.board_has_changed = False
def attempt_move(self, src, dst, player):
if self.winner == "None":
if (src, dst) in self.legal_moves:
self.previous_move = [src, dst]
self._make_move(src, dst, player)
return self.board, self.score
else:
self.board_has_changed = False
return self.board, 0
def _make_move(self, src, dst, player):
if player != self.turn_player:
print("It's not your turn yet.")
self.board_has_changed = False
return self.board
# In case we have castling.
if player == "w":
if self.is_king(src):
self._set_king_pos(dst, player)
self.king_w_moved = True
if self.is_rook(src):
if src == self.rook_small_w:
self.rook_small_w_moved = True
if src == self.rook_big_w:
self.rook_big_w_moved = True
else:
if self.is_king(src):
self._set_king_pos(dst, player)
self.king_b_moved = True
if self.is_rook(src):
if src == self.rook_small_b:
self.rook_small_b_moved = True
if src == self.rook_big_b:
self.rook_big_b_moved = True
if src == (0, 4) and dst == (0, 7):
self.board[src[0]][src[1]] = "**"
self.board[dst[0]][dst[1]] = "**"
self.board[0][6] = "bk"
self.board[0][5] = "br"
self._set_king_pos((0, 6), "b")
self.king_b_moved = True
elif src == (0, 4) and dst == (0, 0):
self.board[src[0]][src[1]] = "**"
self.board[dst[0]][dst[1]] = "**"
self.board[0][2] = "bk"
self.board[0][3] = "br"
self._set_king_pos((0, 2), "b")
self.king_b_moved = True
elif src == (7, 4) and dst == (7, 7):
self.board[src[0]][src[1]] = "**"
self.board[dst[0]][dst[1]] = "**"
self.board[7][6] = "wk"
self.board[7][5] = "wr"
self._set_king_pos((7, 6), "w")
self.king_w_moved = True
elif src == (7, 4) and dst == (7, 0):
self.board[src[0]][src[1]] = "**"
self.board[dst[0]][dst[1]] = "**"
self.board[7][2] = "wk"
self.board[7][3] = "wr"
self._set_king_pos((7, 2), "w")
self.king_w_moved = True
else:
piece = self.get_piece(dst)
# Care might conflict with pawn move forwards
if self.is_pawn(src) and self.get_piece(dst) == "*":
if player == "w":
self.board[dst[0]][dst[1]] = self.board[src[0]][src[1]]
self.board[src[0]][src[1]] = "**"
if self.board[dst[0] + 1][dst[1]] == "bp":
self.score += 1
self.board[dst[0] + 1][dst[1]] = "**"
else:
self.board[dst[0]][dst[1]] = self.board[src[0]][src[1]]
self.board[src[0]][src[1]] = "**"
if self.board[dst[0] - 1][dst[1]] == "wp":
self.score -= 1
self.board[dst[0] - 1][dst[1]] = "**"
else:
# Big head way to calculate score
flag = 1 if player == "w" else -1
if piece != "*":
if piece == "p":
self.score += flag * 1
elif piece == "n":
self.score += flag * 3
elif piece == "b":
self.score += flag * 3
elif piece == "r":
self.score += flag * 5
elif piece == "q":
self.score += flag * 9
self.board[dst[0]][dst[1]] = self.board[src[0]][src[1]]
self.board[src[0]][src[1]] = "**"
# Promotion stuff
if player == "w":
if self.is_pawn(dst) and dst[0] == 0:
if self.board_choice == "Normal":
self.board[dst[0]][dst[1]] = "wq"
self.score += 9
else:
self.board[dst[0]][dst[1]] = "wr"
self.score += 5
else:
if self.is_pawn(dst) and dst[0] == self.dim_y - 1:
if self.board_choice == "Normal":
self.board[dst[0]][dst[1]] = "bq"
self.score -= 9
else:
self.board[dst[0]][dst[1]] = "br"
self.score -= 5
# Swaps the player and calculates legal moves for the next player.
self.turn_player = "b" if self.turn_player == "w" else "w"
self.board_has_changed = True
self.pseudolegal_moves.clear()
self.threatmap.clear()
self.legal_moves.clear()
self.pinrays.clear()
self.checkers.clear()
# Note: If you change the position of generate legal moves you will have issue with pawns and checks because
# you move the pawn and then check for a check BE CAREFUL!
self.generate_legal_moves(self.turn_player)
self.moves += 1
def generate_legal_moves(self, color):
# Generating the pseudo legal moves
self._generate_pseudolegal_moves(color)
self.legal_moves = self.pseudolegal_moves
legal_moves_set = set(self.legal_moves)
king_moves = self._get_king_moves(self._get_king_pos(color), color)
king_moves_set = set([x[1] for x in king_moves])
threatmap_set = set(self.threatmap)
king_illegal_moves = set([x for x in king_moves if x[1] in (king_moves_set & threatmap_set)])
pins, pin_axis_moves, self.pinrays = self._get_absolute_pins(color)
# Removing the illegal moves.
for pin in pins:
pinned_illegal_moves = set(self._get_moves(pin, color)) - set(pin_axis_moves)
legal_moves_set -= pinned_illegal_moves
legal_moves_set -= king_illegal_moves
self.legal_moves = list(legal_moves_set)
# Adding the castle moves if they exist
castle_big_move = self._big_castle_move(color)
castle_small_move = self._small_castle_move(color)
if castle_big_move:
self.legal_moves.append(castle_big_move)
if castle_small_move:
self.legal_moves.append(castle_small_move)
self.checkers = self._check_check(color)
if len(self.checkers) > 1:
king_legal_moves = [x for x in king_moves if x[1] not in (king_moves_set & threatmap_set)]
self.legal_moves = king_legal_moves
if not self.legal_moves:
self.winner = "w" if color == "b" else "b"
return
elif len(self.checkers) == 1:
checker_captures = set([x for x in self.legal_moves if x[1] == self.checkers[0]])
king_legal_moves = set([x for x in king_moves if x[1] not in (king_moves_set & threatmap_set)])
squares_between_king_and_checker = self._get_squares_between(self._get_king_pos(color), self.checkers[0])
push_moves = set([x for x in self.legal_moves if x[1] in squares_between_king_and_checker])
self.legal_moves = checker_captures | king_legal_moves | push_moves
if not self.legal_moves:
self.winner = "w" if color == "b" else "b"
return
# Remove empty moves
self.legal_moves = [x for x in self.legal_moves if x]
if not self.legal_moves:
self.winner = "d"
if self._check_ins_material():
self.winner = "d"
if self._check_threefold_repetition(self.previous_positions):
self.winner = "d"
def _generate_pseudolegal_moves(self, color):
for i in range(self.dim_x):
for j in range(self.dim_y):
piece_color = self.get_color((j, i))
enemy_color = "b" if color == "w" else "w"
if piece_color == color:
moves = self._get_moves((j, i), color)
self.pseudolegal_moves.append(moves)
elif piece_color == enemy_color:
threatmap = self._get_threatmap((j, i), piece_color)
self.threatmap.append(threatmap)
# Remove empty moves
self.pseudolegal_moves = [x for x in self.pseudolegal_moves if x]
self.threatmap = [x for x in self.threatmap if x]
# Flatten the list of moves
self.pseudolegal_moves = list(itertools.chain(*self.pseudolegal_moves))
self.threatmap = list(itertools.chain(*self.threatmap))
# Getting ONLY the dst coordinate.
self.threatmap = [threat[1] for threat in self.threatmap]
# Removing duplicate threats
self.threatmap.sort()
grouped = itertools.groupby(self.threatmap)
self.threatmap = [key for key, _ in grouped]
# Access a location on the board finds if there is a piece on it and depending on the piece's type
# get its moves it also returns the threat-map for each piece (ray pieces(rook,bishop)) use a different function
# for the threat-map calculation
def _get_moves(self, src, color):
if self.is_pawn((src[0], src[1])):
return self._get_pawn_moves((src[0], src[1]), color)
elif self.is_rook((src[0], src[1])):
return self._get_rook_moves((src[0], src[1]), color)
elif self.is_knight((src[0], src[1])):
return self._get_knight_moves((src[0], src[1]), color)
elif self.is_bishop((src[0], src[1])):
return self._get_bishop_moves((src[0], src[1]), color)
elif self.is_king((src[0], src[1])):
return self._get_king_moves((src[0], src[1]), color)
elif self.is_queen((src[0], src[1])):
return self._get_queen_moves((src[0], src[1]), color)
# Theoretically finds a piece's moves given its location.
# Think of it as: If I place x piece on a square what would be the available moves in the current board?
def get_pieces_moves(self, src, piece, color):
if piece == "p":
return self._get_pawn_moves(src, color)
if piece == "b":
return self._get_bishop_moves(src, color)
if piece == "n":
return self._get_knight_moves(src, color)
if piece == "r":
return self._get_rook_moves(src, color)
if piece == "q":
return self._get_queen_moves(src, color)
if piece == "k":
return self._get_king_moves(src, color)
# TODO: EN PASSANT CHANGES THE MOVES FOR THE PAWNS CARE!!!!!!!!!
def _get_pawn_moves(self, src, color):
if self.board_choice == "MicroChess" or "RKvsRK" or "PKvsK" or "KvsPK" or "RNKvsRK" or "RKvsRNK":
moves = []
if color == "w":
directions = [(-1, -1), (-1, 0), (-1, 1)]
for d in directions:
calc_y = src[0] + d[0]
calc_x = src[1] + d[1]
if 0 <= calc_y < self.dim_y and 0 <= calc_x < self.dim_x:
if d == (-1, 0) and self.get_piece((calc_y, calc_x)) == "*":
moves.append(((src[0], src[1]), (calc_y, calc_x)))
if (d == (-1, -1) or d == (-1, 1)) and \
self.get_piece((calc_y, calc_x)) != "*" and self.get_color((calc_y, calc_x)) != color:
moves.append(((src[0], src[1]), (calc_y, calc_x)))
else:
directions = [(1, -1), (1, 0), (1, 1)]
for d in directions:
calc_y = src[0] + d[0]
calc_x = src[1] + d[1]
if 0 <= calc_y < self.dim_y and 0 <= calc_x < self.dim_x:
if d == (1, 0) and self.get_piece((calc_y, calc_x)) == "*":
moves.append(((src[0], src[1]), (calc_y, calc_x)))
if (d == (1, -1) or d == (1, 1)) and \
self.get_piece((calc_y, calc_x)) != "*" and self.get_color((calc_y, calc_x)) != color:
moves.append(((src[0], src[1]), (calc_y, calc_x)))
else:
moves = []
if color == "w":
directions = [(-1, -1), (-1, 0), (-2, 0), (-1, 1)]
for d in directions:
calc_y = src[0] + d[0]
calc_x = src[1] + d[1]
if 0 <= calc_y < self.dim_y and 0 <= calc_x < self.dim_x:
if d == (-1, 0) and self.get_piece((calc_y, calc_x)) == "*":
moves.append(((src[0], src[1]), (calc_y, calc_x)))
if d == (-2, 0) and self.get_piece((calc_y, calc_x)) == "*" \
and self.get_piece((calc_y + 1, calc_x)) == "*" and src[0] == (self.dim_y - 1) - 1:
moves.append(((src[0], src[1]), (calc_y, calc_x)))
if (d == (-1, -1) or d == (-1, 1)) and \
self.get_piece((calc_y, calc_x)) != "*" and self.get_color((calc_y, calc_x)) != color:
moves.append(((src[0], src[1]), (calc_y, calc_x)))
if (d == (-1, -1) and len(self.previous_move) > 0 and self.get_piece(
(calc_y, calc_x)) == "*" and self.get_piece((src[0], src[1] - 1)) == "p") \
and self.get_color((src[0], src[1] - 1)) == "b":
if ((self.previous_move[0][0] - self.previous_move[1][0],
self.previous_move[0][1] - self.previous_move[1][1]) == (-2, 0)):
moves.append(((src[0], src[1]), (calc_y, calc_x)))
if (d == (-1, 1) and len(self.previous_move) > 0 and self.get_piece(
(calc_y, calc_x)) == "*" and self.get_piece((src[0], src[1] + 1)) == "p") \
and self.get_color((src[0], src[1] + 1)) == "b":
if ((self.previous_move[0][0] - self.previous_move[1][0],
self.previous_move[0][1] - self.previous_move[1][1]) == (-2, 0)):
moves.append(((src[0], src[1]), (calc_y, calc_x)))
else:
directions = [(1, -1), (1, 0), (2, 0), (1, 1)]
for d in directions:
calc_y = src[0] + d[0]
calc_x = src[1] + d[1]
if 0 <= calc_y < self.dim_y and 0 <= calc_x < self.dim_x:
if d == (1, 0) and self.get_piece((calc_y, calc_x)) == "*":
moves.append(((src[0], src[1]), (calc_y, calc_x)))
if d == (2, 0) and self.get_piece((calc_y, calc_x)) == "*" \
and self.get_piece((calc_y - 1, calc_x)) == "*" and src[0] == 1:
moves.append(((src[0], src[1]), (calc_y, calc_x)))
if (d == (1, -1) or d == (1, 1)) and self.get_piece((calc_y, calc_x)) != "*" and self.get_color(
(calc_y, calc_x)) != color:
moves.append(((src[0], src[1]), (calc_y, calc_x)))
if (d == (1, 1) and len(self.previous_move) > 0 and self.get_piece((calc_y, calc_x)) == "*"
and self.get_color((src[0], src[1] + 1)) == "w"):
if ((self.previous_move[0][0] - self.previous_move[1][0],
self.previous_move[0][1] - self.previous_move[1][1]) == (2, 0)):
moves.append(((src[0], src[1]), (calc_y, calc_x)))
if (d == (1, -1) and len(self.previous_move) > 0 and self.get_piece((calc_y, calc_x)) == "*"
and self.get_color((src[0], src[1] - 1)) == "w"):
if ((self.previous_move[0][0] - self.previous_move[1][0],
self.previous_move[0][1] - self.previous_move[1][1]) == (2, 0)):
moves.append(((src[0], src[1]), (calc_y, calc_x)))
return moves
def _get_rook_moves(self, src, color):
moves = []
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
# Dim_y is chosen here because it is the biggest dimension
# it requires some thought in order to be clear why it is like that
# Hint: The line that a rook moves on is infinitely big
biggest_dim = self.dim_y if self.dim_y >= self.dim_x else self.dim_x
for d in directions:
for i in range(1, biggest_dim):
calc_y = src[0] + i * d[0]
calc_x = src[1] + i * d[1]
if 0 <= calc_y < self.dim_y and 0 <= calc_x < self.dim_x:
if self.get_piece([calc_y, calc_x]) == "*":
moves.append(((src[0], src[1]), (calc_y, calc_x)))
elif self.get_color([calc_y, calc_x]) != color:
# If an enemy piece is found in this direction break and check another direction
moves.append(((src[0], src[1]), (calc_y, calc_x)))
break
else:
break
else:
break
return moves
def _get_knight_moves(self, src, color):
moves = []
directions = [(-2, -1), (-1, -2), (2, -1), (1, -2), (2, 1), (1, 2), (-2, 1), (-1, 2)]
for d in directions:
calc_y = src[0] + d[0]
calc_x = src[1] + d[1]
if 0 <= calc_y < self.dim_y and 0 <= calc_x < self.dim_x:
# Knight only cares about the landing square
if self.get_color([calc_y, calc_x]) != color:
moves.append(((src[0], src[1]), (calc_y, calc_x)))
return moves
# Same as rook only thing that changes is the direction of motion.
def _get_bishop_moves(self, src, color):
moves = []
directions = [(-1, -1), (-1, 1), (1, -1), (1, 1)]
biggest_dim = self.dim_y if self.dim_y >= self.dim_x else self.dim_x
for d in directions:
for i in range(1, biggest_dim):
calc_y = src[0] + i * d[0]
calc_x = src[1] + i * d[1]
if 0 <= calc_y < self.dim_y and 0 <= calc_x < self.dim_x:
if self.get_piece([calc_y, calc_x]) == "*":
moves.append(((src[0], src[1]), (calc_y, calc_x)))
elif self.get_color([calc_y, calc_x]) != color:
# If an enemy piece is found in this direction break and check another direction
moves.append(((src[0], src[1]), (calc_y, calc_x)))
break
else:
break
else:
break
return moves
def _get_king_moves(self, src, color):
moves = []
directions = [(-1, -1), (-1, 1), (1, -1), (1, 1), (-1, 0), (1, 0), (0, -1), (0, 1)]
for d in directions:
calc_y = src[0] + d[0]
calc_x = src[1] + d[1]
if 0 <= calc_y < self.dim_y and 0 <= calc_x < self.dim_x:
if self.get_color([calc_y, calc_x]) != color:
moves.append(((src[0], src[1]), (calc_y, calc_x)))
return moves
def _get_queen_moves(self, src, color):
moves = [self._get_rook_moves(src, color), self._get_bishop_moves(src, color)]
# Remove empty moves
moves = [x for x in moves if x]
# Flatten the list of moves
moves = list(itertools.chain(*moves))
return moves
# Threat maps for non ray pieces are the same as their moves except pawns
# because the only capture diagonally.
def _get_threatmap(self, src, color):
if self.is_pawn((src[0], src[1])):
return self._get_pawn_threatmap((src[0], src[1]), color)
elif self.is_rook((src[0], src[1])):
return self._get_rook_threatmap((src[0], src[1]), color)
elif self.is_knight((src[0], src[1])):
return self._get_knight_threatmap((src[0], src[1]))
elif self.is_bishop((src[0], src[1])):
return self._get_bishop_threatmap((src[0], src[1]), color)
elif self.is_king((src[0], src[1])):
return self._get_king_threatmap((src[0], src[1]))
elif self.is_queen((src[0], src[1])):
return self._get_queen_threatmap((src[0], src[1]), color)
# TODO: EN PASSANT CHANGES THE THREATMAP FOR THE PAWNS CARE!!!!!!!!!
def _get_pawn_threatmap(self, src, color):
threatmap = []
if color == "w":
directions = [(-1, -1), (-1, 1)]
for d in directions:
calc_y = src[0] + d[0]
calc_x = src[1] + d[1]
if 0 <= calc_y < self.dim_y and 0 <= calc_x < self.dim_x:
# Append regardless of the square
threatmap.append(((src[0], src[1]), (calc_y, calc_x)))
else:
directions = [(1, -1), (1, 1)]
for d in directions:
calc_y = src[0] + d[0]
calc_x = src[1] + d[1]
if 0 <= calc_y < self.dim_y and 0 <= calc_x < self.dim_x:
threatmap.append(((src[0], src[1]), (calc_y, calc_x)))
return threatmap
def _get_rook_threatmap(self, src, color):
# Getting enemy kings coords
if color == "w":
temp_king = self.king_pos_b
piece = "bk"
else:
temp_king = self.king_pos_w
piece = "wk"
# Temporarily removing enemy king from the board
self.board[temp_king[0]][temp_king[1]] = "**"
# Getting our moves.
threatmap = []
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
# Dim_y is chosen here because it is the biggest dimension
# it requires some thought in order to be clear why it is like that
# Hint: The line that a rook moves on is infinitely big
biggest_dim = self.dim_y if self.dim_y >= self.dim_x else self.dim_x
for d in directions:
for i in range(1, biggest_dim):
calc_y = src[0] + i * d[0]
calc_x = src[1] + i * d[1]
if 0 <= calc_y < self.dim_y and 0 <= calc_x < self.dim_x:
if self.get_piece([calc_y, calc_x]) == "*":
threatmap.append(((src[0], src[1]), (calc_y, calc_x)))
# If a piece is found regardless of color it's intended trust me.
elif self.get_color([calc_y, calc_x]) != "*":
threatmap.append(((src[0], src[1]), (calc_y, calc_x)))
break
else:
break
else:
break
# Restoring enemy king to his original place
self.board[temp_king[0]][temp_king[1]] = piece
return threatmap
def _get_knight_threatmap(self, src):
threatmap = []
directions = [(-2, -1), (-1, -2), (2, -1), (1, -2), (2, 1), (1, 2), (-2, 1), (-1, 2)]
for d in directions:
calc_y = src[0] + d[0]
calc_x = src[1] + d[1]
if 0 <= calc_y < self.dim_y and 0 <= calc_x < self.dim_x:
# Knight only cares about the landing square
threatmap.append(((src[0], src[1]), (calc_y, calc_x)))
return threatmap
# Same as get rook threatmap
def _get_bishop_threatmap(self, src, color):
if color == "w":
temp_king = self.king_pos_b
piece = "bk"
else:
temp_king = self.king_pos_w
piece = "wk"
self.board[temp_king[0]][temp_king[1]] = "**"
threatmap = []
directions = [(-1, -1), (-1, 1), (1, -1), (1, 1)]
biggest_dim = self.dim_y if self.dim_y >= self.dim_x else self.dim_x
for d in directions:
for i in range(1, biggest_dim):
calc_y = src[0] + i * d[0]
calc_x = src[1] + i * d[1]
if 0 <= calc_y < self.dim_y and 0 <= calc_x < self.dim_x:
if self.get_piece([calc_y, calc_x]) == "*":
threatmap.append(((src[0], src[1]), (calc_y, calc_x)))
elif self.get_color([calc_y, calc_x]) != "*":
# If an enemy piece is found in this direction break and check another direction
threatmap.append(((src[0], src[1]), (calc_y, calc_x)))
break
else:
break
else:
break
self.board[temp_king[0]][temp_king[1]] = piece
return threatmap
def _get_king_threatmap(self, src):
threatmap = []
directions = [(-1, -1), (-1, 1), (1, -1), (1, 1), (-1, 0), (1, 0), (0, -1), (0, 1)]
for d in directions:
calc_y = src[0] + d[0]
calc_x = src[1] + d[1]
if 0 <= calc_y < self.dim_y and 0 <= calc_x < self.dim_x:
threatmap.append(((src[0], src[1]), (calc_y, calc_x)))
return threatmap
def _get_queen_threatmap(self, src, color):
threatmap = [self._get_rook_threatmap(src, color), self._get_bishop_threatmap(src, color)]
threatmap = [x for x in threatmap if x]
threatmap = list(itertools.chain(*threatmap))
return threatmap
# Just check's if there is a check I thought I sounded funny xD
# Check is the player->color has any checks. And returns checking piece's position.
def _check_check(self, color):
king_pos = self.king_pos_w if color == "w" else self.king_pos_b
checkers = []
for piece in self.pieces:
moves = self.get_pieces_moves(king_pos, piece, color)
for move in moves:
if self.get_piece(move[1]) == piece:
checkers.append(move[1])
return checkers
def _small_castle_move(self, color):
if color == "w":
if self._check_small_castle(color):
if (7, 4) not in self.threatmap and \
(7, 5) not in self.threatmap and \
(7, 6) not in self.threatmap:
return (7, 4), (7, 7)
else:
return []
else:
return []
else:
if self._check_small_castle(color):
if (0, 4) not in self.threatmap and \
(0, 5) not in self.threatmap and \
(0, 6) not in self.threatmap:
return (0, 4), (0, 7)
else:
return []
else:
return []
def _big_castle_move(self, color):
if color == "w":
if self._check_big_castle(color):
if (7, 4) not in self.threatmap and \
(7, 1) not in self.threatmap and \
(7, 2) not in self.threatmap and \
(7, 3) not in self.threatmap:
return (7, 4), (7, 0)
else:
return []
else:
return []
else:
if self._check_big_castle(color):
if (0, 4) not in self.threatmap and \
(0, 1) not in self.threatmap and \
(0, 2) not in self.threatmap and \
(0, 3) not in self.threatmap:
return (0, 4), (0, 0)
else:
return []
else:
return []
def _check_big_castle(self, color):
if self.board_choice == "Normal":
if color == "w":
if not self.king_w_moved and not self.rook_big_w_moved and \
self.board[7][0] == "wr" and self.board[7][1] == "**" and \
self.board[7][2] == "**" and self.board[7][3] == "**":
return True
else:
return False
else:
if not self.king_b_moved and not self.rook_big_b_moved and \
self.board[0][0] == "br" and self.board[0][1] == "**" and \
self.board[0][2] == "**" and self.board[0][3] == "**":
return True
else:
return False
else:
return False
def _check_small_castle(self, color):
if self.board_choice == "Normal":
if color == "w":
if not self.king_w_moved and not self.rook_small_w_moved and \
self.board[7][7] == "wr" and self.board[7][5] == "**" \
and self.board[7][6] == "**":
return True
else:
return False
else:
if not self.king_b_moved and not self.rook_small_b_moved and \
self.board[0][7] == "br" and self.board[0][5] == "**" \
and self.board[0][6] == "**":
return True
else:
return False
else:
return False
def get_color(self, src):
return self.board[src[0]][src[1]][0]
def get_piece(self, src):
return self.board[src[0]][src[1]][1]
def is_king(self, src):
return True if self.get_piece((src[0], src[1])) == "k" else False
def is_queen(self, src):
return True if self.get_piece((src[0], src[1])) == "q" else False
def is_rook(self, src):
return True if self.get_piece((src[0], src[1])) == "r" else False
def is_bishop(self, src):
return True if self.get_piece((src[0], src[1])) == "b" else False
def is_knight(self, src):
return True if self.get_piece((src[0], src[1])) == "n" else False
def is_pawn(self, src):
return True if self.get_piece((src[0], src[1])) == "p" else False
def _set_king_pos(self, src, color):
if color == "w":
self.king_pos_w = src
else:
self.king_pos_b = src
def _get_king_pos(self, color):
return self.king_pos_b if color == "b" else self.king_pos_w
def _get_absolute_pins(self, color):
king = self._get_king_pos(color)
enemy_color = "w" if color == "b" else "b"
directions = [(-1, 0), (1, 0), (0, -1), (0, 1),
(-1, -1), (-1, 1), (1, -1), (1, 1)]
j = 0
pins = []
pin_ray = []
biggest_dim = self.dim_y if self.dim_y >= self.dim_x else self.dim_x
for d in directions:
counter = 0
pins.insert(j, [])
pin_ray.insert(j, [])
for i in range(1, biggest_dim):
calc_y = king[0] + i * d[0]
calc_x = king[1] + i * d[1]
if 0 <= calc_y < self.dim_y and 0 <= calc_x < self.dim_x:
piece_color = self.get_color((calc_y, calc_x))
piece = self.get_piece((calc_y, calc_x))
if piece_color == color:
if counter == 0:
pins[j].append((calc_y, calc_x))
pin_ray[j].append((calc_y, calc_x))
counter += 1
elif counter == 1:
pin_ray[j] = []
pins[j] = []
break
elif pins[j] and piece_color == enemy_color:
if piece in ("b", "r", "q"):
# If the enemy piece is a rook at the direction we are looking at
# is a straight line
if piece == "r" and d in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
pins[j].append((calc_y, calc_x))
pin_ray[j].append((calc_y, calc_x))
break
# If the enemy piece is a bishop at the direction we are looking at
# is a diagonal
elif piece == "b" and d in [(-1, -1), (-1, 1), (1, -1), (1, 1)]:
pins[j].append((calc_y, calc_x))
pin_ray[j].append((calc_y, calc_x))
break
elif piece == "q":
pins[j].append((calc_y, calc_x))
pin_ray[j].append((calc_y, calc_x))
break
else:
break
else:
break
else:
pin_ray[j].append((calc_y, calc_x))
else:
break
# If you didn't find a pin
if len(pins[j]) != 2:
pin_ray[j] = []
pins[j] = []
j += 1
pins = [x[0] for x in pins if x]
pin_ray = [x for x in pin_ray if x]
pin_axis_moves = [[(pins[i], element) for element in pin_ray[i]] for i in range(len(pins))]
pin_ray = list(itertools.chain(*pin_ray))
pin_axis_moves = list(itertools.chain(*pin_axis_moves))
return pins, pin_axis_moves, pin_ray
def _get_squares_between(self, src, dst):
if not self.is_knight(dst):
direction = [dst[0] - src[0], dst[1] - src[1]]
max_val = max([abs(direction[0]), abs(direction[1])])
direction = [int(d / max_val) for d in direction]
biggest_dim = self.dim_y if self.dim_y >= self.dim_x else self.dim_x
piece = self.get_piece(dst)
squares = []
for i in range(1, biggest_dim):
calc_y = src[0] + i * direction[0]
calc_x = src[1] + i * direction[1]
if 0 <= calc_y < self.dim_y and 0 <= calc_x < self.dim_x:
if self.get_piece((calc_y, calc_x)) == piece:
break
else:
squares.append((calc_y, calc_x))
return squares
else:
return []
def _check_ins_material(self):