
    MgJ                         S SK r S SKrS SKrS SKrS SKJr  S SKJrJr  \\\   /\4   r	\\\   /\4   r
S\\   S\S\4S jrS\\   S\S\4S	 jrS\\   S\4S
 jrS\\   S\4S jrS\\   S\4S jrS\\   S\4S jrS\\   S\4S jrS\\   S\4S jrS\\   S\4S jrS\\   S\4S jr " S S5      r " S S\5      r " S S5      r " S S5      r " S S\5      r " S S\5      r " S S5      r\" S 5       " S! S"5      5       rg)#    N)plugin)CallableListhandfacereturnc                 (    XR                  U5      -  $ )zg
In the same face categories only the occurrences of the specified dice face
are counted in the score.
)count)r   r   s     ;/Users/admin/workspace/ai/Jarvis/jarviscli/plugins/balut.pycalc_same_face_scorer      s    
 **T"""    fieldsc                 2    SnSn[        U 5      X-  :  a  U$ g)a  
In the same face categories in order to get the 2 points of the category,
the total score of the 4 fields in the scoresheet should indicate that there
was at least 13 occurrences of the specified dice face in the total rolls
that was settled in that category.
      r   sum)r   r   SAME_FACE_POINTSMIN_REQUIRED_OCCURRENCESs       r   calc_same_face_pointsr      s(     !
6{d55r   c                 T    / SQn/ SQn[        U 5      nX1:X  d  X2:X  a  [        U5      $ g)z
In the Straight category the score is equivalent to the sum of all the dice
faces in the hand, but only if the hand contains either a small or a large
straight (consecutive dice faces starting from 1 or 2 respectively),
otherwise the score is 0.
)   r            )r   r   r   r      r   )sortedr   )r   small_straightlarge_straightsorted_hands       r   calc_straight_scorer!   $   s0     %N$N,K$(E;r   c                     SnSU ;  a  U$ g)z
In the Straight category the in order to get the 4 points of the category
all of its 4 fields in the scoresheet should contain straights, which is
equivalent to all of the 4 fields having score.
r   r    )r   STRAIGHT_POINTSs     r   calc_straight_pointsr%   4   s     Or   c                     SnSn[        S5       Vs/ s H  o0R                  US-   5      PM     nnX;   a  X$;   a  [        U 5      $ gs  snf )z
In the Full House category the score is equivalent to the sum of all the dice
faces in the hand, but only if the hand contains a full house (two of a kind
& three of a kind), otherwise the score is 0.
r   r   r   r   r   )ranger
   r   )r   TWO_OF_A_KINDTHREE_OF_A_KINDiface_countss        r   calc_full_house_scorer,   A   sN     MO.3Ah7h::a!e$hK7#(F4y 8s   Ac                     SnSU ;  a  U$ g)z
In the Full House category in order to get the 3 points of the category
all of its 4 fields in the scoresheet should contain full houses, which
is equivalent to all of the 4 fields having score.
r   r   r#   )r   FULL_HOUSE_POINTSs     r   calc_full_house_pointsr/   P   s       r   c                     [        U 5      $ )z^
In the Choice category the score is equivalent to the sum of all the dice faces
in the hand.
r   r   s    r   calc_choice_scorer2   ]   s    
 t9r   c                 .    SnSn[        U 5      U:  a  U$ g)z
In the Choice category in order to get the 2 points of the category is
the total score of the 4 fields in the scoresheet should be greater than
or equal to 100.
r   d   r   r   )r   CHOICE_POINTSMIN_REQUIRED_SCOREs      r   calc_choice_pointsr7   e   s%     M
6{((r   c                 T    Sn[        [        U 5      5      S:X  a  U[        U 5      -   $ g)z
In the Balut category the score is equivalent to the sum of all the dice
faces in the hand plus 20, but only if the hand contains a Balut (five of
a kind), otherwise the score is 0.
   r   r   )lensetr   )r   BALUT_BASE_SCOREs     r   calc_balut_scorer=   s   s,     
3t9~#d)++r   c                 V    Sn[        U  Vs/ s H  o"S:w  d  M
  UPM     sn5      $ s  snf )z
In the Balut category you get 2 points for every Balut that is settled in
any of its 4 fields in the scoresheet, which equivalent to a field contain
score not equal to 0.
r   r   r   )r   POINTS_PER_BALUTfields      r   calc_balut_pointsrA      s,     fCfU
 fCDDCs   	&&c                   v    \ rS rSrS\S\S\SS4S jr\S\4S j5       r	\S\4S	 j5       r
\S\4S
 j5       rSrg)Category   labelscore_calculatorpoints_calculatorr   Nc                 (    Xl         X l        X0l        g N)_label_score_calculator_points_calculator)selfrE   rF   rG   s       r   initCategory.init   s    !1"3r   c                     U R                   $ rI   )rJ   rM   s    r   rE   Category.label   s    {{r   c                     U R                   $ rI   )rK   rQ   s    r   
calc_scoreCategory.calc_score   s    %%%r   c                     U R                   $ rI   )rL   rQ   s    r   calc_pointsCategory.calc_points   s    &&&r   )rJ   rL   rK   )__name__
__module____qualname____firstlineno__strScoreCalculatorPointsCalculatorrN   propertyrE   rT   rW   __static_attributes__r#   r   r   rC   rC      sw    4# 4 4/4484 s   &O & & '- ' 'r   rC   c                       \ rS rSrSrg)InvalidCategoryValueError   r#   NrY   rZ   r[   r\   ra   r#   r   r   rc   rc          r   rc   c                       \ rS rSrSrSrSrS\\   S\\\	      SS4S	 jr
S
\	S\4S jrS\	4S jrS
\	S\	4S jrS
\	S\	S\\	   SS4S jrS
\	S\\	   SS4S jrSS jrS\\	   SS4S jrS\	S\	4S jrS\	4S jrSrg)
Scoresheet   r      
categoriesscoresheet_matrixr   Nc                     Xl         X l        g rI   _categories_scoresheet_matrix)rM   rl   rm   s      r   rN   Scoresheet.init   s    %"3r   categoryc                     SUs=::  a  U R                   ::  d  O  [        S5      eU R                  U R                  US-
     ;  a  [        S5      eg)Nr   zBOops! Category should be an integer between 1 and 7. Try again...
z>Oops! The category you selected is already full. Try again...
T)NUM_OF_CATEGORIESrc   EMPTY_FIELD_PLACEHOLDERrq   )rM   rs   s     r   _validate_categoryScoresheet._validate_category   s^    H6 6 66+UW W ''t/F/FxRS|/TT+QS S r   c                       [        [        S5      5      nU R                  U5      (       a  U$  M0  ! [         a  n[	        [        U5      5         S nAN%S nAf[         a    [	        S5         N?f = f)Nz/Select the category to settle the score (1-7): z2Oops! Category should be an integer. Try again...
)intinputrw   rc   printr]   
ValueError)rM   rs   errors      r   select_category_to_settle_score*Scoresheet.select_category_to_settle_score   st    	MOPQ  **844#O 5  - "c%j!! MKLMs   +2 
A0AA0/A0c                 r    [        U R                  US-
     5       H  u  p#X0R                  :X  d  M  Us  $    g Nr   )	enumeraterq   rv   )rM   rs   r@   field_scores       r   _find_first_empty_field"Scoresheet._find_first_empty_field   s5    "+D,C,CHqL,Q"RE::: #Sr   r@   r   c                 l    U R                   US-
     R                  U5      U R                  US-
     U'   g r   )rp   rT   rq   )rM   rs   r@   r   s       r   _settle_score_to_field!Scoresheet._settle_score_to_field   s8    X\*55d; 	1-e4r   c                 J    U R                  U5      nU R                  XU5        g rI   )r   r   )rM   rs   r   r@   s       r   settle_scoreScoresheet.settle_score   s"    ,,X6##HT:r   c           
      l   [        S5        [        SS SS SS SS S	S S
S 35        [        U R                  5       Hh  u  nu  p#pEUS:X  a  SOUnUS:X  a  SOUnUS:X  a  SOUnUS:X  a  SOUn[        US-   S U R                  U   R                  S US US US US 35        Mj     [        S5        g )Nz7
===================== Scoresheet =====================  <3rC    <12Field 1 ^10Field 2Field 3Field 4rk   -r   z7======================================================
)r|   r   rq   rp   rE   )rM   r*   field_1field_2field_3field_4s         r   displayScoresheet.display   s    HICD))D)94 4 040@B 	C 8AAXAX7Y3A3'$]cG$]cG$]cG$]cGQqSId..q177=gd^TN74.@ A 8Z 	HIr   c                    [        S5        [        SS SS SS SS S	S S
S SS 35        [        U R                  5       H  u  nu  p4pVUS:X  a  SOUnUS:X  a  SOUnUS:X  a  SOUnUS:X  a  SOUnUS:X  a"  SU R                  U   R	                  U5       3nOSn[        US-   S U R                  U   R
                  S US US US US US 35        M     [        S5        g )NzC
=========================== Scoresheet ===========================r   r   rC   r   r   r   r   r   r   z
Score Gainrk   r   +Fullr   zC==================================================================
)r|   r   rq   rp   rT   rE   )rM   r   r*   r   r   r   r   
score_gains           r   display_with_scoreScoresheet.display_with_score   s-   TUCD))D)94 4 040@d@SU 	V 8AAXAX7Y3A3'$]cG$]cG$]cG$]cG#~ !1!1!!4!?!?!E FG
#
QqSId..q177=gd^TN74.z$>OQ R 8Z 	TUr   total_scorec                 &   SUs=::  a  S::  a   g  SUs=::  a  S::  a   g  SUs=::  a  S::  a   g  S	Us=::  a  S
::  a   g  SUs=::  a  S::  a   g  SUs=::  a  S::  a   g  SUs=::  a  S::  a   g  SUs=::  a  S::  a   g  SUs=::  a  S::  a   g  g g )Nr   i+  ,  i]  rk   i^  i  i  i  r   i  i  r   i  i%  r   i&  iW  r   iX  i  r   i  i,  r   r#   )rM   r   s     r   _calc_total_score_points#Scoresheet._calc_total_score_points   s    "s" #K&3& 'K&3& 'K&3& 'K&3& 'K&3& 'K&3& 'K&3& 'K&3& 'r   c                     SnSn[        U R                  5       H3  u  p4U[        U5      -  nX R                  U   R	                  U5      -  nM5     X R                  U5      -  nU$ )Nr   )r   rq   r   rp   rW   r   )rM   r   total_pointsrs   r   s        r   rW   Scoresheet.calc_points  sl     )$*A*A BH3v;&K,,X6BB6JJL !C 	55kBBr   ro   r   N)rY   rZ   r[   r\   NUM_OF_FIELDSru   rv   r   rC   rz   rN   boolrw   r   r   r   r   r   r   r   rW   ra   r#   r   r   rh   rh      s    M 4tH~ 4#DI4374
	3 	4 	M M  
<s <3 <d3i <TX <;S ;S	 ;d ;J VtCy VT V*C C (	S 	r   rh   c                   h    \ rS rSrS\S\SS4S jr\S\4S j5       rS\	\
   SS4S	 jrS\
4S
 jrSrg)Playeri  username
scoresheetr   Nc                     Xl         X l        g rI   )	_username_scoresheet)rM   r   r   s      r   rN   Player.init  s    !%r   c                     U R                   $ rI   )r   rQ   s    r   r   Player.username  s    ~~r   r   c                     U R                   R                  U5        U R                   R                  5       nU R                   R                  X!5        U R                   R	                  5         g rI   )r   r   r   r   r   )rM   r   rs   s      r   settle_score_to_scoresheet!Player.settle_score_to_scoresheet"  sP    ++D1##CCE%%h5  "r   c                 6    U R                   R                  5       $ rI   )r   rW   rQ   s    r   rW   Player.calc_points(  s    ++--r   )r   r   )rY   rZ   r[   r\   r]   rh   rN   r`   r   r   rz   r   rW   ra   r#   r   r   r   r     sZ    &S &j &T & #  #tCy #T #.S .r   r   c                       \ rS rSrSrg)InvalidDiceValueErrorr   r#   Nre   r#   r   r   r   r   ,  rf   r   r   c                       \ rS rSrSrg)PreserveHandi0  r#   Nre   r#   r   r   r   r   0  rf   r   r   c                       \ rS rSrSrSrS\\   SS4S jrSS jr	S	\
SS4S
 jrS\SS4S jrSS jrS\S\4S jrS\
S\\   4S jrSS jrS\SS4S jrS\\   SS4S jrS\\   SS4S jrS\\   SS4S jrSrg)Baluti4  r      r   r   Nc                     Xl         g rI   _hand)rM   r   s     r   rN   
Balut.init8  s    
r   c                    [         R                  " SR                  / SPSS PSS PSS PS	PS
S PSS PSS PS	PSS PSS PSS PS	PSS PSS PSS PS	PSS PSS PSS PS	PSS PSS PSS PS	PSS PSS PSS PS	PSS PSS PSS PSPSS PSS PS	PSS PSS  PS	PS!S PS"S  PS	PS#S PS$S% PS	PS&S PS'S% PS	PS(S PSS% PS	PS)S PSS% PS	PS*S PSS% PS	PS+S PS,S% PS	PS-S PS.S% PS/P5      5      n[        U5        g )0Nr   a  
        =========================== Instructions ===========================
        Balut is a dice game that consists of 28 rounds. In each round you
        can roll the dices up to 3 times and then you have to enter your
        roll in one of the 7 categories on the scoresheet. You must fill 4
        fields in each category. Once a field is settled it cannot be changed
        for the rest of the game. Each category has some requirements, that
        if they are satisfied you receive the points of the category.
        The object of the game is to maximize your number of points.

        Categories:
        1) Fours: The score for this category is the sum of 4 in your hand.
        2) Fives: The score for this category is the sum of 5 in your hand.
        3) Sixes: The score for this category is the sum of 6 in your hand.
        4) Straight: The score for this category is the sum of all the dice
           faces in your hand if the dice faces are consecutive (straight),
           otherwise it is zero.
        5) Full House: The score for this category is the sum of all the dice
           faces in your hand if you have both three of a kind and a pair (full
           house), otherwise it is zero.
        6) Choice: The score for this category is the sum of all the dice faces
           in your hand.
        7) Balut: The score for this category is 20 plus the sum of all the dice
           faces in your hand if you have five of a kind, otherwise it is zero.


        CATEGORYz <14REQUIREMENTSz <18zPOINTS AWARDEDz ^14z	
        FourszScore >= 522FiveszScore >= 65SixeszScore >= 78StraightzAll Straights4
Full HousezAll Full Houses3ChoicezScore >= 100r   zFor each Balutz


        zTOTAL SCOREz0-299z ^12z-2z ^16z300-349z-1z350-3990z ^17z400-4491z450-499z500-549z550-599z600-6495z650-8126zV
        ====================================================================
        )textwrapdedentjoinr|   )rM   instructionss     r   display_instructionsBalut.display_instructions;  s    .( .( ,	 .(4 
D5.(4 ).5.(4 0@.E5.(4F	5.(6 
7.(6 %T*7.(6 ,/t*7.(65	7.(8 
9.(8 %T*9.(8 ,/t*9.(85	9.(: 
;.(: %T*;.(: ,/t*;.(:5	;.(< 
D=.(< *$/=.(< 14Dz=.(<:	=.(> 
d?.(> .d3?.(> 58:?.(>>	?.(@ 
$A.(@ 't,A.(@ .1JA.(@7	A.(B 
C.(B (-C.(B /2$ZC.(B8	C.(F 
tG.(F .d3G.(F4	G.(H 
I.(H DkI.(H"	I.(J 
4K.(J dK.(J$	K.(L 
4M.(L T
M.(L#	M.(N 
4O.(N T
O.(N#	O.(P 
4Q.(P T
Q.(P#	Q.(R 
4S.(R T
S.(R#	S.(T 
4U.(T T
U.(T#	U.(V 
4W.(V T
W.(V#	W.(X 
4Y.(X T
Y.(X#	Y.( .` 	lr   r   c                     [        SU S35        [        U R                  5       H  u  p#[        SUS-    SU 35        M     [        5         g )N
z you have rolled:zDice r   z has a face of )r|   r   r   )rM   r   r*   r   s       r   display_handBalut.display_handn  sG    8*-./ ,GAE!a%v67 -r   dice_to_rollc                 R    [         R                  " SS5      U R                  US-
  '   g )Nr   r   )randomrandintr   )rM   r   s     r   	roll_diceBalut.roll_dicet  s     '-~~a';

<!#$r   c                 `    [        U R                  SS9 H  u  pU R                  U5        M     g )Nr   start)r   r   r   )rM   dice_s      r   roll_all_dicesBalut.roll_all_dicesw  s%     15GDNN4  6r   r   c                 L    SUs=::  a  U R                   ::  d  O  [        S5      eg)Nr   z=Oops! Dices should be integers between 1 and 5. Try again...
T)NUM_OF_DICESr   )rM   r   s     r   _validate_diceBalut._validate_dice{  s*    D-D---'PR Rr   selected_dicesc                     UR                  S5       Vs/ s H/  nU R                  [        U5      5      (       d  M$  [        U5      PM1     sn$ s  snf )N )splitr   rz   )rM   r   r   s      r   _convert_to_dices_to_reroll!Balut._convert_to_dices_to_reroll  sJ    &4&:&:3&? 3&?d&&s4y1 D	&? 3 	3 3s
   #AAc                 D     [        S5      R                  5       nUS:X  a
  [        5       eU R                  U5      nU H  nU R	                  U5        M     g ! [
         a  n[        [        U5      5         S nAO%S nAf[         a  n[        S5         S nAOS nAff = fM  )NzJSelect the dices to reroll seperated by space or press enter to continue: r   z-Oops! Dices should be integers. Try again...
)	r{   stripr   r   r   r   r|   r]   r}   )rM   r   dices_to_rerollr   r~   s        r   reroll_dicesBalut.reroll_dices  s    H!&$" %' 
 "R'&.("&"B"B>"R$3LNN<0 %4( "c%j!! HFGGH# s$   AA 
B"A;;BBBplayerc                 4   U R                  5         U R                  UR                  5        SnUS:  a9   U R                  5         U R                  UR                  5        US-  nUS:  a  M9  UR                  U R                  5        g ! [         a     M)  f = f)Nr   r   r   )r   r   r   r   r   r   r   )rM   r   rerollss      r   
play_roundBalut.play_round  s    &//*k!!#!!&//21	 k 	))$**5   s   0B	 	
BBmax_playersc                    [        U5      S:X  a  [        SUS   R                   S35        g [        U5      S:  aI  [        SSS9  [        USS9 H0  u  p#U[        U5      :w  a  S	OS
n[        SUR                   S3US9  M2     g g )Nr   z
Congratulations r   z you are the winner!z
There is a tie between:r   )endr   &r   r   )r:   r|   r   r   )rM   r  r*   r   	end_values        r   _display_winnerBalut._display_winner  s    {q &{1~'>'>&??STU!-B7&{!<	#$K(8#8Cd	&//*!,)< = "r   playersc                    [        S5      n/ nU HQ  nUR                  5       n[        UR                   SU 35        XR:  a  UnU/nM9  XR:X  d  M@  UR	                  U5        MS     [        U5      S:  a  U R                  U5        g g )Nz-infz's total points are: r   )floatrW   r|   r   appendr:   r	  )rM   r  
max_pointsr  r   player_pointss         r   display_resultsBalut.display_results  s    6]
F"..0MV__%%:=/JK)*
%h,""6*  w<!  - r   c                     U R                  5         [        R                  " U5      n[        [	        U5      U R
                  -  5       H  n[        U5      nU R                  U5        M!     U R                  U5        g rI   )	r   	itertoolscycler'   r:   NUM_OF_ROUNDSnextr  r  )rM   r  players_cycler   r   s        r   play
Balut.play  s`    !!#!0s7|d&8&889A-(FOOF# : 	W%r   r   r   )rY   rZ   r[   r\   r   r  r   rz   rN   r   r]   r   r   r   r   r   r   r   r   r  r	  r  r  ra   r#   r   r   r   r   4  s    LMc t 1fS T <c <d <!3 4 3# 3$s) 3H*6 6D 6=4< =D =.tF| . . &DL &T &r   r   balutc                       \ rS rSrS\\   4S jrS\4S jrS\S\\	   4S jr
S\S\\   S\\   4S jrS	\\	   S
\\   S\\   4S jrSS jrSrg)BalutPlugini  r   c           	         [        5       nUR                  S[        R                  " [        SS9[        R                  " [
        SS95        [        5       nUR                  S[        R                  " [        SS9[        R                  " [
        SS95        [        5       nUR                  S[        R                  " [        SS9[        R                  " [
        SS95        [        5       nUR                  S[        [        5        [        5       nUR                  S	[        [        5        [        5       nUR                  S
[        [        5        [        5       nUR                  S[        [        5        XX4XVU/$ )Nr   r   )r   r   r   r   r   r   r   r   r   )rC   rN   	functoolspartialr   r   r!   r%   r,   r/   r2   r7   r=   rA   )rM   foursfivessixesstraight
full_housechoicer  s           r   create_categoriesBalutPlugin.create_categories  s    


72;3!<	> 


72;3!<	> 


72;3!<	> :j"57KLZ
&;=STH/1CD


7,.?@ez5IIr   c                       [        [        S5      5      nUS::  a
  [        5       eU$ ! [         a    [        S5         Of = fMD  )NzNumber of players: r   z.Oops! Invalid number of players. Try again...
)rz   r{   r}   r|   )rM   num_of_playerss     r   read_num_of_playersBalutPlugin.read_num_of_players  sP    I!$U+@%A!B!Q&$,&%% IGHI s   %) A Ar*  c                 t    / n[        U5       H&  n[        SUS-    S35      nUR                  U5        M(     U$ )NzPlayer r   z username: )r'   r{   r  )rM   r*  	usernamesr*   r   s        r   read_usernamesBalutPlugin.read_usernames  sC    	~&Awqse;78HX& ' r   rl   c                 X   / n[        U5       H  n[        5       n[        [        R                  5       Vs/ s H;  n[        [        R                  5       Vs/ s H  n[        R                  PM     snPM=     nnUR                  X&5        UR                  U5        M     U$ s  snf s  snf rI   )r'   rh   ru   r   rv   rN   r  )rM   r*  rl   scoresheetsr   r   rm   s          r   create_scoresheetsBalutPlugin.create_scoresheets  s    ~&A#J z;;<!<A >C:C[C[=\]=\33=\]<  ! OOJ:z* '  ^!s   !B'B"1B'"B'r.  r2  c                     / n[        X5       H1  u  pE[        5       nUR                  XE5        UR                  U5        M3     U$ rI   )zipr   rN   r  )rM   r.  r2  r  r   r   r   s          r   create_playersBalutPlugin.create_players  s@    $'	$? HXFKK-NN6" %@ r   Nc                 X   U R                  5       nU R                  U5      nU R                  5       nU R                  X55      nU R	                  XF5      n[        5       nUR                  [        [
        R                  5       Vs/ s H  nSPM     snS9  UR                  U5        g s  snf )Nrk   r1   )
r+  r/  r'  r3  r7  r   rN   r'   r   r  )	rM   jarvisr   r*  r.  rl   r2  r  r  s	            r   __call__BalutPlugin.__call__  s    113''7	++-
--nI%%i=

U5+=+=%>?%>%>?
@

7 @s   B'r#   r   )rY   rZ   r[   r\   r   rC   r'  rz   r+  r]   r/  rh   r3  r   r7  r;  ra   r#   r   r   r  r    s    J4> J<IS IS T#Y  X+/
+;S	 j).26l	r   r  )r   r   r  r  r   typingr   r   rz   r^   r_   r   r   r!   r%   r,   r/   r2   r7   r=   rA   rC   r}   rc   rh   r   r   	Exceptionr   r   r  r#   r   r   <module>r?     s        ! DI;+,T#YK,- #tCy # # #$s) 3 3 d3i C  
c 
s 
S	 c 
49 
 
DI # tCy S 
49 
 
Ed3i EC E' '*	
 	q qh. .(	J 		9 	V& V&r P P Pr   