a e#@s(ddlZddlmZGdddeZdS)N)Cardc@seZdZdZdZdZdZdZdZdZ dZ d Z d Z ed ed ed edede de de de di Z dddddddddd ZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*S)+ LookupTablea4 Number of Distinct Hand Values: Straight Flush 10 Four of a Kind 156 [(13 choose 2) * (2 choose 1)] Full Houses 156 [(13 choose 2) * (2 choose 1)] Flush 1277 [(13 choose 5) - 10 straight flushes] Straight 10 Three of a Kind 858 [(13 choose 3) * (3 choose 1)] Two Pair 858 [(13 choose 3) * (3 choose 2)] One Pair 2860 [(13 choose 4) * (4 choose 1)] High Card + 1277 [(13 choose 5) - 10 straights] ------------------------- TOTAL 7462 Here we create a lookup table which maps: 5 card hand's unique prime product => rank in range [1, 7462] Examples: * Royal flush (best hand possible) => 1 * 7-5-4-3-2 unsuited (worst hand possible) => 7462 iBi?iIi i i)i& zStraight FlushzFour of a Kindz Full HouseZFlushZStraightzThree of a KindzTwo PairZPairz High Card) rrrr r r r r rcCs i|_i|_||dS)z* Calculates lookup tables N) flush_lookupunsuited_lookupflushes multiples)selfr9/Users/seanglover/Development/deuces-rs/deuces3/lookup.py__init__:szLookupTable.__init__c Csgd}g}|tdd}tdt|dD]4}t|}d}|D]}||AsDd}qD|r0||q0|d}|D]F}td|t |} td | ||j | <|d7}td |qvt|j t j d}|D] }t |} ||j | <|d7}q| ||d S) z Straight flushes and flushes. Lookup is done on 13 bit integer (2^13 > 7462): xxxbbbbb bbbbbbbb => integer hand index ) iiiii|>iZ0b11111rirTFzsf = zprime_product = zrank = N)%get_lexographically_next_bit_sequenceintrangelennextappendreverseprintrprime_product_from_rankbitsrrMAX_FULL_HOUSEstraight_and_highcards) rZstraight_flushesrgenifZnotSFZsfrank prime_productrrrrGs6         zLookupTable.flushescCsdtjd}|D] }t|}||j|<|d7}qtjd}|D] }t|}||j|<|d7}q>dS)zx Unique five card sets. Straights and highcards. Reuses bit sequences from flush calculations. rN)r MAX_FLUSHrr#rMAX_PAIR)rZ straightsZ highcardsr)sr*hrrrr%s       z"LookupTable.straight_and_highcardscCstttjddd}tjd}|D]R}t|dd}|||D].}tj|dtj|}||j |<|d7}qFq$tj d}|D]V}t|dd}|||D]2}tj|dtj|d}||j |<|d7}qqtj d}|D]r} t|dd}|| t |d} | D]B}|\} } tj| dtj| tj| }||j |<|d7}qqtjd}t |d} | D]v}|\}}t|dd}|||||D]>}tj|dtj|dtj|}||j |<|d7}qqztjd}|D]}t|dd}||t |d}|D]N}|\}}}tj|dtj|tj|tj|}||j |<|d7}q.qdS)zO Pair, Two Pair, Three of a Kind, Full House, and 4 of a Kind. rNr rr)rrr INT_RANKSrMAX_STRAIGHT_FLUSHlistremovePRIMESrMAX_FOUR_OF_A_KIND MAX_STRAIGHT itertools combinationsMAX_THREE_OF_A_KIND MAX_TWO_PAIR)rZbackwards_ranksr)r'ZkickerskproductZ pairranksZprrr&Zc1Zc2ZtpgentpZpair1Zpair2ZkickerZpairrankZkgenk1k2Zk3rrrrsh           "     &      zLookupTable.multiplescCs^t|d@}|D]&\}}|t|dt|dqWdn1sP0YdS)z- Writes lookup table to disk w, N)openZ iteritemswritestr)rtablefilepathr(Z prime_prodr)rrrwrite_table_to_disks zLookupTable.write_table_to_diskccsz||dBd}|t|| @|| @d?dB}|V||dBd}|t|| @|| @d?dB}|Vq:dS)z Bit hack from here: http://www-graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation Generator even does this in poker order rank so no need to sort when done! Perfect. rN)r)rbitstrrrrrs $$z1LookupTable.get_lexographically_next_bit_sequenceN)__name__ __module__ __qualname____doc__r1r5r$r+r6r9r:r, MAX_HIGH_CARDMAX_TO_RANK_CLASSRANK_CLASS_TO_STRINGrrr%rrIrrrrrrsH   MVr)r7 deuces3.cardrobjectrrrrrs