Skip to content

Commit

Permalink
Micro optimize generate_legal_moves
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Mar 6, 2017
1 parent 3cd6f65 commit 5e3df3f
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions chess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3210,15 +3210,18 @@ def _generate_evasions(self, from_mask=BB_ALL, to_mask=BB_ALL):
yield move

def generate_legal_moves(self, from_mask=BB_ALL, to_mask=BB_ALL):
blockers = self._slider_blockers()

if self.is_variant_end():
moves = []
return
elif self.is_check():
moves = self._generate_evasions(from_mask, to_mask)
for move in self._generate_evasions(from_mask, to_mask):
if self._is_safe(blockers, move):
yield move
else:
moves = self.generate_pseudo_legal_moves(from_mask, to_mask)

blockers = self._slider_blockers()
return (move for move in moves if self._is_safe(blockers, move))
for move in self.generate_pseudo_legal_moves(from_mask, to_mask):
if self._is_safe(blockers, move):
yield move

def generate_legal_ep(self, from_mask=BB_ALL, to_mask=BB_ALL):
if self.is_variant_end():
Expand Down

0 comments on commit 5e3df3f

Please sign in to comment.