
    h0                         S SK r S SKrS SKrS SKJr  S SKJr  S r/ SQr	S r
S rS rS	 rS
 rS rS rS r " S S\R$                  5      rg)    N)spaces)DependencyNotInstalledc                 8    [        X:  5      [        X:  5      -
  $ N)float)abs     [/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/envs/toy_text/blackjack.pycmpr   
   s    <%,&&    )                        	   
   r   r   r   c                 >    [        U R                  [        5      5      $ r   )intchoicedeck	np_randoms    r
   	draw_cardr      s    y%&&r   c                 .    [        U 5      [        U 5      /$ r   )r   r   s    r
   	draw_handr      s    i )I"677r   c                 N    [        SU ;   =(       a    [        U 5      S-   S:*  5      $ )Nr   r      )r   sumhands    r
   
usable_acer%      s#    qDy1SY^r122r   c                 T    [        U 5      (       a  [        U 5      S-   $ [        U 5      $ )Nr   )r%   r"   r#   s    r
   sum_handr'      s%    $4y2~t9r   c                     [        U 5      S:  $ )Nr!   )r'   r#   s    r
   is_bustr)   $   s    D>Br   c                 <    [        U 5      (       a  S$ [        U 5      $ Nr   )r)   r'   r#   s    r
   scorer,   (   s    118D>1r   c                 "    [        U 5      SS/:H  $ )Nr   r   )sortedr#   s    r
   
is_naturalr/   ,   s    $<Ar7""r   c                 L    [        U 5      nSU ;   a  US-   S::  a  US-   S4$ US4$ )z
Helper to compute both sum with and without ace counted as 11,
and whether the hand has a usable ace.
Returns (effective_sum, usable_ace: int)
r   r   r!   r   )r"   )r$   ss     r
   _hand_sum_and_usable_acer2   0   s5     	D	ADyQVr\2vqya4Kr   c                      ^  \ rS rSrSrSS/SS.rSS\S-  4S	 jjrS
 rS r	  SS\
S-  S\S-  4U 4S jjjrS rS rSrU =r$ )BlackjackEnv<   u  
Blackjack is a card game where the goal is to beat the dealer by obtaining cards
that sum to closer to 21 (without going over 21) than the dealers cards.

## Description
The game starts with the dealer having one face up and one face down card,
while the player has two face up cards. All cards are drawn from an infinite deck
(i.e. with replacement).

The card values are:
- Face cards (Jack, Queen, King) have a point value of 10.
- Aces can either count as 11 (called a 'usable ace') or 1.
- Numerical cards (2-10) have a value equal to their number.

The player has the sum of cards held. The player can request
additional cards (hit) until they decide to stop (stick) or exceed 21 (bust,
immediate loss).

After the player sticks, the dealer reveals their facedown card, and draws cards
until their sum is 17 or greater. If the dealer goes bust, the player wins.

If neither the player nor the dealer busts, the outcome (win, lose, draw) is
decided by whose sum is closer to 21.

This environment corresponds to the version of the blackjack problem
described in Example 5.1 in Reinforcement Learning: An Introduction
by Sutton and Barto [<a href="#blackjack_ref">1</a>].

## Action Space
The action shape is `(1,)` in the range `{0, 1}` indicating
whether to stick or hit.

- 0: Stick
- 1: Hit

## Observation Space
The observation consists of a 3-tuple containing: the player's current sum,
the value of the dealer's one showing card (1-10 where 1 is ace),
and whether the player holds a usable ace (0 or 1).

The observation is returned as `(int(), int(), int())`.

## Starting State
The starting state is initialised with the following values.

| Observation               | Values         |
|---------------------------|----------------|
| Player current sum        |  4, 5, ..., 21 |
| Dealer showing card value |  1, 2, ..., 10 |
| Usable Ace                |  0, 1          |

## Rewards
- win game: +1
- lose game: -1
- draw game: 0
- win game with natural blackjack:
+1.5 (if <a href="#nat">natural</a> is True)
+1 (if <a href="#nat">natural</a> is False)

## Episode End
The episode ends if the following happens:

- Termination:
1. The player hits and the sum of hand exceeds 21.
2. The player sticks.

An ace will always be counted as usable (11) unless it busts the player.

## Information

No additional information is returned.

## Arguments

```python
import gymnasium as gym
gym.make('Blackjack-v1', natural=False, sab=False)
```

