Skip to content
This repository has been archived by the owner on Feb 12, 2018. It is now read-only.

Commit

Permalink
beggar added
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy Tolmachev committed Dec 10, 2012
1 parent 0db084d commit 6bd7a40
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
27 changes: 25 additions & 2 deletions tolmachev_co/actors/beggar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
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
33 changes: 30 additions & 3 deletions tolmachev_co/actors/visitors/actor_moving_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 21 additions & 2 deletions tolmachev_co/map/map.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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()
Expand All @@ -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




Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from actors.bottle import Bottle
from way_finder import WayFinder

class BeggarWayFinderWithBottle(WayFinder):
Expand Down

0 comments on commit 6bd7a40

Please sign in to comment.