1

Just like the title says. A mailbox would allow for better handling of checking "which piece is at a certain square," and I see no real downsides to it besides the need to synchronize it with the bitboards properly.

3
  • Since it is redundant you can just remove it.
    – ferdy
    Jun 7 at 0:51
  • Well, it's not really "redundant" per se. It is useful for finding out what piece is on a given square.
    – AAce3
    Jun 7 at 1:38
  • Not sure if the same idea you're proposing, but DanChess uses an "hybrid" system. Also, I guess you could get more help in this forum (but you'd have to provide more details about your idea).
    – emdio
    Jun 7 at 9:50

1 Answer 1

4

Is it worth it to use a redundant mailbox representation in addition to bitboards?

In my estimation, yes.

Bitboards are nice. But as you mentioned, getting information about what pieces sit on a certain square is a pain. You could look through all of your bitboards and test whether a bit is set at that square, but that becomes costly when you're running said functions millions of times.

So as you recognize, a hybrid approach is likely most optimal. And note this isn't at all a strange idea. Most of the top chess engines use it. For example, here's the relevant code from Stockfish:

class Position {

...

private:
  // Data members
  Piece board[SQUARE_NB];
  Bitboard byTypeBB[PIECE_TYPE_NB];
  Bitboard byColorBB[COLOR_NB];
  int pieceCount[PIECE_NB];
  int castlingRightsMask[SQUARE_NB];
  Square castlingRookSquare[CASTLING_RIGHT_NB];
  Bitboard castlingPath[CASTLING_RIGHT_NB];
  Thread* thisThread;
  StateInfo* st;
  int gamePly;
  Color sideToMove;
  Score psq;
  bool chess960;
};

Where board is defined as a 64-element array of Piece objects. You'll see similar definitions in many other engines as well. And I myself actually use this method, after learning the hard way the inadequacy of bitboards to deal with square-centric information.

Do note however that not all engines use this method. Some use the approach of copying the board before making a move, and since this copying process can be expensive, they can't afford to add a whole 64-element piece array to the position. So they find acceptable workarounds.

So my advice? Go for the hybrid approach, it's likely the easiest, most efficient, and most sustainable approach in the long run for writing a good chess engine. But don't just take my word for it either. Experiment and profile and see what works best for your specfic engine. Half the fun of creating an engine is solving your own problems and adapting ideas to fit your unique needs.

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.