diff --git a/tolmachev_co/actors/policeman.py b/tolmachev_co/actors/policeman.py index 7072acf..0133996 100644 --- a/tolmachev_co/actors/policeman.py +++ b/tolmachev_co/actors/policeman.py @@ -5,13 +5,18 @@ class PolicemanState: WALKING_TO_ALCOHOLIC = 2 WALKING_WITH_ALCOHOLIC = 3 -class Policeman (Actor): - def __init__(self): + +class Policeman(Actor): + def __init__(self, station_coordinate): self.__current_state = PolicemanState.AT_THE_STATION + self.__station_coordinate = station_coordinate def accept_visitor(self, visitor): visitor.visit_policeman(self) + def get_station_coordinate(self): + return self.__station_coordinate + def is_at_the_station(self): return self.__current_state == PolicemanState.AT_THE_STATION diff --git a/tolmachev_co/actors/visitors/actor_moving_visitor.py b/tolmachev_co/actors/visitors/actor_moving_visitor.py index a14ea3b..e8ea04e 100644 --- a/tolmachev_co/actors/visitors/actor_moving_visitor.py +++ b/tolmachev_co/actors/visitors/actor_moving_visitor.py @@ -4,7 +4,8 @@ from actors.visitors.actor_visitor import ActorVisitor import random from map.coordinate import Coordinate -from map.way_finder import WayFinder +from map.way_finder.policeman_way_finder import PolicemanWayFinder +from map.way_finder.policeman_way_finder_with_alcoholic import PolicemanWayFinderWithAlcoholic class ActorMovingVisitor(ActorVisitor): class MovingDirection: @@ -71,31 +72,30 @@ def visit_bottle(self, bottle): def visit_policeman(self, policeman): if policeman.is_at_the_station(): dict = self.__map.get_lightened_sleeping_alcos() - if dict : + if dict: policeman.start_walking_to_alcoholic() - elif policeman.is_walking_to_alcoholic() : + + if policeman.is_walking_to_alcoholic(): coordinate = self.__map.get_policeman_coord() dict = self.__map.get_lightened_sleeping_alcos() - wayFinder = WayFinder(self.__map, coordinate, dict.keys()) - path = wayFinder.find_path() - if path : - new_coord = path[0] + wayFinder = PolicemanWayFinder(self.__map, coordinate, dict.keys()) + new_coord = wayFinder.get_next_coordinate_to_go() + if new_coord: if self.__map.has_actor_at(new_coord): policeman.start_walking_with_alcoholic() self.__map.remove(coordinate) self.__map.put(new_coord, policeman) - elif policeman.is_walking_with_alcoholic(): + + if policeman.is_walking_with_alcoholic(): coordinate = self.__map.get_policeman_coord() - end_coords = [Coordinate(3, 14)] - wayFinder = WayFinder(self.__map, coordinate, end_coords) - path = wayFinder.find_path() - if path : - new_coord = path[0] - else : - new_coord = Coordinate(3, 15) - policeman.start_to_be_at_station() - self.__map.remove(coordinate) - self.__map.put(new_coord, policeman) + end_coords = [policeman.get_station_coordinate()] + wayFinder = PolicemanWayFinderWithAlcoholic(self.__map, coordinate, end_coords) + new_coord = wayFinder.get_next_coordinate_to_go() + if new_coord: + if new_coord in end_coords: + policeman.start_to_be_at_station() + self.__map.remove(coordinate) + self.__map.put(new_coord, policeman) def visit_tavern(self, tavern): @@ -108,5 +108,3 @@ def visit_tavern(self, tavern): - - diff --git a/tolmachev_co/main.py b/tolmachev_co/main.py index 709f107..a94f47b 100644 --- a/tolmachev_co/main.py +++ b/tolmachev_co/main.py @@ -1,4 +1,3 @@ -import os from map.map import Map def main(): diff --git a/tolmachev_co/map/map.py b/tolmachev_co/map/map.py index e22b72d..ac1c99b 100644 --- a/tolmachev_co/map/map.py +++ b/tolmachev_co/map/map.py @@ -16,7 +16,7 @@ def __init__(self, width=15, height=15): self.__height = height self.__actors_dictionary[Coordinate(7, 7)] = Pillar() self.__actors_dictionary[Coordinate(3, 10)] = Lamp() - self.__actors_dictionary[Coordinate(3, 15)] = Policeman() + self.__actors_dictionary[Coordinate(3, 15)] = Policeman(Coordinate(3, 15)) self.__actors_dictionary[Coordinate(15, 4)] = Beggar() self.__actors_dictionary[Coordinate(-1, 9)] = Tavern() @@ -89,22 +89,6 @@ def get_policeman_coord(self): if isinstance(actor, Policeman) : return coordinate - def is_empty_field(self, coordinate): - if coordinate in self.__actors_dictionary: - return False - else : - return True - - def is_empty_field_except_sleeping_alcoholic(self, coordinate): - if coordinate in self.__actors_dictionary: - actor = self.__actors_dictionary[coordinate] - if(isinstance(actor, Alcoholic)) : - if actor.is_sleeping() : - return True - return False - else : - return True - diff --git a/tolmachev_co/map/way_finder/__init__.py b/tolmachev_co/map/way_finder/__init__.py new file mode 100644 index 0000000..67e33b8 --- /dev/null +++ b/tolmachev_co/map/way_finder/__init__.py @@ -0,0 +1 @@ +__author__ = 'sunlight' diff --git a/tolmachev_co/map/way_finder/beggar_way_finder.py b/tolmachev_co/map/way_finder/beggar_way_finder.py new file mode 100644 index 0000000..69c0f0f --- /dev/null +++ b/tolmachev_co/map/way_finder/beggar_way_finder.py @@ -0,0 +1,16 @@ +from actors.bottle import Bottle +from way_finder import WayFinder + +class BeggarWayFinder(WayFinder): + def __init__(self, map, start, end): + WayFinder.__init__(self, map, start, end) + + + def valid_field_to_step(self, coordinate) : + if self._map.has_actor_at(coordinate): + actor = self._map.get(coordinate) + if isinstance(actor, Bottle) : + return True + return False + else : + return True \ No newline at end of file diff --git a/tolmachev_co/map/way_finder/beggar_way_finder_with_bottle.py b/tolmachev_co/map/way_finder/beggar_way_finder_with_bottle.py new file mode 100644 index 0000000..5741539 --- /dev/null +++ b/tolmachev_co/map/way_finder/beggar_way_finder_with_bottle.py @@ -0,0 +1,13 @@ +from actors.bottle import Bottle +from way_finder import WayFinder + +class BeggarWayFinderWithBottle(WayFinder): + def __init__(self, map, start, end): + WayFinder.__init__(self, map, start, end) + + + def valid_field_to_step(self, coordinate) : + if self._map.has_actor_at(coordinate): + return False + else : + return True \ No newline at end of file diff --git a/tolmachev_co/map/way_finder/policeman_way_finder.py b/tolmachev_co/map/way_finder/policeman_way_finder.py new file mode 100644 index 0000000..b37022a --- /dev/null +++ b/tolmachev_co/map/way_finder/policeman_way_finder.py @@ -0,0 +1,18 @@ +from actors.alcoholic import Alcoholic +from way_finder import WayFinder + +class PolicemanWayFinder(WayFinder): + def __init__(self, map, start, end): + WayFinder.__init__(self, map, start, end) + + + def valid_field_to_step(self, coordinate) : + if self._map.has_actor_at(coordinate): + actor = self._map.get(coordinate) + if isinstance(actor, Alcoholic) : + if actor.is_sleeping() : + return True + return False + else : + return True + diff --git a/tolmachev_co/map/way_finder/policeman_way_finder_with_alcoholic.py b/tolmachev_co/map/way_finder/policeman_way_finder_with_alcoholic.py new file mode 100644 index 0000000..c7f3554 --- /dev/null +++ b/tolmachev_co/map/way_finder/policeman_way_finder_with_alcoholic.py @@ -0,0 +1,12 @@ +from way_finder import WayFinder + +class PolicemanWayFinderWithAlcoholic(WayFinder): + def __init__(self, map, start, end): + WayFinder.__init__(self, map, start, end) + + + def valid_field_to_step(self, coordinate) : + if self._map.has_actor_at(coordinate): + return False + else : + return True \ No newline at end of file diff --git a/tolmachev_co/map/way_finder.py b/tolmachev_co/map/way_finder/way_finder.py similarity index 77% rename from tolmachev_co/map/way_finder.py rename to tolmachev_co/map/way_finder/way_finder.py index f4b7de4..2924a09 100644 --- a/tolmachev_co/map/way_finder.py +++ b/tolmachev_co/map/way_finder/way_finder.py @@ -1,19 +1,20 @@ -from coordinate import Coordinate +from map.coordinate import Coordinate class WayFinder: def __init__(self, map, start, end): - self.__map = map + self._map = map self.__start = start self.__end = end - def get_path(self, end_vertex): - path = [] - vertex = end_vertex - while vertex.get_prev_vertex().get_coordinate() != vertex.get_coordinate(): - path.append(vertex.get_coordinate()) - vertex = vertex.get_prev_vertex() - path.reverse() - return path + def valid_field_to_step(self, coordinate): + raise NotImplementedError("Abstract method is called") + + def get_next_coordinate_to_go(self): + path = self.find_path() + if not path: + return None + else: + return path[0] def find_path(self): v = Vertex(self.__start, None, 0) @@ -27,11 +28,10 @@ def find_path(self): next_front = [] while True: - for front_coord in front: if front_coord in self.__end: end_vertex = map[front_coord] - return self.get_path(end_vertex) + return self.__get_path(end_vertex) neighbours = self.__get_neighbours(front_coord) for neighbour in neighbours: @@ -45,10 +45,13 @@ def find_path(self): front = next_front next_front = [] - if not front : + if not front: return [] return [] + def __get_map(self): + return self._map + def __get_neighbours(self, coord): left = Coordinate(coord.get_x() - 1, coord.get_y()) right = Coordinate(coord.get_x() + 1, coord.get_y()) @@ -58,10 +61,20 @@ def __get_neighbours(self, coord): neighbours = [] for coord in candidates: - if self.__map.is_in_bounds(coord) and self.__map.is_empty_field_except_sleeping_alcoholic(coord): + if (self._map.is_in_bounds(coord) or coord == self.__start or coord in self.__end)\ + and self.valid_field_to_step(coord): neighbours.append(coord) return neighbours + def __get_path(self, end_vertex): + path = [] + vertex = end_vertex + while vertex.get_prev_vertex().get_coordinate() != vertex.get_coordinate(): + path.append(vertex.get_coordinate()) + vertex = vertex.get_prev_vertex() + path.reverse() + return path + class Vertex: def __init__(self, coordinate, prev_vertex, layer):