A pawn in the game
Next we consider matters peculiar to pawns:
if p == ‘P’ and d in (N+W, N+E) and q == '.’ and j not in (self.ep, self.kp): break if p == ‘P’ and d in (N, 2*N) and q != '.‘: break if p == ‘P’ and d == 2*N and (i < A1+N or self. board[i+N] != '.‘): break
First, pawns cannot move diagonally into an empty square, unless an en passant capture can take place. Next, they can only move forwards (one or two squares, we check if the latter is allowed in the next line). The i < A1 + N comparison will return true for any pawn that has moved beyond the second row. Two-square rights are likewise denied if there’s a piece in front of the pawn. The closing stanza of gen_moves() yield (i, j) if p in ('P’, ‘N’, ‘K'): break if q.islower(): break Having got all the constraints, we can now pass on the move under consideration, it may yet turn out not to be valid (eg if it doesn’t alleviate a check situation) but it’s passed the first level of filtration. Pawns, knights and rooks aren’t allowed to slide, and those pieces that are have to stop doing so if they capture a piece (ie land on a lowercase entry in board
We’ve already discussed the rotate() function. But impressively the board can be transposed (which results in the equivalent game with the colours switched and the board rotated 180 degrees) just by reversing the board string and switching cases. We must take care of the other parts of our
reads:
).