Rules for a pawn

A pawn has a unique movement too in that it moves forward, but it captures diagonally. Let's similarly override the moves_available class from within the Pawn class, as follows (see 4.04piece.py):

class Pawn(Piece):

def moves_available(self, current_position):
model = self.model
piece = self
if self.color == 'white':
initial_position, direction, enemy = 1, 1, 'black'
else:
initial_position, direction, enemy = 6, -1, 'white'
allowed_moves = []
# Moving
prohibited = model.all_occupied_positions()
start_position = get_numeric_notation(current_position.upper())
forward = start_position[0] + direction, start_position[1]
if model.get_alphanumeric_position(forward) not in prohibited:
allowed_moves.append(forward)
if start_position[0] == initial_position:
# If pawn is in starting position allow double moves
double_forward = (forward[0] + direction, forward[1])
if model.get_alphanumeric_position(double_forward) not in
prohibited:
allowed_moves.append(double_forward)
# Attacking
for a in range(-1, 2, 2):
attack = start_position[0] + direction,
start_position[1] + a
if model.get_alphanumeric_position(attack) in
model.all_positions_occupied_by_color(enemy):
allowed_moves.append(attack)
allowed_moves = filter(model.is_on_board, allowed_moves)
return map(model.get_alphanumeric_position, allowed_moves)

The following is a description of the preceding code:

  • We first assigned the initial_row_position, direction, and enemy variables depending on whether the pawn is black or white.
  • Similar to the previous moves_allowed methods, this method collects all the allowed moves in a blank list named allowed_moves.
  • Then, we collected a list of all the prohibited moves by concatenating two lists of squares occupied by all the black and white chess pieces.
  • We defined a variable named forward which holds the position of the square immediately ahead of the current position of the pawn.
  • A pawn cannot move forward if there is a chess piece in front of it. If the forward position is not prohibited, the position is appended to the allowed_moves list.  A pawn can move two places forward from its starting position. We check whether the current position is the starting position, and if it is the starting position, we append the double move to the allowed_moves list.
  • A pawn can capture only the diagonally adjacent chess pieces in front of it. Therefore, we assigned a variable attack to track the diagonally adjacent positions on the chessboard. If the diagonally adjacent square is occupied by an enemy, that position qualifies to be appended to the allowed_moves list.
  • Then, we filtered the list to remove all the positions that may fall outside the chessboard. The last line returns all the allowed moves as a list of corresponding alphanumeric notations, as we did in all the previous definitions.

This completes the current iteration. We coded the logic needed to enforce the rules related to the movement of chess pieces on the chessboard.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.235.8