Question: A regular deck contains 52 cards. There are 4 suits: Hearts, Diamonds, Spades and Clubs There are 13 ranks: Ace, King, Queen, Jack, 10, 9,…
Question:A regular deck contains 52 cards.
- There are 4 suits: Hearts, Diamonds, Spades and Clubs
- There are 13 ranks: Ace, King, Queen, Jack, 10, 9, 8, 7, 6, 5, 4, 3, 2.
Create two classes, Card and Deck.
Class Card
Card is meant to represent a single card. The main function is to store the rank and value of the card and compare them using the following symbols
- ” < “
- ” == “
- ” > “
- ” >= “
- ” <= “
- ” != “
There are multiple ways to compare the cards, use this order:
- Suits: Clubs (lowest), followed by Diamonds, Hearts, and Spades (highest)
- Ranks: Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King (from smallest to highest).
(Hint: use a list/dictionary to assign values).
For example: King of Clubs is smaller (<) than 2 of Spades. First you compare Suits, and, if the same, you compare Ranks.
:”’This class represents a single cardAlso compares two cardsc1 = card(4, “Clubs”)print(c1)4 of Clubsc2 = card(3, “Hearts”)print(c2)3 of Heartsc1 < c2Truec1 > c2Falsec1 == c2False”’
The tests above show only three comparison operators: “<“, “>” and “==”. There should be appropriate methods for all of them.
Once you have finished creating Card class, you can move on to a Deck class.
Deck will contain
- __init__ – a constructor that will initialize a new Deck object. This will contain
- An instance variable called deck, which will be a deck of cards as a list (initially full).
- Deck should be initialized in sorted order by suit and card value. The suit order is Clubs, Diamonds, Hearts, and then Spades. Aces are low and can be treated as the number 1.
- An instance variable called dealt_cards, which will be a list of cards that have been dealt out
- shuffle() – a method that will be used to add dealt_cards back into deck and then shuffle the deck
- This should use the numpy function random choice exactly once. Documentation here. Think about the proper parameters here: what is a? what is the sizeand what is the value for replace?
- Note: in order to grade your assignment, we should make the random generator “predictable”, so you all will get the same answer. In order to do that the special method, seed(), is used. Here is a link for more detailed explanation: seeding. This will make it so that each time you call random, you will get the same output.
- Note 2: Make sure to convert the numpy array back into a list after shuffling it with choice.
- deal_cards() – a method that will take in n and will deal out n cards from the top of the deck.
- The top n cards should be removed from deck and added to dealt_cards
- You will return the list of cards that were dealt.
REFER TO STARTER CODE DOCTESTS FOR OUTPUTS. Be sure to match everything.
Constructor’s doctests:
> cards = deck()> len(cards.deck)> len(cards.dealt_cards)> print(cards)>>> cards = deck()>>> np.random.seed()>>> cards.shuffle()>>> print(cards.deck[:])[ of Hearts, of Hearts, of Spades, of Diamonds, of Hearts]>>> cards = deck()>>> hand = cards.deal_cards()>>> np.random.seed()>>> cards.shuffle()>>> cards.deck[:][A of Spades, of Hearts, Q of Spades, Q of Diamonds, J of Hearts] : “”” >>> cards = Deck() >>> len(cards.deck) 52 >>> cards = Deck() >>> len(cards.dealt_cards) 0 >>> cards = Deck() >>> print(cards) In Deck: A of Clubs 2 of Clubs 3 of Clubs 4 of Clubs 5 of Clubs 6 of Clubs 7 of Clubs 8 of Clubs 9 of Clubs 10 of Clubs J of Clubs Q of Clubs K of Clubs A of Diamonds 2 of Diamonds 3 of Diamonds 4 of Diamonds 5 of Diamonds 6 of Diamonds 7 of Diamonds 8 of Diamonds 9 of Diamonds 10 of Diamonds J of Diamonds Q of Diamonds K of Diamonds A of Hearts 2 of Hearts 3 of Hearts 4 of Hearts 5 of Hearts 6 of Hearts 7 of Hearts 8 of Hearts 9 of Hearts 10 of Hearts J of Hearts Q of Hearts K of Hearts A of Spades 2 of Spades 3 of Spades 4 of Spades 5 of Spades 6 of Spades 7 of Spades 8 of Spades 9 of Spades 10 of Spades J of Spades Q of Spades K of Spades Dealt Out: >>> deck_to_shuffle = Deck() >>> np.random.seed(5) >>> deck_to_shuffle.shuffle() >>> print(deck_to_shuffle.deck[:5]) [9 of Hearts, 4 of Hearts, 7 of Spades, 7 of Diamonds, 6 of Hearts] >>> deck_to_deal = Deck() >>> hand = deck_to_deal.deal_cards(5) >>> np.random.seed(5) >>> deck_to_deal.shuffle() >>> deck_to_deal.deck[:5] [A of Spades, 9 of Hearts, Q of Spades, Q of Diamonds, J of Hearts] >>> cards = Deck() >>> cards.deal_cards(5) [A of Clubs, 2 of Clubs, 3 of Clubs, 4 of Clubs, 5 of Clubs] >>> cards = Deck() >>> hand = cards.deal_cards(5) >>> cards.deal_cards(5) [6 of Clubs, 7 of Clubs, 8 of Clubs, 9 of Clubs, 10 of Clubs] >>> cards = Deck() >>> hand = cards.deal_cards(5) >>> print(cards) In Deck: 6 of Clubs 7 of Clubs 8 of Clubs 9 of Clubs 10 of Clubs J of Clubs Q of Clubs K of Clubs A of Diamonds 2 of Diamonds 3 of Diamonds 4 of Diamonds 5 of Diamonds 6 of Diamonds 7 of Diamonds 8 of Diamonds 9 of Diamonds 10 of Diamonds J of Diamonds Q of Diamonds K of Diamonds A of Hearts 2 of Hearts 3 of Hearts 4 of Hearts 5 of Hearts 6 of Hearts 7 of Hearts 8 of Hearts 9 of Hearts 10 of Hearts J of Hearts Q of Hearts K of Hearts A of Spades 2 of Spades 3 of Spades 4 of Spades 5 of Spades 6 of Spades 7 of Spades 8 of Spades 9 of Spades 10 of Spades J of Spades Q of Spades K of Spades Dealt Out: A of Clubs 2 of Clubs 3 of Clubs 4 of Clubs 5 of Clubs “”” : “”” Initializes the deck of cards “”” : “”” This method shuffles the deck using np.random.choice “”” : “”” This method deals out n cards and sends them all to the dealt cards list. It also returns the list of the cards. :param n: the number of cards as a positive integer :returns: a list of n cards “”” : : : “”” This class represents a single card Also compares two cards >>> c1 = Card(4, “Clubs”) >>> print(c1) 4 of Clubs >>> c2 = Card(3, “Hearts”) >>> print(c2) 3 of Hearts >>> c1 < c2 True >>> c1 > c2 False >>> c1 == c2 False “”” : “”” :param rank: one of the 13 ranks as a string or positive integer :param suit: one of the 4 suits as a string “”” : : : : : : : :
How it Works
1
It only takes a couple of minutes to fill in your details, select the type of paper you need (essay, term paper, etc.), give us all necessary information regarding your assignment.
2
Once we receive your request, one of our customer support representatives will contact you within 24 hours with more specific information about how much it'll cost for this particular project.
3
After receiving payment confirmation via PayPal or credit card – we begin working on your detailed outline, which is based on the requirements given by yourself upon ordering.
4
Once approved, your order is complete and will be emailed directly to the email address provided before payment was made!