Skip to content

Commit f8f37bf

Browse files
committed
Completed Text-based Interface, and added test cases to the vaccuum runner
1 parent 32368fb commit f8f37bf

3 files changed

Lines changed: 192 additions & 6 deletions

File tree

agents.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,20 @@ def is_thing_at(self, location, tclass=Thing):
250250
return True
251251
return False
252252

253-
def old_list_things_at(self, location, tclass=Thing):
253+
# def old_list_things_at(self, location, tclass=Thing):
254+
# "Return all things exactly at a given location."
255+
# return [thing for thing in self.things
256+
# if thing.location == location
257+
# and isinstance(thing, tclass)]
258+
259+
def get_things(self, tclass=Thing):
254260
"Return all things exactly at a given location."
255-
return [thing for thing in self.things
256-
if thing.location == location
257-
and isinstance(thing, tclass)]
261+
rlist = []
262+
for thing in self.things:
263+
if not isinstance(thing, tclass):
264+
continue
265+
rlist.append(thing)
266+
return rlist
258267

259268
def list_things_at(self, location, tclass=Thing):
260269
"Return all things exactly at a given location."

envgui.py

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,101 @@ def cell_topleft_xy(self, row, column):
249249
of the cell(row, column)'s top left corner."""
250250

251251
w = self.cellwidth
252-
return w * row, w * column
252+
return w * row, w * column
253+
254+
# a Text User Interface for the Agent program
255+
class EnvTUI(object):
256+
def __init__(self, env, title='AIMA GUI', cellsize=200, n=10):
257+
# Initialize window
258+
259+
# Create components
260+
w = env.width
261+
h = env.height
262+
self.env = env
263+
self.grid = [['.' for x in range(w)] for y in range(h)]
264+
self.fnMap = { Empty: '.'}
265+
266+
def mapImageNames(self, fnMap):
267+
self.fnMap.update(fnMap)
268+
269+
def displayString(self):
270+
display = ''
271+
first = True
272+
for y in range(len(self.grid)):
273+
if not first:
274+
display += '\n'
275+
first = False
276+
for x in range(len(self.grid[y])):
277+
tList = self.env.list_things_at((x, y))
278+
tname = self.fnMap[Empty]
279+
for thing in tList:
280+
tclass = thing.__class__
281+
tname = self.fnMap[tclass]
282+
display += tname
283+
return display
284+
285+
def help(self):
286+
for line in [
287+
'Commands are:',
288+
' h: print this help message',
289+
' s n: advance n steps',
290+
' t: list things',
291+
' a: list agents',
292+
' an empty string advances n steps',
293+
]:
294+
print(line)
295+
296+
def list_things(self, MyClass=object):
297+
print(MyClass.__name__ + 's in the environment:')
298+
for obj in self.env.things:
299+
if isinstance(obj, MyClass):
300+
print("%s at %s" % (obj, obj.location))
301+
302+
def list_agents(self):
303+
print("Agents in the environment:")
304+
for agt in self.env.agents:
305+
print("%s at %s" % (agt, agt.location))
306+
307+
def step(self, n=1):
308+
for s in range(n):
309+
self.env.step()
310+
print(str(n) + ' step(s) later:')
311+
print(self.displayString())
312+
313+
def mainloop(self):
314+
print(self.displayString())
315+
reply = ''
316+
n = 1
317+
while reply != 'q':
318+
reply = input('Command (h for help): ').strip()
319+
if reply == '':
320+
self.step(n)
321+
continue
322+
323+
if reply == 'h':
324+
self.help()
325+
continue
326+
327+
if reply == 'q':
328+
continue
329+
330+
if reply == 'a':
331+
self.list_agents()
332+
continue
333+
334+
if reply == 't':
335+
self.list_things()
336+
continue
337+
338+
if reply[0] == 's':
339+
command = reply.split()
340+
try:
341+
arg = command[1]
342+
except:
343+
arg = str(n)
344+
try:
345+
n = int(arg)
346+
self.step(n)
347+
except:
348+
print('"' + arg + '" is not an integer')
349+
continue

submissions/aardvark/vacuum2Runner.py

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,87 @@ def execute_action(self, agent, action):
4747
if action != 'NoOp':
4848
agent.performance -= 1
4949

50-
# Launch GUI of more complex environment
50+
# Launch a Text-Based Environment
51+
print('Two Cells, Agent on Left:')
52+
v = VacuumEnvironment(4, 3)
53+
v.add_thing(Dirt(), (1, 1))
54+
v.add_thing(Dirt(), (2, 1))
55+
a = v2.HW2Agent()
56+
a = ag.TraceAgent(a)
57+
v.add_thing(a, (1, 1))
58+
t = gui.EnvTUI(v)
59+
t.mapImageNames({
60+
ag.Wall: '#',
61+
# Floor: 'images/floor.png',
62+
Dirt: '@',
63+
ag.Agent: 'V',
64+
})
65+
t.step(0)
66+
t.list_things(Dirt)
67+
t.step(4)
68+
if len(t.env.get_things(Dirt)) > 0:
69+
t.list_things(Dirt)
70+
else:
71+
print('All clean!')
72+
73+
# Check to continue
74+
if input('Do you want to continue [y/N]? ') != 'y':
75+
exit(0)
76+
77+
# Repeat, but put Agent on the Right
78+
print('Two Cells, Agent on Right:')
79+
v = VacuumEnvironment(4, 3)
80+
v.add_thing(Dirt(), (1, 1))
81+
v.add_thing(Dirt(), (2, 1))
82+
a = v2.HW2Agent()
83+
a = ag.TraceAgent(a)
84+
v.add_thing(a, (2, 1))
85+
t = gui.EnvTUI(v)
86+
t.mapImageNames({
87+
ag.Wall: '#',
88+
# Floor: 'images/floor.png',
89+
Dirt: '@',
90+
ag.Agent: 'V',
91+
})
92+
t.step(0)
93+
t.list_things(Dirt)
94+
t.step(4)
95+
if len(t.env.get_things(Dirt)) > 0:
96+
t.list_things(Dirt)
97+
else:
98+
print('All clean!')
99+
100+
# Check to continue
101+
if input('Do you want to continue [y/N]? ') != 'y':
102+
exit(0)
103+
104+
# Repeat, but put Agent on the Right
105+
print('Two Cells, Agent on Top:')
106+
v = VacuumEnvironment(3, 4)
107+
v.add_thing(Dirt(), (1, 1))
108+
v.add_thing(Dirt(), (1, 2))
109+
a = v2.HW2Agent()
110+
a = ag.TraceAgent(a)
111+
v.add_thing(a, (1, 1))
112+
t = gui.EnvTUI(v)
113+
t.mapImageNames({
114+
ag.Wall: '#',
115+
# Floor: 'images/floor.png',
116+
Dirt: '@',
117+
ag.Agent: 'V',
118+
})
119+
t.step(0)
120+
t.list_things(Dirt)
121+
t.step(4)
122+
if len(t.env.get_things(Dirt)) > 0:
123+
t.list_things(Dirt)
124+
else:
125+
print('All clean!')
126+
127+
# Check to continue
128+
if input('Do you want to continue [y/N]? ') != 'y':
129+
exit(0)
130+
51131
v = VacuumEnvironment(6, 4)
52132
a = v2.HW2Agent()
53133
a = ag.TraceAgent(a)

0 commit comments

Comments
 (0)