@@ -50,7 +50,7 @@ class Thing:
50
50
.__name__ slot (used for output only)."""
51
51
52
52
def __repr__ (self ):
53
- return '<{}>' . format ( getattr (self , '__name__' , self .__class__ .__name__ ))
53
+ return f"< { getattr (self , '__name__' , self .__class__ .__name__ )} >"
54
54
55
55
def is_alive (self ):
56
56
"""Things that are 'alive' should return true."""
@@ -338,7 +338,7 @@ def step(self):
338
338
339
339
def run (self , steps = 1000 ):
340
340
"""Run the Environment for given number of time steps."""
341
- for step in range (steps ):
341
+ for _ in range (steps ):
342
342
if self .is_done ():
343
343
return
344
344
self .step ()
@@ -378,8 +378,8 @@ def delete_thing(self, thing):
378
378
except ValueError as e :
379
379
print (e )
380
380
print (" in Environment delete_thing" )
381
- print (" Thing to be removed: {} at {}" . format ( thing , thing .location ) )
382
- print (" from list: {}" . format ( [(thing , thing .location ) for thing in self .things ]) )
381
+ print (f " Thing to be removed: { thing } at { thing .location } " )
382
+ print (f " from list: { [(thing , thing .location ) for thing in self .things ]} " )
383
383
if thing in self .agents :
384
384
self .agents .remove (thing )
385
385
@@ -506,8 +506,11 @@ def execute_action(self, agent, action):
506
506
elif action == 'Forward' :
507
507
agent .bump = self .move_to (agent , agent .direction .move_forward (agent .location ))
508
508
elif action == 'Grab' :
509
- things = [thing for thing in self .list_things_at (agent .location ) if agent .can_grab (thing )]
510
- if things :
509
+ if things := [
510
+ thing
511
+ for thing in self .list_things_at (agent .location )
512
+ if agent .can_grab (thing )
513
+ ]:
511
514
agent .holding .append (things [0 ])
512
515
print ("Grabbing " , things [0 ].__class__ .__name__ )
513
516
self .delete_thing (things [0 ])
@@ -552,7 +555,12 @@ def add_thing(self, thing, location=None, exclude_duplicate_class_items=False):
552
555
def is_inbounds (self , location ):
553
556
"""Checks to make sure that the location is inbounds (within walls if we have walls)"""
554
557
x , y = location
555
- return not (x < self .x_start or x > self .x_end or y < self .y_start or y > self .y_end )
558
+ return (
559
+ x >= self .x_start
560
+ and x <= self .x_end
561
+ and y >= self .y_start
562
+ and y <= self .y_end
563
+ )
556
564
557
565
def random_location_inbounds (self , exclude = None ):
558
566
"""Returns a random location that is inbounds (within walls if we have walls)"""
@@ -634,9 +642,7 @@ def get_world(self):
634
642
x_start , y_start = (0 , 0 )
635
643
x_end , y_end = self .width , self .height
636
644
for x in range (x_start , x_end ):
637
- row = []
638
- for y in range (y_start , y_end ):
639
- row .append (self .list_things_at ((x , y )))
645
+ row = [self .list_things_at ((x , y )) for y in range (y_start , y_end )]
640
646
result .append (row )
641
647
return result
642
648
@@ -660,7 +666,7 @@ def run(self, steps=1000, delay=1):
660
666
def run (self , steps = 1000 , delay = 1 ):
661
667
"""Run the Environment for given number of time steps,
662
668
but update the GUI too."""
663
- for step in range (steps ):
669
+ for _ in range (steps ):
664
670
self .update (delay )
665
671
if self .is_done ():
666
672
break
@@ -908,9 +914,7 @@ def get_world(self, show_walls=True):
908
914
x_end , y_end = self .width - 1 , self .height - 1
909
915
910
916
for x in range (x_start , x_end ):
911
- row = []
912
- for y in range (y_start , y_end ):
913
- row .append (self .list_things_at ((x , y )))
917
+ row = [self .list_things_at ((x , y )) for y in range (y_start , y_end )]
914
918
result .append (row )
915
919
return result
916
920
@@ -938,8 +942,7 @@ def percept(self, agent):
938
942
"""Return things in adjacent (not diagonal) cells of the agent.
939
943
Result format: [Left, Right, Up, Down, Center / Current location]"""
940
944
x , y = agent .location
941
- result = []
942
- result .append (self .percepts_from (agent , (x - 1 , y )))
945
+ result = [self .percepts_from (agent , (x - 1 , y ))]
943
946
result .append (self .percepts_from (agent , (x + 1 , y )))
944
947
result .append (self .percepts_from (agent , (x , y - 1 )))
945
948
result .append (self .percepts_from (agent , (x , y + 1 )))
@@ -999,10 +1002,11 @@ def is_done(self):
999
1002
if explorer [0 ].alive :
1000
1003
return False
1001
1004
else :
1002
- print ("Death by {} [-1000]." . format ( explorer [ 0 ]. killed_by ) )
1005
+ print (f "Death by { explorer [ 0 ]. killed_by } [-1000]." )
1003
1006
else :
1004
- print ("Explorer climbed out {}."
1005
- .format ("with Gold [+1000]!" if Gold () not in self .things else "without Gold [+0]" ))
1007
+ print (
1008
+ f'Explorer climbed out { "with Gold [+1000]!" if Gold () not in self .things else "without Gold [+0]" } .'
1009
+ )
1006
1010
return True
1007
1011
1008
1012
# TODO: Arrow needs to be implemented
@@ -1024,7 +1028,7 @@ def compare_agents(EnvFactory, AgentFactories, n=10, steps=1000):
1024
1028
>>> performance_ReflexVacuumAgent <= performance_ModelBasedVacuumAgent
1025
1029
True
1026
1030
"""
1027
- envs = [EnvFactory () for i in range (n )]
1031
+ envs = [EnvFactory () for _ in range (n )]
1028
1032
return [(A , test_agent (A , steps , copy .deepcopy (envs )))
1029
1033
for A in AgentFactories ]
1030
1034
0 commit comments