CPSC120A
Fundamentals of Computer Science I

Lab 33

Classes

Use the command line to create a new directory called lab33 in your labs directory. Make sure all of the .py files that you create for this activity are in that directory.

Card

A great advantage of creating classes is that they facilitate re-use in multiple programs. One class that would be handy to have is a playing card class. It could be used to create many different games.

Details

Create a class called Card in a file called card.py. The Card class should have the following methods:

Example

>>> first_card = Card(1, 1)
>>> second_card = Card(2, 2)
>>> print(first_card)
A♠
>>> print(second_card)
2♣
>>> print(first_card.greater_than(second_card))
False
>>> print(second_card.greater_than(first_card))
True
>>> first_card = Card(10, 3)
>>> second_card = Card(10, 4)
>>> print(first_card)
10♥
>>> print(second_card)
10♦
>>> print(first_card.greater_than(second_card))
False
>>> print(second_card.greater_than(first_card))
False
>>> first_card = Card(0, 0)
>>> print(first_card)
JOKER

Hint

  • Create the init function. Initialize attributes of the Card self to the input parameters.
  • Create the str function. Convert the rank to a string, convert suit to a string and return the concatenation of the two strings. Don't forget to test if the card is a joker.
  • Test the init and str functions by creating a few cards and printing them.
  • Create the greater than function. If the rank is stored as an integer, the function simply returns whether the rank of self is greater than the rank of other. Don't forget to test if the card is a joker.
  • Test the greater than function with a few different cards.

 

Challenge

Create a game using the card class. You could re-create the blackjack game from a previous lab or create a poker game. You may want to add more functions to the class to make implementing the game easier.

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.