3737from utils4e import distance_squared , turn_heading
3838from statistics import mean
3939from ipythonblocks import BlockGrid
40- from IPython .display import HTML , display
40+ from IPython .display import HTML , display , clear_output
4141from time import sleep
4242
4343import random
@@ -89,7 +89,7 @@ def __init__(self, program=None):
8989 self .bump = False
9090 self .holding = []
9191 self .performance = 0
92- if program is None or not isinstance (program , collections .Callable ):
92+ if program is None or not isinstance (program , collections .abc . Callable ):
9393 print ("Can't find a valid program for {}, falling back to default." .format (self .__class__ .__name__ ))
9494
9595 def program (percept ):
@@ -455,15 +455,17 @@ def move_forward(self, from_location):
455455 >>> l1
456456 (1, 0)
457457 """
458+ # get the iterable class to return
459+ iclass = from_location .__class__
458460 x , y = from_location
459461 if self .direction == self .R :
460- return x + 1 , y
462+ return iclass (( x + 1 , y ))
461463 elif self .direction == self .L :
462- return x - 1 , y
464+ return iclass (( x - 1 , y ))
463465 elif self .direction == self .U :
464- return x , y - 1
466+ return iclass (( x , y - 1 ))
465467 elif self .direction == self .D :
466- return x , y + 1
468+ return iclass (( x , y + 1 ))
467469
468470
469471class XYEnvironment (Environment ):
@@ -518,7 +520,11 @@ def execute_action(self, agent, action):
518520 agent .holding .pop ()
519521
520522 def default_location (self , thing ):
521- return random .choice (self .width ), random .choice (self .height )
523+ location = self .random_location_inbounds ()
524+ while self .some_things_at (location , Obstacle ):
525+ # we will find a random location with no obstacles
526+ location = self .random_location_inbounds ()
527+ return location
522528
523529 def move_to (self , thing , destination ):
524530 """Move a thing to a new location. Returns True on success or False if there is an Obstacle.
@@ -534,10 +540,12 @@ def move_to(self, thing, destination):
534540 t .location = destination
535541 return thing .bump
536542
537- def add_thing (self , thing , location = ( 1 , 1 ) , exclude_duplicate_class_items = False ):
543+ def add_thing (self , thing , location = None , exclude_duplicate_class_items = False ):
538544 """Add things to the world. If (exclude_duplicate_class_items) then the item won't be
539545 added if the location has at least one item of the same class."""
540- if self .is_inbounds (location ):
546+ if location is None :
547+ super ().add_thing (thing )
548+ elif self .is_inbounds (location ):
541549 if (exclude_duplicate_class_items and
542550 any (isinstance (t , thing .__class__ ) for t in self .list_things_at (location ))):
543551 return
@@ -666,16 +674,16 @@ def run(self, steps=1000, delay=1):
666674
667675 def update (self , delay = 1 ):
668676 sleep (delay )
669- if self .visible :
670- self .conceal ()
671- self .reveal ()
672- else :
673- self .reveal ()
677+ self .reveal ()
674678
675679 def reveal (self ):
676680 """Display the BlockGrid for this world - the last thing to be added
677681 at a location defines the location color."""
678682 self .draw_world ()
683+ # wait for the world to update and
684+ # apply changes to the same grid instead
685+ # of making a new one.
686+ clear_output (1 )
679687 self .grid .show ()
680688 self .visible = True
681689
0 commit comments