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
With reference to Part 2 Ask students to Write a function that takes the board and the start and end coordinates as input, and returns True if the path is clear, False if there is an obstacle. We will focus on horizontal/vertical movement (rook).
Show and discuss the Solution: 07 Pathclearfunction.pdf
Challenge students with the following task:
Write a function named print_row(board, row) that receives the chessboard and a row index row (from 0 to 7) and prints all the symbols stored in that row. The function is allowed to print on the console and does not return any value.
SOLUTION
def print_row(board, row):
for c in range(8):
print(board[row][c])
Challenge students with the following task:
Write the function count_pawns_in_row(board, r) that takes the chessboard board and a row index r (from 0 to 7) as input. The function must scan all squares in row r and count how many white pawns "P" and how many black pawns "p" are present. It must return both counts as a pair (white_pawns, black_pawns). The function must not print anything.
SOLUTION
def count_pawns_in_row(board, r):
white_pawns = 0
black_pawns = 0
for c in range(8):
piece = board[r][c]
if piece == "P":
white_pawns = white_pawns + 1
elif piece == "p":
black_pawns = black_pawns + 1
return white_pawns, black_pawns
Challenge students with the following task:
Write the function board_occupancy(board) that scans all 64 squares and returns a pair (empty, occupied), where empty is the number of "." cells and occupied is the number of non-empty squares. The function must not print anything.
SOLUTION
def board_occupancy(board):
empty = 0
occupied = 0
for r in range(8):
for c in range(8):
if board[r][c] == ".":
empty = empty + 1
else:
occupied = occupied + 1
return empty, occupied
Challenge students with the following task:
Write the function rook_can_capture(board, start_r, start_c, end_r, end_c) that returns True only if the move is not a null move (that is, the start and end squares are different), the destination square is not "." (meaning there is a piece to capture), and the move is horizontal or vertical with a clear path between the start and end squares. Otherwise, it must return False. The function must not print anything.
SOLUTION
def rook_can_capture(board, start_r, start_c, end_r, end_c):
if board[end_r][end_c] == ".":
return False
if start_r == end_r and start_c == end_c:
return False
if start_c == end_c:
start_path = min(start_r, end_r) + 1
end_path = max(start_r, end_r)
for r in range(start_path, end_path):
if board[r][start_c] != ".":
return False
elif start_r == end_r:
start_path = min(start_c, end_c) + 1
end_path = max(start_c, end_c)
for c in range(start_path, end_path):
if board[start_r][c] != ".":
return False
else:
return False
return True
Challenge students with the following task:
Write a program fragment that repeatedly asks the user to choose a difficulty level for a game. The input is considered valid only if it matches one of the following options: “easy”, “medium”, or “hard”. If the user enters an invalid value, the program must display an error message and prompt the user again. When a valid value is entered, the program must print a confirmation message showing the chosen difficulty.
SOLUTION
difficulty = input("Choose the difficulty (easy/medium/hard): ")
while difficulty != "easy" and difficulty != "medium" and difficulty != "hard":
print("Invalid value.")
difficulty = input("Choose the difficulty (easy/medium/hard): ")
print(f"You chose the difficulty: {difficulty}")