Files
blackjack/src/pyjack/deck.py

45 lines
1.1 KiB
Python
Raw Normal View History

2025-10-26 19:38:46 +01:00
import random
2025-10-28 21:04:15 +01:00
from pyjack.card import Card
2025-10-26 19:38:46 +01:00
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())