
    Ii                        S r SSKJr  SSKJr  SSKr\(       a  SSKJrJr  SSS	S
SSS.r	/ SQr
/ SQr/ SQr/ SQr/ SQr/ SQr\
\\\\\S.rSS jrSS jrSS jrSS jrg)z5Chess bot AI with material and positional evaluation.    )annotations)TYPE_CHECKINGN   )	ChessGameChessPlayerd   i@  iJ  i  i  i N  )pawnknightbishoprookqueenking)@r   r   r   r   r   r   r   r   2   r   r   r   r   r   r   r   
   r         r   r   r   r      r   r      r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )@r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )@r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )@r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )@r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )@r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   c                H    U S-  nU S-  nUS:X  a  SU-
  S-  U-   $ US-  U-   $ )z0Convert board index to piece-square table index.   white    )sq_indexcolorrankfiles       5c:\Users\dbart\PlayPalace11\server\games\chess\bot.py
_pst_indexr&     s=    q=Da<DDA~$$ax$    c                D   US   nUS   nU R                   U   nU R                   U   nSnU(       d  gUS   nU(       a6  U[        R                  US   S5      S-  [        R                  US5      -
  -  nUS:X  a  X@R                  :X  a  U[        S   S-  -  n[        R                  U5      n	U	(       a&  U	[        X25         n
U	[        XB5         nX{U
-
  S-  -  nUS:X  a  [        US	-  US	-  -
  5      nUS:X  a  US
-  nUS:X  a)  US	-  nUS:X  a  US:X  d  US:X  a  US:X  a  U[        S   -  nU R                  5       nU R                  X45        US:X  a  SOSnU R                  U5      (       a  US-  nU R                  U5        U[        R                  " SS5      -  nU$ )zScore a single move for the bot without deep search.

Considers:
- Material gain from captures (MVV-LVA)
- Positional improvement (piece-square table delta)
- Check bonus
- Castling bonus
- Pawn promotion bonus
fromtor   piecer   r	      r   r   <   r   r   blackr   r   r   r   )boardPIECE_VALUESgeten_passant_targetPSTr&   abssave_positionexecute_move_silentis_in_checkrestore_positionrandomrandint)gamemover"   from_sqto_sqr+   targetscore
piece_typepstold_pstnew_pst	file_diffto_ranksavedopponents                   r%   _score_moverI     s    6lGJEJJwEZZFEwJ !!&/15:\=M=MjZ[=\\\ V)?)? ?f%** ''*
C
j01j./G#q(( Vw{34	>RKE V1*WA5G3CST\'**E  EW,7*wH!!%  
V^^B""ELr'   c                X   U R                  UR                  5      nU(       d  g/ nU H+  n[        XUR                  5      nUR                  XT45        M-     UR	                  S SS9  US   S   nU VVs/ s H  u  pxXvS-
  :  d  M  UPM     n	nn[
        R                  " U	5      $ s  snnf )z/Find the best move using single-ply evaluation.Nc                    U S   $ )Nr   r    )xs    r%   <lambda> find_best_move.<locals>.<lambda>  s    AaDr'   T)keyreverser   r   )get_legal_movesr"   rI   appendsortr9   choice)
r;   playermovesscored_movesr<   r@   	top_scoresm	top_movess
             r%   find_best_mover\     s      .ELD5UM*  .$7 Q"I+C|tqqN/B|IC==## Ds   5B&B&c                  ^ ^ T R                   (       a  gT R                  (       ab  T R                  TR                  :w  aH  [        U U4S j[	        S5       5       5      n[        U U4S j[	        S5       5       5      nX#S-
  :  a  ggT R
                  (       a  T R
                  TR                  :w  a  gT R                  T:w  a  g	[        T T5      nUb  S
US    SUS    3$ g	)z'Decide what action the bot should take.promote_queenc              3     >#    U  Hc  nTR                   U   (       d  M  TR                   U   S    TR                  :X  d  M;  [        R                  TR                   U   S   S5      v   Me     g7fr"   r+   r   Nr/   r"   r0   r1   .0ir;   rU   s     r%   	<genexpr>bot_think.<locals>.<genexpr>  s`      
zz!} 8!%Aw!76<<!G 8LTZZ]73Q77   A.A.-A.@   c              3     >#    U  Hc  nTR                   U   (       d  M  TR                   U   S    TR                  :w  d  M;  [        R                  TR                   U   S   S5      v   Me     g7fr`   ra   rb   s     r%   re   rf     s`      
zz!} 8!%Aw!76<<!G 8LTZZ]73Q77rg      accept_drawdecline_drawdecline_undoNsquare_r)   _r*   )promotion_pendingdraw_offer_fromidsumrangeundo_request_fromcurrent_playerr\   )r;   rU   my_materialopp_materialr<   s   ``   r%   	bot_thinkry     s     4 4		 A 
2Y
 

  
2Y
 

 ++ $"8"8FII"Ef$$'DfaT
|44r'   )r!   intr"   strreturnrz   )r;   'ChessGame'r<   dictr"   r{   r|   rz   )r;   r}   rU   'ChessPlayer'r|   zdict | None)r;   r}   rU   r   r|   z
str | None)__doc__
__future__r   typingr   r9   r;   r   r   r0   
PAWN_TABLEKNIGHT_TABLEBISHOP_TABLE
ROOK_TABLEQUEEN_TABLEKING_TABLE_MIDGAMEr3   r&   rI   r\   ry   r    r'   r%   <module>r      s    ; "   , A
FAFAFA
FAFA H ;|$(r'   