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
The Rook and the collision problem
The rook, bishop, and queen are sliding pieces: they can move along a line for many squares, but they cannot jump over other pieces (unlike the knight). For example, if we want to move a rook from a1 to a5, we must verify that squares a2, a3, and a4 are all empty. The presence of even a single piece along the path between the starting square and the destination square is enough to make the move illegal.
Linear search algorithm
We need to write code that checks that the squares between the start and the end are free. To do this, we must scan the board using a for loop that stops as soon as an obstacle is detected.
If the rook has to move from a1 to a5, or from a5 to a1, the squares to scan are always a2, a3, and a4: only the order in which they are examined changes. In algorithmic terms, to scan the intermediate squares regardless of the direction of movement, we can use a for loop that starts from the smaller row index and goes up to the larger one. To determine the smaller and larger row indices, we can use the min() and max() functions, which return, respectively, the minimum and the maximum of the two arguments provided.