B ^5ÕÓn¾òeã@sXdZddlZddlZGdd„dƒZdd„Zdd„Zd d „Zd d „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 Z border_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|_|jdddS)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|jkr|d7}qWq W|| 8}|dkr´d|||f<t   ¡r€|j  t  t dd¡¡¡|j |d|d|j¡|j  d¡| sôd|_qÔ|dkrÒ| rÒd|||f<qÔ| rÔ|j |d|dd¡d|_qÔWq¢W||_|j ¡dS)z| |jdkr>| dkr>| |jdkr>| d}| d}| ||¡nt %¡qøqøWdS)Nr(rrrr>éérr)rF)r%éz ZCcz6 Hit any key to stop continuously updating the screen.éÿÿÿÿú/rZEeZQqZRrZSs)&rr r?rrZ init_pairZ COLOR_BLUEZ COLOR_CYANZ COLOR_GREENZ COLOR_MAGENTAZ COLOR_REDZ COLOR_YELLOWZ COLOR_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Úsz\