Classes

Your Assignment

Python is a classy language! <sad trombone> Anyways, classes are a programming construct that makes your code much more reasonable to read, to use and to extend.

In this lab, I will ask that you create a Python class that represents a standard playing card.

Playing cards (in America, at least) have rank and suit.
  • The suits of a deck of playing cards are Hearts, Spades, Clubs, and Diamonds.
  • Cards of each suit range in rank from Ace (1), 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack (11), Queen (12) and King (13).

Additionally, when playing Blackjack, the values of each card is usually the same as its rank except that face cards (Jack, King, Queen) have a value of 10, and the Ace has a value of 1 (“Aces low”).

http://cliparts.co/cliparts/zcX/5BG/zcX5BGgbi.png

The modern set of playing cards. Courtesy of cliparts.co.

Assignment Requirements

Implement a class representing a playing card, and name it PlayingCard. These class methods are required:

  1. __init__(self, rank, suit): This is your class initializer.
    • self is required, of course. Make sure you understand its purpose in classes.
    • rank is an int value that maps to the rank of a playing card. This number ranges from 1-13 (Ace, 2, 3, …, King.)
    • suit is a single character "d", "c", "h", "s" respectifully representing each suit a card could possibly have (Diamonds, Clubs, Hearts, or Spades)
  2. get_rank(self): When called on an instance of your
    PlayingCard class, it will return the rank of that particular card as an integer from 1-13 (Ace, 2, 3, …, King.)
  3. get_suit(self): When called on an instance of your
    PlayingCard class, it will return the suit of that particular card as single-character string of either "d", "c", "h", "s", where the letters represent Diamonds, Clubs, Hearts, or Spades, respectfully.
  4. bj_value(self): When called on an instance of your
    PlayingCard class, it will return the Blackjack value of that particular card as an integer.
  5. __repr__: You will be introduced to another dunder name. This
    one, when defined inside your class, allows any instance of your class to properly describe itself when you attempt to, say, print an object representing an instance of your class PlayingCard. Basically, it’s a technique to convert a non-printing object into a string.

Additionally:

  1. You will write a main() function that tests your

    PlayingCard class by creating n instances of randomly generated cards and then asking each instance of these cards to print their information and their Blackjack value.

    Note: You will print an instance of you PlayingCard object directly, making indirect use of the __repr__ method!

Sample output

First, define your PlayingCard class, but don’t yet define main(). You can tell IDLE to load your program. Once your program is loaded, and you are at the “>>>” prompt, you can test your class yourself, directly:

>>> c = PlayingCard(1,"s")
>>> print(c)
Ace of Spades
>>>

Let’s try making a Queen of Clubs card:

>>> c2 = PlayingCard(12,"c")
>>> print(c2)
Queen of Clubs
>>> c2.bj_value()
10
>>> c2.get_rank()
12
>>> c2.get_suit()
'c'
>>>

Let’s see how your program is require to run, according to the assignment requirements, above. This is what your main() should do (printing the PlayingCard instance and it’s Blackjack value):

Testing card class
How many cards would you like to see? 5
Two of Hearts counts 2
King of Hearts counts 10
Ten of Spades counts 10
Five of Hearts counts 5
Ace of Diamonds counts 1

Submission Requirements

There are a few standards to adhere to when submitting your lab. Please read, and follow them carefully.

  1. Submission Filename: Submit a file named lab_classes.py.

  2. Documentation String (“docstring”): The following docstring (properly edited with your name) needs to be the very first thing in your Python program file.

    """CSC 161 Lab: Classes
    
    This lab...
    
    Fox Mulder
    Lab Section TR 2:00-3:15pm
    Fall 2015
    """
    

    Read more about docstrings: Program Documentation String

  3. Comments (lines prefaced with #): Unclear parts of your code can receive point deductions, so be judicious about using comments. Do not overuse comments. Too much unnecessary commenting can complicate reading otherwise clear code!

    Read more about comments: Program Comments

  4. Python Code Style: You will need to run your Python program file through the online PEP 8 style checker found in CSC 161 Style.

  5. The “__main__, top-level script environment” check is required:

    if __name__ == '__main__':
        main()
    

    You can read more about this trick on the official Python website.

The Blackboard post for this lab assignment will contain the details regarding the due date and other details regarding class administration for this assignment.

Grading

The following is the grading rubric for this lab to be used by the course graders. The lab is worth 100 points.

Reasonable variations of this lab should be acceptable. In particular, we would accept programs that take input files with different formats (comma separated, space separated, one number per line, …)

Here’s a brief grading rubric:

Generally:
  • Syntax errors: -30 points
  • A bad/missing docstring (i.e. no name, lab section, etc): -10 points
  • The submitted filename is wrong: -5 points
  • No Python comments used when they are needed to clarify code: -5 points
  • Code doesn’t attempt to adhere to Python coding style: -10 points
  • Your top-level script environment check is missing: -5 points
  • Missing main() function: -10 points

Assignment-specific:

  • You must use a Python class named PlayingCard to solve this lab assignment: -50pts (this is the point of the assignment!)
  • Your class constructor should take single-letter codes for the card suit (‘d’, ‘c’, ‘h’, or ‘s’), else -10 pts:
  • You write a __repr__ method to allow your PlayingCard objects to return a string representation of iteself, else -5 pts.
  • Your functions need to return the correct data type, else -5 pts each.
  • Generally, deviations from the above assignment specifications will incur a -5pts deduction, each. The lab TAs have discretion to deduct more points for more egregious issues.

There may be additional criteria for grading, this is just a summary of the major rubric items.

Extra Credit

Extra credit ideas:

  • The solution to this assignment uses a very clever way of choosing a random letter for the suit. Let’s say it involves strings and randrange. Can you come up with a single line technique to choose a suit? +5 pts

I will allow you to add something creative and interesting to your program for extra credit. Your extra credit additions needs to be relevant to the techniques we are learning. You are not guaranteed to receive extra credit, it will be up to the lab TA to decide. Any extra credit work you do needs to be clearly commented in your code. If your extra credit code is not clearly documented using comments, you risk losing points instead of receiving bonuses.

Extra credit is added your normal submission file, do not submit a second file. Extra credit supersedes any of the above grading rubric if it conflicts.