Skip to content

Commit fbdb36d

Browse files
tirthasheshpatelantmarakis
authored andcommitted
Add example for TableDrivenVacuumAgent and FIX: grid not updating in GraphicEnvironment (#1133)
* Add example for TableDrivenVacuumAgent * Add example of TableDrivenVacuumAgent in agents4e.py * FIX: grid not updating in GraphicEnvironment * FIX: grid not updating in GraphicEnvironment in agents4e.py * FIX: list_things_at to support all iterables
1 parent 6fd1428 commit fbdb36d

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

agents.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import random
4444
import copy
4545
import collections
46+
import numbers
4647

4748

4849
# ______________________________________________________________________________
@@ -211,7 +212,14 @@ def RandomVacuumAgent():
211212

212213

213214
def TableDrivenVacuumAgent():
214-
"""[Figure 2.3]"""
215+
"""Tabular approach towards vacuum world as mentioned in [Figure 2.3]
216+
>>> agent = TableDrivenVacuumAgent()
217+
>>> environment = TrivialVacuumEnvironment()
218+
>>> environment.add_thing(agent)
219+
>>> environment.run()
220+
>>> environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
221+
True
222+
"""
215223
table = {((loc_A, 'Clean'),): 'Right',
216224
((loc_A, 'Dirty'),): 'Suck',
217225
((loc_B, 'Clean'),): 'Left',
@@ -342,7 +350,12 @@ def run(self, steps=1000):
342350

343351
def list_things_at(self, location, tclass=Thing):
344352
"""Return all things exactly at a given location."""
345-
return [thing for thing in self.things if thing.location == location and isinstance(thing, tclass)]
353+
if isinstance(location, numbers.Number):
354+
return [thing for thing in self.things
355+
if thing.location == location and isinstance(thing, tclass)]
356+
return [thing for thing in self.things
357+
if all(x==y for x,y in zip(thing.location, location))
358+
and isinstance(thing, tclass)]
346359

347360
def some_things_at(self, location, tclass=Thing):
348361
"""Return true if at least one of the things at location
@@ -621,7 +634,7 @@ def get_world(self):
621634
for x in range(x_start, x_end):
622635
row = []
623636
for y in range(y_start, y_end):
624-
row.append(self.list_things_at([x, y]))
637+
row.append(self.list_things_at((x, y)))
625638
result.append(row)
626639
return result
627640

agents4e.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import random
4444
import copy
4545
import collections
46+
import numbers
4647

4748

4849
# ______________________________________________________________________________
@@ -211,7 +212,14 @@ def RandomVacuumAgent():
211212

212213

213214
def TableDrivenVacuumAgent():
214-
"""[Figure 2.3]"""
215+
"""Tabular approach towards vacuum world as mentioned in [Figure 2.3]
216+
>>> agent = TableDrivenVacuumAgent()
217+
>>> environment = TrivialVacuumEnvironment()
218+
>>> environment.add_thing(agent)
219+
>>> environment.run()
220+
>>> environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
221+
True
222+
"""
215223
table = {((loc_A, 'Clean'),): 'Right',
216224
((loc_A, 'Dirty'),): 'Suck',
217225
((loc_B, 'Clean'),): 'Left',
@@ -342,7 +350,12 @@ def run(self, steps=1000):
342350

343351
def list_things_at(self, location, tclass=Thing):
344352
"""Return all things exactly at a given location."""
345-
return [thing for thing in self.things if thing.location == location and isinstance(thing, tclass)]
353+
if isinstance(location, numbers.Number):
354+
return [thing for thing in self.things
355+
if thing.location == location and isinstance(thing, tclass)]
356+
return [thing for thing in self.things
357+
if all(x==y for x,y in zip(thing.location, location))
358+
and isinstance(thing, tclass)]
346359

347360
def some_things_at(self, location, tclass=Thing):
348361
"""Return true if at least one of the things at location
@@ -621,7 +634,7 @@ def get_world(self):
621634
for x in range(x_start, x_end):
622635
row = []
623636
for y in range(y_start, y_end):
624-
row.append(self.list_things_at([x, y]))
637+
row.append(self.list_things_at((x, y)))
625638
result.append(row)
626639
return result
627640

0 commit comments

Comments
 (0)