Question: PYTHON 3 For this part, you will write a program that acts as a deck of cards. A regular deck contains 52 cards. There are

PYTHON 3

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 WRITE UP.

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: PYTHON 3 For this part, you will write a program that acts

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.

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

>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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!