def __init__(self, enable_render=False, layout_name="mediumClassic", view_distance=(2, 2)): self.layouts = dict() self.layout_name = layout_name self.pacman = KeyboardAgent() self.ghosts = [ RandomGhost(i + 1) if i % 2 == 0 else DirectionalGhost(i + 1) for i in range(20) ] frameTime = 0.03 textDisplay.SLEEP_TIME = frameTime self.display_text = textDisplay.PacmanGraphics() self.display_graphics = graphicsDisplay.PacmanGraphics( 1.0, frameTime=frameTime) self.beQuiet = True self.game = None self.view_distance = view_distance self.textGraphics = False self.reset(enable_render=enable_render, layout_name=layout_name)
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='testClassic') parser.add_option( '-p', '--agent', dest='agent', 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', '--numAgent', type='int', dest='numAgent', help=default('The maximum number of Agents to use'), default=1) 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( '--timeout', dest='timeout', type='int', help=default( 'Maximum length of time an agent can spend computing in a single game' ), default=3000) parser.add_option( '-m', dest='manual', type='int', help=default( 'The index number of the manual agent [or -1 for all AI]'), default=0) parser.add_option('-c', '--team', dest='team', metavar='TYPE', help=default('The team competeness mode'), default=None) parser.add_option('--life', dest='life', type='int', help=default('The life number of an agent'), default=5) 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) agentType = loadAgent(options.agent, noKeyboard) agentOpts = parseAgentArgs(options.agentArgs) if options.numTraining > 0: args['numTraining'] = options.numTraining if 'numTraining' not in agentOpts: agentOpts['numTraining'] = options.numTraining if options.numAgent > 1 and options.manual >= 0: from keyboardAgents import KeyboardAgent args['agents'] = [agentType(i) for i in range(0, options.manual)] + [ agentType(i) for i in range(options.manual + 1, options.numAgent) ] args['agents'].insert(options.manual, KeyboardAgent(options.manual)) elif options.numAgent > 1: args['agents'] = [agentType(i) for i in range(options.numAgent) ] # Instantiate Pacman with agentArgs else: args['agents'] = [agentType(0)] if not options.team is None: if options.numAgent % 2: raise Exception("For team mode, agents number must be even!") args['teamMode'] = True TeamagentType = loadAgent(options.team, noKeyboard) args['team'] = [ TeamagentType(0, args['agents'][0:options.numAgent:2], range(0, options.numAgent, 2)), TeamagentType(1, args['agents'][1:options.numAgent:2], range(1, options.numAgent, 2)) ] # Don't display training games if 'numTrain' in agentOpts: options.numQuiet = int(agentOpts['numTrain']) options.numIgnore = int(agentOpts['numTrain']) # 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['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) if options.life > 0: args['life'] = options.life else: args['life'] = 5 return args