created Deck and Hand class
This commit is contained in:
44
src/pyjack/deck.py
Normal file
44
src/pyjack/deck.py
Normal file
@@ -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())
|
||||
44
src/pyjack/hand.py
Normal file
44
src/pyjack/hand.py
Normal file
@@ -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)
|
||||
Reference in New Issue
Block a user