<a id="nat"></a>`natural=False`: Whether to give an additional reward for
starting with a natural blackjack, i.e. starting with an ace and ten (sum is 21).

<a id="sab"></a>`sab=False`: Whether to follow the exact rules outlined in the book by
Sutton and Barto. If `sab` is `True`, the keyword argument `natural` will be ignored.
If the player achieves a natural blackjack and the dealer does not, the player
will win (i.e. get a reward of +1). The reverse rule does not apply.
If both the player and the dealer get a natural, it will be a draw (i.e. reward 0).

## References
<a id="blackjack_ref"></a>[1] R. Sutton and A. Barto, “Reinforcement Learning:
An Introduction” 2020. [Online]. Available: [http://www.incompleteideas.net/book/RLbook2020.pdf](http://www.incompleteideas.net/book/RLbook2020.pdf)

## Version History
* v1: Fix the natural handling in Blackjack
* v0: Initial version release
human	rgb_arrayr   )render_modes
render_fpsNrender_modec                    [         R                  " S5      U l        [         R                  " [         R                  " S5      [         R                  " S5      [         R                  " S5      45      U l        X l        X0l        Xl        g )Nr          )r   Discreteaction_spaceTupleobservation_spacenaturalsabr:   )selfr:   rB   rC   s       r
   __init__BlackjackEnv.__init__   s]    "OOA.!'__R &//""5vq7IJ"
  &r   c                    U R                   R                  U5      (       d   eU(       aT  U R                  R                  [	        U R
                  5      5        [        U R                  5      (       a  SnSnGO(SnSnGO"Sn[        U R                  5      S:  aI  U R                  R                  [	        U R
                  5      5        [        U R                  5      S:  a  MI  [        [        U R                  5      [        U R                  5      5      nU R                  (       a7  [        U R                  5      (       a  [        U R                  5      (       d  SnODU R                  (       d3  U R                  (       a"  [        U R                  5      (       a  US:X  a  SnU R                  S:X  a  U R                  5         U R!                  5       X2S0 4$ )	NTg      Fg           g      ?      ?r6   )r?   containsplayerappendr   r   r)   r'   dealerr   r,   rC   r/   rB   r:   render_get_obs)rD   action
terminatedrewards       r
   stepBlackjackEnv.step   sA     ))&1111KKy89t{{##!
"
J4;;'",""9T^^#<= 4;;'",t{{+U4;;-?@FxxJt{{33Jt{{<S<SHHLLt{{++cM w&KKM}}E2==r   c                 R    [        U R                  5      u  pXR                  S   U4$ r+   )r2   rK   rM   )rD   
player_sumplayer_usable_aces      r
   rO   BlackjackEnv._get_obs   s'    (@(M%
KKN,=>>r   seedoptionsc                   > [         TU ]  US9  [        U R                  5      U l        [        U R                  5      U l        U R                  5       u  p4n/ SQnU R                  R                  U5      U l        US:X  a  SU l	        O9US:X  a#  U R                  R                  / SQ5      U l	        O[        U5      U l	        U R                  S:X  a  U R                  5         U R                  5       0 4$ )N)rY   )CDHSr   Ar   )JQKr6   )superresetr   r   rM   rK   rO   r   dealer_top_card_suitdealer_top_card_value_strstrr:   rN   )rD   rY   rZ   _dealer_card_valuesuits	__class__s         r
   re   BlackjackEnv.reset   s    
 	4 //"&--/a$$(NN$9$9%$@!!-0D*"$-1^^-B-B?-SD*-01B-CD*w&KKM}}""r   c           
      	  ^^^ U R                   cG  U R                  c   e[        R                  R	                  SU R                  R
                   S35        g  SS KmU R                  5       u  p#nSu  pVUS-  m[        TS-  S-  5      mUS	-  nS
nSn	[        U S5      (       d  TR                  5         U R                   S:X  a<  TR                  R                  5         TR                  R                  XV45      U l        O1TR                   R                  5         TR#                  XV45      U l        [        U S5      (       d  TR$                  R'                  5       U l        U R                  R+                  U5        U4S jn
U4S jnU" [,        R.                  R1                  SS5      US-  5      nUR3                  S[5        U5      -   SU	5      nU R                  R7                  XU45      nUUU4S jnU" U
" [,        R.                  R1                  SU R8                   U R:                   S35      5      5      nU R                  R7                  UUS-  T-
  US-  -
  UR<                  U-   45      nU" U
" [,        R.                  R1                  SS5      5      5      nU R                  R7                  UUS-  US-  -   UR<                  U-   45        UR3                  SSU	5      nU R                  R7                  UUUR<                  SU-  -   45      nU" [,        R.                  R1                  SS5      US-  5      nUR3                  [5        U5      SU	5      nU R                  R7                  UUS-  UR?                  5       S-  -
  UR<                  U-   45      nU(       aX  UR3                  SSU	5      nU R                  R7                  UUS-  UR?                  5       S-  -
  UR<                  US-  -   45        U R                   S:X  a]  TR@                  RC                  5         TR                  RE                  5         U R(                  RG                  U RH                  S   5        g [J        RL                  " [J        RN                  " TRP                  RS                  U R                  5      5      S S!9$ ! [         a  n[        S5      UeS nAff = f)"NzYou are calling render method without specifying any render mode. You can specify the render_mode at initialization, e.g. gym.make("z", render_mode="rgb_array")r   z@pygame is not installed, run `pip install "gymnasium[toy-text]"`)iX  i  r            )r   c   $   )   rt   rt   screenr6   clockc                    > [         R                  R                  [        5      nTR                  R                  [         R                  R                  X5      5      nU$ r   )ospathdirname__file__imageloadjoin)ry   cwdr|   pygames      r
   	get_image&BlackjackEnv.render.<locals>.get_image  s;    ''//(+CLL%%bggll3&=>ELr   c                    > [         R                  R                  [        5      nTR                  R                  [         R                  R                  X 5      U5      nU$ r   )rx   ry   rz   r{   fontFontr~   )ry   sizer   r   r   s       r
   get_font%BlackjackEnv.render.<locals>.get_font  s=    ''//(+C;;##BGGLL$;TBDKr   r   zMinecraft.ttf   zDealer: Tc                 @   > TR                   R                  U TT45      $ r   )	transformscale)card_imgcard_img_heightcard_img_widthr   s    r
   scale_card_img+BlackjackEnv.render.<locals>.scale_card_img*  s!    ##))(^_4UVVr   imgz.pngr   zCard.pngPlayerrI   r   z
usable acer9   )r   r   r   )axes)*r:   specgymloggerwarnidr   ImportErrorr   rO   r   hasattrinitdisplayset_moderu   r   SurfacetimeClockrv   fillrx   ry   r~   rN   rh   blitrf   rg   bottom	get_widtheventpumpupdatetickmetadatanp	transposearray	surfarraypixels3d)rD   erV   rj   r%   screen_widthscreen_heightspacingbg_colorwhiter   r   
small_fontdealer_textdealer_text_rectr   dealer_card_imgdealer_card_recthidden_card_imgplayer_textplayer_text_rect
large_fontplayer_sum_textplayer_sum_text_rectusable_ace_textr   r   r   s                            @@@r
   rN   BlackjackEnv.render   sd   #99(((JJOO""&)),,/JL
 	 59MMO1
z&.#'1,_s2S892%tX&&KKM7*##%$nn55|6ST  "$nnl-JKtW%%**,DJ"	
	
 GGLL1=B3F

 !''.//u
  ;;++K79KL	W )001$2P2P1QQUV
  ;;++!N2W\A '''1
 )277<<z3R)ST!GqL0 '''1	
 !''$>;;++'#3#:#:S7]#JK
 bggll6?C]VWEWX
$++C
OT5I#{{//!O$=$=$?1$DD '''1 
 (//dEJOKK A%(A(A(Cq(HH(//'Q,> w&LLNN!!#JJOODMM,78<<))224;;?@y O  	(R	s   R3 3
S=S		Sc                     [        U S5      (       a/  SS KnUR                  R                  5         UR                  5         g g )Nru   r   )r   r   r   quit)rD   r   s     r
   closeBlackjackEnv.closeg  s0    4""NN!KKM	 #r   )r?   rv   rM   rf   rg   rB   rA   rK   r:   rC   ru   )NFF)NN)__name__
__module____qualname____firstlineno____doc__r   rh   rE   rS   rO   r   dictre   rN   r   __static_attributes____classcell__)rl   s   @r
   r4   r4   <   sv    _D !+.H
'C$J '>@?  ##Dj# # #2un r   r4   )rx   numpyr   	gymnasiumr   r   gymnasium.errorr   r   r   r   r   r%   r'   r)   r,   r/   r2   Envr4    r   r
   <module>r      sZ    	    2'
 3'832#	p377 pr   