Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Introduction

The move generator is the part of a chess engine that produces all possible moves from a given board position. In Rustic, this means generating pseudo-legal moves: these moves follow the rules of how pieces move, without checking whether the king is left in check. The result is a list of candidate moves that the rest of the engine can work with.

The generator takes the current position as input and, based on the placement of pieces and the side to move, creates moves for each piece type. This includes normal moves as well as special cases such as promotions, castling, and en passant. The move generator does not try to determine whether a move is fully legal. It only determines if it is structurally valid according to the movement rules.

Rustic’s move generator is bitboard-based. Non-sliding pieces are handled directly. Knights, kings, and pawns have a limited and fixed set of movement patterns, so their pseudo-legal moves can be generated by simple table lookups based on their square. The legal moves are then created by combining this lookup with occupancy bitboards from the position. These operations are extremely fast and require no looping or calculation, except for using bitboards to mask out friendly pieces.

For sliding pieces such as bishops, rooks, and queens, the generator relies on magic bitboards: these are precomputed tables indexed via carefully chosen “magic numbers.“ These allow the engine to determine attack sets based on board occupancy with a small number of operations. This is much faster than tracing each ray a slider has. A queen, for example, has 8 rays going out from its starting square, and it can go to 27 squares on an empty board. You can imagine that it takes a lot of time to determine which squares are in which ray, and if those squares ar reachable. The magic bitboard method avoids this costly operation.

This design keeps the move generator focused and efficient. Its only responsibility is to enumerate moves as quickly as possible, using fast bitwise operations and precomputed data. More complex checks, such as whether a move leaves the king in check, are deferred to later stages of move making.

If some of these terms such as magic numbers, attack tables or lookups are not yet familiar, that is not a problem. They will be introduced and explained step by step throughout this chapter. For now, it is enough to understand the role of the move generator: to efficiently produce a complete set of pseudo-legal moves for the current position. Let’s take a look at how it is set up.