Your IP : 216.73.216.213


Current Path : /proc/242857/root/opt/alt/python34/lib64/python3.4/Tools/demo/__pycache__/
Upload File :
Current File : //proc/242857/root/opt/alt/python34/lib64/python3.4/Tools/demo/__pycache__/life.cpython-34.pyc

�
e f'#�@s�dZddlZddlZGdd�d�Zdd�Zdd�Zd	d
�Zdd�Zed
kr}ej	e�ndS)a�
A curses-based version of Conway's Game of Life.

An empty board will be displayed, and the following commands are available:
 E : Erase the board
 R : Fill the board randomly
 S : Step for a single generation
 C : Update continuously until a key is struck
 Q : Quit
 Cursor keys :  Move the cursor around the board
 Space or Enter : Toggle the contents of the cursor's position

Contributed by Andrew Kuchling, Mouse support and color by Dafydd Crosby.
�Nc@sjeZdZdZed�dd�Zdd�Zdd�Zd	d
�Zddd
�Z	dd�Z
dS)�	LifeBoarda�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
    make_random() -- 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

    �*cCs
i|_||_|jj�\}}|d|dd|_|_||_|jj�d|jdd}|jjdd|�|jj|jdd|�xUtd|j�D]A}|jjd|dd�|jjd||jdd�q�W|jj	�dS)z�Create a new LifeBoard instance.

        scr -- curses screen object to use for display
        char -- character used to render live cells (default: '*')
        ���+�-r�|N)
�state�scr�getmaxyx�X�Y�char�clear�addstr�range�refresh)�selfr
rr
rZborder_line�y�r�4/opt/alt/python34/lib64/python3.4/Tools/demo/life.py�__init__(s			
%zLifeBoard.__init__cCsf|dks6|j|ks6|dks6|j|krOtd||f��nd|j||f<dS)zSet a cell to the live staterzCoordinates out of range %i,%irN)rr
�
ValueErrorr	)rr�xrrr�set>s6z
LifeBoard.setcCs|dks6|j|ks6|dks6|j|krOtd||f��n||f|jkr�|j||f=|jj|d|dd�nxd|j||f<tj�r�|jjtj	t
jdd���n|jj|d|d|j�|jjd�|jj
�dS)z+Toggle a cell's state between live and deadrzCoordinates out of range %i,%ir� �N)rr
rr	r
�addch�curses�
has_colors�attrset�
color_pair�random�	randrangerr)rrrrrr�toggleDs6!(!zLifeBoard.togglecCsi|_|jdd�dS)z3Clear the entire board and update the board display�update_boardFN)r	�display)rrrr�eraseTs	zLifeBoard.eraseTcCs�|j|j}}|s�x�td|�D]w}xntd|�D]]}||f|jkr~|jj|d|d|j�q?|jj|d|dd�q?Wq)W|jj�dSi}d|_x�td|�D]�}tt	d|d�t
||d��}x�td|�D]}}d}||f|jk}	xdtt	d|d�t
||d��D]9}
x0|D](}||
f|jkrk|d7}qkqkWq^W||	8}|dkr;d|||f<tj�r�|jj
tjtjdd���n|jj|d|d|j�|jj
d�|	s�d|_q�q|dkr`|	r`d|||f<q|	r|jj|d|dd�d|_qqWq�W||_|jj�dS)z<Display the whole board, optionally computing one generationrrrNr�r)rr
rr	r
rrrZboring�max�minrrr r!r"r#)rr%�M�N�i�j�d�L�sZlive�k�lrrrr&YsJ$&
	)0

!	zLifeBoard.displaycCsli|_x\td|j�D]H}x?td|j�D]+}tj�dkr5|j||�q5q5WqWdS)z$Fill the board with a random patternrg�?N)r	rrr
r"r)rr-r.rrr�make_random�s
	zLifeBoard.make_randomN)�__name__�
__module__�__qualname__�__doc__�ordrrr$r'r&r4rrrrrs.rcCs<|j|d�|j�|j|dd�|j�dS)z&Clear the space where the menu residesrrN)�moveZclrtoeol)�stdscr�menu_yrrr�
erase_menu�s
r=cCsmt||�tj�r2|jtjd��n|j|dd�|j|ddd�|jd�dS)z/Display the menu of possible keystroke commandsr�zAUse the cursor keys to move, and space or Enter to toggle a cell.zBE)rase the board, R)andom fill, S)tep once or C)ontinuously, Q)uitrN)r=rrr r!r)r;r<rrr�display_menu�s
r?cCsD|j�|j�\}}|dd}t||�tj�r�tjdtjd�tjdtjd�tjdtjd�tjdtj	d�tjdtj
d�tjdtjd�tjdtjd�ntj
tj�|j|d|dd�}t|d	td
��}|jdd�|jd|jd}}x�|jd|d|�|j�}d|ko�d
knr�t|�}|dkr�|j||�q<|dkr�t||�|j|dd�|j�|jd�xd|j�}|dkrPn|jddd�|j�|j�|jddd�|j�qW|jd�t||�q<|dkr�|j�q<|dkr�Pq<|dkr�|j�|jdd�q<|dkr<|j�q<qR|tjkr|dkr|d8}qR|tj krI|d|jkrI|d7}qR|tj!krq|dkrq|d8}qR|tj"kr�|d|jkr�|d7}qR|tj#krRtj$�\}	}
}}}
|
dkr/|
|jdkr/|dkr/||jdkr/|
d}|d}|j||�q<tj%�qRqRWdS)Nr(rrrr>��rrrr%F�z 
ZCcz6 Hit any key to stop continuously updating the screen.�/rZEeZQqZRrZSs���)&rrr?rrZ	init_pairZ
COLOR_BLUEZ
COLOR_CYANZCOLOR_GREENZ
COLOR_MAGENTAZ	COLOR_REDZCOLOR_YELLOWZCOLOR_WHITEZ	mousemaskZBUTTON1_CLICKED�subwinrr9r&rr
r:Zgetch�chrr$r=rrZnodelayr'r4ZKEY_UPZKEY_DOWNZKEY_LEFTZ	KEY_RIGHTZ	KEY_MOUSEZgetmouseZflash)r;Zstdscr_yZstdscr_xr<rEZboardZxposZypos�cZmouse_idZmouse_xZmouse_yZmouse_zZbutton_staterrr�keyloop�s�











"

"



rHcCst|�dS)N)rH)r;rrr�mainsrI�__main__)
r8rr"rr=r?rHrIr5�wrapperrrrr�<module>sz\