Your IP : 216.73.216.213


Current Path : /opt/alt/python36/lib64/python3.6/Tools/demo/__pycache__/
Upload File :
Current File : //opt/alt/python36/lib64/python3.6/Tools/demo/__pycache__/life.cpython-36.opt-1.pyc

3

� f'#�@sXdZddlZddlZGdd�d�Zdd�Zdd�Zd	d
�Zdd�Zed
krTej	e�dS)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�fdd�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|�xBtd|j�D]2}|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/python36/lib64/python3.6/Tools/demo/life.py�__init__(s
zLifeBoard.__init__cCsF|dks$|j|ks$|dks$|j|kr4td||f��d|j||f<dS)zSet a cell to the live staterzCoordinates out of range %i,%irN)rr
�
ValueErrorr	)rr�xrrr�set>s$z
LifeBoard.setcCs�|dks$|j|ks$|dks$|j|kr4td||f��||f|jkrh|j||f=|jj|d|dd�nVd|j||f<tj�r�|jjtj	t
jdd���|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�toggleDs$zLifeBoard.togglecCsi|_|jdd�dS)z3Clear the entire board and update the board displayF)�update_boardN)r	�display)rrrr�eraseTszLifeBoard.eraseTcCs|j|j}}|s�xhtd|�D]Z}xTtd|�D]F}||f|jkr\|jj|d|d|j�q.|jj|d|dd�q.WqW|jj�dSi}d|_�xdtd|�D�]T}tt	d|d�t
||d��}�x.td|�D�]}d}||f|jk}	xPtt	d|d�t
||d��D].}
x&|D]}||
f|jk�r|d7}�qW�qW||	8}|dk�r�d|||f<tj��r�|jj
tjtjdd���|jj|d|d|j�|jj
d�|	�s�d|_q�|dk�r�|	�r�d|||f<q�|	r�|jj|d|dd�d|_q�Wq�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 
$



zLifeBoard.displaycCsNi|_xBtd|j�D]2}x,td|j�D]}tj�dkr&|j||�q&WqWdS)z$Fill the board with a random patternrg�?N)r	rrr
r"r)rr-r.rrr�make_random�s
zLifeBoard.make_randomN)T)�__name__�
__module__�__qualname__�__doc__�ordrrr$r'r&r4rrrrrs
.rcCs0|j|d�|j�|j|dd�|j�dS)z&Clear the space where the menu residesrrN)�moveZclrtoeol)�stdscr�menu_yrrr�
erase_menu�sr=cCsPt||�tj�r"|jtjd��|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?cCsN|j�|j�\}}|dd}t||�tj�r�tjdtjd�tjdtjd�tjdtjd�tjdtj	d�tjdtj
d�tjdtjd�tjdtjd�tj
tj�|j|d|dd�}t|td	�d
�}|jdd�|jd|jd}}�xR|jd|d|�|j�}d|k�o(d
kn�rBt|�}|dk�rP|j||�n�|dk�r�t||�|j|dd�|j�|jd�xN|j�}|dk�r�P|jddd�|j�|j�|jddd�|j��q�W|jd�t||�nV|dk�r�|j�nB|dk�rPn4|dk�r,|j�|jdd�n|dk�rF|j�nq�|tjk�rb|dk�rb|d8}q�|tj k�r�|d|jk�r�|d7}q�|tj!k�r�|dk�r�|d8}q�|tj"k�r�|d|jk�r�|d7}q�|tj#kr�tj$�\}	}
}}}
|
dk�r<|
|jdk�r<|dk�r<||jdk�r<|
d}|d}|j||�ntj%�q�q�WdS)Nr(rrrr>��rr)rF)r%�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�


















rHcCst|�dS)N)rH)r;rrr�mainsrI�__main__)
r8rr"rr=r?rHrIr5�wrapperrrrr�<module>sz\