Every chess engine starts by settling what a position is: where the pieces stand, whose turn it is, and a few facts that the board alone cannot tell you. This lesson pairs with Coding Adventure: Chess by Sebastian Lague, linked under Watch & read. The video begins with a chapter on the board and pieces. Here you build the representation, a reader and a writer for the standard one-line text format, and a strict validator that guards the rest of your engine from bad input.
Start with the squares. Number them 0 to 63 with a1 = 0, h1 = 7, a2 = 8 and h8 = 63. The file letter gives the column (a is 0, h is 7), the rank digit minus one gives the row, and the index is row × 8 + column. The square e4 is column 4 in row 3, so it is square 28. The board is a tuple of 64 one-character strings: uppercase for white, lowercase for black, . for an empty square. A tuple and a frozen dataclass, because a position that cannot change can be compared, shared and used as a dictionary key.
names = [f + r for r in "12345678" for f in "abcdefgh"]
print(names[0], names[7], names[8], names[63])
print(names.index("e4"), 3 * 8 + 4)The board does not say everything. A position also needs the side to move, the castling rights that remain, the square where an en passant capture is possible, and two counters: the halfmove clock, which counts moves since a capture or a pawn move, and the fullmove number. FEN (Forsyth-Edwards Notation) packs all of it into one line of six fields separated by single spaces. The starting position is rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1.
The first field is the placement. The ranks are written from rank 8 down to rank 1, separated by slashes. A letter is a piece, and a digit from 1 to 8 is a run of that many empty squares. The text reads like a diagram, from the top, while your index 0 is at the bottom, so the first rank string fills row 7 and the last one fills row 0. The other fields are w or b; the castling rights as letters of KQkq in that order, or -; the en passant target, which is the square a pawn just skipped over with a double step, or -; and the two counters.
Take the position after 1. e4. Its placement is rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR. The fifth string, 4P3, is rank 4, so it fills row 3, and the P lands on column 4: square 28, which is e4. White has just moved, so black is to move, and the en passant target is e3, the square the pawn passed over. It sits on rank 3 because the pawn that can be captured is white's. With white to move the target would be on rank 6.
placement = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR"
board = [None] * 64
for i, rank in enumerate(placement.split("/")):
squares = "".join("." * int(ch) if ch in "12345678" else ch for ch in rank)
board[(7 - i) * 8:(7 - i) * 8 + 8] = squares
print("".join(board[24:32]), "".join(board[8:16]))
print(board.index("P", 16))Writing is the same walk backwards. Go through the eight squares of a rank, count the empty ones, and write the count when a piece or the end of the rank arrives. The end of the rank is the case people forget: a rank such as 8 never meets a piece, so a writer that only flushes the count at a piece drops it.
Three mistakes account for most broken parsers. The first is reading the ranks from the bottom, which mirrors the board. The files stay right and the pieces look plausible, so only the case of the letters gives it away. The second is treating digits as characters instead of as counts, or as characters that merely look like digits: a check such as str.isdigit accepts 0 and 9 and digits of other scripts, and a zero adds no squares, so a rank that is already full still passes. The third is accepting looser input than the writer produces. Rejecting two digits in a row, such as 44 for 8, gives every accepted string one spelling, so that writing a parsed position returns the original text.
The tests check the result three ways. Real positions are read and written back to the same text. An independent reader, which expands digits and reverses the ranks, must agree with yours. Random boards are written by a second method that groups runs, and a long list of malformed strings must all raise ValueError. These show that your code follows the format. They do not show that a position can occur in a real game: pawns on the first rank are accepted here on purpose, and nothing checks that an en passant square has a pawn behind it. Rules like that belong to move generation, which comes later in this course.