Question: All the cards you'll be creating here will have a rank (a number between 0 and 9), a suit (triangles, squares, or pentagons), and a

All the cards you'll be creating here will have a rank (a number between 0 and 9), a suit (triangles, squares, or pentagons), and a class (either "royal" or "common"). There is exactly one card for every possible combination of rank, suit, and class, so there will be 60 cards in the deck. Some cards are considered "higher" than other cards. Royal cards are always considered higher than common cards. If the classes are the same, then the suits determine which card is higher. Pentagons are higher than squares, and squares are higher than triangles. If both the suit and the class are the same, then the rank is used to determine which card is higher. So for example, the 2 of royal triangles is worth more than the 5 of common pentagons, which is worth more than the 8 of common squares, which is worth more than the 5 of common squares. The game you'll be implementing is a simplified variant of a game called High-Low. Here's how it works: A 60-card deck is shuffled and one card is dealt face up. The player has to guess whether the next card dealt will be higher or lower than this card. Once the player makes their guess, a second card is dealt. If they player was correct, then they win. If the player was wrong, then they lose.

Create a class definition for a Deck of Cards. A Deck should have one non-public attribute: a list of Cards. Your class definition should also fit the following requirements.

The class must have a constructor that can take either a sequence of Cards (in this case, the Deck should have all of the Cards in the sequence), or a single card (in this case, the Deck will only have the one Card in it), or no arguments at all (in this case, the Deck should be initialized to a complete 60-card deck with one of each possible Card).

So for example, you could make a Deck with two cards with a command like Deck( [Card(3, "common", "pentagon"), Card(7, "royal", "triangle")] ). Or you could make a Deck with one card with a command like Deck( Card(4, "royal", "square") ). Or you could make a complete 60-card deck with the command Deck().

Bonus: make the constructor work with an arbitrary number of Cards (e.g., Deck( Card(2, "royal", "pentagon"), Card(2, "royal", "triangle"), Card(1, "common", "square") )). This isn't super complicated, but it does require that you use a feature of Python that we haven't talked about in class, and that is sometimes confusing to beginners.

The Deck class should have appropriate .__str__() and .__repr__() methods. You can choose the details of formatting, but the .__str__() method should produce a string suitable for displaying a Deck to the user*, and the .__repr__() method should produce a string that you could use as a Python command to create a Deck.

*For your .__str__() method, you can't just get away with printing the list. Remember that it should look appropriate for a user who doesn't know anything about programming. So something like Deck( [Card(3,"common","pentagon"), Card(7,"royal","triangle"), Card(0,"royal","pentagon")] ) is not appropriate (you could use that for .__repr__(), however). The details are up to you (put each card on its own line? separate them with spaces? with tabs? with commas?), but whatever you do, you'll need to loop through the Cards to build the string, and you'll want to use str() on each Card to build up the string for the whole Deck.

Add a method called .shuffle() that randomizes the order of the Cards in the Deck's list. You'll probably want to use the shuffle() function from the random module.

Add a method called .deal() that removes the first Card from the Deck andreturns that Card. This is one of those rare methods that both modifies the Deckand returns something.

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!