@@ -85,16 +85,25 @@ def is_variant_draw(self) -> bool:
8585 return self .is_stalemate () and self ._material_balance () == 0
8686
8787 def has_insufficient_material (self , color : chess .Color ) -> bool :
88- if self .occupied != self .bishops :
88+ if not self .occupied_co [color ]:
89+ return False
90+ elif not self .occupied_co [not color ]:
91+ return True
92+ elif self .occupied == self .bishops :
93+ # In a position with only bishops, check if all our bishops can be
94+ # captured.
95+ we_some_on_light = bool (self .occupied_co [color ] & chess .BB_LIGHT_SQUARES )
96+ we_some_on_dark = bool (self .occupied_co [color ] & chess .BB_DARK_SQUARES )
97+ they_all_on_dark = not (self .occupied_co [not color ] & chess .BB_LIGHT_SQUARES )
98+ they_all_on_light = not (self .occupied_co [not color ] & chess .BB_DARK_SQUARES )
99+ return (we_some_on_light and they_all_on_dark ) or (we_some_on_dark and they_all_on_light )
100+ elif self .occupied == self .knights and chess .popcount (self .knights ) == 2 :
101+ return (
102+ self .turn == color ^
103+ bool (self .occupied_co [chess .WHITE ] & chess .BB_LIGHT_SQUARES ) ^
104+ bool (self .occupied_co [chess .BLACK ] & chess .BB_DARK_SQUARES ))
105+ else :
89106 return False
90-
91- # In a position with only bishops, check if all our bishops can be
92- # captured.
93- we_some_on_light = bool (self .occupied_co [color ] & chess .BB_LIGHT_SQUARES )
94- we_some_on_dark = bool (self .occupied_co [color ] & chess .BB_DARK_SQUARES )
95- they_all_on_dark = not (self .occupied_co [not color ] & chess .BB_LIGHT_SQUARES )
96- they_all_on_light = not (self .occupied_co [not color ] & chess .BB_DARK_SQUARES )
97- return (we_some_on_light and they_all_on_dark ) or (we_some_on_dark and they_all_on_light )
98107
99108 def generate_pseudo_legal_moves (self , from_mask : chess .Bitboard = chess .BB_ALL , to_mask : chess .Bitboard = chess .BB_ALL ) -> Iterator [chess .Move ]:
100109 for move in super ().generate_pseudo_legal_moves (from_mask , to_mask ):
0 commit comments