Question

In: Computer Science

Python Programming Write a simple implementation of a card deck to deal cards out randomly. Your...

Python Programming

Write a simple implementation of a card deck to deal cards out randomly. Your Deck class will contain a list of card objects. Initially, the deck will contain one instance of each of the 52 possible cards. Your deck should implement a deal() method that chooses a random location from the list and "pops" that card. You should also implement a cardsLeft method that tells how many cards are left in the deck.

Please type code and include screenshots of output of the code. Also code must be well commented.

Solutions

Expert Solution

Thanks for the question, here is the complete code screenshot and comments for this project. Have added detailed comments so that you can follow each line of code precisely.

Let me know in case you find anything unclear.

thanks a lot !

=====================================================================

# need this module to geenerate random number for the card location
import random
# class Card
class Card():
    # contains two variable one rank and one suit name
   
def __init__(self, rank, suit):
        self.rank = rank
        self.suit = suit
    # returns the rank name
   
def getRank(self): return self.rank
    # returns the suit
   
def getSuit(self): return self.suit
    # when we print the card object we print the full description of the card
   
def __str__(self):
        rank_names = {'2': '2', '3': '3', '4': '4', '5': '5', '6': '6', '7': '7', '8': '8', '9': '9', 'T': 'Ten',
                      'J': 'Jack', 'Q': 'Queen', 'K': 'King', 'A': 'Ace'}
        suit_names = {'D': 'Diamond', 'S': 'Spades', 'C': 'Clubs', 'H': 'Heart'}
        return '{} of {}'.format(rank_names.get(self.rank), suit_names.get(self.suit))
class Deck():
    # class variables
   
suits = ['D', 'H', 'S', 'C']
    ranks = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']
    def __init__(self):
        self.cards = [] # contains a list that will hold the Card object
        # for each suit and for each rank create an object of Card
        # and append that to the list
       
for suit in Deck.suits:
            for rank in Deck.ranks:
                card = Card(rank, suit)
                self.cards.append(card)
    # deal a random card
   
def deal(self):
        # keep dealing until we have a card in the list
       
if len(self.cards) != 0:
            # generate a random number between 0 and last index-1
           
location = random.randint(0, len(self.cards)-1)
            # return the card at that index also removing the card from the list at the same time
           
return self.cards.pop(location)
        else:# if there are no card return None
           
return None
    def
cardsLeft(self):
        return len(self.cards)
# create an object of Deck class
deck = Deck()
# we are running aloop 1o times
for _ in range(10):
    # in every iteration we deal one card from the deck object
   
print('Card deal: {}'.format(deck.deal()))
    # we print the number of cards left in the deck
   
print('Cards left: {}'.format(deck.cardsLeft()))


Related Solutions

A magician randomly draws a card from a deck of cards ( 52). On a scale...
A magician randomly draws a card from a deck of cards ( 52). On a scale of 1 to 3 ( 1 being most likely to happen, 2 being somewhat likely to happen, and 3 being least likely to happen).  Show all steps. Drawing a 3. Drawing any black card. Drawing a King or Queen.
Three cards are randomly selected from a deck of 52 cards. After every draw, the card...
Three cards are randomly selected from a deck of 52 cards. After every draw, the card is NOT replaced back in the deck. Find the probability of drawing a King, followed by two Aces in a row. An instructor gives a pop quiz consisting of 10 multiple choice questions, where each question has 5 choices, (a) through (e). What is the probability of passing the pop quiz if you guess the answers and you have to get 8 questions correct?...
Given a standard deck of 52 cards, what is the probability of randomly drawing a card...
Given a standard deck of 52 cards, what is the probability of randomly drawing a card and the face value is 2? Round your answer to two decimal places.
1) A card is drawn randomly from a standard deck of 52 cards. Find the probability...
1) A card is drawn randomly from a standard deck of 52 cards. Find the probability of the given event. a red 9 2) Assume that all elementary events in the same sample space are equally likely. A pair of fair dice are tossed. What is the probability of obtaining a sum of 11? 10? 7? probability of obtaining a sum of 11 probability of obtaining a sum of 10 probability of obtaining a sum of 7
We have a poker deck of 52 cards. Abby randomly shuffles the card, and divides it...
We have a poker deck of 52 cards. Abby randomly shuffles the card, and divides it into 13 piles of 4 cards each. Use Hall marriage theorem to show that it is possible to pick a card from each pile, so that you get all 13 numbers from A to K.
Consider five-card-hands out of a deck of 52 cards. a) (10%) What is the probability that...
Consider five-card-hands out of a deck of 52 cards. a) (10%) What is the probability that a hand selected at random contains five cards of the same suit? (The suits are hearts, spades, diamonds and clubs.) Show a formula and compute a final answer. Show your intermediate computations. b) (10%) In how many different ways can a hand contain no more than 2 cards of the same kind. (E.g. not more than 2 queens.) Show a formula but you do...
You randomly draw five cards from a standard 52-card deck. a) What is the probability of...
You randomly draw five cards from a standard 52-card deck. a) What is the probability of getting exactly 1 Queen? b) What is the probability of getting exactly two Queens? c) What is the probability of getting exactly three Queens? d) What is the probability of getting no Queens?
Program a simple game that shuffles a deck of 52 cards and hands them out to...
Program a simple game that shuffles a deck of 52 cards and hands them out to twoplayers randomly (26 each). Write a simulation of the card game, where, at each turn, bothplayers take the card from the top of their pile and the player with the higher card wins thatround. The winner of that round takes both cards and adds it to a stack of won cards.In essence, there are four stacks. Each player has one stack of cards they...
. If 2 cards are selected from a standard deck of cards. The first card is...
. If 2 cards are selected from a standard deck of cards. The first card is not replaced in the deck before the second card is drawn. Find the following probabilities: a) P(2 Aces) b) P(Queen of hearts and a King) c) P(Q of Hearts and Q of hearts )
C++ In this assignment you will use your Card class to simulate a deck of cards....
C++ In this assignment you will use your Card class to simulate a deck of cards. You will create a Deck class as follows: The constructor will create a full 52-card deck of cards. 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace for each suit: Clubs, Diamonds, Hearts, Spades Each card will be created on the heap and the deck will be stored using an STL vector of Card pointers. The destructor will free the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT