forked from coding-horror/basic-computer-games
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcard.py
More file actions
50 lines (37 loc) · 942 Bytes
/
card.py
File metadata and controls
50 lines (37 loc) · 942 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Card primitives: Suits, Ranks, and the Card class."""
from dataclasses import dataclass
from enum import Enum, IntEnum
class CardSuit(Enum):
"""Enumeration of card suits."""
CLUBS = 0
DIAMONDS = 1
HEARTS = 2
SPADES = 3
def __str__(self) -> str:
return self.name.capitalize()
class CardRank(IntEnum):
"""Card ranks from Two (0) to Ace (12)."""
TWO = 0
THREE = 1
FOUR = 2
FIVE = 3
SIX = 4
SEVEN = 5
EIGHT = 6
NINE = 7
TEN = 8
JACK = 9
QUEEN = 10
KING = 11
ACE = 12
def __str__(self) -> str:
if self.value <= 8: # TWO(0) through TEN(8)
return f" {str(self.value + 2)} "
return ("Jack", "Queen", "King", "Ace")[self.value - 9]
@dataclass(frozen=True)
class Card:
"""A single playing card."""
suit: CardSuit
rank: CardRank
def __str__(self) -> str:
return f"{self.rank} of {self.suit}"