Skip to content

Instantly share code, notes, and snippets.

@NotTheEconomist
Created April 14, 2016 17:54
Show Gist options
  • Save NotTheEconomist/74d2369f144852141054b9cd11195789 to your computer and use it in GitHub Desktop.
Save NotTheEconomist/74d2369f144852141054b9cd11195789 to your computer and use it in GitHub Desktop.
from enum import Enum
Ranks = Enum("Ranks", "Ace Two Three Four Five Six Seven Eight "
"Nine Ten Jack Queen King")
Suits = Enum("Suits", "Diamonds Clubs Hearts Spades")
class Card(object):
__slots__ = ('_rank', '_suit')
# can be memory saving, especially if you're creating 6+ decks of cards for your shoe, e.g. 300+ cards.
# this can *definitely* be seen as premature optimization, though! Chances are you'll never need to implement __slots__
def __init__(self, rank, suit):
"""Card represents each card in the shoe.
`rank` and `suit` allow any of the following:
* An Enum instance (e.g. Ranks["Ace"])
* A number in the Enum (1-13 for ranks, 1-4 for suits)
* A name in the Enum ("Ace", "Two", "Hearts", etc...)
"""
if isinstance(rank, Ranks):
self._rank = rank
elif isinstance(rank, int):
self._rank = Ranks(rank)
else:
self._rank = Ranks[rank]
if isinstance(suit, Suits):
self._suit = suit
elif isinstance(rank, int):
self._suit = Ranks(rank)
else:
self._rank = Ranks[rank]
@property
def rank(self):
return self._rank.name
@property
def suit(self):
return self._suit.name
@property
def value(self):
return min(self._rank.value, 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment