Hide

Problem A
Cards

Languages en is

In this project, you need to implement three classes: Card, Deck, and Hand. Each class should be implemented in its own Python file. Two example main programs, each using the three classes, are given (see attached main1.py and main2.py).

Note that some of the other items depend on both passing the tests for class and instance variables as well as implementing the initializer correctly.

Card class (30 points)

  • (10 points) __init__(): An initializer with the parameters rank (either a string or an integer) and suit (a character). You can use the type() function to check the type of the parameter. You can assume that the constructor for Card is always invoked with valid parameter values.

  • (10 points) The Card class has two public instance variables: rank and suit. The rank is stored as an integer in the range 2–14. The suit is stored as a character: ’H’, ’S’, ’D’, or ’C’, for hearts, spades, diamonds, and clubs, respectively.

  • (10 points) __str__(): The string representation for an instance of a Card is a right justified field of three characters: the rank followed by the suit. The letters ’J’, ’Q’, ’K’, and ’A’, are used for ranks 11, 12, 13, and 14, respectively. For example, the string representation for the 10 of hearts and ace of spades is "10H" and
    " AS", respectively.

Deck class (40 points)

  • (4 points) __init__(): An initializer without any parameters, which creates a deck of 52 cards. The deck is constructed in such a way that the cards are first ordered by their suit, HSDC (hearts first, then spades, diamonds, and clubs), and then by their rank in ascending order. The first three cards in a deck are therefore: “2H”, “3H”, and “4H”.

  • (9 points) The Deck class has one public instance variable, deck, which contains a list of 13 cards in each of the four suits, that is a total of 52 cards.

  • (9 points) __str__(): The string representation for an instance of a Deck lists all the cards in the deck in the order they appear (with a single space after each card), breaking into a new line after each 13 cards.

  • (9 points) shuffle(): Shuffles the cards in the deck using random.shuffle(). Note, you should not call random.seed within the class.

  • (9 points) deal(): Deals a single card from the deck by returning the card removed from the top of the deck (the first card). You may assume this is never called when the deck is empty.

Hand class (30 points)

  • (6 points) __init__(): An initializer without any parameters.

  • (8 points) The Hand class has one public instance variable, cards, for storing a list of up to 13 cards. Additionally, you must have the required constants, as can be seen from the attached programs.

  • (8 points) __str__(): The string representation for an instance of a Hand consists of single line containing the strings representation of each card (with a single space after each card). If the hand is empty, the string “Empty” is returned.

  • (8 points) add_card(card): Adds the given card to the hand. If the hand is full, then nothing happens.

Constants

You should use constants in your implentation. You need to figure out which constants to use, but as can be seen in the two main programs, you need at least the constant NUMBER_OF_CARDS in the Hand class.

Output of main1.py

 5S
 QD
 KH
 2C
 AS
10D

 JS  KD  3C 10C  5C  5S  3H  8D  5H  7S  AS  9C  8H 
 4D 10D  9H  AD  7C  JC  KS  7H  3S 10H 10S  JH  8C 
 AH  KH  8S  JD  QC  2D  2C  QS  4S  6H  9S  6C  9D 
 KC  QH  4C  6S  7D  5D  2S  2H  AC  6D  3D  4H  QD 

 JS  KD  3C 10C  5C  5S  3H  8D  5H  7S  AS  9C  8H 

 4D 10D  9H  AD  7C  JC  KS  7H  3S 10H 10S  JH  8C 
 AH  KH  8S  JD  QC  2D  2C  QS  4S  6H  9S  6C  9D 
 KC  QH  4C  6S  7D  5D  2S  2H  AC  6D  3D  4H  QD 

Please log in to submit a solution to this problem

Log in