def _pieces_blocking_move(self, piece, coords): """Helper method. Return bool.""" if piece.type == 'Knight': # Knight can jump over pieces return False # Sort coords so logical direction of move not important # (x=5, y=6) -> (x=1, y=2) same as (x=1, y=2) -> (x=5, y=6) min_x_coord, max_x_coord = sorted([piece.x_coord, coords.x]) min_y_coord, max_y_coord = sorted([piece.y_coord, coords.y]) direction = move_direction(piece, coords) if direction == 'vertical': for next_y_coord in range(min_y_coord + 1, max_y_coord): if self.board[piece.x_coord][next_y_coord] is not None: return True elif direction == 'horizontal': for next_x_coord in range(min_x_coord + 1, max_x_coord): if self.board[next_x_coord][piece.y_coord] is not None: return True elif direction == 'diagonal': next_y_coord = min_y_coord + 1 for next_x_coord in range(min_x_coord + 1, max_x_coord): if self.board[next_x_coord][next_y_coord] is not None: return True next_y_coord += 1 # No pieces blocking return False
def test_move_direction_non_linear(self): to_coords = self.coords(x=3, y=2) direction = move_direction(self.pawn, to_coords) assert direction == 'non_linear'
def test_move_direction_vertical(self): to_coords = self.coords(x=4, y=6) direction = move_direction(self.pawn, to_coords) assert direction == 'vertical'
def test_move_direction_horizontal(self): to_coords = self.coords(x=6, y=4) direction = move_direction(self.pawn, to_coords) assert direction == 'horizontal'
def test_move_direction_diagonal_down(self): to_coords = self.coords(x=6, y=6) direction = move_direction(self.pawn, to_coords) assert direction == 'diagonal'
def test_move_direction_diagonal_up(self): to_coords = self.coords(x=2, y=2) direction = move_direction(self.pawn, to_coords) assert direction == 'diagonal'
def _valid(self, coords): # Any linear move/capture valid for Queen if move_direction(self, coords) != 'non_linear': return True return False