37
37
from utils4e import distance_squared , turn_heading
38
38
from statistics import mean
39
39
from ipythonblocks import BlockGrid
40
- from IPython .display import HTML , display
40
+ from IPython .display import HTML , display , clear_output
41
41
from time import sleep
42
42
43
43
import random
@@ -89,7 +89,7 @@ def __init__(self, program=None):
89
89
self .bump = False
90
90
self .holding = []
91
91
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 ):
93
93
print ("Can't find a valid program for {}, falling back to default." .format (self .__class__ .__name__ ))
94
94
95
95
def program (percept ):
@@ -455,15 +455,17 @@ def move_forward(self, from_location):
455
455
>>> l1
456
456
(1, 0)
457
457
"""
458
+ # get the iterable class to return
459
+ iclass = from_location .__class__
458
460
x , y = from_location
459
461
if self .direction == self .R :
460
- return x + 1 , y
462
+ return iclass (( x + 1 , y ))
461
463
elif self .direction == self .L :
462
- return x - 1 , y
464
+ return iclass (( x - 1 , y ))
463
465
elif self .direction == self .U :
464
- return x , y - 1
466
+ return iclass (( x , y - 1 ))
465
467
elif self .direction == self .D :
466
- return x , y + 1
468
+ return iclass (( x , y + 1 ))
467
469
468
470
469
471
class XYEnvironment (Environment ):
@@ -518,7 +520,11 @@ def execute_action(self, agent, action):
518
520
agent .holding .pop ()
519
521
520
522
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
522
528
523
529
def move_to (self , thing , destination ):
524
530
"""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):
534
540
t .location = destination
535
541
return thing .bump
536
542
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 ):
538
544
"""Add things to the world. If (exclude_duplicate_class_items) then the item won't be
539
545
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 ):
541
549
if (exclude_duplicate_class_items and
542
550
any (isinstance (t , thing .__class__ ) for t in self .list_things_at (location ))):
543
551
return
@@ -666,16 +674,16 @@ def run(self, steps=1000, delay=1):
666
674
667
675
def update (self , delay = 1 ):
668
676
sleep (delay )
669
- if self .visible :
670
- self .conceal ()
671
- self .reveal ()
672
- else :
673
- self .reveal ()
677
+ self .reveal ()
674
678
675
679
def reveal (self ):
676
680
"""Display the BlockGrid for this world - the last thing to be added
677
681
at a location defines the location color."""
678
682
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 )
679
687
self .grid .show ()
680
688
self .visible = True
681
689
0 commit comments