RESOURCES: Class slides: https://docs.google.com/presentation/d/1Tl0DfU3mn-KnBnjzLnZoHtzr2vujPMQgdehusWt2rXw/edit?usp=sharing HOMEWORK / MINI-PROJECT: Create a program that simulates a standard deck of cards without Jokers. There are 4 suits of cards (Clubs, Diamonds, Hearts, Spades), each with 13 cards: Jack, Queen, King, Ace, and the numbers 2 through 10. This makes for a total of 4*13 = 52 cards.
Your program should also keep track of one person's hand as well as a deck of cards, and the sum of the cards in the deck and that person's hand should of course be 52.
Allow the user to draw cards into the person's hand, shuffle the deck, print out the cards in deck or in hand, etc. through commands entered through input.
(Implement a draw function with a number of cards to draw, a shuffle function, printing for the hand/deck.)
Create a card game with the existing code for this project, like Blackjack, Big 2, etc. I recommend having the game be a "pass-around" system, so every player in the card game has their own hand and enters input through the program. Write your answer as a comment, and feel free to email the TAs (Shravya and Eric) if you're stuck. See you on Tuesday!
Solution
import random
deck = ['CJ', 'CQ', 'CK', 'CA', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'C10', 'DJ', 'DQ', 'DK', 'DA', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'D10', 'HJ', 'HQ', 'HK', 'HA', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8', 'H9', 'H10', 'SJ', 'SQ', 'SK', 'SA', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'S10']
hand = []
handnum = int(input("How many cards do you want in your hand?"))
if handnum > 52:
print("oops")
print("The number you entered is bigger than 52")
for x in range(handnum):
z=random.randint(0,(len(deck)-1))
hand.append(deck[z])
deck.remove(deck[z])
print("Cards in hand:", hand)
print("Cards in deck:", deck)
action = ""
while action.lower() != "exit":
action = input("What would you like to do next? Draw card, shuffle deck, or exit?")
if action.lower() == "draw card":
a = random.randint(0,(len(deck)-1))
hand.append(deck[a])
deck.remove(deck[a])
print(hand)
elif action.lower() == "shuffle deck":
random.shuffle(deck)
print(deck)
elif action.lower() == "exit":
print("Thank you for playing")
pass
else:
print("Try again")
pass