def _pos_iter(piece, transformation): """Iterate through positions based on the given transformation function.""" pos = piece.position while True: pos = Position(*transformation(*pos)) if not pos.is_valid(): break other_piece = piece.grid[pos] if other_piece: if other_piece.color != piece.color and piece.move_attacks: yield pos break yield pos
def attack_range(self): """Get an attack range for pawn.""" r, f = self.position n_rank = r + self.get_direction(self).value rng = [] for n_file in (f + 1, f - 1): pos = Position(n_rank, n_file) other = self.grid[pos] if pos.is_valid() and (not other or other.color != self.color): rng.append(pos) return rng
def move(self, position): # noqa: D102 new_pos = Position(*position) if not new_pos.is_valid(): return False if position == self.position: return False if new_pos not in self.move_range + self.attack_range: return False self.previous_positions.append(self.position) self.position = position return True