forked from niklasf/python-chess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·4871 lines (3950 loc) · 203 KB
/
test.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
863
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 python3
import asyncio
import copy
import logging
import os
import os.path
import platform
import sys
import tempfile
import textwrap
import unittest
import io
import chess
import chess.gaviota
import chess.engine
import chess.pgn
import chess.polyglot
import chess.svg
import chess.syzygy
import chess.variant
class RaiseLogHandler(logging.StreamHandler):
def handle(self, record):
super().handle(record)
raise RuntimeError("was expecting no log messages")
def catchAndSkip(signature, message=None):
def _decorator(f):
def _wrapper(self):
try:
return f(self)
except signature as err:
raise unittest.SkipTest(message or err)
return _wrapper
return _decorator
class SquareTestCase(unittest.TestCase):
def test_square(self):
for square in chess.SQUARES:
file_index = chess.square_file(square)
rank_index = chess.square_rank(square)
self.assertEqual(chess.square(file_index, rank_index), square, chess.square_name(square))
def test_shifts(self):
shifts = [
chess.shift_down,
chess.shift_2_down,
chess.shift_up,
chess.shift_2_up,
chess.shift_right,
chess.shift_2_right,
chess.shift_left,
chess.shift_2_left,
chess.shift_up_left,
chess.shift_up_right,
chess.shift_down_left,
chess.shift_down_right,
]
for shift in shifts:
for bb_square in chess.BB_SQUARES:
shifted = shift(bb_square)
c = chess.popcount(shifted)
self.assertLessEqual(c, 1)
self.assertEqual(c, chess.popcount(shifted & chess.BB_ALL))
def test_parse_square(self):
self.assertEqual(chess.parse_square("a1"), 0)
with self.assertRaises(ValueError):
self.assertEqual(chess.parse_square("A1"))
with self.assertRaises(ValueError):
self.assertEqual(chess.parse_square("a0"))
def test_square_distance(self):
self.assertEqual(chess.square_distance(chess.A1, chess.A1), 0)
self.assertEqual(chess.square_distance(chess.A1, chess.H8), 7)
self.assertEqual(chess.square_distance(chess.E1, chess.E8), 7)
self.assertEqual(chess.square_distance(chess.A4, chess.H4), 7)
self.assertEqual(chess.square_distance(chess.D4, chess.E5), 1)
def test_square_manhattan_distance(self):
self.assertEqual(chess.square_manhattan_distance(chess.A1, chess.A1), 0)
self.assertEqual(chess.square_manhattan_distance(chess.A1, chess.H8), 14)
self.assertEqual(chess.square_manhattan_distance(chess.E1, chess.E8), 7)
self.assertEqual(chess.square_manhattan_distance(chess.A4, chess.H4), 7)
self.assertEqual(chess.square_manhattan_distance(chess.D4, chess.E5), 2)
def test_square_knight_distance(self):
self.assertEqual(chess.square_knight_distance(chess.A1, chess.A1), 0)
self.assertEqual(chess.square_knight_distance(chess.A1, chess.H8), 6)
self.assertEqual(chess.square_knight_distance(chess.G1, chess.F3), 1)
self.assertEqual(chess.square_knight_distance(chess.E1, chess.E8), 5)
self.assertEqual(chess.square_knight_distance(chess.A4, chess.H4), 5)
self.assertEqual(chess.square_knight_distance(chess.A1, chess.B1), 3)
self.assertEqual(chess.square_knight_distance(chess.A1, chess.C3), 4)
self.assertEqual(chess.square_knight_distance(chess.A1, chess.B2), 4)
self.assertEqual(chess.square_knight_distance(chess.C1, chess.B2), 2)
class MoveTestCase(unittest.TestCase):
def test_equality(self):
a = chess.Move(chess.A1, chess.A2)
b = chess.Move(chess.A1, chess.A2)
c = chess.Move(chess.H7, chess.H8, chess.BISHOP)
d1 = chess.Move(chess.H7, chess.H8)
d2 = chess.Move(chess.H7, chess.H8)
self.assertEqual(a, b)
self.assertEqual(b, a)
self.assertEqual(d1, d2)
self.assertNotEqual(a, c)
self.assertNotEqual(c, d1)
self.assertNotEqual(b, d1)
self.assertFalse(d1 != d2)
def test_uci_parsing(self):
self.assertEqual(chess.Move.from_uci("b5c7").uci(), "b5c7")
self.assertEqual(chess.Move.from_uci("e7e8q").uci(), "e7e8q")
self.assertEqual(chess.Move.from_uci("P@e4").uci(), "P@e4")
self.assertEqual(chess.Move.from_uci("B@f4").uci(), "B@f4")
self.assertEqual(chess.Move.from_uci("0000").uci(), "0000")
def test_invalid_uci(self):
with self.assertRaises(chess.InvalidMoveError):
chess.Move.from_uci("")
with self.assertRaises(chess.InvalidMoveError):
chess.Move.from_uci("N")
with self.assertRaises(chess.InvalidMoveError):
chess.Move.from_uci("z1g3")
with self.assertRaises(chess.InvalidMoveError):
chess.Move.from_uci("Q@g9")
def test_xboard_move(self):
self.assertEqual(chess.Move.from_uci("b5c7").xboard(), "b5c7")
self.assertEqual(chess.Move.from_uci("e7e8q").xboard(), "e7e8q")
self.assertEqual(chess.Move.from_uci("P@e4").xboard(), "P@e4")
self.assertEqual(chess.Move.from_uci("B@f4").xboard(), "B@f4")
self.assertEqual(chess.Move.from_uci("0000").xboard(), "@@@@")
def test_copy(self):
a = chess.Move.from_uci("N@f3")
b = chess.Move.from_uci("a1h8")
c = chess.Move.from_uci("g7g8r")
self.assertEqual(copy.copy(a), a)
self.assertEqual(copy.copy(b), b)
self.assertEqual(copy.copy(c), c)
class PieceTestCase(unittest.TestCase):
def test_equality(self):
a = chess.Piece(chess.BISHOP, chess.WHITE)
b = chess.Piece(chess.KING, chess.BLACK)
c = chess.Piece(chess.KING, chess.WHITE)
d1 = chess.Piece(chess.BISHOP, chess.WHITE)
d2 = chess.Piece(chess.BISHOP, chess.WHITE)
self.assertEqual(len(set([a, b, c, d1, d2])), 3)
self.assertEqual(a, d1)
self.assertEqual(d1, a)
self.assertEqual(d1, d2)
self.assertEqual(repr(a), repr(d1))
self.assertNotEqual(a, b)
self.assertNotEqual(b, c)
self.assertNotEqual(b, d1)
self.assertNotEqual(a, c)
self.assertFalse(d1 != d2)
self.assertNotEqual(repr(a), repr(b))
self.assertNotEqual(repr(b), repr(c))
self.assertNotEqual(repr(b), repr(d1))
self.assertNotEqual(repr(a), repr(c))
def test_from_symbol(self):
white_knight = chess.Piece.from_symbol("N")
self.assertEqual(white_knight.color, chess.WHITE)
self.assertEqual(white_knight.piece_type, chess.KNIGHT)
self.assertEqual(white_knight.symbol(), "N")
self.assertEqual(str(white_knight), "N")
black_queen = chess.Piece.from_symbol("q")
self.assertEqual(black_queen.color, chess.BLACK)
self.assertEqual(black_queen.piece_type, chess.QUEEN)
self.assertEqual(black_queen.symbol(), "q")
self.assertEqual(str(black_queen), "q")
def test_hash(self):
pieces = {chess.Piece.from_symbol(symbol) for symbol in "pnbrqkPNBRQK"}
self.assertEqual(len(pieces), 12)
hashes = {hash(piece) for piece in pieces}
self.assertEqual(hashes, set(range(12)))
class BoardTestCase(unittest.TestCase):
def test_default_position(self):
board = chess.Board()
self.assertEqual(board.piece_at(chess.B1), chess.Piece.from_symbol("N"))
self.assertEqual(board.fen(), chess.STARTING_FEN)
self.assertEqual(board.turn, chess.WHITE)
def test_empty(self):
board = chess.Board.empty()
self.assertEqual(board.fen(), "8/8/8/8/8/8/8/8 w - - 0 1")
self.assertEqual(board, chess.Board(None))
def test_ply(self):
board = chess.Board()
self.assertEqual(board.ply(), 0)
board.push_san("d4")
self.assertEqual(board.ply(), 1)
board.push_san("d5")
self.assertEqual(board.ply(), 2)
board.clear_stack()
self.assertEqual(board.ply(), 2)
board.push_san("Nf3")
self.assertEqual(board.ply(), 3)
def test_from_epd(self):
base_epd = "rnbqkb1r/ppp1pppp/5n2/3P4/8/8/PPPP1PPP/RNBQKBNR w KQkq -"
board, ops = chess.Board.from_epd(base_epd " ce 55;")
self.assertEqual(ops["ce"], 55)
self.assertEqual(board.fen(), base_epd " 0 1")
def test_move_making(self):
board = chess.Board()
move = chess.Move(chess.E2, chess.E4)
board.push(move)
self.assertEqual(board.peek(), move)
def test_fen(self):
board = chess.Board()
self.assertEqual(board.fen(), chess.STARTING_FEN)
fen = "6k1/pb3pp1/1p2p2p/1Bn1P3/8/5N2/PP1q1PPP/6K1 w - - 0 24"
board.set_fen(fen)
self.assertEqual(board.fen(), fen)
board.push(chess.Move.from_uci("f3d2"))
self.assertEqual(board.fen(), "6k1/pb3pp1/1p2p2p/1Bn1P3/8/8/PP1N1PPP/6K1 b - - 0 24")
def test_xfen(self):
# https://de.wikipedia.org/wiki/Forsyth-Edwards-Notation#Beispiel
xfen = "rn2k1r1/ppp1pp1p/3p2p1/5bn1/P7/2N2B2/1PPPPP2/2BNK1RR w Gkq - 4 11"
board = chess.Board(xfen, chess960=True)
self.assertEqual(board.castling_rights, chess.BB_G1 | chess.BB_A8 | chess.BB_G8)
self.assertEqual(board.clean_castling_rights(), chess.BB_G1 | chess.BB_A8 | chess.BB_G8)
self.assertEqual(board.shredder_fen(), "rn2k1r1/ppp1pp1p/3p2p1/5bn1/P7/2N2B2/1PPPPP2/2BNK1RR w Gga - 4 11")
self.assertEqual(board.fen(), xfen)
self.assertTrue(board.has_castling_rights(chess.WHITE))
self.assertTrue(board.has_castling_rights(chess.BLACK))
self.assertTrue(board.has_kingside_castling_rights(chess.BLACK))
self.assertTrue(board.has_kingside_castling_rights(chess.WHITE))
self.assertTrue(board.has_queenside_castling_rights(chess.BLACK))
self.assertFalse(board.has_queenside_castling_rights(chess.WHITE))
# Chess960 position #284.
board = chess.Board("rkbqrbnn/pppppppp/8/8/8/8/PPPPPPPP/RKBQRBNN w - - 0 1", chess960=True)
board.castling_rights = board.rooks
self.assertTrue(board.clean_castling_rights() & chess.BB_A1)
self.assertEqual(board.fen(), "rkbqrbnn/pppppppp/8/8/8/8/PPPPPPPP/RKBQRBNN w KQkq - 0 1")
self.assertEqual(board.shredder_fen(), "rkbqrbnn/pppppppp/8/8/8/8/PPPPPPPP/RKBQRBNN w EAea - 0 1")
# Valid en passant square on illegal board.
fen = "8/8/8/pP6/8/8/8/8 w - a6 0 1"
board = chess.Board(fen)
self.assertEqual(board.fen(), fen)
# Illegal en passant square on illegal board.
fen = "1r6/8/8/pP6/8/8/8/1K6 w - a6 0 1"
board = chess.Board(fen)
self.assertEqual(board.fen(), "1r6/8/8/pP6/8/8/8/1K6 w - - 0 1")
def test_fen_en_passant(self):
board = chess.Board()
board.push_san("e4")
self.assertEqual(board.fen(en_passant="fen"), "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1")
self.assertEqual(board.fen(en_passant="xfen"), "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1")
def test_get_set(self):
board = chess.Board()
self.assertEqual(board.piece_at(chess.B1), chess.Piece.from_symbol("N"))
board.remove_piece_at(chess.E2)
self.assertEqual(board.piece_at(chess.E2), None)
board.set_piece_at(chess.E4, chess.Piece.from_symbol("r"))
self.assertEqual(board.piece_type_at(chess.E4), chess.ROOK)
board.set_piece_at(chess.F1, None)
self.assertEqual(board.piece_at(chess.F1), None)
board.set_piece_at(chess.H7, chess.Piece.from_symbol("Q"), promoted=True)
self.assertEqual(board.promoted, chess.BB_H7)
board.set_piece_at(chess.H7, None)
self.assertEqual(board.promoted, chess.BB_EMPTY)
self.assertEqual(board.piece_at(chess.H7), None)
def test_color_at(self):
board = chess.Board()
self.assertEqual(board.color_at(chess.A1), chess.WHITE)
self.assertEqual(board.color_at(chess.G7), chess.BLACK)
self.assertEqual(board.color_at(chess.E4), None)
def test_pawn_captures(self):
board = chess.Board()
# King's Gambit.
board.push(chess.Move.from_uci("e2e4"))
board.push(chess.Move.from_uci("e7e5"))
board.push(chess.Move.from_uci("f2f4"))
# Accepted.
exf4 = chess.Move.from_uci("e5f4")
self.assertIn(exf4, board.pseudo_legal_moves)
self.assertIn(exf4, board.legal_moves)
board.push(exf4)
board.pop()
def test_pawn_move_generation(self):
board = chess.Board("8/2R1P3/8/2pp4/2k1r3/P7/8/1K6 w - - 1 55")
self.assertEqual(len(list(board.generate_pseudo_legal_moves())), 16)
def test_single_step_pawn_move(self):
board = chess.Board()
a3 = chess.Move.from_uci("a2a3")
self.assertIn(a3, board.pseudo_legal_moves)
self.assertIn(a3, board.legal_moves)
board.push(a3)
board.pop()
self.assertEqual(board.fen(), chess.STARTING_FEN)
def test_castling(self):
board = chess.Board("r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 1 1")
# Let white castle short.
move = board.parse_xboard("O-O")
self.assertEqual(move, chess.Move.from_uci("e1g1"))
self.assertEqual(board.san(move), "O-O")
self.assertEqual(board.xboard(move), "e1g1")
self.assertIn(move, board.legal_moves)
board.push(move)
# Let black castle long.
move = board.parse_xboard("O-O-O")
self.assertEqual(board.san(move), "O-O-O")
self.assertEqual(board.xboard(move), "e8c8")
self.assertIn(move, board.legal_moves)
board.push(move)
self.assertEqual(board.fen(), "2kr3r/8/8/8/8/8/8/R4RK1 w - - 3 2")
# Undo both castling moves.
board.pop()
board.pop()
self.assertEqual(board.fen(), "r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 1 1")
# Let white castle long.
move = board.parse_san("O-O-O")
self.assertEqual(board.san(move), "O-O-O")
self.assertIn(move, board.legal_moves)
board.push(move)
# Let black castle short.
move = board.parse_san("O-O")
self.assertEqual(board.san(move), "O-O")
self.assertIn(move, board.legal_moves)
board.push(move)
self.assertEqual(board.fen(), "r4rk1/8/8/8/8/8/8/2KR3R w - - 3 2")
# Undo both castling moves.
board.pop()
board.pop()
self.assertEqual(board.fen(), "r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 1 1")
def test_castling_san(self):
board = chess.Board("4k3/8/8/8/8/8/8/4K2R w K - 0 1")
self.assertEqual(board.parse_san("O-O"), chess.Move.from_uci("e1g1"))
with self.assertRaises(chess.IllegalMoveError):
board.parse_san("Kg1")
with self.assertRaises(chess.IllegalMoveError):
board.parse_san("Kh1")
def test_ninesixty_castling(self):
fen = "3r1k1r/4pp2/8/8/8/8/8/4RKR1 w Gd - 1 1"
board = chess.Board(fen, chess960=True)
# Let white do the kingside swap.
move = board.parse_san("O-O")
self.assertEqual(board.san(move), "O-O")
self.assertEqual(board.xboard(move), "O-O")
self.assertEqual(move.from_square, chess.F1)
self.assertEqual(move.to_square, chess.G1)
self.assertIn(move, board.legal_moves)
board.push(move)
self.assertEqual(board.shredder_fen(), "3r1k1r/4pp2/8/8/8/8/8/4RRK1 b d - 2 1")
# Black can not castle kingside.
self.assertNotIn(chess.Move.from_uci("e8h8"), board.legal_moves)
# Let black castle queenside.
move = board.parse_san("O-O-O")
self.assertEqual(board.san(move), "O-O-O")
self.assertEqual(board.xboard(move), "O-O-O")
self.assertEqual(move.from_square, chess.F8)
self.assertEqual(move.to_square, chess.D8)
self.assertIn(move, board.legal_moves)
board.push(move)
self.assertEqual(board.shredder_fen(), "2kr3r/4pp2/8/8/8/8/8/4RRK1 w - - 3 2")
# Restore initial position.
board.pop()
board.pop()
self.assertEqual(board.shredder_fen(), fen)
fen = "Qr4k1/4pppp/8/8/8/8/8/R5KR w Hb - 0 1"
board = chess.Board(fen, chess960=True)
# White can just hop the rook over.
move = board.parse_san("O-O")
self.assertEqual(board.san(move), "O-O")
self.assertEqual(move.from_square, chess.G1)
self.assertEqual(move.to_square, chess.H1)
self.assertIn(move, board.legal_moves)
board.push(move)
self.assertEqual(board.shredder_fen(), "Qr4k1/4pppp/8/8/8/8/8/R4RK1 b b - 1 1")
# Black can not castle queenside nor kingside.
self.assertFalse(any(board.generate_castling_moves()))
# Restore initial position.
board.pop()
self.assertEqual(board.shredder_fen(), fen)
def test_hside_rook_blocks_aside_castling(self):
board = chess.Board("4rrk1/pbbp2p1/1ppnp3/3n1pqp/3N1PQP/1PPNP3/PBBP2P1/4RRK1 w Ff - 10 18", chess960=True)
self.assertNotIn(chess.Move.from_uci("g1f1"), board.legal_moves)
self.assertNotIn(chess.Move.from_uci("g1e1"), board.legal_moves)
self.assertNotIn(chess.Move.from_uci("g1c1"), board.legal_moves)
self.assertNotIn(chess.Move.from_uci("g1a1"), board.legal_moves)
self.assertIn(chess.Move.from_uci("g1h1"), board.legal_moves) # Kh1
def test_selective_castling(self):
board = chess.Board("r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2R w KQkq - 0 1")
# King not selected.
self.assertFalse(any(board.generate_castling_moves(chess.BB_ALL & ~board.kings)))
# Rook on h1 not selected.
moves = board.generate_castling_moves(chess.BB_ALL, chess.BB_ALL & ~chess.BB_H1)
self.assertEqual(len(list(moves)), 1)
def test_castling_right_not_destroyed_bug(self):
# A rook move from h8 to h1 was only taking white's possible castling
# rights away.
board = chess.Board("2r1k2r/2qbbpp1/p2pp3/1p3PP1/Pn2P3/1PN1B3/1P3QB1/1K1R3R b k - 0 22")
board.push_san("Rxh1")
self.assertEqual(board.epd(), "2r1k3/2qbbpp1/p2pp3/1p3PP1/Pn2P3/1PN1B3/1P3QB1/1K1R3r w - -")
def test_invalid_castling_rights(self):
# KQkq is not valid in this standard chess position.
board = chess.Board("1r2k3/8/8/8/8/8/8/R3KR2 w KQkq - 0 1")
self.assertEqual(board.status(), chess.STATUS_BAD_CASTLING_RIGHTS)
self.assertEqual(board.fen(), "1r2k3/8/8/8/8/8/8/R3KR2 w Q - 0 1")
self.assertTrue(board.has_queenside_castling_rights(chess.WHITE))
self.assertFalse(board.has_kingside_castling_rights(chess.WHITE))
self.assertFalse(board.has_queenside_castling_rights(chess.BLACK))
self.assertFalse(board.has_kingside_castling_rights(chess.BLACK))
board = chess.Board("4k2r/8/8/8/8/8/8/R1K5 w KQkq - 0 1", chess960=True)
self.assertEqual(board.status(), chess.STATUS_BAD_CASTLING_RIGHTS)
self.assertEqual(board.fen(), "4k2r/8/8/8/8/8/8/R1K5 w Qk - 0 1")
board = chess.Board("1r2k3/8/1p6/8/8/5P2/8/1R2KR2 w KQkq - 0 1", chess960=True)
self.assertEqual(board.status(), chess.STATUS_BAD_CASTLING_RIGHTS)
self.assertEqual(board.fen(), "1r2k3/8/1p6/8/8/5P2/8/1R2KR2 w KQq - 0 1")
def test_ninesixty_different_king_and_rook_file(self):
# Theoretically, this position (with castling rights) can not be reached
# with a series of legal moves from one of the 960 starting positions.
# Decision: We don't care, neither do Stockfish or lichess.org.
fen = "1r1k1r2/5p2/8/8/8/8/3N4/R5KR b KQkq - 0 1"
board = chess.Board(fen, chess960=True)
self.assertEqual(board.fen(), fen)
def test_ninesixty_prevented_castle(self):
board = chess.Board("4k3/8/8/1b6/8/8/8/5RKR w KQ - 0 1", chess960=True)
self.assertFalse(board.is_legal(chess.Move.from_uci("g1f1")))
def test_find_move(self):
board = chess.Board("4k3/1P6/8/8/8/8/3P4/4K2R w K - 0 1")
# Pawn moves.
self.assertEqual(board.find_move(chess.D2, chess.D4), chess.Move.from_uci("d2d4"))
self.assertEqual(board.find_move(chess.B7, chess.B8), chess.Move.from_uci("b7b8q"))
self.assertEqual(board.find_move(chess.B7, chess.B8, chess.KNIGHT), chess.Move.from_uci("b7b8n"))
# Illegal moves.
with self.assertRaises(chess.IllegalMoveError):
board.find_move(chess.D2, chess.D8)
with self.assertRaises(chess.IllegalMoveError):
board.find_move(chess.E1, chess.A1)
# Castling.
self.assertEqual(board.find_move(chess.E1, chess.G1), chess.Move.from_uci("e1g1"))
self.assertEqual(board.find_move(chess.E1, chess.H1), chess.Move.from_uci("e1g1"))
board.chess960 = True
self.assertEqual(board.find_move(chess.E1, chess.H1), chess.Move.from_uci("e1h1"))
def test_clean_castling_rights(self):
board = chess.Board()
board.set_board_fen("k6K/8/8/pppppppp/8/8/8/QqQq4")
self.assertEqual(board.clean_castling_rights(), chess.BB_EMPTY)
self.assertEqual(board.fen(), "k6K/8/8/pppppppp/8/8/8/QqQq4 w - - 0 1")
board.push_san("Qxc5")
self.assertEqual(board.clean_castling_rights(), chess.BB_EMPTY)
self.assertEqual(board.fen(), "k6K/8/8/ppQppppp/8/8/8/Qq1q4 b - - 0 1")
def test_insufficient_material(self):
def _check(board, white, black):
self.assertEqual(board.has_insufficient_material(chess.WHITE), white)
self.assertEqual(board.has_insufficient_material(chess.BLACK), black)
self.assertEqual(board.is_insufficient_material(), white and black)
# Imperfect implementation.
false_negative = False
_check(chess.Board(), False, False)
_check(chess.Board("k1K1B1B1/8/8/8/8/8/8/8 w - - 7 32"), True, True)
_check(chess.Board("kbK1B1B1/8/8/8/8/8/8/8 w - - 7 32"), False, False)
_check(chess.Board("8/5k2/8/8/8/8/3K4/8 w - - 0 1"), True, True)
_check(chess.Board("8/3k4/8/8/2N5/8/3K4/8 b - - 0 1"), True, True)
_check(chess.Board("8/4rk2/8/8/8/8/3K4/8 w - - 0 1"), True, False)
_check(chess.Board("8/4qk2/8/8/8/8/3K4/8 w - - 0 1"), True, False)
_check(chess.Board("8/4bk2/8/8/8/8/3KB3/8 w - - 0 1"), False, False)
_check(chess.Board("8/8/3Q4/2bK4/B7/8/1k6/8 w - - 1 68"), False, False)
_check(chess.Board("8/5k2/8/8/8/4B3/3K1B2/8 w - - 0 1"), True, True)
_check(chess.Board("5K2/8/8/1B6/8/k7/6b1/8 w - - 0 39"), True, True)
_check(chess.Board("8/8/8/4k3/5b2/3K4/8/2B5 w - - 0 33"), True, True)
_check(chess.Board("3b4/8/8/6b1/8/8/R7/K1k5 w - - 0 1"), False, True)
_check(chess.variant.AtomicBoard("8/3k4/8/8/2N5/8/3K4/8 b - - 0 1"), True, True)
_check(chess.variant.AtomicBoard("8/4rk2/8/8/8/8/3K4/8 w - - 0 1"), True, True)
_check(chess.variant.AtomicBoard("8/4qk2/8/8/8/8/3K4/8 w - - 0 1"), True, False)
_check(chess.variant.AtomicBoard("8/1k6/8/2n5/8/3NK3/8/8 b - - 0 1"), False, False)
_check(chess.variant.AtomicBoard("8/4bk2/8/8/8/8/3KB3/8 w - - 0 1"), True, True)
_check(chess.variant.AtomicBoard("4b3/5k2/8/8/8/8/3KB3/8 w - - 0 1"), False, False)
_check(chess.variant.AtomicBoard("3Q4/5kKB/8/8/8/8/8/8 b - - 0 1"), False, True)
_check(chess.variant.AtomicBoard("8/5k2/8/8/8/8/5K2/4bb2 w - - 0 1"), True, False)
_check(chess.variant.AtomicBoard("8/5k2/8/8/8/8/5K2/4nb2 w - - 0 1"), True, False)
_check(chess.variant.GiveawayBoard("8/4bk2/8/8/8/8/3KB3/8 w - - 0 1"), False, False)
_check(chess.variant.GiveawayBoard("4b3/5k2/8/8/8/8/3KB3/8 w - - 0 1"), False, False)
_check(chess.variant.GiveawayBoard("8/8/8/6b1/8/3B4/4B3/5B2 w - - 0 1"), True, True)
_check(chess.variant.GiveawayBoard("8/8/5b2/8/8/3B4/3B4/8 w - - 0 1"), True, False)
_check(chess.variant.SuicideBoard("8/5p2/5P2/8/3B4/1bB5/8/8 b - - 0 1"), false_negative, false_negative)
_check(chess.variant.AntichessBoard("8/8/8/1n2N3/8/8/8/8 w - - 0 32"), True, False)
_check(chess.variant.AntichessBoard("8/3N4/8/1n6/8/8/8/8 b - - 1 32"), True, False)
_check(chess.variant.AntichessBoard("6n1/8/8/4N3/8/8/8/8 b - - 0 27"), False, True)
_check(chess.variant.AntichessBoard("8/8/5n2/4N3/8/8/8/8 w - - 1 28"), False, True)
_check(chess.variant.AntichessBoard("8/3n4/8/8/8/8/8/8 w - - 0 29"), False, True)
_check(chess.variant.KingOfTheHillBoard("8/5k2/8/8/8/8/3K4/8 w - - 0 1"), False, False)
_check(chess.variant.RacingKingsBoard("8/5k2/8/8/8/8/3K4/8 w - - 0 1"), False, False)
_check(chess.variant.ThreeCheckBoard("8/5k2/8/8/8/8/3K4/8 w - - 3 3 0 1"), True, True)
_check(chess.variant.ThreeCheckBoard("8/5k2/8/8/8/8/3K2N1/8 w - - 3 3 0 1"), False, True)
_check(chess.variant.CrazyhouseBoard("8/5k2/8/8/8/8/3K2N1/8[] w - - 0 1"), True, True)
_check(chess.variant.CrazyhouseBoard("8/5k2/8/8/8/5B2/3KB3/8[] w - - 0 1"), False, False)
_check(chess.variant.CrazyhouseBoard("8/8/8/8/3k4/3N~4/3K4/8 w - - 0 1"), False, False)
_check(chess.variant.HordeBoard("8/5k2/8/8/8/4NN2/8/8 w - - 0 1"), True, False)
_check(chess.variant.HordeBoard("8/1b5r/1P6/1Pk3q1/1PP5/r1P5/P1P5/2P5 b - - 0 52"), False, False)
def test_promotion_with_check(self):
board = chess.Board("8/6P1/2p5/1Pqk4/6P1/2P1RKP1/4P1P1/8 w - - 0 1")
board.push(chess.Move.from_uci("g7g8q"))
self.assertTrue(board.is_check())
self.assertEqual(board.fen(), "6Q1/8/2p5/1Pqk4/6P1/2P1RKP1/4P1P1/8 b - - 0 1")
board = chess.Board("8/8/8/3R1P2/8/2k2K2/3p4/r7 b - - 0 82")
board.push_san("d1=Q ")
self.assertEqual(board.fen(), "8/8/8/3R1P2/8/2k2K2/8/r2q4 w - - 0 83")
def test_ambiguous_move(self):
board = chess.Board("8/8/1n6/3R1P2/1n6/2k2K2/3p4/r6r b - - 0 82")
with self.assertRaises(chess.AmbiguousMoveError):
board.parse_san("Rf1")
with self.assertRaises(chess.AmbiguousMoveError):
board.parse_san("Nd5")
def test_scholars_mate(self):
board = chess.Board()
e4 = chess.Move.from_uci("e2e4")
self.assertIn(e4, board.legal_moves)
board.push(e4)
e5 = chess.Move.from_uci("e7e5")
self.assertIn(e5, board.legal_moves)
board.push(e5)
Qf3 = chess.Move.from_uci("d1f3")
self.assertIn(Qf3, board.legal_moves)
board.push(Qf3)
Nc6 = chess.Move.from_uci("b8c6")
self.assertIn(Nc6, board.legal_moves)
board.push(Nc6)
Bc4 = chess.Move.from_uci("f1c4")
self.assertIn(Bc4, board.legal_moves)
board.push(Bc4)
Rb8 = chess.Move.from_uci("a8b8")
self.assertIn(Rb8, board.legal_moves)
board.push(Rb8)
self.assertFalse(board.is_check())
self.assertFalse(board.is_checkmate())
self.assertFalse(board.is_game_over())
self.assertFalse(board.is_stalemate())
Qf7_mate = chess.Move.from_uci("f3f7")
self.assertIn(Qf7_mate, board.legal_moves)
board.push(Qf7_mate)
self.assertTrue(board.is_check())
self.assertTrue(board.is_checkmate())
self.assertTrue(board.is_game_over())
self.assertTrue(board.is_game_over(claim_draw=True))
self.assertFalse(board.is_stalemate())
self.assertEqual(board.fen(), "1rbqkbnr/pppp1Qpp/2n5/4p3/2B1P3/8/PPPP1PPP/RNB1K1NR b KQk - 0 4")
def test_result(self):
# Undetermined.
board = chess.Board()
self.assertEqual(board.result(claim_draw=True), "*")
# White checkmated.
board = chess.Board("rnb1kbnr/pppp1ppp/8/4p3/6Pq/5P2/PPPPP2P/RNBQKBNR w KQkq - 1 3")
self.assertEqual(board.result(claim_draw=True), "0-1")
# Stalemate.
board = chess.Board("7K/7P/7k/8/6q1/8/8/8 w - - 0 1")
self.assertEqual(board.result(), "1/2-1/2")
# Insufficient material.
board = chess.Board("4k3/8/8/8/8/5B2/8/4K3 w - - 0 1")
self.assertEqual(board.result(), "1/2-1/2")
# Fiftyseven-move rule.
board = chess.Board("4k3/8/6r1/8/8/8/2R5/4K3 w - - 369 1")
self.assertEqual(board.result(), "1/2-1/2")
# Fifty-move rule.
board = chess.Board("4k3/8/6r1/8/8/8/2R5/4K3 w - - 120 1")
self.assertEqual(board.result(), "*")
self.assertEqual(board.result(claim_draw=True), "1/2-1/2")
def test_san(self):
# Castling with check.
fen = "rnbk1b1r/ppp2pp1/5n1p/4p1B1/2P5/2N5/PP2PPPP/R3KBNR w KQ - 0 7"
board = chess.Board(fen)
long_castle_check = chess.Move.from_uci("e1a1")
self.assertEqual(board.san(long_castle_check), "O-O-O ")
self.assertEqual(board.fen(), fen)
# En passant mate.
fen = "6bk/7b/8/3pP3/8/8/8/Q3K3 w - d6 0 2"
board = chess.Board(fen)
fxe6_mate_ep = chess.Move.from_uci("e5d6")
self.assertEqual(board.san(fxe6_mate_ep), "exd6#")
self.assertEqual(board.fen(), fen)
# Test disambiguation.
fen = "N3k2N/8/8/3N4/N4N1N/2R5/1R6/4K3 w - - 0 1"
board = chess.Board(fen)
self.assertEqual(board.san(chess.Move.from_uci("e1f1")), "Kf1")
self.assertEqual(board.san(chess.Move.from_uci("c3c2")), "Rcc2")
self.assertEqual(board.san(chess.Move.from_uci("b2c2")), "Rbc2")
self.assertEqual(board.san(chess.Move.from_uci("a4b6")), "N4b6")
self.assertEqual(board.san(chess.Move.from_uci("h8g6")), "N8g6")
self.assertEqual(board.san(chess.Move.from_uci("h4g6")), "Nh4g6")
self.assertEqual(board.fen(), fen)
# Do not disambiguate illegal alternatives.
fen = "8/8/8/R2nkn2/8/8/2K5/8 b - - 0 1"
board = chess.Board(fen)
self.assertEqual(board.san(chess.Move.from_uci("f5e3")), "Ne3 ")
self.assertEqual(board.fen(), fen)
# Promotion.
fen = "7k/1p2Npbp/8/2P5/1P1r4/3b2QP/3q1pPK/2RB4 b - - 1 29"
board = chess.Board(fen)
self.assertEqual(board.san(chess.Move.from_uci("f2f1q")), "f1=Q")
self.assertEqual(board.san(chess.Move.from_uci("f2f1n")), "f1=N ")
self.assertEqual(board.fen(), fen)
def test_lan(self):
# Normal moves always with origin square.
fen = "N3k2N/8/8/3N4/N4N1N/2R5/1R6/4K3 w - - 0 1"
board = chess.Board(fen)
self.assertEqual(board.lan(chess.Move.from_uci("e1f1")), "Ke1-f1")
self.assertEqual(board.lan(chess.Move.from_uci("c3c2")), "Rc3-c2")
self.assertEqual(board.lan(chess.Move.from_uci("a4c5")), "Na4-c5")
self.assertEqual(board.fen(), fen)
# Normal capture.
fen = "rnbq1rk1/ppp1bpp1/4pn1p/3p2B1/2PP4/2N1PN2/PP3PPP/R2QKB1R w KQ - 0 7"
board = chess.Board(fen)
self.assertEqual(board.lan(chess.Move.from_uci("g5f6")), "Bg5xf6")
self.assertEqual(board.fen(), fen)
# Pawn captures and moves.
fen = "6bk/7b/8/3pP3/8/8/8/Q3K3 w - d6 0 2"
board = chess.Board(fen)
self.assertEqual(board.lan(chess.Move.from_uci("e5d6")), "e5xd6#")
self.assertEqual(board.lan(chess.Move.from_uci("e5e6")), "e5-e6 ")
self.assertEqual(board.fen(), fen)
def test_san_newline(self):
board = chess.Board("rnbqk2r/ppppppbp/5np1/8/8/5NP1/PPPPPPBP/RNBQK2R w KQkq - 2 4")
with self.assertRaises(chess.InvalidMoveError):
board.parse_san("O-O\n")
with self.assertRaises(chess.InvalidMoveError):
board.parse_san("Nc3\n")
def test_pawn_capture_san_without_file(self):
board = chess.Board("2rq1rk1/pb2bppp/1p2p3/n1ppPn2/2PP4/PP3N2/1B1NQPPP/RB3RK1 b - - 4 13")
with self.assertRaises(chess.IllegalMoveError):
board.parse_san("c4")
board = chess.Board("4k3/8/8/4Pp2/8/8/8/4K3 w - f6 0 2")
with self.assertRaises(chess.IllegalMoveError):
board.parse_san("f6")
def test_variation_san(self):
board = chess.Board()
self.assertEqual('1. e4 e5 2. Nf3',
board.variation_san([chess.Move.from_uci(m) for m in
['e2e4', 'e7e5', 'g1f3']]))
self.assertEqual('1. e4 e5 2. Nf3 Nc6 3. Bb5 a6',
board.variation_san([chess.Move.from_uci(m) for m in
['e2e4', 'e7e5', 'g1f3', 'b8c6', 'f1b5', 'a7a6']]))
fen = "rn1qr1k1/1p2bppp/p3p3/3pP3/P2P1B2/2RB1Q1P/1P3PP1/R5K1 w - - 0 19"
board = chess.Board(fen)
variation = ['d3h7', 'g8h7', 'f3h5', 'h7g8', 'c3g3', 'e7f8', 'f4g5',
'e8e7', 'g5f6', 'b8d7', 'h5h6', 'd7f6', 'e5f6', 'g7g6',
'f6e7', 'f8e7']
var_w = board.variation_san([chess.Move.from_uci(m) for m in variation])
self.assertEqual(("19. Bxh7 Kxh7 20. Qh5 Kg8 21. Rg3 Bf8 22. Bg5 Re7 "
"23. Bf6 Nd7 24. Qh6 Nxf6 25. exf6 g6 26. fxe7 Bxe7"),
var_w)
self.assertEqual(fen, board.fen(), msg="Board unchanged by variation_san")
board.push(chess.Move.from_uci(variation.pop(0)))
var_b = board.variation_san([chess.Move.from_uci(m) for m in variation])
self.assertEqual(("19...Kxh7 20. Qh5 Kg8 21. Rg3 Bf8 22. Bg5 Re7 "
"23. Bf6 Nd7 24. Qh6 Nxf6 25. exf6 g6 26. fxe7 Bxe7"),
var_b)
illegal_variation = ['d3h7', 'g8h7', 'f3h6', 'h7g8']
board = chess.Board(fen)
with self.assertRaises(chess.IllegalMoveError) as err:
board.variation_san([chess.Move.from_uci(m) for m in illegal_variation])
message = str(err.exception)
self.assertIn('illegal move', message.lower(),
msg=f"Error [{message}] mentions illegal move")
self.assertIn('f3h6', message,
msg=f"Illegal move f3h6 appears in message [{message}]")
def test_move_stack_usage(self):
board = chess.Board()
board.push_uci("d2d4")
board.push_uci("d7d5")
board.push_uci("g1f3")
board.push_uci("c8f5")
board.push_uci("e2e3")
board.push_uci("e7e6")
board.push_uci("f1d3")
board.push_uci("f8d6")
board.push_uci("e1h1")
san = chess.Board().variation_san(board.move_stack)
self.assertEqual(san, "1. d4 d5 2. Nf3 Bf5 3. e3 e6 4. Bd3 Bd6 5. O-O")
def test_is_legal_move(self):
fen = "3k4/6P1/7P/8/K7/8/8/4R3 w - - 0 1"
board = chess.Board(fen)
# Legal moves: Rg1, g8=R .
self.assertIn(chess.Move.from_uci("e1g1"), board.legal_moves)
self.assertIn(chess.Move.from_uci("g7g8r"), board.legal_moves)
# Impossible promotion: Kb5, h7.
self.assertNotIn(chess.Move.from_uci("a5b5q"), board.legal_moves)
self.assertNotIn(chess.Move.from_uci("h6h7n"), board.legal_moves)
# Missing promotion.
self.assertNotIn(chess.Move.from_uci("g7g8"), board.legal_moves)
# Promote to pawn or king.
self.assertFalse(board.is_legal(chess.Move.from_uci("g7g8p")))
self.assertFalse(board.is_pseudo_legal(chess.Move.from_uci("g7g8p")))
self.assertFalse(board.is_legal(chess.Move.from_uci("g7g8k")))
self.assertFalse(board.is_pseudo_legal(chess.Move.from_uci("g7g8k")))
self.assertEqual(board.fen(), fen)
def test_move_count(self):
board = chess.Board("1N2k3/P7/8/8/3n4/8/2PP4/R3K2R w KQ - 0 1")
self.assertEqual(board.pseudo_legal_moves.count(), 8 4 3 2 1 6 9)
def test_polyglot(self):
# Test Polyglot compatibility using test data from
# http://hardy.uhasselt.be/Toga/book_format.html. Forfeiting castling
# rights should not reset the half-move counter, though.
board = chess.Board()
self.assertEqual(board.fen(), "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
self.assertEqual(chess.polyglot.zobrist_hash(board), 0x463b96181691fc9c)
board.push_san("e4")
self.assertEqual(board.fen(), "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1")
self.assertEqual(chess.polyglot.zobrist_hash(board), 0x823c9b50fd114196)
board.push_san("d5")
self.assertEqual(board.fen(), "rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2")
self.assertEqual(chess.polyglot.zobrist_hash(board), 0x0756b94461c50fb0)
board.push_san("e5")
self.assertEqual(board.fen(), "rnbqkbnr/ppp1pppp/8/3pP3/8/8/PPPP1PPP/RNBQKBNR b KQkq - 0 2")
self.assertEqual(chess.polyglot.zobrist_hash(board), 0x662fafb965db29d4)
board.push_san("f5")
self.assertEqual(board.fen(), "rnbqkbnr/ppp1p1pp/8/3pPp2/8/8/PPPP1PPP/RNBQKBNR w KQkq f6 0 3")
self.assertEqual(chess.polyglot.zobrist_hash(board), 0x22a48b5a8e47ff78)
board.push_san("Ke2")
self.assertEqual(board.fen(), "rnbqkbnr/ppp1p1pp/8/3pPp2/8/8/PPPPKPPP/RNBQ1BNR b kq - 1 3")
self.assertEqual(chess.polyglot.zobrist_hash(board), 0x652a607ca3f242c1)
board.push_san("Kf7")
self.assertEqual(board.fen(), "rnbq1bnr/ppp1pkpp/8/3pPp2/8/8/PPPPKPPP/RNBQ1BNR w - - 2 4")
self.assertEqual(chess.polyglot.zobrist_hash(board), 0x00fdd303c946bdd9)
board = chess.Board()
board.push_san("a4")
board.push_san("b5")
board.push_san("h4")
board.push_san("b4")
board.push_san("c4")
self.assertEqual(board.fen(), "rnbqkbnr/p1pppppp/8/8/PpP4P/8/1P1PPPP1/RNBQKBNR b KQkq c3 0 3")
self.assertEqual(chess.polyglot.zobrist_hash(board), 0x3c8123ea7b067637)
board.push_san("bxc3")
board.push_san("Ra3")
self.assertEqual(board.fen(), "rnbqkbnr/p1pppppp/8/8/P6P/R1p5/1P1PPPP1/1NBQKBNR b Kkq - 1 4")
self.assertEqual(chess.polyglot.zobrist_hash(board), 0x5c3f9b829b279560)
def test_castling_move_generation_bug(self):
# Specific test position right after castling.
fen = "rnbqkbnr/2pp1ppp/8/4p3/2BPP3/P1N2N2/PB3PPP/2RQ1RK1 b kq - 1 10"
board = chess.Board(fen)
illegal_move = chess.Move.from_uci("g1g2")
self.assertNotIn(illegal_move, board.legal_moves)
self.assertNotIn(illegal_move, list(board.legal_moves))
self.assertNotIn(illegal_move, board.pseudo_legal_moves)
self.assertNotIn(illegal_move, list(board.pseudo_legal_moves))
# Make a move.
board.push_san("exd4")
# Already castled short, can not castle long.
illegal_move = chess.Move.from_uci("e1c1")
self.assertNotIn(illegal_move, board.pseudo_legal_moves)
self.assertNotIn(illegal_move, board.generate_pseudo_legal_moves())
self.assertNotIn(illegal_move, board.legal_moves)
self.assertNotIn(illegal_move, list(board.legal_moves))
# Unmake the move.
board.pop()
# Generate all pseudo-legal moves, two moves deep.
for move in board.pseudo_legal_moves:
board.push(move)
for move in board.pseudo_legal_moves:
board.push(move)
board.pop()
board.pop()
# Check that board is still consistent.
self.assertEqual(board.fen(), fen)
self.assertTrue(board.kings & chess.BB_G1)
self.assertTrue(board.occupied & chess.BB_G1)
self.assertTrue(board.occupied_co[chess.WHITE] & chess.BB_G1)
self.assertEqual(board.piece_at(chess.G1), chess.Piece(chess.KING, chess.WHITE))
self.assertEqual(board.piece_at(chess.C1), chess.Piece(chess.ROOK, chess.WHITE))
def test_move_generation_bug(self):
# Specific problematic position.
fen = "4kb1r/3b1ppp/8/1r2pNB1/6P1/pP2QP2/P6P/4R1K1 w k - 0 27"
board = chess.Board(fen)
# Make a move.
board.push_san("Re2")
# Check for the illegal move.
illegal_move = chess.Move.from_uci("e8f8")
self.assertNotIn(illegal_move, board.pseudo_legal_moves)
self.assertNotIn(illegal_move, board.generate_pseudo_legal_moves())
self.assertNotIn(illegal_move, board.legal_moves)
self.assertNotIn(illegal_move, board.generate_legal_moves())
# Generate all pseudo-legal moves.
for a in board.pseudo_legal_moves:
board.push(a)
board.pop()
# Unmake the move.
board.pop()
# Check that board is still consistent.
self.assertEqual(board.fen(), fen)
def test_stateful_move_generation_bug(self):
board = chess.Board("r1b1k3/p2p1Nr1/n2b3p/3pp1pP/2BB1p2/P3P2R/Q1P3P1/R3K1N1 b Qq - 0 1")
count = 0
for move in board.legal_moves:
board.push(move)
list(board.generate_legal_moves())
count = 1
board.pop()
self.assertEqual(count, 26)
def test_ninesixty_castling_bug(self):
board = chess.Board("4r3/3k4/8/8/8/8/q5PP/1R1KR3 w Q - 2 2", chess960=True)
move = chess.Move.from_uci("d1b1")
self.assertTrue(board.is_castling(move))
self.assertIn(move, board.generate_pseudo_legal_moves())
self.assertTrue(board.is_pseudo_legal(move))
self.assertIn(move, board.generate_legal_moves())
self.assertTrue(board.is_legal(move))
self.assertEqual(board.parse_san("O-O-O "), move)
self.assertEqual(board.san(move), "O-O-O ")
def test_equality(self):
self.assertEqual(chess.Board(), chess.Board())
self.assertFalse(chess.Board() != chess.Board())
a = chess.Board()
a.push_san("d4")
b = chess.Board()
b.push_san("d3")
self.assertNotEqual(a, b)
self.assertFalse(a == b)
def test_status(self):
board = chess.Board()
self.assertEqual(board.status(), chess.STATUS_VALID)
self.assertTrue(board.is_valid())
board.remove_piece_at(chess.H1)
self.assertTrue(board.status() & chess.STATUS_BAD_CASTLING_RIGHTS)
board.remove_piece_at(chess.E8)
self.assertTrue(board.status() & chess.STATUS_NO_BLACK_KING)
# The en passant square should be set even if no capture is actually
# possible.
board = chess.Board()
board.push_san("e4")
self.assertEqual(board.ep_square, chess.E3)
self.assertEqual(board.status(), chess.STATUS_VALID)
# But there must indeed be a pawn there.
board.remove_piece_at(chess.E4)
self.assertEqual(board.status(), chess.STATUS_INVALID_EP_SQUARE)
# King must be between the two rooks.
board = chess.Board("2rrk3/8/8/8/8/8/3PPPPP/2RK4 w cd - 0 1")