Thursday, February 11, 2021

Python Example Show Big2 Cards Order

Python Example: Show Big2 Cards Order



How to print out the big2 card order in Python?

> cards= list(range(52))
> cards.sort(key=lambda x:(x+12)%13*4+x//13)

[1, 14, 27, 40, 2, 15, 28, 41, 3, 16, 29, 42, 4, 17, 30, 43, 5, 18, 31, 44, 6, 19, 32, 45, 7, 20, 33, 46, 8, 21, 34, 47, 9, 22, 35, 48, 10, 23, 36, 49, 11, 24, 37, 50, 12, 25, 38, 51, 0, 13, 26, 39]

Diamond-23456789TJQKA Club-23456789TJQKA ...

The code id 1 mean D3, 14-C3, 2-D4...39-Spade2

The 2 lines of python code looks simple, but hard to read and understand. I will show you how to do it step by step.

Normal card


First I create a deck of normal cards (52 cards), assign an id for each card from 0..51. Starts from Diamond suit 0-10, JQKA, then Club, Heart and finally Spade. The id does not show the card order by value. The order by value should be D2,C2,H2,S2,... D3,C3…, DA(Diamond Ace), CA, HA, SA.

$ edit card.py
# poker card program

def get_card_name(card):
    ''' pass in the card value, return card name eg. D3-Diamond 3, ST-Spade 10, HA-Heart Ace '''
    suit= ''  # Diamond, Club, Heart or Spade
    value= 0
    if card>=0 and card<13:
        suit= 'Diamond'
    elif card>=13 and card<26:
        suit= 'Club'
    elif card>=26 and card<39:
        suit= 'Heart'
    else:
        suit= 'Spade'
    
    value= (card % 13) + 2
    if value==11:
        value= 'J'
    elif value==12:
        value= 'Q'
    elif value==13:
        value= 'K'
    elif value==14:
        value= 'A'
    else:
        pass
    
    return (suit,value)

def show_hand(cards):
    ''' show cards on hand '''
    for card in cards:
        print(get_card_name(card))

def get_card_value(card):
    ''' convert id 0..51 to value eg. D2=0, C2=1, H2= 2, S2=3, D3= 4 '''
    suit= card//13
    value= card%13
    return value*4+suit
get_card_name() shows a more readable card name:
> get_card_name(0) ⇒ ('Diamond',2)

show_hand() shows all the cards in a list:
> show_hand([0,8,23,24,44])
('Diamond', 2)
('Diamond', 10)
('Club', 'Q')
('Club', 'K')
('Spade', 7)

get_card_value() return the actual value or the card. eg. D2-0 C2-1,...
> cards= list(range(52))
> cards.sort(key=get_card_value)
> show_hand(cards)
('Diamond', 2)
('Club', 2)
('Heart', 2)
('Spade', 2)
('Diamond', 3)
...
('Heart', 'A')
('Spade', 'A')

Big2 card


In the big2 game, 2 is the largest value in a suit. So the order is Diamond 3456789TJQKA2. To sort the cards, you need a new function to replace the get_card_value().
$ edit big2_card
from card import show_hand

def get_big2_value(card):
    ''' D3=0 C3 H3 S3 D4 ... SA D2 C2 H2 A2 '''
    ''' id 1=D3 5=C3 9=H3 13=D4 '''
    suit= card//13
    value= (card+12)%13  # change 2 as the largest value
    return value*4+suit

cards.sort(key=get_big2_value)
show_hand(cards)  
  

⇒ [1, 14, 27, 40, 2, 15, 28, 41, 3, 16, 29, 42, 4, 17, 30, 43, 5, 18, 31, 44, 6, 19, 32, 45, 7, 20, 33, 46, 8, 21, 34, 47, 9, 22, 35, 48, 10, 23, 36, 49, 11, 24, 37, 50, 12, 25, 38, 51, 0, 13, 26, 39] (means [D3, C3, ... S2])

('Diamond', 3)
('Club', 3)
('Heart', 3)
('Spade', 3)
('Diamond', 4)
...


Use sort with Lambda



Lambda function format: f= lambda x: x*x. The lambda function only have 1 return expression, is this case it is x*x.
> f(5) ⇒ 25

> cards= list(range(52))
> # cards.sort(key=get_big2_value)
> cards.sort(key=lambda x:get_big2_value(x))

It also can be written as
> cards= list(range(52))
> cards.sort(key=lambda x:(x+12)%13*4+x//13)

Note: The expression evaluated from get_big2_value(), is hard to read. Referring to get_big2_value() should be easier.

No comments: