cardy/cardy/playing_cards.py

61 lines
1.4 KiB
Python

from itertools import product
from typing import List
from cardy.deck import Card
from cardy.deck import Pile
from cardy.deck import WildCard
# SUITES is a collection of tuples with suites and their colors
SUITS = (
("diamonds", "red"),
("hearts", "red"),
("clubs", "black"),
("spades", "black"),
)
# FACES maps special values to their face names
FACES = {
11: "Jack",
12: "Queen",
13: "King",
14: "Ace",
-1: "Joker",
}
class PlayingCard(Card):
@property
def face(self) -> str:
return FACES.get(self.value, str(self.value))
def __str__(self) -> str:
return (
f"[{self.face.capitalize()} of {self.suit.capitalize()} "
f"({self.color.capitalize()})]"
)
class Joker(WildCard, PlayingCard):
def __str__(self) -> str:
return "[Joker]"
class Deck(Pile):
def __init__(self, num_jokers: int = 0, aces_high: bool = False):
cards: List[PlayingCard] = []
card_values = range(2, 15) if aces_high else range(1, 14)
for c in product(card_values, SUITS):
cards.append(
PlayingCard(
value=c[0],
suit=c[1][0],
color=c[1][1],
)
)
for _ in range(0, num_jokers):
cards.append(Joker())
super().__init__(cards2)