def get_display(graphics_by_default, options=None): graphics = graphics_by_default if options is not None and options.no_graphics: graphics = False if graphics: try: import graphics_display return graphics_display.PacmanGraphics(1, frame_time=.05) except ImportError: pass import text_display return text_display.NullGraphics()
def read_command(argv): """ Processes the command used to run pacman from the command line. """ from optparse import OptionParser usage_str = """ USAGE: python pacman.py <options> EXAMPLES: (1) python pacman.py - starts an interactive game (2) python pacman.py --layout small_classic --zoom 2 OR python pacman.py -l small_classic -z 2 - starts an interactive game on a smaller board, zoomed in """ parser = OptionParser(usage_str) parser.add_option('-n', '--num_games', dest='num_games', 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='medium_classic') parser.add_option( '-p', '--pacman', dest='pacman', help=default('the agent TYPE in the pacman_agents module to use'), metavar='TYPE', default='KeyboardAgent') parser.add_option('-t', '--text_graphics', action='store_true', dest='text_graphics', help='Display output as text only', default=False) parser.add_option('-q', '--quiet_text_graphics', action='store_true', dest='quiet_graphics', help='Generate minimal output and no graphics', default=False) parser.add_option( '-g', '--ghosts', dest='ghost', help=default('the ghost agent TYPE in the ghost_agents module to use'), metavar='TYPE', default='RandomGhost') parser.add_option('-k', '--numghosts', type='int', dest='num_ghosts', 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', '--fix_random_seed', action='store_true', dest='fix_random_seed', help='Fixes the random seed to always play the same game', default=False) parser.add_option( '-r', '--record_actions', 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='game_to_replay', help='A recorded game file (pickle) to replay', default=None) parser.add_option( '-a', '--agent_args', dest='agent_args', help= 'Comma separated values sent to agent. e.g. "opt1=val1,opt2,opt3=val3"' ) parser.add_option( '-x', '--num_training', dest='num_training', type='int', help=default('How many episodes are training (suppresses output)'), default=0) parser.add_option( '--frame_time', dest='frame_time', type='float', help=default('Time to delay between frames; <0 means keyboard'), default=0.1) parser.add_option( '-c', '--catch_exceptions', action='store_true', dest='catch_exceptions', 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.fix_random_seed: random.seed('cs188') # Choose a layout args['layout'] = layout.get_layout(options.layout) if args['layout'] == None: raise Exception("The layout " + options.layout + " cannot be found") # Choose a Pacman agent no_keyboard = options.game_to_replay == None and (options.text_graphics or options.quiet_graphics) pacman_type = load_agent(options.pacman, no_keyboard) agent_opts = parse_agent_args(options.agent_args) if options.num_training > 0: args['num_training'] = options.num_training if 'num_training' not in agent_opts: agent_opts['num_training'] = options.num_training pacman = pacman_type(**agent_opts) # Instantiate Pacman with agent_args args['pacman'] = pacman # Don't display training games if 'num_train' in agent_opts: options.num_quiet = int(agent_opts['num_train']) options.num_ignore = int(agent_opts['num_train']) # Choose a ghost agent ghost_type = load_agent(options.ghost, no_keyboard) args['ghosts'] = [ghost_type(i + 1) for i in range(options.num_ghosts)] # Choose a display format if options.quiet_graphics: import text_display args['display'] = text_display.NullGraphics() elif options.text_graphics: import text_display text_display.SLEEP_TIME = options.frame_time args['display'] = text_display.PacmanGraphics() else: import graphics_display args['display'] = graphics_display.PacmanGraphics( options.zoom, frame_time=options.frame_time) args['num_games'] = options.num_games args['record'] = options.record args['catch_exceptions'] = options.catch_exceptions args['timeout'] = options.timeout # Special case: recorded games don't use the run_games method or args structure if options.game_to_replay != None: print('Replaying recorded game %s.' % options.game_to_replay) import pickle f = open(options.game_to_replay) try: recorded = pickle.load(f) finally: f.close() recorded['display'] = args['display'] replay_game(**recorded) sys.exit(0) return args