best way to implement a deck for a card game in python

前端 未结 5 412
执念已碎
执念已碎 2020-12-31 05:06

What is the best way to store the cards and suits in python so that I can hold a reference to these values in another variable?

For example, if I have a list called

5条回答
  •  心在旅途
    2020-12-31 05:44

    Poker servers tend to use a 2-character string to identify each card, which is nice because it's easy to deal with programmatically and just as easy to read for a human.

    >>> import random
    >>> import itertools
    >>> SUITS = 'cdhs'
    >>> RANKS = '23456789TJQKA'
    >>> DECK = tuple(''.join(card) for card in itertools.product(RANKS, SUITS))
    >>> hand = random.sample(DECK, 5)
    >>> print hand
    ['Kh', 'Kc', '6c', '7d', '3d']
    

    Edit: This is actually straight from a poker module I wrote to evaluate poker hands, you can see more here: http://pastebin.com/mzNmCdV5

提交回复
热议问题