Last active
January 8, 2024 15:50
-
-
Save carymrobbins/b3d327bf5179280d4fa768e309b0224e to your computer and use it in GitHub Desktop.
Render a playing card as a unicode string in Python π‘ π½
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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