Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need a python 3 help. Part 2: Deck Of Cards For this part, you will write a program that acts as a deck of

I need a python 3 help.

Part 2: Deck Of Cards

For this part, you will write a program that acts as a deck of cards.

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.

You will need two create two classes, card and deck.

Class card

The purpose of this class is to represent a single card. Also you should be able to compare two cards using

  • "
  • " == "
  • " > "
  • " >= "
  • "
  • " != "

symbols.

There are multiple ways to compare the cards, we will 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).

For example: King of Clubs is smaller (

class card():

'''

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

True

>>> c1 > c2

False

>>> c1 == c2

False

'''

The tests above show only three comparison operators: "" and "==". You need to write appropriate methods for all of them (see above).

Once this class is created, you can move on to a deck class.

It should contain

  • a deck of cards, as a list (initially full)
  • a list of dealt cards (initially empty)
  • the method shuffle
  • method deal_cards.

When you initialize the deck, it should be stored in sorted order by suit and then by card value. The suit order is Clubs, Diamonds, Hearts, and then Spades. Aces are low and can be treated as the number 1.

Constructor's doctests:

>>> cards = deck()

>>> len(cards.deck)

52

>>> len(cards.dealt_cards)

0

>>> print(cards)

# PLEASE REFER TO THE CODE.

You should override the print function to match our output.

Shuffle method

The method shuffle should use the numpy function https://docs.scipy.org/documpy-1.14.0/reference/generatedumpy.random.choice.html exactly once. You need to think about a proper parameters there: what is a? what is the size and 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

In short, each time you call random, it will give you the same output:

image text in transcribed

Important: make sure to convert the numpy array back into a list after shuffling it with choice.

A few doctests:

>>> cards = deck()

>>> np.random.seed(5)

>>> cards.shuffle()

>>> print(cards.deck[:5])

[9 of Hearts, 4 of Hearts, 7 of Spades, 7 of Diamonds, 6 of Hearts]

>>> cards = deck()

>>> hand = cards.deal_cards(5)

>>> np.random.seed(5)

>>> cards.shuffle()

>>> cards.deck[:5]

[A of Spades, 9 of Hearts, Q of Spades, Q of Diamonds, J of Hearts]

deal_cards method

The method deal_cards should take in an a positive integer n and then deal n cards from the front of the deck list.

When dealing cards, they should be removed from the deck and added to another list called dealt_cards.

This function should return the n cards that were dealt.

The shuffle method should then recombine these two lists by sending the dealt cards to the end of the cards list and then shuffling.

That's it for now. Deck of Cards will return in the future.

This is what I have on my program. follow the direction as it shows Thank you

import numpy as np

class deck(): """ >>> 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 """ def __init__(self): """Initializes the deck of cards """ # Your Code Here def shuffle(self): """ This method shuffles the deck using np.random.choice """ # Your Code Here def deal_cards(self,n): """ 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 """ # Your Code Here

def __repr__(self): # Replace pass with your code pass

def __str__(self): # Replace pass with your code pass

class card(): """ 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

>>> c1 > c2 False

>>> c1 == c2 False """ def __init__(self, rank, suit): """ :param rank: one of the 13 ranks as a string or positive integer :param suit: one of the 4 suits as a string """ self.ranks = rank self.suits = suit

def __repr__(self): # Your Code Here pass

def __str__(self): # Replace pass with your code pass

def __eq__(self,other): # Replace pass with your code pass

def __ne__(self,other): # Replace pass with your code pass

def __lt__(self,other): # Replace pass with your code pass

def __gt__(self,other): # Replace pass with your code pass def __le__(self,other): # Replace pass with your code pass def __ge__(self,other): # Replace pass with your code pass

>numpy.random.seed(e)numpy.random.rand(4) array(I 0.55, 8.72, 0.6, 0.54]) >numpy.random.seed(e) numpy.random.rand (4) array(I 0.55, 0.72 , e.6, e.54])

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

1. What is destination governance?

Answered: 1 week ago