ó
’Ÿ?`c           @   s‡   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 „  Z e	 d k rƒ e j
 e ƒ n  d S(
   iÿÿÿÿNt	   LifeBoardc           B   sP   e  Z d  Z e d ƒ d „ Z d „  Z d „  Z d „  Z e d „ Z	 d „  Z
 RS(   s  Encapsulates a Life board

    Attributes:
    X,Y : horizontal and vertical size of the board
    state : dictionary mapping (x,y) to 0 or 1

    Methods:
    display(update_board) -- If update_board is true, compute the
                             next generation.  Then display the state
                             of the board and refresh the screen.
    erase() -- clear the entire board
    makeRandom() -- fill the board randomly
    set(y,x) -- set the given cell to Live; doesn't refresh the screen
    toggle(y,x) -- change the given cell from live to dead, or vice
                   versa, and refresh the screen display

    t   *c         C   s  i  |  _  | |  _ |  j j ƒ  \ } } | d | d d |  _ |  _ | |  _ |  j j ƒ  d |  j d d } |  j j d d | ƒ |  j j |  j d d | ƒ xU t d |  j ƒ D]A } |  j j d | d d ƒ |  j j d | |  j d d ƒ q· W|  j j	 ƒ  d S(   s¤   Create a new LifeBoard instance.

        scr -- curses screen object to use for display
        char -- character used to render live cells (default: '*')
        i   i   t   +t   -i    t   |N(
   t   statet   scrt   getmaxyxt   Xt   Yt   chart   cleart   addstrt   ranget   refresh(   t   selfR   R
   R	   R   t   border_linet   y(    (    s(   /usr/lib64/python2.7/Demo/curses/life.pyt   __init__)   s    			%c         C   sc   | d k  s6 |  j  | k s6 | d k  s6 |  j | k rL t d | | f ‚ n  d |  j | | f <d S(   s   Set a cell to the live statei    s   Coordinates out of range %i,%ii   N(   R   R	   t
   ValueErrorR   (   R   R   t   x(    (    s(   /usr/lib64/python2.7/Demo/curses/life.pyt   set?   s    6c         C   sÚ   | d k  s6 |  j  | k s6 | d k  s6 |  j | k rL t d | | f ‚ n  |  j j | | f ƒ r• |  j | | f =|  j j | d | d d ƒ n4 d |  j | | f <|  j j | d | d |  j ƒ |  j j ƒ  d S(   s+   Toggle a cell's state between live and deadi    s   Coordinates out of range %i,%ii   t    N(	   R   R	   R   R   t   has_keyR   t   addchR
   R   (   R   R   R   (    (    s(   /usr/lib64/python2.7/Demo/curses/life.pyt   toggleE   s    6!!c         C   s   i  |  _  |  j d t ƒ d S(   s3   Clear the entire board and update the board displayt   update_boardN(   R   t   displayt   False(   R   (    (    s(   /usr/lib64/python2.7/Demo/curses/life.pyt   eraseQ   s    	c         C   sw  |  j  |  j } } | s¸ x‹ t d | ƒ D]z } xq t d | ƒ D]` } |  j j | | f ƒ r |  j j | d | d |  j ƒ q? |  j j | d | d d ƒ q? Wq) W|  j j ƒ  d Si  } d |  _	 x“t d | ƒ D]‚} t t
 d | d ƒ t | | d ƒ ƒ } xPt d | ƒ D]?} d } |  j j | | f ƒ }	 xg t t
 d | d ƒ t | | d ƒ ƒ D]< }
 x3 | D]+ } |  j j | |
 f ƒ rq| d 7} qqqqWqdW| |	 8} | d k r d | | | f <|  j j | d | d |  j ƒ |	 sUd |  _	 qUq| d k r%|	 r%d | | | f <q|	 r|  j j | d | d d ƒ d |  _	 qqWq× W| |  _ |  j j ƒ  d S(   s<   Display the whole board, optionally computing one generationi    i   R   Ni   i   (   R   R	   R   R   R   R   R   R
   R   t   boringt   maxt   min(   R   R   t   Mt   Nt   it   jt   dt   Lt   st   livet   kt   l(    (    s(   /usr/lib64/python2.7/Demo/curses/life.pyR   V   sB    $&	)0
!  	c         C   sl   i  |  _  x\ t d |  j ƒ D]H } x? t d |  j ƒ D]+ } t j ƒ  d k r5 |  j | | ƒ q5 q5 Wq Wd S(   s$   Fill the board with a random patterni    g      à?N(   R   R   R   R	   t   randomR   (   R   R#   R$   (    (    s(   /usr/lib64/python2.7/Demo/curses/life.pyt
   makeRandom|   s
    	(   t   __name__t
   __module__t   __doc__t   ordR   R   R   R   t   TrueR   R,   (    (    (    s(   /usr/lib64/python2.7/Demo/curses/life.pyR       s   			&c         C   s<   |  j  | d ƒ |  j ƒ  |  j  | d d ƒ |  j ƒ  d S(   s&   Clear the space where the menu residesi    i   N(   t   movet   clrtoeol(   t   stdscrt   menu_y(    (    s(   /usr/lib64/python2.7/Demo/curses/life.pyt
   erase_menu…   s    
c         C   s;   t  |  | ƒ |  j | d d ƒ |  j | d d d ƒ d S(   s/   Display the menu of possible keystroke commandsi   sA   Use the cursor keys to move, and space or Enter to toggle a cell.i   sB   E)rase the board, R)andom fill, S)tep once or C)ontinuously, Q)uitN(   R6   R   (   R4   R5   (    (    s(   /usr/lib64/python2.7/Demo/curses/life.pyt   display_menuŒ   s
    c   	      C   sí  |  j  ƒ  |  j ƒ  \ } } | d d } t |  | ƒ |  j | d | d d ƒ } t | d t d ƒ ƒ} | j d t ƒ | j d | j	 d } } xP|  j
 d | d | ƒ |  j ƒ  } d | k  oÔ d k  n r8t | ƒ } | d	 k r| j | | ƒ qæ| d
 k rÊt |  | ƒ |  j | d d ƒ |  j ƒ  |  j d ƒ xc |  j ƒ  } | d k rfPn  |  j d d d ƒ |  j ƒ  | j ƒ  |  j d d d ƒ |  j ƒ  qJ|  j d ƒ t |  | ƒ qæ| d k rã| j ƒ  qæ| d k róPqæ| d k r| j ƒ  | j d t ƒ qæ| d k ræ| j ƒ  qæq™ | t j k r`| d k r`| d 8} q™ | t j k r| | j	 d k  r| d 7} q™ | t j k r·| d k r·| d 8} q™ | t j k r™ | | j d k  r™ | d 7} q™ q™ d  S(   Ni   i   i    R
   R   R   i   i   s    
t   Cci   s6    Hit any key to stop continuously updating the screen.iÿÿÿÿt   /R   t   Eet   Qqt   Rrt   Ss(   R   R   R7   t   subwinR    R0   R   R   R   R	   R2   t   getcht   chrR   R6   R   R   t   nodelayR   R,   t   cursest   KEY_UPt   KEY_DOWNt   KEY_LEFTt	   KEY_RIGHT(	   R4   t   stdscr_yt   stdscr_xR5   R>   t   boardt   xpost   ypost   c(    (    s(   /usr/lib64/python2.7/Demo/curses/life.pyt   keyloop”   sd    




 "  " c         C   s   t  |  ƒ d  S(   N(   RM   (   R4   (    (    s(   /usr/lib64/python2.7/Demo/curses/life.pyt   mainÓ   s    t   __main__(    (   R+   t   stringt	   tracebackRB   R    R6   R7   RM   RN   R-   t   wrapper(    (    (    s(   /usr/lib64/python2.7/Demo/curses/life.pyt   <module>   s   $n			?	