How can I get coordinates working in this code?
You need to add the proper offset to the current piece position, and return the list of possible legal positions.
maybe something along these lines:
def get_possible_knight_moves(pos): x, y = pos possible_moves = [] for dx, dy in knight_offsets: new_pos = (x + dx, y + dy) if is_legal(new_pos): possible_moves.append(new_pos) return possible_movesdef is_legal(pos): x, y = pos return 0 <= x < 8 and 0 <= y < 8knight_offsets = [(2, 1), (2, -1), (1, 2), (1, -2), (-2, 1), (-2, -1),(-1, 2),(-1, -2)]pieceposition = (3 , 4)movelist = get_possible_knight_moves(pieceposition)print (movelist)
output:
[(5, 5), (5, 3), (4, 6), (4, 2), (1, 5), (1, 3), (2, 6), (2, 2)]
For a knight in position (0, 0)
, the output is [(2, 1), (1, 2)]