How do you go about writing an evaluation function for a bitboard bases chess engine? I want to start of simple and only evaluate based on piece value, and where the pieces stand. For example a knight on c3 should give more points than a knight on a3.

I can start with making an array of size 64 for each piece with weights. For example I can store the value 0.1 in the square c3 and -0.1 in the square a3 for the knights. Then I could loop over the 64 squares and check if any of the 12 piece types are in this square and add (or subtract) it from the total evaluation. However it seems like this is very slow.

If I were only counting the piece values then I could use a pop count on each bitboard to get the value, but I don't see how I can implement the square weights this way.

Is there a smarter way to think about this?

  • You should simultaneously store a "mailbox" chess board, alongside your bitboards. After all, how can you quickly query the piece on a particular square (even during move generation) without this? Oct 29 '21 at 9:09
  • @NiharKarve If I understand it correctly a "mailbox" would be an array of 64 say integers that represent which pieces are on the 64 different squares? So kind of like a non-bitboard board representation? Oct 29 '21 at 9:53
  • Yes, exactly. Stockfish does this too IIRC Oct 29 '21 at 10:31

Given there is a maximum of 16 pieces on the chessboard you could loop on the pieces instead of the 64 squares. In this way, the less pieces on the board the faster. Each piece contains the index of the square, you then only have to pick the value of the corresponding weight to have your score. As each piece could have their own array of weight you can even pre compute the values on the weight adding the piece value and the position. If the performances really matter, use a profiler to know where is the bottleneck. You might end up using integers instead of floats, pointers instead of indexes, masks for operations and other smart operators to run operations in the same time.

  • But after doing a bin scan on the pieces bitboard, how do I check which piece is actually on that square? By doing if(white_pawns & pieces) etc for all piece bitboards? Oct 27 '21 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.