
   Qso                   F   S r SSKrSSKrSSKrSrSrSrSr\S-  r\\S-  S	-  \S-  S
-  -   -  r	Sr
SrSrS rS rS r " S S\5      r " S S\R$                  5      r " S S\5      r " S S\5      r " S S\5      rS r\S:X  a  \" 5         \R2                  " 5         gg)zturtle-example-suite:

            tdemo_nim.py

Play nim against the computer. The player
who takes the last stick is the winner.

Implements the model-view-controller
design pattern.
    Ni  i                    )?   r	   r   )      r   )r   r   r
   c                  @    [         R                  " [        [        5      $ N)randomrandint	MINSTICKS	MAXSTICKS     %/usr/lib/python3.13/turtledemo/nim.py	randomrowr      s    >>)Y//r   c                     U S   U S   -  U S   -  nUS:X  a  [        U 5      $ [        S5       H  nX   U-  nX0U   ::  d  M  X#4nUs  $    g )Nr      r      )
randommoverange)statexoredzsmoves        r   computerzugr    !   s^    !HuQx%(*Ez%  1XHua=6DK	 r   c                     [        U 5      n [        R                  " SS5      nX   US:  :  a  OM%  [        R                  " US:  X   S-
  5      nX#4$ )Nr   r   r   )maxr   r   )r   mr   rands       r   r   r   +   sW    E
A
NN1Q8q1u  >>!a%!,D7Nr   c                   2    \ rS rSrS rS rS rS rS rSr	g)	NimModel5   c                     Xl         g r   )game)selfr)   s     r   __init__NimModel.__init__6   s    	r   c                 \   U R                   R                  [        R                  [        R                  4;  a  g [        5       [        5       [        5       /U l        SU l        S U l        U R                   R                  R                  5         [        R                  U R                   l        g )Nr   )r)   r   NimCREATEDOVERr   sticksplayerwinnerviewsetupRUNNINGr*   s    r   r5   NimModel.setup9   sg    99??3;;"99 {IK=		++		r   c                     U R                   U   nX R                   U'   U R                  R                  R                  XX0R                  5        U R                  5       (       aU  [        R                  U R                  l        U R                  U l	        U R                  R                  R                  5         g U R                  S:X  a7  SU l        [        U R                   5      u  pU R                  X5        SU l        g g )Nr   r   )r1   r)   r4   notify_mover2   	game_overr.   r0   r   r3   notify_overr    r   )r*   rowcol	maxspaltes       r   r   NimModel.moveB   s    KK$	C		""3YD>>!hhDIIO++DKIINN&&([[ADK"4;;/HCIIcDK	 r   c                 $    U R                   / SQ:H  $ )N)r   r   r   )r1   r7   s    r   r;   NimModel.game_overP   s    {{i''r   c                 N    U R                   U   U::  a  g U R                  X5        g r   )r1   r   r*   r=   r>   s      r   r:   NimModel.notify_moveS   s"    ;;ss"		#r   )r)   r2   r1   r3   N)
__name__
__module____qualname____firstlineno__r+   r5   r   r;   r:   __static_attributes__r   r   r   r&   r&   5   s    &(r   r&   c                   &    \ rS rSrS rS rS rSrg)StickY   c                    [         R                  R                  U SS9  Xl        X l        X0l        U R                  X5      u  pEU R                  S5        U R                  [        S-  [        S-  5        U R                  S5        U R                  5         U R                  XE5        U R                  S5        U R                  5         g )NFvisiblesquareg      $@g      4@r   white)turtleTurtler+   r=   r>   r)   coordsshape	shapesizeHUNITWUNITspeedpugotocolor
showturtle)r*   r=   r>   r)   xys         r   r+   Stick.__init__Z   s    tU3	{{3$

8uTz5:.

1			!

7r   c                     [        US5      u  p4SSU-  -   SU-  -   [        -  nSSU-  -   [        -  nU[        S-  -
  [        S-  -   [        S-  U-
  [        S-  -
  4$ )Nr   r   r   r   )divmodrY   rX   SCREENWIDTHSCREENHEIGHT)r*   r=   r>   packet	remainderr_   r`   s          r   rU   Stick.coordsh   so    "3Nf_q9},5S[E!;!##eqj0,!2Ca2G%ST*2TTTr   c                     U R                   R                  [        R                  :w  a  g U R                   R                  R                  U R                  U R                  5        g r   )r)   r   r.   r6   
controllerr:   r=   r>   )r*   r_   r`   s      r   makemoveStick.makemoven   s;    99??ckk)		((488<r   )r>   r)   r=   N)rF   rG   rH   rI   r+   rU   rk   rJ   r   r   r   rL   rL   Y   s    U=r   rL   c                   <    \ rS rSrS rS
S jrS rS rS rS r	S	r
g)NimViewt   c                 |   Xl         UR                  U l        UR                  U l        U R                  R                  S5        U R                  R	                  S5        U R                  R                  S5        [        R                  " SS9U l        U R                  R                  5         U R                  R                  S5        0 U l        [        S5       H3  n[        [        5       H  n[        X#U5      U R                  X#4'   M     M5     U R                  S5        U R                  R	                  S5        g )	Nr
   F)   rq   r
   rO   r   r   z... a moment please ...T)r)   screenmodel	colormodetracerbgcolorrS   rT   writerr[   rZ   r1   r   r   rL   display)r*   r)   r=   r>   s       r   r+   NimView.__init__u   s    	kkZZ
