@@ -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
0 commit comments