Более подробно: доска создается как объект, и для навигации пчел по доске используется компас, при создании каждой пчеле дается свой компас. пчелы, а затем эти пчелы добавляются в объект улья, который добавляется в объект game_map. Результат скрытого теста идентичен моему для первых 8 итераций из 10, однако на последних 2 итерациях пчела должна покинуть улей и переместиться еще раз, однако моя пчела выходит из улья по другой координате и вместо двигаясь снова, он снова возвращается.
Код: Выделить всё
from compass import Compass
from flower import *
class Bee:
def __init__(self, row: int, col: int, speed: int, perception: int):
self.row = row
self.col = col
self.speed = speed
self.perception = perception
self.compass = Compass(row, col, speed)
self.pollen_carried = None
self.has_pollen = False
self.on_hive = False
#moves bee generally if sees flower or not
def move(self, game_map, hive_location):
if self.pollen_carried is None and not self.has_pollen:
#looking for flowers within perception
flower = self.nearest_flower(game_map)
if flower:
#move to the flower if its in range and has pollen
if self.row == flower.row and self.col == flower.col:
self.get_pollen(flower)
self.target_location = self.return_to_hive(hive_location, game_map)
self.has_pollen = True
else:
self.move_to(flower.row, flower.col)
else:
#if no flowers with pollen in range of perception use compass
self.compass_move(game_map, hive_location)
else:
#if carrying pollen go to hive
self.return_to_hive(hive_location, game_map)
def compass_move(self, game_map, hive):
trajectory = self.compass.get_next_trajectory()
direction = trajectory.get_direction_in_degrees()
distance = trajectory.get_distance()
new_row = self.row
new_col = self.col
if direction == 0:
new_col += distance
if direction == 45:
new_col += distance
new_row += distance
if direction == 90:
new_row += distance
if direction == 135:
new_col -= distance
new_row += distance
if direction == 180:
new_col -= distance
if direction == 225:
new_col -= distance
new_row -= distance
if direction == 270:
new_row -= distance
if direction == 315:
new_col += distance
new_row -= distance
# check for in range sizes
new_row = max(0, min(new_row, game_map.size - 1))
new_col = max(0, min(new_col, game_map.size - 1))
#check if the next position is going to be the hive
if (new_row, new_col) == hive.get_hive_location():
on_hive = True
self.row = new_row
self.col = new_col
else:
self.row = new_row
self.col = new_col
def return_to_hive(self, hive, game_map):
hive_row, hive_col = hive.get_hive_location()
if self.row == hive_row and self.col == hive_col:
self.deposit_pollen(game_map, hive, is_on_hive = True) #store pollen when at hive
self.has_pollen = False
# self.compass_move(game_map, hive)
else:
self.move_to(hive_row, hive_col)
return hive.get_hive_location
def move_to(self, target_row, target_col):
#move to either hive or flower
if target_row > self.row:
self.row += 1
elif target_row < self.row:
self.row -= 1
if target_col > self.col:
self.col += 1
elif target_col < self.col:
self.col -= 1
def get_pollen(self, flower):
#check if bee is alr carryin pollen
if self.pollen_carried is None and flower.pollen_list:
# print(f' the bee at ({self.row}, {self.col}) is collecting pollen from flower at ({flower.row}, {flower.col})')
#collect 1 pollen granule
self.pollen_carried = flower.pollen_list.pop(0)
# else:
# print(f'Bee at ({self.row}, {self.col}) could not collect pollen')
def nearest_flower(self, game_map):
nearest_flower = None
smallest_distance = 1000000
for obj in game_map.map_objects:
if isinstance(obj, Flower) and len(obj.pollen_list) > 0: #only flowers that contain pollen
cheby_distance = max(abs(self.row - obj.row), abs(self.col - obj.col))
#check if it is in range with perception
if cheby_distance
Подробнее здесь: [url]https://stackoverflow.com/questions/79028319/i-keep-getting-the-wrong-output-for-movement-in-the-hidden-test-case[/url]