
/Qc           @   s  d  Z  d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l Z d d l	 Z	 d d l
 Z
 d d l Z d d l Z d d l Z d d l Z d	 d d
     YZ d Z d Z d Z d d d     YZ d d d     YZ d d  d     YZ d   Z d   Z d   Z d   Z d   Z d e d d  Z e d k re e
 j d  Z e e   n  d S(!   s*  
Pacman.py holds the logic for the classic pacman game along with the main
code to run a game.  This file is divided into three sections:

  (i)  Your interface to the pacman world:
          Pacman is a complex environment.  You probably don't want to
          read through all of the code we wrote to make the game runs
          correctly.  This section contains the parts of the code
          that you will need to understand in order to complete the
          project.  There is also some code in game.py that you should
          understand.

  (ii)  The hidden secrets of pacman:
          This section contains all of the logic code that the pacman
          environment uses to decide who can move where, who dies when
          things collide, etc.  You shouldn't need to read this section
          of code, but you can if you want.

  (iii) Framework to start a game:
          The final section contains the code for reading the command
          you use to set up the game, then starting up a new game, along with
          linking in all the external parts (agent functions, graphics).
          Check this section out to see all the options available to you.

To play your first game, type 'python pacman.py' from the command line.
The keys are 'a', 's', 'd', and 'w' to move (or arrow keys).  Have fun!
i(   t   GameStateData(   t   Game(   t
   Directions(   t   Actions(   t   nearestPoint(   t   manhattanDistanceNt	   GameStatec           B   s  e  Z d  Z e   Z d   Z e e  Z d d  Z d   Z d   Z	 d   Z
 d   Z d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d d  Z d   Z d   Z d   Z d   Z  d d  Z! RS(   s)  
    A GameState specifies the full game state, including the food, capsules,
    agent configurations and score changes.

    GameStates are used by the Game object to capture the actual state of the game and
    can be used by agents to reason about the game.

    Much of the information in a GameState is stored in a GameStateData object.  We
    strongly suggest that you access that data via the accessor methods below rather
    than referring to the GameStateData object directly.

    Note that in classic Pacman, Pacman is always agent 0.
    c          C   s   t  j j   }  t   t  _ |  S(   N(   R   t   exploredt   copyt   set(   t   tmp(    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   getAndResetExploredH   s    i    c         C   sY   t  j j |   |  j   s( |  j   r, g  S| d k rE t j |   St j |  |  Sd S(   sD   
        Returns the legal actions for the agent specified.
        i    N(   R   R   t   addt   isWint   isLoset   PacmanRulest   getLegalActionst
   GhostRules(   t   selft
   agentIndex(    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyR   N   s     c         C   s  |  j    s |  j   r' t d   n  t |   } | d k r} g  t | j    D] } t ^ qR | j _ t	 j
 | |  n t j
 | | |  | d k r | j j t 7_ n t j | j j |  t j | |  | | j _ | j j | j j 7_ | S(   sY   
        Returns the successor state after the specified agent takes the action.
        s/   Can't generate a successor of a terminal state.i    (   R   R   t	   ExceptionR   t   ranget   getNumAgentst   Falset   datat   _eatenR   t   applyActionR   t   scoreChanget   TIME_PENALTYt   decrementTimert   agentStatest
   checkDeatht   _agentMovedt   score(   R   R   t   actiont   statet   i(    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   generateSuccessorZ   s     +c         C   s   |  j  d  S(   Ni    (   R   (   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   getLegalPacmanActionsy   s    c         C   s   |  j  d |  S(   sO   
        Generates the successor state after the specified pacman move
        i    (   R%   (   R   R"   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   generatePacmanSuccessor|   s    c         C   s   |  j  j d j   S(   s   
        Returns an AgentState object for pacman (in game.py)

        state.pos gives the current position
        state.direction gives the travel vector
        i    (   R   R   R   (   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   getPacmanState   s    c         C   s   |  j  j d j   S(   Ni    (   R   R   t   getPosition(   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   getPacmanPosition   s    c         C   s   |  j  j d S(   Ni   (   R   R   (   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   getGhostStates   s    c         C   s;   | d k s | |  j    k r- t d   n  |  j j | S(   Ni    s%   Invalid index passed to getGhostState(   R   R   R   R   (   R   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   getGhostState   s    c         C   s/   | d k r t  d   n  |  j j | j   S(   Ni    s)   Pacman's index passed to getGhostPosition(   R   R   R   R)   (   R   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   getGhostPosition   s    c         C   s#   g  |  j    D] } | j   ^ q S(   N(   R+   R)   (   R   t   s(    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   getGhostPositions   s    c         C   s   t  |  j j  S(   N(   t   lenR   R   (   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyR      s    c         C   s
   |  j  j S(   N(   R   R!   (   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   getScore   s    c         C   s
   |  j  j S(   sN   
        Returns a list of positions (x,y) of the remaining capsules.
        (   R   t   capsules(   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   getCapsules   s    c         C   s   |  j  j j   S(   N(   R   t   foodt   count(   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt
   getNumFood   s    c         C   s
   |  j  j S(   s  
        Returns a Grid of boolean food indicator variables.

        Grids can be accessed via list notation, so to check
        if there is food at (x,y), just call

        currentFood = state.getFood()
        if currentFood[x][y] == True: ...
        (   R   R4   (   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   getFood   s    
c         C   s   |  j  j j S(   s   
        Returns a Grid of boolean wall indicator variables.

        Grids can be accessed via list notation, so to check
        if there is a wall at (x,y), just call

        walls = state.getWalls()
        if walls[x][y] == True: ...
        (   R   t   layoutt   walls(   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   getWalls   s    
c         C   s   |  j  j | | S(   N(   R   R4   (   R   t   xt   y(    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   hasFood   s    c         C   s   |  j  j j | | S(   N(   R   R8   R9   (   R   R;   R<   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   hasWall   s    c         C   s
   |  j  j S(   N(   R   t   _lose(   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyR      s    c         C   s
   |  j  j S(   N(   R   t   _win(   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyR      s    c         C   s1   | d k r! t | j  |  _ n t   |  _ d S(   sT   
        Generates a new state by copying information from its predecessor.
        N(   t   NoneR    R   (   R   t	   prevState(    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   __init__   s    c         C   s"   t  |   } |  j j   | _ | S(   N(   R   R   t   deepCopy(   R   R#   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyRD      s    c         C   s   |  j  | j  k S(   s3   
        Allows two states to be compared.
        (   R   (   R   t   other(    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   __eq__   s    c         C   s   t  |  j  S(   s;   
        Allows states to be keys of dictionaries.
        (   t   hashR   (   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   __hash__   s    c         C   s   t  |  j  S(   N(   t   strR   (   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   __str__   s    i  c         C   s   |  j  j | |  d S(   sT   
        Creates an initial game state from a layout array (see layout.py).
        N(   R   t
   initialize(   R   R8   t   numGhostAgents(    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyRK      s    N("   t   __name__t
   __module__t   __doc__R	   R   R   t   staticmethodR   R%   R&   R'   R(   R*   R+   R,   R-   R/   R   R1   R3   R6   R7   R:   R=   R>   R   R   RA   RC   RD   RF   RH   RJ   RK   (    (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyR   3   s<   																											i(   gffffff?i   t   ClassicGameRulesc           B   s   e  Z d  Z d d  Z e e d  Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z d   Z d   Z RS(   sq   
    These game rules manage the control flow of a game, deciding when
    and how the game starts and ends.
    i   c         C   s   | |  _  d  S(   N(   t   timeout(   R   RR   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyRC   	  s    c   
      C   ss   | g | | j     } t   } | j | t |   t | | |  d | }	 | |	 _ | j   |  _ | |  _ |	 S(   Nt   catchExceptions(	   t   getNumGhostsR   RK   R0   R   R#   RD   t   initialStatet   quiet(
   R   R8   t   pacmanAgentt   ghostAgentst   displayRV   RS   t   agentst	   initStatet   game(    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   newGame  s    			c         C   sB   | j    r |  j | |  n  | j   r> |  j | |  n  d S(   sC   
        Checks to see whether it is time to end the game.
        N(   R   t   winR   t   lose(   R   R#   R\   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   process  s      c         C   s(   |  j  s d | j j GHn  t | _ d  S(   Ns$   Pacman emerges victorious! Score: %d(   RV   R   R!   t   Truet   gameOver(   R   R#   R\   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyR^     s    	 c         C   s(   |  j  s d | j j GHn  t | _ d  S(   Ns   Pacman died! Score: %d(   RV   R   R!   Ra   Rb   (   R   R#   R\   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyR_   !  s    	 c         C   s    t  | j j    |  j j   S(   N(   t   floatR#   R6   RU   (   R   R\   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   getProgress%  s    c         C   s   | d k r d GHn d GHd  S(   Ni    s   Pacman crasheds   A ghost crashed(    (   R   R\   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt
   agentCrash(  s    c         C   s   |  j  S(   N(   RR   (   R   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   getMaxTotalTime.  s    c         C   s   |  j  S(   N(   RR   (   R   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   getMaxStartupTime1  s    c         C   s   |  j  S(   N(   RR   (   R   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   getMoveWarningTime4  s    c         C   s   |  j  S(   N(   RR   (   R   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   getMoveTimeout7  s    c         C   s   d S(   Ni    (    (   R   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   getMaxTimeWarnings:  s    (   RM   RN   RO   RC   R   R]   R`   R^   R_   Rd   Re   Rf   Rg   Rh   Ri   Rj   (    (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyRQ     s   
									R   c           B   sS   e  Z d  Z d Z d   Z e e  Z d   Z e e  Z d   Z e e  Z RS(   sl   
    These functions govern how pacman interacts with his environment under
    the classic game rules.
    i   c         C   s"   t  j |  j   j |  j j j  S(   s5   
        Returns a list of possible actions.
        (   R   t   getPossibleActionsR(   t   configurationR   R8   R9   (   R#   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyR   D  s    c         C   s   t  j |   } | | k r4 t d t |    n  |  j j d } t j | t  j  } | j	 j
 |  | _	 | j	 j   } t |  } t | |  d k r t  j | |   n  d S(   sG   
        Edits the state to reflect the results of the action.
        s   Illegal action i    g      ?N(   R   R   R   RI   R   R   R   t   directionToVectort   PACMAN_SPEEDRl   R%   R)   R   R   t   consume(   R#   R"   t   legalt   pacmanStatet   vectort   nextt   nearest(    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyR   K  s    c         C   s$  |  \ } } | j  j | | r | j  j d 7_ | j  j j   | j  _ t | j  j | | <|  | j  _ | j   } | d k r | j  j r | j  j d 7_ t | j  _	 q n  |  | j
   k r | j  j j |   |  | j  _ x9 t d t | j  j   D] } t | j  j | _ q Wn  d  S(   Ni
   i    i  i   (   R   R4   R   R   R   t
   _foodEatenR6   R?   Ra   R@   R3   R2   t   removet   _capsuleEatenR   R0   R   t   SCARED_TIMEt   scaredTimer(   t   positionR#   R;   R<   t   numFoodt   index(    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyRo   a  s    "(   RM   RN   RO   Rn   R   RP   R   Ro   (    (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyR   =  s   			R   c           B   s   e  Z d  Z d Z d   Z e e  Z d   Z e e  Z d   Z e e  Z d   Z e e  Z d   Z	 e e	  Z	 d   Z
 e e
  Z
 d   Z e e  Z RS(	   sM   
    These functions dictate how ghosts interact with their environment.
    g      ?c         C   s   |  j  |  j } t j | |  j j j  } t j | j  } t	 j
 | k ra | j t	 j
  n  | | k r t |  d k r | j |  n  | S(   s   
        Ghosts cannot stop, and cannot turn around unless they
        reach a dead end, but can turn 90 degrees at intersections.
        i   (   R,   Rl   R   Rk   R   R8   R9   t   reverseDirectiont	   directionR   t   STOPRv   R0   (   R#   t
   ghostIndext   conft   possibleActionst   reverse(    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyR   |  s    c         C   s   t  j |  |  } | | k r7 t d t |    n  |  j j | } t  j } | j d k rl | d :} n  t j	 | |  } | j
 j |  | _
 d  S(   Ns   Illegal ghost action i    g       @(   R   R   R   RI   R   R   t   GHOST_SPEEDRy   R   Rm   Rl   R%   (   R#   R"   R   Rp   t
   ghostStatet   speedRr   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyR     s    	 c         C   sJ   |  j  } | d k r0 t |  j j  |  j _ n  t d | d  |  _  d  S(   Ni   i    (   Ry   R   Rl   t   post   max(   R   t   timer(    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyR     s    	c         C   s   |  j    } | d k r x t d t |  j j   D]M } |  j j | } | j j   } t j | |  r4 t j	 |  | |  q4 q4 WnG |  j j | } | j j   } t j | |  r t j	 |  | |  n  d  S(   Ni    i   (
   R*   R   R0   R   R   Rl   R)   R   t   canKillt   collide(   R#   R   t   pacmanPositionR|   R   t   ghostPosition(    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyR     s    "c         C   s~   | j  d k rM |  j j d 7_ t j |  |  d | _  t |  j j | <n- |  j j sz |  j j d 8_ t |  j _ n  d  S(   Ni    i   i  (	   Ry   R   R   R   t
   placeGhostRa   R   R@   R?   (   R#   R   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyR     s    	c         C   s   t  | |   t k S(   N(   R   t   COLLISION_TOLERANCE(   R   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyR     s    c         C   s   | j  | _ d  S(   N(   t   startRl   (   R#   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyR     s    (   RM   RN   RO   R   R   RP   R   R   R   R   R   R   (    (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyR   w  s    							c         C   s   |  d S(   Ns    [Default: %default](    (   RI   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   default  s    c         C   su   |  d  k r i  S|  j d  } i  } xI | D]A } d | k rV | j d  \ } } n | d } } | | | <q, W| S(   Nt   ,t   =i   (   RA   t   split(   RI   t   piecest   optst   pt   keyt   val(    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   parseAgentArgs  s     c         C   s  d d l  m } d } | |  } | j d d d d d d	 d
 t d  d d d d | j d d d d d
 t d  d d d d | j d d d d d
 t d  d d d d | j d d d d d d  d
 d! d t | j d" d# d d d d$ d
 d% d t | j d& d' d d( d
 t d)  d d d d* | j d+ d, d d	 d d- d
 t d.  d d/ | j d0 d1 d d2 d d3 d
 t d4  d d5 | j d6 d7 d d d d8 d
 d9 d t | j d: d; d d d d< d
 d= d t | j d> d d? d
 d@ d d\ | j dA dB d dC d
 dD | j dE dF d dG d d	 d
 t dH  d dI | j dJ d dK d d2 d
 t dL  d dM | j dN dO d d d dP d
 dQ d t | j dR d dS d d	 d
 t dT  d dU | j |   \ } } t |  dI k rt dV t	 |    n  t
   } | j r%t j dW  n  t j | j  | d <| d d\ k ret dX | j dY   n  | j d\ k o| j p| j } t | j |  } t | j  }	 | j dI k r| j | dG <dG |	 k r| j |	 dG <qn  | |	   }
 |
 | d <dZ |	 k r-t |	 dZ  | _ t |	 dZ  | _ n  t | j |  } g  t | j  D] } | | d  ^ qO| d[ <| j rd d\ l } | j   | d] <n_ | j rd d\ l } | j  | _! | j"   | d] <n+ d d\ l# } | j" | j$ dK | j  | d] <| j% | d <| j& | d< <| j' | dP <| j( | dS <| j d\ k rd^ | j GHd d\ l) } t* | j  } z | j+ |  } Wd\ | j,   X| d] | d] <t- |   t. j/ dI  n  | S(_   sI   
    Processes the command used to run pacman from the command line.
    i(   t   OptionParsersS  
    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
    s   -ns
   --numGamest   destt   numGamest   typet   intt   helps   the number of GAMES to playt   metavart   GAMESR   i   s   -ls   --layoutR8   s1   the LAYOUT_FILE from which to load the map layoutt   LAYOUT_FILEt   mediumClassics   -ps   --pacmant   pacmans0   the agent TYPE in the pacmanAgents module to uset   TYPEt   KeyboardAgents   -ts   --textGraphicsR"   t
   store_truet   textGraphicss   Display output as text onlys   -qs   --quietTextGraphicst   quietGraphicss'   Generate minimal output and no graphicss   -gs   --ghostst   ghosts5   the ghost agent TYPE in the ghostAgents module to uset   RandomGhosts   -ks   --numghostst	   numGhostss#   The maximum number of ghosts to usei   s   -zs   --zoomRc   t   zooms$   Zoom the size of the graphics windowg      ?s   -fs   --fixRandomSeedt   fixRandomSeeds2   Fixes the random seed to always play the same games   -rs   --recordActionst   recordsD   Writes game histories to a file (named by the time they were played)s   --replayt   gameToReplays'   A recorded game file (pickle) to replays   -as   --agentArgst	   agentArgssE   Comma separated values sent to agent. e.g. "opt1=val1,opt2,opt3=val3"s   -xs   --numTrainingt   numTrainings2   How many episodes are training (suppresses output)i    s   --frameTimet	   frameTimes/   Time to delay between frames; <0 means keyboardg?s   -cs   --catchExceptionsRS   s5   Turns on exception handling and timeouts during gamess	   --timeoutRR   sD   Maximum length of time an agent can spend computing in a single gamei   s#   Command line input not understood: t   cs188s   The layout s    cannot be foundt   numTraint   ghostsNRY   s   Replaying recorded game %s.(0   t   optparseR   t
   add_optionR   R   RA   t
   parse_argsR0   R   RI   t   dictR   t   randomt   seedR8   t	   getLayoutR   R   R   t	   loadAgentR   R   R   R   R   t   numQuiett	   numIgnoreR   R   R   t   textDisplayt   NullGraphicsR   t
   SLEEP_TIMEt   PacmanGraphicst   graphicsDisplayR   R   R   RS   RR   t   cPicklet   opent   loadt   closet
   replayGamet   syst   exit(   t   argvR   t   usageStrt   parsert   optionst	   otherjunkt   argst
   noKeyboardt
   pacmanTypet	   agentOptsR   t	   ghostTypeR$   R   R   R   t   ft   recorded(    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   readCommand  s    		  ! 
0		 
c   	      C   sC  t  j j d  } | j d  d k r9 | j d  } n | j d  } | j d  x | D] } t  j j |  sz q\ n  g  t  j |  D] } | j d  r | ^ q } xy | D]q } y t	 | d   } Wn t
 k
 r q n X|  t |  k r | r| d k rt d	   n  t | |   Sq Wq\ Wt d
 |  d   d  S(   Ns   $PYTHONPATHt   ;it   :t   .s   gents.pyis   keyboardAgents.pys7   Using the keyboard requires graphics (not text display)s
   The agent s$    is not specified in any *Agents.py.(   t   ost   patht
   expandvarst   findR   t   appendt   isdirt   listdirt   endswitht
   __import__t   ImportErrort   dirR   t   getattr(	   R   t
   nographicst   pythonPathStrt   pythonPathDirst	   moduleDirR   t   moduleNamest
   modulenamet   module(    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyR   E  s&     1c         C   s   d d  l  } d d  l } t   } | j   g g  t |  j    D] } | j | d  ^ q@ } | j |  | d | d |  } | j }	 | j	 |	 j
  x= | D]5 }
 |	 j |
   }	 | j |	 j
  | j |	 |  q W| j   d  S(   Nii   i    (   t   pacmanAgentsRX   RQ   t   GreedyAgentR   RT   R   R]   R#   RK   R   R%   t   updateR`   t   finish(   R8   t   actionsRY   R   RX   t   rulesR$   RZ   R\   R#   R"   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyR   \  s    	? 	i    i   c	         C   s  d d  l  }	 | |	 j d <t |  }
 g  } x9t |  D]+} | | k  } | rt d d  l } | j   } t |
 _ n | } t |
 _ |
 j	 |  | | | | |  } | j
   | s | j |  n  | r8 d d  l } d d  l } d | d d j g  | j   d d !D] } t |  ^ q } t | d  } i |  d 6| j d	 6} | j | |  | j   q8 q8 W| | d
 k r}g  | D] } | j j   ^ q~} g  | D] } | j j   ^ q} | j t  t t |   } d Gt |  t t |   GHd Gd j g  | D] } t |  ^ q
 GHd | j t  t |  | f GHd Gd j g  | D] } d d g t |  ^ qV GHn  | S(   Nit   _displays   recorded-game-%di   t   -i   t   wR8   R   i    s   Average Score:s   Scores:       s   , s   Win Rate:      %d/%d (%.2f)s   Record:       t   Losst   Win(   t   __main__t   __dict__RQ   R   R   R   Ra   RV   R   R]   t   runR   t   timeR   t   joint	   localtimeRI   t   filet   moveHistoryt   dumpR   R#   R1   R   R5   Rc   R0   t   sumR   (   R8   R   R   RY   R   R   R   RS   RR   R   R   t   gamesR$   t   beQuietR   t   gameDisplayR\   R   R   t   tt   fnameR   t
   componentst   scorest   winst   winRateR!   R   (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   runGamesn  s@    	
 A""+!8R   (    (    (    (    (    RO   R\   R    R   R   R   t   utilR   R   R8   R   t   typesR   R   R   R   Rx   R   R   RQ   R   R   R   R   R   R   R   R   R  RM   R   R   (    (    (    s^   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\pacman.pyt   <module>%   s2   <9:P			o		(
