def readCommand( argv ): """ Processes the command used to run pacman from the command line. """ from optparse import OptionParser usageStr = """ USAGE: python busters.py <options> EXAMPLE: python busters.py --layout bigHunt - starts an interactive game on a big board """ 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='oneHunt') parser.add_option('-p', '--pacman', dest='pacman', help=default('the agent TYPE in the pacmanAgents module to use'), metavar='TYPE', default='BustersKeyboardAgent') parser.add_option('-a','--agentArgs',dest='agentArgs', help='Comma seperated values sent to agent. e.g. "opt1=val1,opt2,opt3=val3"') 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('-q', '--quietTextGraphics', action='store_true', dest='quietGraphics', help='Generate minimal output and no graphics', default=False) 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('-s', '--showGhosts', action='store_true', dest='showGhosts', help='Renders the ghosts in the display (cheating)', default=False) parser.add_option('-t', '--frameTime', dest='frameTime', type='float', help=default('Time to delay between frames; <0 means keyboard'), default=0.1) options, otherjunk = parser.parse_args() if len(otherjunk) != 0: raise Exception('Command line input not understood: ' + otherjunk) args = dict() # Fix the random seed if options.fixRandomSeed: random.seed('bustersPacman') # Choose a layout args['layout'] = layout.getLayout( options.layout ) if args['layout'] == None: raise Exception("The layout " + options.layout + " cannot be found") # Choose a ghost agent ghostType = loadAgent(options.ghost, options.quietGraphics) args['ghosts'] = [ghostType( i+1 ) for i in range( options.numGhosts )] # Choose a Pacman agent noKeyboard = options.quietGraphics pacmanType = loadAgent(options.pacman, noKeyboard) agentOpts = parseAgentArgs(options.agentArgs) agentOpts['ghostAgents'] = args['ghosts'] pacman = pacmanType(**agentOpts) # Instantiate Pacman with agentArgs args['pacman'] = pacman import graphicsDisplay args['display'] = graphicsDisplay.FirstPersonPacmanGraphics(options.zoom, \ options.showGhosts, \ frameTime = options.frameTime) args['numGames'] = options.numGames return args
def readCommand(argv): """ Processes the command used to run pacman from the command line. """ from optparse import OptionParser usageStr = """ USAGE: python pacman.py <options> """ parser = OptionParser(usageStr) parser.add_option('-T', '--teamName', dest='teamName', help="Enter your team's name", default=None) parser.add_option( '-p', '--agentName', dest='agentName', help="Enter your agents's name (for testing purposes only)", default=None) parser.add_option('-n', '--numGames', dest='numGames', type='int', help=default('the number of GAMES to play'), default=1) parser.add_option('-d', '--dataCollectionMode', action='store_true', dest='dataCollectionMode', help='Enter data-collection mode', default=False) parser.add_option( '-a', '--agentArgs', dest='agentArgs', help= 'Comma separated values sent to agent. e.g. "opt1=val1,opt2,opt3=val3"' ) parser.add_option('-q', '--quietTextGraphics', action='store_true', dest='quietGraphics', help='Generate minimal output and no graphics', default=False) parser.add_option('-m', '--maxMoves', dest='maxMoves', type='int', help=default('the maximum number of moves in a game'), 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( '-t', '--frameTime', dest='frameTime', type='float', help=default('Time to delay between frames; Must be > 0'), default=0.1) parser.add_option('-s', '--seed', dest='seed', type='int', help=default('random seed'), default=3) options, otherjunk = parser.parse_args() if len(otherjunk) != 0: raise Exception('Command line input not understood: ' + otherjunk) if options.frameTime <= 0: raise Exception("the frameTime option must be greater than 0") # set the seed before we do anything else util.set_seeds(options.seed) # add team directory to python path #sys.path.append(os.path.join(os.path.dirname(os.path.abspath("pacman.py")), options.teamDirectory)) sys.path.append(os.path.dirname(os.path.abspath("pacman.py"))) args = dict() # generate a layout import layout args['layout'] = layout.RandomLayout() # make sure we have a team name, not a directory if options.teamName is not None and options.teamName.endswith("/"): options.teamName = options.teamName[:-1] # Choose a Pacman agent pacmanType = loadAgent(options.teamName, options.agentName) agentOpts = parseAgentArgs(options.agentArgs) pacman = pacmanType(**agentOpts) # Instantiate Pacman with agentArgs args['pacman'] = pacman import graphicsDisplay if options.quietGraphics: args['display'] = graphicsDisplay.QuietGraphics() else: args['display'] = graphicsDisplay.FirstPersonPacmanGraphics(options.zoom, \ True, \ frameTime = options.frameTime) args['numGames'] = options.numGames args['maxMoves'] = options.maxMoves args['dataCollectionMode'] = options.dataCollectionMode return args
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 capture.py - starts an interactive game against a random opponent (2) python capture.py --numPlayers 4 --player2 KeyboardAgent2 - starts a two-player interactive game with w,a,s,d & i,j,k,l keys (2) python capture.py --numPlayers 4 --player1 RandomAgent - starts a chaotic random game """ parser = OptionParser(usageStr) parser.add_option('-1', '--player1', dest='p1', help=default('the agent TYPE to use for player 1'), metavar='TYPE', default='KeyboardAgent') parser.add_option( '-2', '--player2', dest='p2', help=default( 'the agent TYPE to use for player 2 (KeyboardAgent2 for i,j,k,l keys)' ), metavar='TYPE', default='OffensiveReflexAgent') parser.add_option('-3', '--player3', dest='p3', help=default('the agent TYPE to use for player 3'), metavar='TYPE', default='OffensiveReflexAgent') parser.add_option('-4', '--player4', dest='p4', help=default('the agent TYPE to use for player 4'), metavar='TYPE', default='OffensiveReflexAgent') parser.add_option('-5', '--player5', dest='p5', help=default('the agent TYPE to use for player 5'), metavar='TYPE', default='OffensiveReflexAgent') parser.add_option('-6', '--player6', dest='p6', help=default('the agent TYPE to use for player 6'), metavar='TYPE', default='OffensiveReflexAgent') parser.add_option( '-l', '--layout', dest='layout', help=default('the LAYOUT_FILE from which to load the map layout'), metavar='LAYOUT_FILE', default='mediumCapture') parser.add_option('-n', '--nographics', action='store_true', dest='nographics', help='Display output as text only', default=False) #parser.add_option('-G', '--pygame', action='store_true', dest='pygame', # help='Display output with Pygame', default=False) parser.add_option('-k', '--numPlayers', type='int', dest='numPlayers', help=default('The maximum number of players'), default=4) parser.add_option('-z', '--zoom', type='float', dest='zoom', help=default('Zoom in the graphics'), default=1) parser.add_option('-t', '--time', type='int', dest='time', help=default('TIME limit of a game in moves'), default=3000, metavar='TIME') parser.add_option('-f', '--fast', action='store_true', dest='fast', help=default('Remove animation to speed up the game'), default=False) parser.add_option('--replay', dest='gameToReplay', help=default('A Pickled game to replay'), default=None) options, otherjunk = parser.parse_args() assert len(otherjunk) == 0 args = dict() # Choose a pacman agent p1 = loadAgent(options.p1, options.nographics)(0) p2 = loadAgent(options.p2, options.nographics)(1) p3 = loadAgent(options.p3, options.nographics)(2) p4 = loadAgent(options.p4, options.nographics)(3) p5 = loadAgent(options.p5, options.nographics)(4) p6 = loadAgent(options.p6, options.nographics)(5) args['agents'] = [p1, p2, p3, p4, p5, p6] # Choose a display format #if options.pygame: # import pygameDisplay # args['display'] = pygameDisplay.PacmanGraphics() if not options.nographics: import graphicsDisplay graphicsDisplay.FRAME_TIME = 0 if not options.fast: graphicsDisplay.PAUSE_TIME = .1 args['display'] = graphicsDisplay.FirstPersonPacmanGraphics( options.zoom, capture=True) else: import textDisplay args['display'] = textDisplay.PacmanGraphics() # Choose a layout if options.layout.lower().find('capture') == -1: raise Exception('You must use a capture layout with capture.py') import layout args['layout'] = layout.getLayout(options.layout) if args['layout'] == None: raise Exception("The layout " + options.layout + " cannot be found") args['agents'] = args['agents'][:min(args['layout'].getNumGhosts( ), options.numPlayers)] args['time'] = options.time # 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 recorded = cPickle.load(open(options.gameToReplay)) print recorded replayGame(recorded, args['display']) 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 pacman.py <options> EXAMPLE: python sonar.py --layout bigHunt - starts an interactive game on a big board """ 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='smallHunt') parser.add_option('-p', '--pacman', dest='pacman', help=default('the agent TYPE in the pacmanAgents module to use'), metavar='TYPE', default='SonarKeyboardAgent') 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('-s', '--showGhosts', action='store_true', dest='showGhosts', help='Renders the ghosts in the display (cheating)', 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) options, otherjunk = parser.parse_args() if len(otherjunk) != 0: raise Exception('Command line input not understood: ' + otherjunk) args = dict() # Fix the random seed if options.fixRandomSeed: random.seed('sonarPacman') # Choose a layout import layout args['layout'] = layout.getLayout( options.layout ) if args['layout'] == None: raise "The layout " + options.layout + " cannot be found" # Choose a pacman agent pacmanType = loadAgent(options.pacman) pacman = pacmanType() # Figure out how to instantiate pacman args['pacman'] = pacman # Choose a ghost agent args['numGhosts'] = options.numGhosts # Choose a display format import graphicsDisplay args['display'] = graphicsDisplay.FirstPersonPacmanGraphics(options.zoom, options.showGhosts) args['numGames'] = options.numGames args['record'] = options.record return args