I'm writing a chess game in python. When a king is checked, i need to filter out the possible moves, to block the check, or to move the king. I want to write a function that will return the possible blocking moves, to block the check. A function will look like this:
def getBlockingCheckMoves(pieceField,kingPos)
For a rook, a long solution would look like this:
- See if x coordinates are same, return tuples made from a range of y's, with the correct increment.
- if y coordinates are the same, return tuples made from a range of x's.
Is there a more pythonic way to accomplish this using itertools?
EDIT: my long version
if pieceField[0] == kingPos[0]:
if pieceField[1] < kingPos[1]:
return [(pieceField[0],x) for x in range(pieceField[1],kingPos[1])]
else:
return [(pieceField[0],x) for x in range(pieceField[1],kingPos[1],-1)]
if pieceField[1] == kingPos[1]:
if pieceField[0] < kingPos[0]:
return [(x,pieceField[1]) for x in range(pieceField[0],kingPos[0])]
else:
return [(x,pieceField[1]) for x in range(pieceField[0],kingPos[0],-1)]