Created
September 23, 2021 11:36
-
-
Save dmoneyballer/7c741e8f80a1e3bcd8e099994916a137 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def main(): | |
d = deck() | |
# loop used to test that dealing all cards work | |
for i in range(52): | |
print(i, d.deal_one_card()) | |
print(d.cards) | |
# we can generate more decks here and shuffle them after adding as many decks as are needed. | |
class deck(): | |
"""creates one deck and shuffles deck by default""" | |
def __init__(self, num_decks=1): | |
self.cards = [] | |
for _ in range(num_decks): | |
self.generate_deck() | |
self.shuffle() | |
def generate_deck(self): | |
"""appends new deck to self.cards""" | |
for suit in ['hearts', 'spades', 'clubs', 'diamonds']: | |
for card in ['Ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King']: | |
self.cards.append('{} of {}'.format(card, suit)) | |
def shuffle(self): | |
"""shuffles self.cards""" | |
random.shuffle(self.cards) | |
print('after shuffle ', self.cards) | |
def deal_one_card(self): | |
"""returns one card from self.cards""" | |
return self.cards.pop() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment