Skip to content

Commit f502be9

Browse files
author
Omar
authored
fixed grabbing behaviour in agent (#1148)
* fixed grabbing behaviour in agent * fixed the grabbing issues and itegrated into wumpus environment * cleaned the code a bit * fixing the code space formatting * fixing format
1 parent 677308e commit f502be9

File tree

1 file changed

+13
-32
lines changed

1 file changed

+13
-32
lines changed

agents.py

+13-32
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@
2727
"""
2828

2929
# TODO
30-
# Implement grabbing correctly.
31-
# When an object is grabbed, does it still have a location?
32-
# What if it is released?
33-
# What if the grabbed or the grabber is deleted?
34-
# What if the grabber moves?
3530
# Speed control in GUI does not have any effect -- fix it.
3631

3732
from utils import distance_squared, turn_heading
@@ -510,14 +505,17 @@ def execute_action(self, agent, action):
510505
agent.direction += Direction.L
511506
elif action == 'Forward':
512507
agent.bump = self.move_to(agent, agent.direction.move_forward(agent.location))
513-
# elif action == 'Grab':
514-
# things = [thing for thing in self.list_things_at(agent.location)
515-
# if agent.can_grab(thing)]
516-
# if things:
517-
# agent.holding.append(things[0])
508+
elif action == 'Grab':
509+
things = [thing for thing in self.list_things_at(agent.location) if agent.can_grab(thing)]
510+
if things:
511+
agent.holding.append(things[0])
512+
print("Grabbing ", things[0].__class__.__name__)
513+
self.delete_thing(things[0])
518514
elif action == 'Release':
519515
if agent.holding:
520-
agent.holding.pop()
516+
dropped = agent.holding.pop()
517+
print("Dropping ", dropped.__class__.__name__)
518+
self.add_thing(dropped, location=agent.location)
521519

522520
def default_location(self, thing):
523521
location = self.random_location_inbounds()
@@ -569,10 +567,7 @@ def random_location_inbounds(self, exclude=None):
569567
def delete_thing(self, thing):
570568
"""Deletes thing, and everything it is holding (if thing is an agent)"""
571569
if isinstance(thing, Agent):
572-
for obj in thing.holding:
573-
super().delete_thing(obj)
574-
for obs in self.observers:
575-
obs.thing_deleted(obj)
570+
del thing.holding
576571

577572
super().delete_thing(thing)
578573
for obs in self.observers:
@@ -964,24 +959,10 @@ def execute_action(self, agent, action):
964959

965960
if isinstance(agent, Explorer) and self.in_danger(agent):
966961
return
967-
962+
968963
agent.bump = False
969-
if action == 'TurnRight':
970-
agent.direction += Direction.R
971-
agent.performance -= 1
972-
elif action == 'TurnLeft':
973-
agent.direction += Direction.L
974-
agent.performance -= 1
975-
elif action == 'Forward':
976-
agent.bump = self.move_to(agent, agent.direction.move_forward(agent.location))
977-
agent.performance -= 1
978-
elif action == 'Grab':
979-
things = [thing for thing in self.list_things_at(agent.location)
980-
if agent.can_grab(thing)]
981-
if len(things):
982-
print("Grabbing", things[0].__class__.__name__)
983-
if len(things):
984-
agent.holding.append(things[0])
964+
if action in ['TurnRight', 'TurnLeft', 'Forward', 'Grab']:
965+
super().execute_action(agent, action)
985966
agent.performance -= 1
986967
elif action == 'Climb':
987968
if agent.location == (1, 1): # Agent can only climb out of (1,1)

0 commit comments

Comments
 (0)