ó
+ČÓZc        
   @   sĹ   d  Z  d d l Z d d l Z d d d g d d d g d	 d
 d g d d d	 g d d d
 g d d d g d d d g d d d	 g g Z d d d     YZ d   Z d   Z d d  Z d d  Z	 d S(   s%    Layout positions:
0 1 2
3 4 5
6 7 8
i˙˙˙˙Ni    i   i   i   i   i   i   i   i   t	   BoardNodec           B   s,   e  Z d    Z d   Z d   Z d   Z RS(   c         C   sę   | |  _  d  |  _ g  |  _ g  |  _ g  |  _ d  |  _ d  |  _ |  j   r |  j  j	 d  |  j  j	 d  k r{ d |  _ qŤ d |  _ n$ |  j  j	 d  d k rŤ d |  _ n  |  j d  k	 rŇ |  j |  _ d |  _ n  |  j
   |  j   d  S(   Nt   xt   ot   _i    t   d(   t   layoutt   Nonet   endStatet   parentst   childrent	   bestMovest   whoWinst
   movesToWint   _isWint   countt   gatherParentsBoardst   gatherChildrensBoards(   t   selfR   (    (    s8   /usr/local/www/bert/pbrooks/ai/tic-tac-toe/TTT_Boards.pyt   __init__   s$    							$
c         C   sm   xf t  D]^ } |  j | d d k r |  j | d |  j | d k o\ |  j | d k n r t Sq Wt S(   Ni    R   i   i   (   t   winsR   t   Truet   False(   R   t   awin(    (    s8   /usr/local/www/bert/pbrooks/ai/tic-tac-toe/TTT_Boards.pyR   -   s    Tc         C   sŹ   |  j  j d  d k rĽ d } |  j  j d  |  j  j d  k rK d } n  xW t d  D]F } |  j  | | k rX |  j j |  j  |  d |  j  | d  qX qX Wn  |  j S(   NR   i	   R   R   i   (   R   R   t   rangeR   t   append(   R   t
   lastPlayert   i(    (    s8   /usr/local/www/bert/pbrooks/ai/tic-tac-toe/TTT_Boards.pyR   3   s    $ 	4c         C   sŁ   |  j  d  k r d } |  j j d  |  j j d  k rB d } n  xW t d  D]F } |  j | d k rO |  j j |  j |  | |  j | d  qO qO Wn  |  j S(   NR   R   i	   R   i   (   R   R   R   R   R   R	   R   (   R   t   playerR   (    (    s8   /usr/local/www/bert/pbrooks/ai/tic-tac-toe/TTT_Boards.pyR   =   s    $ 	4(   t   __name__t
   __module__R   R   R   R   (    (    (    s8   /usr/local/www/bert/pbrooks/ai/tic-tac-toe/TTT_Boards.pyR       s   			
c             s&   i  }    f d       |  d  |  S(   Nc            sJ   | |  k rF t  |  } | |  | <x! | j D] }   |  |  q, Wn  d  S(   N(   R    R	   (   t   mastert	   newlayoutt   bt   child_layout(   t   recurse(    s8   /usr/local/www/bert/pbrooks/ai/tic-tac-toe/TTT_Boards.pyR"   I   s
    
t	   _________(    (   t	   allBoards(    (   R"   s8   /usr/local/www/bert/pbrooks/ai/tic-tac-toe/TTT_Boards.pyt   CreateAllBoardsF   s    c         C   sd   g  t  d  D] } g  ^ q } |  j   } x2 | D]* } | j d  } | d | j |  q2 W| S(   Ni
   R   i	   (   R   t   keysR   R   (   t   allbR   t   layerst   layoutsR   t   n(    (    s8   /usr/local/www/bert/pbrooks/ai/tic-tac-toe/TTT_Boards.pyt	   GetLayersS   s    R#   c         C   sç  t  |   } xÔt d d d  D]Ŕ} | | } x­| D]Ľ} |  | } | j d k r6 d } | j d  | j d  k r d } n  g  } x | j D] }	 |  |	 }
 |
 j } |
 j } | | k rÝ | j d | |	 | g  q | d k r| j | |	 | g  q | j d | |	 | g  q W| j d t	  | d	 d
 g | _
 xT t d
 t |   D]= } | | d	 | d	 d	 k rZ| j
 j | | d
  qZqZW| d	 d d
 | _ |  | d	 d
 j | _ | | k rŰd Sq6 q6 Wq Wd S(   s   Calculate and store into the BoardNodes the following instance properties, for all board below stopAtLayout:
    bestMoves, whoWins and movesToWini	   i˙˙˙˙R   R   id   R   iö˙˙˙t   reversei    i   i   N(   R+   R   R   R   R   R	   R   R   t   sortR   R
   t   len(   t	   allboardst   stopAtLayoutR(   R   t   layerR   t   theBoardR   t   posst   childLayoutt   theChildt   childsWhoWinst   childsMovesToWin(    (    s8   /usr/local/www/bert/pbrooks/ai/tic-tac-toe/TTT_Boards.pyt
   SetWhoWins[   s8    

 	
		c   	   
   C   s  t  j    } | d k r$ t   } n  t | |   |  | k rU d d t  j    | f S| |  } | j d k	 rš | j d k r d d t  j    | f Sd d | j t  j    | f f Sn  t j | j  } xľ t d  D]§ } |  | | | k rŘ d d d	 d
 d d d d d g	 | } d | } | j	 d k rDd | j
 d } n d | j	 | j
 d f } | d | | t  j    | f f SqŘ Wd S(   s   returns (bestmove,comments)i˙˙˙˙s.   This board position is NOT valid.  Time: %4.1fR   s   Draw.  Time: %4.1fs   %s has won. Time: %4.1fi	   s
   upper-lefts   upper-middles   upper-rights   middle-leftt   centers   middle-rights
   lower-lefts   lower-middles   lower-rights   Best move: %s. s   Draw in %d moves. i   s   %s wins in %d moves.s   %s %s Time: %4.1fN(   t   timeR   R%   R8   R   t   randomt   choiceR
   R   R   R   (	   t   currentLayoutR/   t
   start_timet   boardt   bestR   t   movetot   smovet   s(    (    s8   /usr/local/www/bert/pbrooks/ai/tic-tac-toe/TTT_Boards.pyt   GetBestMoveAndComments{   s,    
$		
(    (
   t   __doc__R:   R;   R   R    R%   R+   R8   R   RD   (    (    (    s8   /usr/local/www/bert/pbrooks/ai/tic-tac-toe/TTT_Boards.pyt   <module>	   s   f5		 