An engine cannot search until it can list what each side is allowed to play. This lesson pairs with Coding Adventure: Chess by Sebastian Lague, linked under Watch & read; its chapters on the board and pieces and on generating moves cover the same subject from another angle. You build the move generator for all six kinds of piece on a flat 64-square board, plus a test for whether a square is attacked, and it is graded against a second generator that never uses a square number.
Number the squares rank × 8 + file, so a1 is 0, h1 is 7, a2 is 8 and h8 is 63. A board is 64 one-character strings: uppercase for White, lowercase for Black, . for an empty square. A move is a tuple (from, to, promo), where promo is empty or one of q r b n. With this layout a step is an addition: +8 is one rank up, +1 is one file to the right, +9 is up and to the right. A knight has eight jumps: ±6, ±10, ±15 and ±17.
The trouble is the edge. An addition can leave the board without leaving 0..63. One step right from h1 gives 8, which is a2. A knight jump of +6 from b1 gives 7, which is h1, on the other side of the board. Checking that the target is between 0 and 63 is not enough. The fix is one helper that every piece uses:
def jump(sq, offset):
to = sq + offset
return to if 0 <= to < 64 and abs(to % 8 - sq % 8) <= 2 else None
knight = (-17, -15, -10, -6, 6, 10, 15, 17)
print([1 + o for o in knight if 0 <= 1 + o < 64])
print([jump(1, o) for o in knight if jump(1, o) is not None])A real knight or king step changes the file by at most two. A wrapped step changes it by six or seven. So comparing the file before and after separates the two cases, whatever the piece.
Knights and kings are now a loop over a fixed list of offsets. Keep a target when it is empty or holds an enemy piece, and drop it when a friend stands there. In the starting position the knight on b1 has the targets a3 and c3; d2 holds its own pawn.
Rooks, bishops and queens are sliders. For each direction, step over empty squares and record each one. The first occupied square ends the walk. If it holds an enemy piece, record a capture; if it holds a friend, record nothing. Never look behind it.
def ray(board, sq, d):
out, to = [], sq + d
while 0 <= to < 8 and board[to] == ".":
out.append(to)
to += d
if 0 <= to < 8 and board[to].islower():
out.append(to)
return out
print(ray("R..n....", 0, 1))
print(ray("R..N....", 0, 1))Pawns are the only pieces that depend on colour. Forward is +8 for White and -8 for Black. A push needs an empty square. The double push needs the home rank and both squares empty. Captures go to the two forward diagonals and need an enemy piece there. A move onto the last rank is four moves, one for each promotion letter. En passant is a capture onto an empty square: ep names the square behind the pawn that just advanced two ranks, and an enemy pawn beside it may land there. The move tuple does not remove the captured pawn. That belongs to applying a move, which comes later.
pseudo_legal_moves collects the moves of every piece of one side into a sorted list. Pseudo-legal means each move follows its piece's rule; it may still leave its own king in check. Removing those moves is the next lesson. In the starting position the count is 20 for each side: sixteen pawn moves and four knight moves.
is_attacked turns the question around. Rather than generate the enemy's moves and search them, stand on the square and look outward: for a pawn, knight or king at the right offsets, and for the first piece along each ray. A White pawn attacks the squares diagonally above it, so the pawn that attacks e5 stands at e5 - 9 or e5 - 7. A pawn's straight push attacks nothing. A square is attacked even when a piece stands on it.
Three mistakes account for most wrong generators. Offsets that wrap at an edge, which fail for pieces beside the board's side. A ray that walks on past a capture. A pawn that captures straight ahead or pushes onto an occupied square, which only shows when a piece stands in front of it.
The hidden tests rebuild every move with (file, rank) pairs, where nothing can wrap, on sixty seeded random boards, and compare an attack test against the same kind of rebuild on every square of forty more. They also place each piece on all 64 squares. That agreement shows the two programs read the rules the same way. It does not show that either has the rules right in cases the tests never build, and it says nothing about checks, castling or whether a move leaves the king safe.