Your IP : 216.73.216.213


Current Path : /proc/242857/root/opt/alt/python33/lib64/python3.3/Tools/demo/__pycache__/
Upload File :
Current File : //proc/242857/root/opt/alt/python33/lib64/python3.3/Tools/demo/__pycache__/life.cpython-33.pyc

�
��f'#c@s�dZddlZddlZGdd�d�Zdd�Zdd�Zd	d
�Zdd�Zed
kr}ej	e�ndS(u�
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.
iNcBsn|EeZdZdZed�dd�Zdd�Zdd�Zd	d
�Zddd�Z
d
d�ZdS(u	LifeBoardu�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

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

        scr -- curses screen object to use for display
        char -- character used to render live cells (default: '*')
        iiu+u-iu|N(
ustateuscrugetmaxyxuXuYucharuclearuaddstrurangeurefresh(uselfuscrucharuYuXuborder_lineuy((u4/opt/alt/python33/lib64/python3.3/Tools/demo/life.pyu__init__(s			
%uLifeBoard.__init__cCsf|dks6|j|ks6|dks6|j|krOtd||f��nd|j||f<dS(uSet a cell to the live stateiuCoordinates out of range %i,%iiN(uXuYu
ValueErrorustate(uselfuyux((u4/opt/alt/python33/lib64/python3.3/Tools/demo/life.pyuset>s6u
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(u+Toggle a cell's state between live and deadiuCoordinates out of range %i,%iiu iN(uXuYu
ValueErrorustateuscruaddchucursesu
has_colorsuattrsetu
color_pairurandomu	randrangeucharurefresh(uselfuyux((u4/opt/alt/python33/lib64/python3.3/Tools/demo/life.pyutoggleDs6!(!uLifeBoard.togglecCsi|_|jdd�dS(u3Clear the entire board and update the board displayuupdate_boardNF(ustateudisplayuFalse(uself((u4/opt/alt/python33/lib64/python3.3/Tools/demo/life.pyueraseTs	uLifeBoard.erasec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(u<Display the whole board, optionally computing one generationiiu Niii(uXuYurangeustateuscruaddchucharurefreshuboringumaxuminucursesu
has_colorsuattrsetu
color_pairurandomu	randrange(uselfuupdate_boarduMuNuiujuduLusuliveukul((u4/opt/alt/python33/lib64/python3.3/Tools/demo/life.pyudisplayYsJ$&
	)0

!	uLifeBoard.displaycCsli|_x\td|j�D]H}x?td|j�D]+}tj�dkr5|j||�q5q5WqWdS(u$Fill the board with a random patternig�?N(ustateurangeuXuYurandomuset(uselfuiuj((u4/opt/alt/python33/lib64/python3.3/Tools/demo/life.pyumake_random�s
	uLifeBoard.make_randomNT(u__name__u
__module__u__qualname__u__doc__uordu__init__usetutoggleueraseuTrueudisplayumake_random(u
__locals__((u4/opt/alt/python33/lib64/python3.3/Tools/demo/life.pyu	LifeBoards.u	LifeBoardcCs<|j|d�|j�|j|dd�|j�dS(u&Clear the space where the menu residesiiN(umoveuclrtoeol(ustdscrumenu_y((u4/opt/alt/python33/lib64/python3.3/Tools/demo/life.pyu
erase_menu�s
u
erase_menucCsmt||�tj�r2|jtjd��n|j|dd�|j|ddd�|jd�dS(u/Display the menu of possible keystroke commandsiiuAUse the cursor keys to move, and space or Enter to toggle a cell.uBE)rase the board, R)andom fill, S)tep once or C)ontinuously, Q)uitiN(u
erase_menuucursesu
has_colorsuattrsetu
color_pairuaddstr(ustdscrumenu_y((u4/opt/alt/python33/lib64/python3.3/Tools/demo/life.pyudisplay_menu�s
udisplay_menucCsB|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|�}|d
kr�|j||�q;|dkr�t||�|j|dd�|j�|jd�xc|j�}|dkrPn|jddd�|j�|j�|jddd�|j�q|jd�t||�q;|dkr�|j�q;|dkr�Pq;|dkr�|j�|jdd�q;|dkr;|j�q;qR|tj kr|dkr|d8}qR|tj!krH|d|jkrH|d7}qR|tj"krp|dkrp|d8}qR|tj#kr�|d|jkr�|d7}qR|tj$krRtj%�\}	}
}}}
|
dkr.|
|jdkr.|dkr.||jdkr.|
d}|d}|j||�q;tj&�qRqRdS(Niiiiiiiiucharu*uupdate_boardiu 
uCcu6 Hit any key to stop continuously updating the screen.u/u+uEeuQquRruSsFi����('uclearugetmaxyxudisplay_menuucursesu
has_colorsu	init_pairu
COLOR_BLUEu
COLOR_CYANuCOLOR_GREENu
COLOR_MAGENTAu	COLOR_REDuCOLOR_YELLOWuCOLOR_WHITEu	mousemaskuBUTTON1_CLICKEDusubwinu	LifeBoarduordudisplayuFalseuXuYumoveugetchuchrutoggleu
erase_menuuaddstrurefreshunodelayueraseumake_randomuKEY_UPuKEY_DOWNuKEY_LEFTu	KEY_RIGHTu	KEY_MOUSEugetmouseuflash(ustdscrustdscr_yustdscr_xumenu_yusubwinuboarduxposuyposucumouse_idumouse_xumouse_yumouse_zubutton_state((u4/opt/alt/python33/lib64/python3.3/Tools/demo/life.pyukeyloop�s�












"

"



ukeyloopcCst|�dS(N(ukeyloop(ustdscr((u4/opt/alt/python33/lib64/python3.3/Tools/demo/life.pyumainsumainu__main__(
u__doc__ucursesurandomu	LifeBoardu
erase_menuudisplay_menuukeyloopumainu__name__uwrapper(((u4/opt/alt/python33/lib64/python3.3/Tools/demo/life.pyu<module>sz\