From f446dcfa8f8d6497004d9e48bf1e09733057d59e Mon Sep 17 00:00:00 2001 From: cerberus Date: Sun, 26 Oct 2025 19:38:46 +0100 Subject: [PATCH] created Deck and Hand class --- src/pyjack/deck.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/pyjack/hand.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/pyjack/deck.py create mode 100644 src/pyjack/hand.py diff --git a/src/pyjack/deck.py b/src/pyjack/deck.py new file mode 100644 index 0000000..26a8b6b --- /dev/null +++ b/src/pyjack/deck.py @@ -0,0 +1,44 @@ +import random +from card import Card + +SUITS = ["hearts", "diamonds", "clubs", "spades"] +RANKS = list(range(2, 11)) + ["J", "Q", "K", "A"] + + +class Deck: + def __init__(self) -> None: + """Init the deck of 52 cards""" + self.cards = [] + + # looping through the SUITS + for s in SUITS: + # for each SUIT create one card of each rank + for r in RANKS: + new_card = Card(s, str(r)) + self.cards.append(new_card) # append to deck + + def __str__(self) -> str: + """Returns the cards object in a human readable format""" + human_readable_cards = [str(card) for card in self.cards] + return "\n".join(human_readable_cards) + + def shuffle(self) -> None: + """Shuffles the deck""" + random.shuffle(self.cards) + + def deal_card(self) -> Card: + """Removes the last card off the deck and returns it.""" + card = self.cards.pop() + return card + + +# my_deck = Deck() +# shuffled_deck = my_deck.shuffle() +# +# print(my_deck) +# +# new_card = my_deck.deal_card() +# print("New card: \n") +# print(str(new_card)) +# +# print(new_card.get_value()) diff --git a/src/pyjack/hand.py b/src/pyjack/hand.py new file mode 100644 index 0000000..2b3b2b3 --- /dev/null +++ b/src/pyjack/hand.py @@ -0,0 +1,44 @@ +from card import Card + + +class Hand: + """Represents the hand of a player""" + + def __init__(self) -> None: + self.cards: list = [] + self.value: int = 0 + self.aces: int = 0 + + def add_card(self, card: Card) -> None: + """Adds a card to the Hand and uptdates the hands values.""" + + # Adding the card to the had + cards = self.cards + cards.append(card) + self.cards = cards + + # Adding the card value to the Hands value + self.value += card.get_value() + + # Adding one to self.aces if in the hand is one ace. + if "A" in str(card): + self.aces += 1 + + # adjusts the value if an ace is on the hand. + self.adjust_for_aces() + + def adjust_for_aces(self) -> None: + """adjusts the aces value if the hands value is above 21.""" + if self.value > 21 and self.aces > 0: + self.value -= 10 + self.aces -= 1 + + +# my_hand = Hand() +# +# my_hand.add_card(Card("diamond", "K")) +# my_hand.add_card(Card("diamond", "8")) +# my_hand.add_card(Card("diamond", "A")) +# +# print(my_hand.value) +# print(my_hand.cards)