Your IP : 216.73.216.196


Current Path : /opt/alt/python37/lib64/python3.7/Tools/demo/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib64/python3.7/Tools/demo/__pycache__/life.cpython-37.pyc

B

� f'#�@sXdZddlZddlZGdd�d�Zdd�Zdd�Zd	d
�Zdd�Zed
krTe�	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|_||_|j��\}}|d|dd|_|_||_|j��d|jdd}|j�dd|�|j�|jdd|�xBtd|j�D]2}|j�d|dd�|j�d||jdd�q�W|j�	�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/python37/lib64/python3.7/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=|j�|d|dd�nVd|j||f<t��r�|j�t�	t
�dd���|j�|d|d|j�|j�d�|j�
�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\|j�|d|d|j�q.|j�|d|dd�q.WqW|j��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<t���r�|j�
t�t�dd���|j�|d|d|j�|j�
d�|	�s�d|_q�|dk�r�|	�r�d|||f<q�|	r�|j�|d|dd�d|_q�Wq�W||_|j��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]}t��dkr&|�||�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|�|d�|��|�|dd�|��dS)z&Clear the space where the menu residesrrN)�moveZclrtoeol)�stdscr�menu_yrrr�
erase_menu�sr=cCsPt||�t��r"|�t�d��|�|dd�|�|ddd�|�d�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?cCsP|��|��\}}|dd}t||�t��r�t�dtjd�t�dtjd�t�dtjd�t�dtj	d�t�dtj
d�t�dtjd�t�dtjd�t�
tj�|�|d|dd�}t|td	�d
�}|jdd�|jd|jd}}�xT|�d|d|�|��}d|k�r,d
k�rDn�nt|�}|dk�rR|�||�n�|dk�r�t||�|�|dd�|��|�d�xN|��}|dk�r�P|�ddd�|��|��|�ddd�|���q�W|�d�t||�nV|dk�r|��nB|dk�rPn4|dk�r.|��|jdd�n|dk�rH|��nq�|tjk�rd|dk�rd|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�t�$�\}	}
}}}
|
dk�r>|
|jdk�r>|dk�r>||jdk�r>|
d}|d}|�||�nt�%�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\