c"5!O,mmE2!8CY'*/$*?SJ' (  	./4 r   Nc                    U R                   R                  S5        U R                  R                  5         Ub]  U R                  R	                  S[
        * S-  S-   5        U R                  R                  S5        U R                  R                  USSS9  U R                  R	                  S[
        * S-  S	-   5        U R                  R                  S
5        U R                  R                  USSS9  U R                   R                  S5        g )NFr   r   0   redcenter)Courier   bold)alignfont   black)r~      r   T)rr   ru   rw   clearr\   re   pencolorwrite)r*   msg1msg2s      r   rx   NimView.display   s    5!KKQ,! 3b 89KK  'KKd(9NOlNa/"45W%$h5JK4 r   c                    U R                   R                  S5        [        S5       HO  n[        U R                  R                  U   5       H&  nU R                  X4   R                  [        5        M(     MQ     [        S5       HP  n[        U R                  R                  U   [        5       H"  nU R                  X4   R                  S5        M$     MR     U R                  S5        U R                   R                  S5        g )NFr   rR   *Your turn! Click leftmost stick to remove.T)	rr   ru   r   rs   r1   r]   SCOLORr   rx   rD   s      r   r5   NimView.setup   s    5!8CTZZ..s34SJ'--f5 5  8CTZZ..s3Y?SJ'--g6 @  	AB4 r   c                    US:X  a8  [         n[        X#5       H"  nU R                  X4   R                  U5        M$     g U R	                  S5        [
        R                  " S5        U R	                  S5        [        n[        US-
  US-
  S5       H8  n[
        R                  " S5        U R                  X4   R                  U5        M:     U R	                  S5        g )	Nr   z ... thinking ...         g      ?z ... thinking ... aaah ...r   g?r   )HCOLORr   r1   r]   rx   timesleepCOLOR)r*   r=   r>   r?   r2   farber   s          r   r:   NimView.notify_move   s    Q;E3*SH%++E2 + LL56JJsOLL56E9Q;Ar2

3SH%++E2 3 LLEFr   c                 z    U R                   R                  R                  S:X  a  SnOSnU R                  SU5        g )Nr   zCongrats. You're the winner!!!z"Sorry, the computer is the winner.z2To play again press space bar. To leave press ESC.)r)   rs   r3   rx   )r*   r   s     r   r<   NimView.notify_over   s1    99??!!Q&3D7DI4Pr   c                     U R                   R                  [        R                  :X  a  U R                  R                  5         g g r   )r)   r   r.   r0   rr   r   r7   s    r   r   NimView.clear   s+    99??chh&KK 'r   )r)   rs   rr   r1   rw   r   )rF   rG   rH   rI   r+   rx   r5   r:   r<   r   rJ   r   r   r   rn   rn   t   s"    !"
!	!GQ r   rn   c                        \ rS rSrS rS rSrg)NimController   c                 n   Xl         UR                  R                  U l        SU l        U R                  R	                  5        H  nUR                  UR                  5        M      U R                   R                  R                  U R                   R                  R                  S5        U R                   R                  R                  U R                   R                  R                  S5        U R                   R                  R                  S5        U R                   R                  R                  5         g )NFspaceEscapezPress space bar to start game)r)   r4   r1   BUSYvaluesonclickrk   rr   onkeyrs   r5   r   rx   listen)r*   r)   sticks      r   r+   NimController.__init__   s    	ii&&	[['')EMM%..) *		tyy44g>		tyy~~33X>		>?		!r   c                     U R                   (       a  g SU l         U R                  R                  R                  X5        SU l         g )NTF)r   r)   rs   r:   rD   s      r   r:   NimController.notify_move   s0    99			##C-	r   )r   r)   r1   N)rF   rG   rH   rI   r+   r:   rJ   r   r   r   r   r      s    	"r   r   c                   &    \ rS rSrSrSrSrS rSrg)r.      r   r   r   c                     [         R                  U l        Xl        [	        U 5      U l        [        U 5      U l        [        U 5      U l	        g r   )
r.   r/   r   rr   r&   rs   rn   r4   r   rj   )r*   rr   s     r   r+   Nim.__init__   s4    [[
d^
DM	'-r   )rj   rs   rr   r   r4   N)	rF   rG   rH   rI   r/   r6   r0   r+   rJ   r   r   r   r.   r.      s    GGD.r   r.   c                      [         R                  " 5       n U R                  S5        U R                  [        [
        5        [        U 5      ng)Nstandard	EVENTLOOP)rS   Screenmoder5   rd   re   r.   )
mainscreennims     r   mainr      s5    JOOJ[,/
j/Cr   __main__)__doc__rS   r   r   rd   re   r   r   rX   rY   r   r   r   r   r    r   objectr&   rT   rL   rn   r   r.   r   rF   mainloopr   r   r   <module>r      s   	   		a2-Q!0CCD		0!v !H=FMM =6A f A HF *	.& 	. zF
OO r   