In this project, you will write a program that simulates a standard deck of card
ID: 3802066 • Letter: I
Question
In this project, you will write a program that simulates a standard deck of cards.
Background - What is a standard deck of cards?
A standard deck of playing cards contains a total of 52 cards. Each card has two attributes - a value and a suit. There are 13 possible values and 4 possible suits. (Since the deck contains each possible combination of cards, that makes for 13 x 4 = 52 total cards.) The 13 possible values are as follows: ace 2 3 4 5 6 7 8 9 10 jack queen king The four suits are: hearts diamonds spades clubs When describing a card, you indicate both its value and its suit. For example, a card might be "the ace of diamonds" or "the 2 of spades".
Step 1 - Make a deck of cards.
In this project, you will represent each possible card value as a single character: "A" for ace, "9" for 9, etc. You will also represent each possible suit as a single character: "H" for hearts, "D" for diamonds, etc. A card is going to be represented as a two-character string where the first character is the value and the second character is the suit. Each value should be represented by: A capital letter that is the first letter in the value name, if the value is ace, jack, queen, or king. A capital "T" if the value is 10. The digit as a character, if the value is in the range 2 - 9 (so a 2 would be represented as "2"). Each suit should be represented by a capital letter that is the first letter in the suit name. Thus, the two of hearts would be represented as "2H", the king of diamonds would be represented by "KD", etc. Your first task is to make a list of 52 strings representing each of the 52 possible cards in a deck. You should print your list to verify that it contains all 52 cards. But wait! For full credit, you shouldn't actually type out every possible two-character string in your program. Instead, use a double for loop where the outer loop goes through each possible value and the inner for loop goes through each possible suit. You can then use concatenation to generate each string you need.
Step 2 - Shuffle the deck of cards.
In order to randomize the order of the cards, we'll need to make use of a function called "shuffle" that lives in a module called "random". (We'll cover modules later.) At the very top of your program, add a line that says: from random import shuffle Then, before you print the cards in your deck, shuffle them using a function called shuffle. This function takes one argument - the sequence you want to shuffle. It returns nothing, but the sequence you provide as an argument will be modified. At this point, when you print your deck of cards, the strings should appear in a seemingly random order.
Step 3 - Deal a card.
Make a variable to hold a single card in your deck. Retrieve the topmost card from your deck (which, for the purposes of this program, is the card at index 0). Make sure you also remove this card from the deck. Print the card. Each time you run your program, it should be something different. Also print the length of the deck of cards after the one card has been dealt. It should now be 51.
Step 4 - Move your code into functions.
Now, change your program so that it has two functions: get_shuffled_deck() - This function takes no arguments and returns a shuffled list of 52 "cards" (strings). deal_card(...) - This function takes a single argument - a list - and does two things: it removes the thing at index 0, and it returns what it removed. The main part of your program outside these functions should now: Call the get_shuffled_deck() function and store the result in a variable. Print the resulting deck of cards. Call the deal_card(...) function, passing the deck of cards as the argument and storing the result in another variable. Print the card that was returned by the deal_card(...) function. Print the length of the deck of cards at this point. It should be 51.
Step 5 - Add comments.
Add a comment to each function, explaining what it does.
Explanation / Answer
from random import shuffle
#Lists for suites and values
suites = ['H', 'D', 'S', 'C']
values = ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K']
#Step1
def generate_cards():
"This generates the cards"
cards = []
for suite in suites:
for value in values:
#concat value and suite to form string representing a card
card = value + suite;
#Append this card to list of cards
cards.append(card)
#Print created cards
print "Initial deck"
for card in cards:
print card
#Finally return created cards
return cards
#Step2
def get_shuffled_deck():
"This function generates and shuffles the deck"
#Generate cards
cards = generate_cards()
#Shuffle deck
shuffle(cards)
#Now print the shuffled deck
print "Shuffled deck"
for card in cards:
print card
return cards
#Step3
def deal_card(remaining_cards):
"This function removes first element in the cards list and returns that element"
#Check if we have cards in the deck
if remaining_cards:
current_card = remaining_cards.pop(0)
return current_card
else:
return "NA"
#Step4
cards = get_shuffled_deck()
card = deal_card(cards)
print "Current card is: " + card
print "Remaining size of deck is: %d", len(cards)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.