Skip to content

Number of Legal Moves to empty squares only #846

Answered by niklasf
infinitless asked this question in Q&A
Discussion options

You must be logged in to vote

You can use bitboard masks to achieve this.

gen = board.generate_legal_moves(to_mask=~board.occupied)  # To empty squares only
gen = board.generate_legal_moves(to_mask=board.occupied)   # To occupied squares only

Note that en passant captures are moves to empty squares. Use board.generate_legal_captures() to include these.

These functions return generators. Moves can be counted using len(list(gen)) or sum(1 for _ in gen).

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@infinitless
Comment options

@cherepanovic
Comment options

Answer selected by infinitless
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants