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
Challenge students with the following task:
Write a Python function named show_board_size() that takes no parameters and, when called, prints the following two lines:
Chessboard size: 8 x 8 squares.
Rows and columns are indexed from 0 to 7.
Then, in the main program, call the function.
SOLUTION
def show_board_size():
print("Chessboard size: 8 x 8 squares.")
print("Rows and columns are indexed from 0 to 7.")
show_board_size()
Challenge students with the following task:
Write a Python function named describe_move(piece, start_square, end_square) that takes three parameters:
· piece: a string representing the name of the piece (e.g., "King", "Rook", "Knight");
· start_square: a string representing the starting square in chess notation (e.g., "e2");
The function must print the sentence “The {piece} moved from {start_square} to {end_square}”, correctly replacing the placeholders with the parameter values. If start_square and end_square are the same (a null move), the function must print an error message.
In the main program, call the function at least twice:
· once with a valid move (different squares);
SOLUTION
def describe_move(piece, start_square, end_square):
if start_square == end_square:
print("Error: null move")
else:
print(f"The {piece} moved from {start_square} to {end_square}")
describe_move("King", "e2", "e4")# Valid
describe_move("King", "e4", "e4")# Null move -> rejected
Challenge students with the following task:
Write the function is_empty(board, r, c) that returns True if the square identified by row r and column c is empty, and False otherwise. The function must not print anything to the console; it must only return the boolean value using a return statement. The variable board represents a matrix in which each cell contains a symbol that identifies the piece placed on that square, while the character "." indicates an empty square.
SOLUTION
def is_empty(board, r, c):
value = board[r][c]
return value == "."
Challenge students with the following task:
In a chess program, you want to manage the alternation of turns between the two players. Write a function named update_turn(turn) that takes as input a string called turn, which can be either "White" or "Black", and returns "Black" when the input is "White" and "White" when the input is “Black”. The function must not print anything; it must return the updated value using only the return keyword. In the main program, initialize the variable turn to "White", call the function with turn as an argument, and then print the variable’s updated value.
SOLUTION
def update_turn(turn):
if turn == "White":
return "Black"
else:
return "White"
turn = "White"
turn = update_turn(turn)
print(turn)
Challenge students with the following task:
In a simple chess program, you want to keep track of the total number of moves made during a game using a global variable. Write a program that defines a global variable named move_count initialized to 0, then defines a function called register_move() that increments it by 1 each time the function is called and prints the updated value.
In the main program, call register_move() at least five times to simulate five moves and verify that the counter increases correctly.
SOLUTION
move_count = 0
def register_move():
global move_count
move_count += 1
print(f"Move number: {move_count}")
register_move()
register_move()
register_move()
register_move()
register_move()