def readCommand(argv): """ Processes the command used to run pacman from the command line. """ from optparse import OptionParser usageStr = """ USAGE: python pacman.py <options> EXAMPLES: (1) python pacman.py - starts an interactive game (2) python pacman.py --layout smallClassic --zoom 2 OR python pacman.py -l smallClassic -z 2 - starts an interactive game on a smaller board, zoomed in """ parser = OptionParser(usageStr) parser.add_option('-n', '--numGames', dest='numGames', type='int', help=default('the number of GAMES to play'), metavar='GAMES', default=1) parser.add_option( '-l', '--layout', dest='layout', help=default('the LAYOUT_FILE from which to load the map layout'), metavar='LAYOUT_FILE', default='mediumClassic') parser.add_option( '-p', '--pacman', dest='pacman', help=default('the agent TYPE in the pacmanAgents module to use'), metavar='TYPE', default='KeyboardAgent') parser.add_option('-t', '--textGraphics', action='store_true', dest='textGraphics', help='Display output as text only', default=False) parser.add_option('-q', '--quietTextGraphics', action='store_true', dest='quietGraphics', help='Generate minimal output and no graphics', default=False) parser.add_option( '-g', '--ghosts', dest='ghost', help=default('the ghost agent TYPE in the ghostAgents module to use'), metavar='TYPE', default='RandomGhost') parser.add_option('-k', '--numghosts', type='int', dest='numGhosts', help=default('The maximum number of ghosts to use'), default=4) parser.add_option('-z', '--zoom', type='float', dest='zoom', help=default('Zoom the size of the graphics window'), default=1.0) parser.add_option( '-f', '--fixRandomSeed', action='store_true', dest='fixRandomSeed', help='Fixes the random seed to always play the same game', default=False) parser.add_option( '-r', '--recordActions', action='store_true', dest='record', help= 'Writes game histories to a file (named by the time they were played)', default=False) parser.add_option('--replay', dest='gameToReplay', help='A recorded game file (pickle) to replay', default=None) parser.add_option( '-a', '--agentArgs', dest='agentArgs', help= 'Comma separated values sent to agent. e.g. "opt1=val1,opt2,opt3=val3"' ) parser.add_option( '-x', '--numTraining', dest='numTraining', type='int', help=default('How many episodes are training (suppresses output)'), default=0) parser.add_option( '--frameTime', dest='frameTime', type='float', help=default('Time to delay between frames; <0 means keyboard'), default=0.1) parser.add_option( '-c', '--catchExceptions', action='store_true', dest='catchExceptions', help='Turns on exception handling and timeouts during games', default=False) parser.add_option( '--timeout', dest='timeout', type='int', help=default( 'Maximum length of time an agent can spend computing in a single game' ), default=30) options, otherjunk = parser.parse_args(argv) if len(otherjunk) != 0: raise Exception('Command line input not understood: ' + str(otherjunk)) args = dict() # Fix the random seed if options.fixRandomSeed: random.seed('cs188') # Choose a layout args['layout'] = layout.getLayout(options.layout) if args['layout'] == None: raise Exception("The layout " + options.layout + " cannot be found") # Choose a Pacman agent noKeyboard = options.gameToReplay == None and (options.textGraphics or options.quietGraphics) pacmanType = loadAgent(options.pacman, noKeyboard) agentOpts = parseAgentArgs(options.agentArgs) if options.numTraining > 0: args['numTraining'] = options.numTraining if 'numTraining' not in agentOpts: agentOpts['numTraining'] = options.numTraining pacman = pacmanType(**agentOpts) # Instantiate Pacman with agentArgs args['pacman'] = pacman # Don't display training games if 'numTrain' in agentOpts: options.numQuiet = int(agentOpts['numTrain']) options.numIgnore = int(agentOpts['numTrain']) # Choose a ghost agent ghostType = loadAgent(options.ghost, noKeyboard) args['ghosts'] = [ghostType(i + 1) for i in range(options.numGhosts)] # Choose a display format if options.quietGraphics: import textDisplay args['display'] = textDisplay.NullGraphics() elif options.textGraphics: import textDisplay textDisplay.SLEEP_TIME = options.frameTime args['display'] = textDisplay.PacmanGraphics() else: import graphicsDisplay args['display'] = graphicsDisplay.PacmanGraphics( options.zoom, frameTime=options.frameTime) args['numGames'] = options.numGames args['record'] = options.record args['catchExceptions'] = options.catchExceptions args['timeout'] = options.timeout # Special case: recorded games don't use the runGames method or args structure if options.gameToReplay != None: print 'Replaying recorded game %s.' % options.gameToReplay import cPickle f = open(options.gameToReplay) try: recorded = cPickle.load(f) finally: f.close() recorded['display'] = args['display'] replayGame(**recorded) sys.exit(0) return args
def readCommand( argv ): """ Processes the command used to run pacman from the command line. """ from optparse import OptionParser usageStr = """ USAGE: python pacclient.py <options> EXAMPLES: (1) python pacclient.py - starts an interactive game with a random teammate. EXAMPLES: (2) python pacclient.py -2 OffenseAgent - starts an interactive game with a random teammate. """ parser = OptionParser(usageStr) parser.add_option('-a', '--agents', help=default('Your team'), default='BaselineAgents') parser.add_option('--agentOpts', help=default('Arguments passed to the factory'), default='') parser.add_option('-t', '--textgraphics', action='store_true', help='Display output as text only', default=False) parser.add_option('-q', '--quiet', action='store_true', help='Display minimal output', default=False) # parser.add_option('-G', '--pygame', action='store_true', dest='pygame', # help='Display output with Pygame graphics (faster)', default=False) parser.add_option('-z', '--zoom', type='float', help=default('Zoom in the graphics'), default=1) parser.add_option('-s', '--server', help=default('The SERVER to connect to'), default='robots.stanford.edu') parser.add_option('-p', '--port', type='int', help=default('The PORT to connect to'), default=7226) parser.add_option('-U', '--user', help=default('Your username'), default='guest') parser.add_option('-P', '--password', help=default('Your password'), default='guest') parser.add_option('-g', '--gamename', help=default('The name of the game you wish to contact'), default='') parser.add_option('-f', '--fixRandomSeed', action='store_true', help='Fixes the random seed to always play the same game', default=False) options, otherjunk = parser.parse_args() if len(otherjunk) != 0: raise Exception("Illegal args: " + otherjunk) args = dict() agentArgs = parseAgentArgs(options.agentOpts) args['agentFactoryBuilder'] = loadAgentAccessor(options.agents, agentArgs) # Choose a display format #if options.pygame: # import pygameDisplay # args['display'] = pygameDisplay.PacmanGraphics() if options.quiet: import textDisplay args['display'] = textDisplay.NullGraphics() elif options.textgraphics: import textDisplay args['display'] = textDisplay.PacmanGraphics() else: import graphicsDisplay args['display'] = graphicsDisplay.PacmanGraphics(options.zoom, 0, True) if options.fixRandomSeed: random.seed('cs188') args['server'] = options.server args['port'] = options.port args['user'] = options.user args['password'] = options.password args['gamename'] = options.gamename return args