def update_enemy_start_position(self, delta_position): for x in range(self.width): for y in range(self.height): start_position = Position(x, y) current_position = start_position.add_position(delta_position) if not self.is_position_valid_for_enemy(current_position): self.get_cell(start_position).cannot_be_enemy_start()
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)
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 update_enemy_potential_start_position(self, delta_position): for x in range(self.width): for y in range(self.height): start_position = Position(x, y) current_position = start_position.add_position(delta_position) cell = self.get_cell(current_position) if cell: if not self.get_cell( current_position).can_be_enemy_position: self.get_cell(start_position).cannot_be_enemy_start()
def update_possible_start_position_after_silence_from_current_position( self, position): for x in range(-4, 4, 1): delta_position = Position(x, 0) new_position = position.add_position(delta_position) cell_new_position = self.get_cell(new_position) if cell_new_position: cell_new_position.can_be_enemy_start = not cell_new_position.is_island for y in range(-4, 4, 1): delta_position = Position(0, y) new_position = position.add_position(delta_position) cell_new_position = self.get_cell(new_position) if cell_new_position: cell_new_position.can_be_enemy_start = not cell_new_position.is_island
def update_enemy_current_position(self, delta_position): for x in range(self.width): for y in range(self.height): current_position = Position(x, y) start_position = current_position.add_position( delta_position.invert_position()) if not self.get_cell(start_position): self.get_cell( current_position).can_be_enemy_position = False else: can_be_position = self.get_cell( start_position).can_be_enemy_start self.get_cell(current_position ).can_be_enemy_position = can_be_position
def compute_number_of_potential_positions(self): number_of_positions = 0 for x in range(self.width): for y in range(self.height): if self.get_cell(Position(x,y)).can_be_enemy_position: number_of_positions += 1 return number_of_positions
def get_neighbours(self, cell): neighbours = [] for (x, y) in [(-1, 0), (1, 0), (0, -1), (0, 1)]: neighbour_position = Position(x, y).add_position(cell.position) if bool(self.get_cell(neighbour_position)): neighbours.append(self.get_cell(neighbour_position)) return neighbours
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)
def enemy_was_in_range(self, range_detection, detection_position): enemy_board = self.enemy_board for x in range(enemy_board.width): for y in range(enemy_board.height): starting_cell = enemy_board.get_cell(Position(x, y)) last_turn_position_from_starting_position = Position( x, y).add_position(self.last_turn_delta_position) last_turn_position = enemy_board.get_cell( last_turn_position_from_starting_position) # Case last_turn_position not on board if not bool(last_turn_position): starting_cell.cannot_be_enemy_start() # Case last_turn_position_hors de_range elif last_turn_position.position.get_distance( detection_position) > range_detection: starting_cell.cannot_be_enemy_start() enemy_board.update_enemy_current_position(self.delta_position)
def __init__(self, height, width, lines): self.height = height self.width = width self.map = [] for y, line in enumerate(lines): cell_line = [] for x, char in enumerate(line): cell_line.append(Cell(Position(x, y), char == "x")) self.map.append(cell_line)
def update_board_torpedo_did_not_hit_in_position(self, torpedo_position, delta_position): torpedo_delta_range = [-1, 0, 1] for x in torpedo_delta_range: for y in torpedo_delta_range: torpedo_delta_position = Position(x, y) current_position_without_enemy_boat = torpedo_position.add_position( torpedo_delta_position) start_position_without_enemy_boat = current_position_without_enemy_boat.add_position( delta_position.invert_position()) start_cell = self.get_cell(start_position_without_enemy_boat) if start_cell: start_cell.cannot_be_enemy_start() self.update_enemy_current_position(delta_position)
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()
def get_position_from_attack_order(attack_order): coordinates = attack_order.replace("TORPEDO ", "") list_coordinate = coordinates.split(" ") return Position(list_coordinate[0], list_coordinate[1])
def reset_is_visited(self): for x in range(self.width): for y in range(self.height): self.get_cell(Position(x=x, y=y)).reset_visit()
def extract_position_from_attack_order(attack_order): string_position = attack_order.replace("TORPEDO ", "") list_coordinates = string_position.split(" ") return Position(list_coordinates[0], list_coordinates[1])
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_all_cells(self): cells = [] for x in range(self.width): for y in range(self.height): cells.append(self.get_cell(Position(x, y))) return cells
def reset_delta_position(self): self.delta_position = Position(0, 0)
def __init__(self): self.position = Position(0, 0) self.direction = "N" self.torpedo_cooldown = 3
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