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

Commit

Permalink
policeman visitor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy Tolmachev committed Dec 10, 2012
1 parent 220e4d6 commit fe51bbe
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 54 deletions.
9 changes: 7 additions & 2 deletions tolmachev_co/actors/policeman.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
38 changes: 18 additions & 20 deletions tolmachev_co/actors/visitors/actor_moving_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand All @@ -108,5 +108,3 @@ def visit_tavern(self, tavern):





1 change: 0 additions & 1 deletion tolmachev_co/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from map.map import Map

def main():
Expand Down
18 changes: 1 addition & 17 deletions tolmachev_co/map/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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




1 change: 1 addition & 0 deletions tolmachev_co/map/way_finder/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__author__ = 'sunlight'
16 changes: 16 additions & 0 deletions tolmachev_co/map/way_finder/beggar_way_finder.py
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions tolmachev_co/map/way_finder/beggar_way_finder_with_bottle.py
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions tolmachev_co/map/way_finder/policeman_way_finder.py
Original file line number Diff line number Diff line change
@@ -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

12 changes: 12 additions & 0 deletions tolmachev_co/map/way_finder/policeman_way_finder_with_alcoholic.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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:
Expand All @@ -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())
Expand All @@ -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):
Expand Down

0 comments on commit fe51bbe

Please sign in to comment.