How to efficiently get material balance #864
Answered
by
niklasf
pavolkacej
asked this question in
Q&A
-
How can I get the difference of material at current position? Like if white is down a rook, the result will be -5. |
Beta Was this translation helpful? Give feedback.
Answered by
niklasf
Mar 13, 2022
Replies: 1 comment 1 reply
-
The most efficient bitboard-based implementation is a bit verbose: import chess
def material_balance(board):
white = board.occupied_co[chess.WHITE]
black = board.occupied_co[chess.BLACK]
return (
chess.popcount(white & board.pawns) - chess.popcount(black & board.pawns) +
3 * (chess.popcount(white & board.knights) - chess.popcount(black & board.knights)) +
3 * (chess.popcount(white & board.bishops) - chess.popcount(black & board.bishops)) +
5 * (chess.popcount(white & board.rooks) - chess.popcount(black & board.rooks)) +
9 * (chess.popcount(white & board.queens) - chess.popcount(black & board.queens))
) Chess engines usually maintain an incrementally updated balance. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
pavolkacej
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The most efficient bitboard-based implementation is a bit verbose:
Chess engines usually maintain…