Skip to content

How to efficiently get material balance #864

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

You must be logged in to vote

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…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jacksonthall22
Comment options

Answer selected by pavolkacej
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