I am writing a Chess program in Python that needs to generate all the moves of a knight. For those not familiar with chess, a knight moves in an L shape.
So, given a position of (2, 4)
a knight could move to (0, 3)
, (0, 5)
, (1, 2)
, (3, 2
), etc. for a total of (at most) eight different moves.
I want to write a function called knight_moves
that generates these tuples in a list. What is the easiest way to do this in Python?
def knight_moves(position):
''' Returns a list of new positions given a knight's current position. '''
pass
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…