a .˜eeã@sGdd„dƒZdS)c@súeZdZdZdZedƒZgd¢Zee e eƒeƒƒZ ddddd œZ d Z d d d ddœZddgZedd„ƒZedd„ƒZedd„ƒZedd„ƒZedd„ƒZedd„ƒZedd„ƒZedd„ƒZed d!„ƒZed"d#„ƒZed$d%„ƒZed&d'„ƒZed(d)„ƒZd*S)+ÚCardaÎ Static class that handles cards. We represent cards as 32-bit integers, so there is no object instantiation - they are just ints. Most of the bits are used, and have a specific meaning. See below: Card: bitrank suit rank prime +--------+--------+--------+--------+ |xxxbbbbb|bbbbbbbb|cdhsrrrr|xxpppppp| +--------+--------+--------+--------+ 1) p = prime number of rank (deuce=2,trey=3,four=5,...,ace=41) 2) r = rank of card (deuce=0,trey=1,four=2,five=3,...,ace=12) 3) cdhs = suit of card (bit turned on based on suit of card) 4) b = bit turned on depending on rank of card 5) x = unused This representation will allow us to do very important things like: - Make a unique prime prodcut for each hand - Detect flushes - Detect straights and is also quite performant. Z 23456789TJQKAé ) ééééé réééééé%é)éréé)ÚsÚhÚdÚcZ xshxdxxxcuâ™ uâ¤u♦u♣)rrrrc CsZ|d}|d}tj|}tj|}tj|}d|>d>}|d>}|d>}||B|B|BS)zœ Converts Card string to binary integer representation of card, inspired by: http://www.suffecool.net/poker/evaluator.html éréé r)rÚCHAR_RANK_TO_INT_RANKÚCHAR_SUIT_TO_INT_SUITÚPRIMES) ÚstringZ rank_charZ suit_charÚrank_intÚsuit_intZ rank_primeZbitrankZsuitZrank©rú7/Users/seanglover/Development/deuces-rs/deuces3/card.pyÚnew1s    zCard.newcCs(t |¡}t |¡}tj|tj|S)N)rÚ get_rank_intÚ get_suit_intÚ STR_RANKSÚINT_SUIT_TO_CHAR_SUIT)Úcard_intrrrrr Ú int_to_strEs  zCard.int_to_strcCs |d?d@S)Nrér©r&rrr r"KszCard.get_rank_intcCs |d?d@S)Nrr(rr)rrr r#OszCard.get_suit_intcCs |d?d@S)Nriÿrr)rrr Úget_bitrank_intSszCard.get_bitrank_intcCs|d@S)Né?rr)rrr Ú get_primeWszCard.get_primecCs"g}|D]}| t |¡¡q|S)z‹ Expects a list of cards as strings and returns a list of integers of same length corresponding to those strings. )Úappendrr!)Z card_strsZbhandrrrr Úhand_to_binary[szCard.hand_to_binarycCsd}|D]}||d@9}q|S)z; Expects a list of cards in integer form. réÿr)Ú card_intsÚproductrrrr Úprime_product_from_handfszCard.prime_product_from_handcCs.d}tjD]}|d|>@r |tj|9}q |S)aÞ Returns the prime product using the bitrank (b) bits of the hand. Each 1 in the sequence is converted to the correct prime and multiplied in. Params: rankbits = a single 32-bit (only 13-bits set) integer representing the ranks of 5 _different_ ranked cards (5 of 13 bits are set) Primarily used for evaulating flushes and straights, two occasions where we know the ranks are *ALL* different. Assumes that the input is in form (set bits): rankbits +--------+--------+ |xxxbbbbb|bbbbbbbb| +--------+--------+ r)rÚ INT_RANKSr)Zrankbitsr1Úirrr Úprime_product_from_rankbitsrs   z Card.prime_product_from_rankbitscCsnt|ƒdd…ddd…}td dgd¡dƒ}tt|ƒƒD]}||||t|dƒ<q>| ¡d |¡S) zƒ For debugging purposes. Displays the binary number as a human readable string in groups of four digits. rNéÿÿÿÿÚz0000 rZ0000r)ÚbinÚlistÚjoinÚrangeÚlenÚintÚreverse)r&ZbstrÚoutputr4rrr Ú int_to_binaryŒs zCard.int_to_binarycCs€d}zddlm}d}Wnty*Yn0t |¡}t |¡}tj|}|rb|tjvrb||dƒ}tj|}d|d|dS) z' Prints a single card Fr)ÚcoloredTÚredz [ ú z ] ) Z termcolorrAÚ ImportErrorrr#r"Ú PRETTY_SUITSÚ PRETTY_REDSr$)r&ÚcolorrArrrÚrrrr Úint_to_pretty_strœs       zCard.int_to_pretty_strcCstt |¡ƒdS)z3 Expects a single integer as input N)ÚprintrrIr)rrr Úprint_pretty_card¸szCard.print_pretty_cardcCs`d}tt|ƒƒD]B}||}|t|ƒdkr@|t |¡d7}q|t |¡d7}qt|ƒdS)z: Expects a list of cards in integer form. rCrú,N)r;r<rrIrJ)r0r?r4rrrr Úprint_pretty_cards¿szCard.print_pretty_cardsN)Ú__name__Ú __module__Ú __qualname__Ú__doc__r$r;r3rÚdictÚzipr9rrr%rErFÚ staticmethodr!r'r"r#r*r,r.r2r5r@rIrKrMrrrr rsVüü            rN)rrrrr Ú