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
Not all pieces are as simple as the Rook. The Knight moves in an "L" shape: two squares in one direction, and one in the perpendicular direction. To verify this move, a single comparison is not enough. We must combine multiple conditions using Logical Operators:
The "L" Movement (Math + Logic)
How do we translate the "L" into math? We use the absolute value of the difference between coordinates (the number of steps taken).
Let Delta r = |r1 - r2| and Delta c = |c1 - c2|.
The Knight move is valid if:
(Delta r = 2 AND Delta c = 1) OR (Delta r = 1 AND Delta c = 2)
In Python:
delta_r = abs(r1 - r2)
delta_c = abs(c1 - c2)
knight_move = (delta_r == 2 and delta_c == 1) or (delta_r == 1 and delta_c == 2)2.
Boundaries
A common mistake for beginners is trying to move a piece off the board (e.g., column "I" or row 9). This causes a critical error in programming called an IndexError. We must always validate that coordinates are within the 0-7 limits.
destination_row = 8 # Imagine incorrect user input
# Validation with AND
# The row must be >= 0 AND it must be <= 7
if destination_row >= 0 and destination_row <= 7:
print("Move is inside the board")
else:
print("Error: You fell off the table!")