0

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:

  1. See if x coordinates are same, return tuples made from a range of y's, with the correct increment.
  2. 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)]
4

0 回答 0