From 6bd7a40ef594066f5f3ad9d459fa92e2c7f63e44 Mon Sep 17 00:00:00 2001 From: Dmitriy Tolmachev Date: Mon, 10 Dec 2012 19:58:31 +0400 Subject: [PATCH] beggar added --- tolmachev_co/actors/beggar.py | 27 +++++++++++++-- .../actors/visitors/actor_moving_visitor.py | 33 +++++++++++++++++-- tolmachev_co/map/map.py | 23 +++++++++++-- .../beggar_way_finder_with_bottle.py | 1 - 4 files changed, 76 insertions(+), 8 deletions(-) diff --git a/tolmachev_co/actors/beggar.py b/tolmachev_co/actors/beggar.py index 35267a4..ebabc48 100644 --- a/tolmachev_co/actors/beggar.py +++ b/tolmachev_co/actors/beggar.py @@ -6,8 +6,12 @@ class BeggarState: AT_THE_TAVERN = 3 class Beggar (Actor): - def __init__(self): + TAVERN_TIMEOUT = 40 + + def __init__(self, tavern_coordinate): self.__current_state = BeggarState.SEARCHING_A_BOTTLE + self.__tavern_coordinate = tavern_coordinate + self.__counter = 0 def accept_visitor(self, visitor): visitor.visit_beggar(self) @@ -19,4 +23,23 @@ def is_walking_with_a_bottle(self): return self.__current_state == BeggarState.WALKING_WITH_A_BOTTLE def is_in_tavern(self): - return self.__current_state == BeggarState.AT_THE_TAVERN \ No newline at end of file + return self.__current_state == BeggarState.AT_THE_TAVERN + + def spend_time_in_tavern(self): + self.__counter += 1 + + def is_ready_to_search_a_bottle(self): + return self.__counter == Beggar.TAVERN_TIMEOUT + + def start_searching_a_bottle(self): + self.__counter = 0 + self.__current_state = BeggarState.SEARCHING_A_BOTTLE + + def start_walking_with_bottle(self): + self.__current_state = BeggarState.WALKING_WITH_A_BOTTLE + + def start_to_be_in_tavern(self): + self.__current_state = BeggarState.AT_THE_TAVERN + + def get_tavern_coordinate(self): + return self.__tavern_coordinate \ No newline at end of file diff --git a/tolmachev_co/actors/visitors/actor_moving_visitor.py b/tolmachev_co/actors/visitors/actor_moving_visitor.py index e8ea04e..0b7b5fb 100644 --- a/tolmachev_co/actors/visitors/actor_moving_visitor.py +++ b/tolmachev_co/actors/visitors/actor_moving_visitor.py @@ -4,9 +4,12 @@ from actors.visitors.actor_visitor import ActorVisitor import random from map.coordinate import Coordinate +from map.way_finder.beggar_way_finder import BeggarWayFinder +from map.way_finder.beggar_way_finder_with_bottle import BeggarWayFinderWithBottle 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: UP = 0 @@ -54,7 +57,32 @@ def should_drop_a_bottle(): def visit_beggar(self, beggar): - pass + if beggar.is_in_tavern() : + beggar.spend_time_in_tavern() + if beggar.is_ready_to_search_a_bottle() : + beggar.start_searching_a_bottle() + + if beggar.is_searching_a_bottle() : + coordinate = self.__map.get_beggar_coord() + dict = self.__map.get_bottles() + wayFinder = BeggarWayFinder(self.__map, coordinate, dict.keys()) + new_coord = wayFinder.get_next_coordinate_to_go() + if new_coord: + if self.__map.has_actor_at(new_coord): + beggar.start_walking_with_bottle() + self.__map.remove(coordinate) + self.__map.put(new_coord, beggar) + elif beggar.is_walking_with_a_bottle() : + coordinate = self.__map.get_beggar_coord() + end_coords = [beggar.get_tavern_coordinate()] + wayFinder = BeggarWayFinderWithBottle(self.__map, coordinate, end_coords) + new_coord = wayFinder.get_next_coordinate_to_go() + if new_coord: + if new_coord in end_coords: + beggar.start_to_be_in_tavern() + self.__map.remove(coordinate) + self.__map.put(new_coord, beggar) + def visit_pillar(self, pillar): @@ -85,8 +113,7 @@ def visit_policeman(self, policeman): policeman.start_walking_with_alcoholic() self.__map.remove(coordinate) self.__map.put(new_coord, policeman) - - if policeman.is_walking_with_alcoholic(): + elif policeman.is_walking_with_alcoholic(): coordinate = self.__map.get_policeman_coord() end_coords = [policeman.get_station_coordinate()] wayFinder = PolicemanWayFinderWithAlcoholic(self.__map, coordinate, end_coords) diff --git a/tolmachev_co/map/map.py b/tolmachev_co/map/map.py index ac1c99b..19bfbbb 100644 --- a/tolmachev_co/map/map.py +++ b/tolmachev_co/map/map.py @@ -1,5 +1,6 @@ from actors.alcoholic import Alcoholic from actors.beggar import Beggar +from actors.bottle import Bottle from actors.lamp import Lamp from actors.pilllar import Pillar from actors.policeman import Policeman @@ -17,7 +18,7 @@ def __init__(self, width=15, height=15): self.__actors_dictionary[Coordinate(7, 7)] = Pillar() self.__actors_dictionary[Coordinate(3, 10)] = Lamp() self.__actors_dictionary[Coordinate(3, 15)] = Policeman(Coordinate(3, 15)) - self.__actors_dictionary[Coordinate(15, 4)] = Beggar() + self.__actors_dictionary[Coordinate(15, 4)] = Beggar(Coordinate(15, 4)) self.__actors_dictionary[Coordinate(-1, 9)] = Tavern() def has_actor_at(self, coordinate): @@ -80,7 +81,18 @@ def get_lightened_sleeping_alcos(self): if isinstance(lightened_actor, Alcoholic): if lightened_actor.is_sleeping() : sleeping_alcoholics[lightened_coordinate] = lightened_actor - return sleeping_alcoholics + return sleeping_alcoholics + + def get_bottles(self): + bottles = {} + for y in xrange(0, self.__height): + for x in xrange(0, self.__width): + coordinate = Coordinate(x, y) + if coordinate in self.__actors_dictionary: + actor = self.__actors_dictionary[coordinate] + if isinstance(actor, Bottle): + bottles[coordinate] = actor + return bottles def get_policeman_coord(self): coordinates = self.__actors_dictionary.keys() @@ -89,6 +101,13 @@ def get_policeman_coord(self): if isinstance(actor, Policeman) : return coordinate + def get_beggar_coord(self): + coordinates = self.__actors_dictionary.keys() + actors = self.__actors_dictionary.values() + for coordinate, actor in zip(coordinates, actors): + if isinstance(actor, Beggar) : + return coordinate + 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 index 5741539..39425b4 100644 --- a/tolmachev_co/map/way_finder/beggar_way_finder_with_bottle.py +++ b/tolmachev_co/map/way_finder/beggar_way_finder_with_bottle.py @@ -1,4 +1,3 @@ -from actors.bottle import Bottle from way_finder import WayFinder class BeggarWayFinderWithBottle(WayFinder):