I would like to write the chess engine in C/C++ to improve my C skills, but I just cannot let go of the highly expressive Ruby. Ruby is also much slower compared to C.
So I've decided to start with C++ and see how intolerable it is to me. The first step of writing a chess engine is to program a board representation. A board representation is the foundation of the chess engine. It is used in keeping track of the chessboard state, evaluation of board positions and almost every part of the engine.
There are different types of board representation, which can be categorized into 2 main approaches. Namely the Square Centric, Piece Centric and Hybrid Approach. The descriptions of these approaches can be found in the link here.
Long story short, I've picked the square centric, bitboard representation. A bitboard represents the board with a 64-bit word. Using 0s and 1s to denote whether a pieces/ potential move is there. This representation can take advantage of the 64-bit hardware and speed up the operations.
Take the following example:
Number of attacked pieces can be calculated through bitwise and of two boards! On top of that, this operation only takes 1 CPU cycle to complete! I can imagine the chess engine will perform calculations just like this thousand times over and over again in search. No wonder bitboard is a popular representation among chess engines.queen attacks & opponent pieces = attacked pieces . . . . . . . . 1 . . 1 1 . . 1 . . . . . . . . . . . 1 . . 1 . 1 . 1 1 1 1 1 . . . . 1 . . 1 . . 1 . 1 . 1 . . . 1 . . . . . 1 . 1 . . . . . . . . 1 1 1 . . . . . . . . . . . . . . . . . . . 1 1 1 * 1 1 1 . & . . . * . . 1 . = . . . * . . 1 . . . 1 1 1 . . . . . . . . . . . . . . . . . . . . . . 1 . 1 . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . .