Skip to content

Instantly share code, notes, and snippets.

@carymrobbins
Last active January 8, 2024 15:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save carymrobbins/b3d327bf5179280d4fa768e309b0224e to your computer and use it in GitHub Desktop.
Save carymrobbins/b3d327bf5179280d4fa768e309b0224e to your computer and use it in GitHub Desktop.
Render a playing card as a unicode string in Python πŸ‚‘ πŸ‚½
from collections import namedtuple
from enum import Enum
class Suit(Enum):
Spades, Hearts, Diamonds, Clubs = range(4)
class Rank(Enum):
Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King = range(1,14)
class Card(namedtuple('Card', 'rank suit')):
def render(self):
offset = self.suit.value * 16
offset += self.rank.value
if self.rank.value >= Rank.Queen.value:
offset += 1
return bytes([0xf0, 0x9f, 0x82, 0xa0 + offset]).decode('utf8')
# >>> print(Card(Rank.Ace, Suit.Spades).render())
# πŸ‚‘
# >>> print(Card(Rank.Queen, Suit.Hearts).render())
# πŸ‚½
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment