, , , , , , " " ( ).
, agent_locations World.
. , World , . World , , ( ), . , self.location_is_good self.world.is_location_available(x, y) [1]
, . , World , , . , ? -. , - (x, y) . "" .
: ([(agent.x, agent.y) for agent in self.agents] [2]). agent_locations dict , World.
World... , , Agent. update_agent_location(self, agent, x, y) , x == agent.x; y == agent.y ( , ). World, update_agent_state(self, agent), World . ( eveytime).
class World(object):
def update_agent_state(self, agent, state_change=None):
if state_change == Agent.LOCATION_CHANGE:
self.agent_locations[agent.x, agent.y] = agent
elif state_change == Agent.WHATEVER:
pass
class Agent(object):
LOCATION_CHANGE = 1
def update(self):
for x, y in self.valid_locations():
if not self.can_move_to(x, y)
continue
self.move(x, y)
def can_move_to(self, x, y):
"""Determines if x, y is a location where we can move."""
if not self.world.is_location_available(x, y):
return False
if not self.has_money_to_travel_to(x, y):
return False
return True
def move(self, x, y):
"""Moves to x, y and notifies world of state change."""
self.x = x
self.y = y
self.world.update_agent_state(self, Agent.LOCATION_CHANGE)
- ( ).
[1] , , "" , . . (x, y), 1) , 2) 1000 $ , Agent.can_move_to(x, y), .
[2] , self.agents = {} , append dict. ([])?