< Back

Lab 33: Classes

As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.

$ cd ~/cs120/labs
$ mkdir lab33
$ cd lab33

deck.py
Cards

One of the key benefits of using classes is code reusability. You can write a class once, and use that class in multiple different programs. To do this effectively, you need a lot of practice writing classes, to see how we can divide tasks up this way. For now, we will tell you how to break the classes up.

A classic example of classes is a deck of cards. It's pretty easy to see what the classes would be for a deck of cards: A class for Cards, and a class for Decks. Today, you will implement these two classes, so that you can implement a very simple card game later.

Details

Create a file called deck_of_cards.py. In this file, you will create two classes: Card and Deck. Deck was written for you in class today, so you need to focus on completing the Card class.

The Card class should have two attributes: suit and value. suit will be a string representing the suit of the card: Hearts, Diamonds, Clubs, or Spades. value will be a string, either in the range \([2, 10]\) or a string for the face value cards: A, K, Q, or J.

You need to have four methods for the cards: The constructor, a greater_than(self, other_card) method, an equals(self, other_card) method, and a print_card(self) method. The greater_than method should return true of the card represented by self is less than the other card. For the sake of this program, we will consider a card to be greater than another card if the value is greater than the other card. equals is much the same, just using a different comparison.

Example

$ python3
>>> import deck_of_cards
>>> c1 = Card("2", "Hearts")
>>> c2 = Card("3", "Hearts")
>>> c1.print_card()
1 of Hearts
>>> c2.print_card()
2 of Hearts
>>> c1.greater_than(c2)
False
>>> c2.greater_than(c1)
True
>>> c2.equals(c2)
True
>>> c3 = Card("A", "Spades")
>>> c1.greater_than(c3)
False
>>> c3.greater_than(c1)
True

Hint

  • Remember, the constructor is called __init__. For this program, you will likely want to have two additional paramters to the constructor: a suit and a value.
  • greater_than is a little tricky with the current construction. Everything is fine if they are all integers. The issue is if you need to compare two strings, or a string and an integer. The easiest way to solve this is to write an additional method called get_value, which returns an integer representing the value of the card.

    The get_value method should return the value if it is not a string, it should return 11 for J, 12 for Q, 13 for K, and 14 for A.

  • With the above get_value method, equals should become very easy. Just check to see if the value of self is equal to the value of other_card.
  • print_card is also fairly straight forward. Just print the value and the suit separated by an "of".

Challenge

WAR!

One of the most simple card games to play is War. There isn't a lot of strategy to War. It is entirely luck based. However, it is a decent way to pass about 10 - 30 minutes, and is an excellent use of the Card and Deck classes written above.

War is a two player game. The game starts off by dealing All of the cards to the players. Each player starts the game with 26 cards. Each player then reveals the card from the top of their deck. The player who has the highest value card gets their opponents card, and places both their card and their opponents card on the bottom of their deck. If there is a tie, each player deals out three cards, and their fourth cards are then compared. This can trigger another war too, but if either player runs out of cards they lose. This entire process continues until one player runs out of cards.

Details

Add to your deck_of_cards.py file a function called play_war(). This function should not take any parameters, and will simply play a game of war using the previously defined classes.

Your function should first initialize the main deck. Then, you should create two new, empty decks. These will be the hands for each player. You should deal all of the cards from the first deck to these decks, alternating players. You should then print out a full game of war to the terminal screen.

It will be very difficult to debug if you use all 52 cards. Test your program with fewer cards to begin with (4 is a good number for testing purposes).