The ChessAIThon project (2025-1-ES01-KA220-VET-000354329) is co-funded by the European Union. The views and opinions expressed in this publication are those of the author(s) only and do not necessarily reflect those of the European Union or the Spanish Service for the Internationalisation of Education (SEPIE). Neither the European Union nor the National Agency SEPIE can be held responsible for them.
Table of Contents
In the real world, if you say "The Knight is on b1," you are using a coordinate system to describe a state (at square b1, we find a Knight). In computer science, we do the same using variables and data types.
Pieces as Data (Data Types
A chess piece has intrinsic properties (at this stage, we only want to represent its type: Pawn, Knight, etc., and its color: White or Black). In programming, we must decide how to represent these properties.
We can represent pieces in different ways, depending on the required complexity:
Strings: the simplest and most readable way. Strings are ordered sequences of characters used to store textual data. They are delimited by single or double quotes, and every character has a precise position starting from 0.
current_piece = "Knight"Integers: more efficient for the computer, but less readable for humans without a legend.
current_piece = 2Chars (Characters): containers that hold a single character. In chess, these are often used in standard notation (FEN: Forsyth-Edwards Notation, a standard format for recording a specific board position).
Practical Example in Python: imagine we want to save the state of a single square.
# Strings - variable assignment
square_e4 = "Empty" # A string indicating no piece
square_a1 = "White Rook" # A string describing the piece
print(square_a1)
# Char – FEN notation
square_c2 = 'P' # 'P' for White Pawn
print(square_c2)
Output:
White Rook
P
Coordinates and Assignment
On a chessboard, every square is unique. In programming, every variable must have a unique identifier (or address). Saying "Move the White Pawn to e4" in computing terms means: overwrite the value contained in the variable representing e4 with the value "Pawn" (or "P").
This is the assignment operation: square_e4 = "P"
The assignment operator is the "=" symbol. The variable on the left of the equal sign takes the value on the right. Note: The name is always on the left, the new value is always on the right.