45 lines
1.1 KiB
Python
45 lines
1.1 KiB
Python
|
|
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())
|