Ejemplo n.º 1
0
class Ship(object):
    def __init__(self):
        self.position = Position(0, 0)
        self.direction = "N"
        self.torpedo_cooldown = 3

    def update_turn_data(self, turn_data):
        self.torpedo_cooldown = turn_data["torpedo_cooldown"]

    def move(self, direction):
        self.direction = direction
        self.position = self.position.add_direction(direction)

    def get_position_after_move(self, direction):
        return self.position.add_direction(direction)

    def choose_starting_cell(self, board):
        x = random.randint(0, board.width - 1)
        y = random.randint(0, board.height - 1)
        random_position = Position(x, y)
        while not board.is_position_valid_for_move(random_position):
            random_position.x = random.randint(0, board.width - 1)
            random_position.y = random.randint(0, board.height - 1)
        self.position = random_position
        print("{} {}".format(self.position.x, self.position.y))

    def __str__(self):
        return str(self.position)
Ejemplo n.º 2
0
class Ship(object):
    def __init__(self):
        self.position = Position(0, 0)
        self.direction = "N"
        self.torpedo_cooldown = 3
        self.life = 6

    def update_with_turn_data(self, context_data):
        self.position = Position(x=context_data.current_turn_x,
                                 y=context_data.current_turn_y)
        self.torpedo_cooldown = context_data.current_turn_torpedo_cooldown
        self.life = context_data.current_turn_my_life

    def get_position_after_move(self, direction):
        return self.position.add_direction(direction)

    def choose_starting_cell(self, board):
        x = random.randint(0, board.width - 1)
        y = random.randint(0, board.height - 1)
        random_position = Position(x, y)
        while not board.is_position_valid_for_move(random_position):
            random_position.x = random.randint(0, board.width - 1)
            random_position.y = random.randint(0, board.height - 1)
        self.position = random_position
        print("{} {}".format(self.position.x, self.position.y))

    def __str__(self):
        return str(self.position)
Ejemplo n.º 3
0
class EnemyShip(object):
    def __init__(self, height, width, lines):
        self.delta_position = Position(0, 0)
        self.enemy_board = Board(height=height, width=width, lines=lines)
        self.number_of_possible_positions = height * width

    def update_number_of_possible_positions(self):
        self.number_of_possible_positions = self.enemy_board.compute_number_of_potential_positions(
        )

    def read_opponent_order(self, opponent_order):
        if opponent_order == "NA":
            return
        list_opponent_order = opponent_order.split("|")
        move_order = ServiceOrder.get_move_order(list_opponent_order)
        if move_order:
            self.delta_position = self.delta_position.add_direction(
                ServiceOrder.get_direction_from_order(move_order))
        self.enemy_board.update_enemy_start_position(self.delta_position)
        self.enemy_board.update_enemy_current_position(self.delta_position)
        self.update_number_of_possible_positions()