CPSC 115L: Introduction to Computing Fall2010

Lab 10: Dealing a Bridge Hand

November 10, 11

As usual, you are expected to work with an assigned partner as a pair. Both you and your partner will receive the same grade. Both of you should always save your laboratory work on your own accounts.

Objectives

The main objectives of this assignment are
  1. to give you practice designing and writing a Python program using an object oriented paradigm.
  2. to give practive using inheritance in the design of a Python program.

Introduction

For this assignment we will write a program that deals one hand of bridge to a table of four players named North, East, South, West. Let's suppose that West is the dealer of this hand. West would deal one card to North, then one to East, then one to South, then one to West. This would be repeated 13 times, until all 52 cards have been dealt.

Following the deal, print the contents of each hand, North to West, and their respective point totals, as shown here:

North ( 8 ): 3C, 4C, 7C, 8C, 10C, JC, AC, 10D, 3H, 5H, 9H, QH, JS
East ( 21 ): QC, 9D, QD, 4H, 7H, 10H, JH, KH, AH, 10S, QS, KS, AS
South ( 1 ): 2C, 5C, 3D, 4D, 5D, 6D, 7D, JD, 2H, 6H, 8H, 2S, 6S
West ( 10 ): 6C, 9C, KC, 2D, 8D, KD, AD, 3S, 4S, 5S, 7S, 8S, 9S

Note how the cards are arranged in each hand, first all the Clubs, from lowest to highest, then all the Diamonds, Hearts, and Spades. This is the required order for Bridge.

In bridge, the point value of a hand is used to help players decide how to bid. We aren't going to go into the bidding and playing of a hand. That would be too complicated. But just reporting an accurate count is an interesting problem and provides a useful example of how to use object-oriented programming and inheritance in Python.

Here's how points are counted in Bridge. Aces are worth 4 points, Kings 3, Queens 2, and Jacks 1. The other cards have no point value. Bridge hands can receive various bonus points -- e.g., 3 points for null suit, bonus points for Ace, King, Queen, Jack, etc. -- but we aren't going to worry about that for this assignment.

For each of the following problems you will be defining new classes and methods and writing code segments (Python scripts) to test your definitions and to perform tasks such as dealing a hand a bridge. The scripts should go at the end of your file, after all of the class and method definitions.

1.1

Download into your Lab 10 folder cards.py module. This file includes definitions of the Card, Deck, and Hand classes. Open and read the definitions of the Card, Deck, and Hand classes. You will need to use them in your program. You may want to print a copy of this file so you can easily refer to it during the lab. You may not modify the Card, Deck, and Hand classes in any way.

Create a new file named bridge.py for your Bridge program and place the following import statement at the beginning of this file (after the comment block):

from cards import Card, Deck, Hand

This statement imports all of the definitions from cards.py allowing you to use them in your code.

  1. Define a class named BridgeCard as a subclass of Card.
  2. Override the __init__() method inherited from Card so that it will print abbreviated names for the cards -- e.g., 2C, 3H, AS, etc. -- as shown above. To do this, you will have to redefine the class attributes suit_names and rank_names. You should redefine things so that the Ace of any suit is the highest card. In Bridge, Aces are always the highest cards in the hand.
  3. Test your code by creating and printing several cards:
    card1 = BridgeCard(2,3)    # 3H  3 of Hearts
    card2 = BridgeCard(0, 11)  # JC  Jack of Clubs
    card3 = BridgeCard(3,12)   # KS  King of Spades
    card4 = BridgeCard(3,13)   # AS  Ace of spaces
    

1.2

This problem deals with subclassing the Deck class.

  1. Define a class named BridgeDeck as a subclass of Deck.
  2. Override the __init__() method so that the deck will consist of 52 BridgeCards, rather than 52 generic Cards.
  3. To test this problem, create an instance of BridgeDeck and print it out in its initial state, shuffled, and then sorted. You should get something like the following output (which is truncated to save space).
    2C, 3C, 4C, 5C, 6C, 7C, 8C, 9C, 10C, JC, QC, KC, AC, 2D, 3D, 4D, 5D, 6D, 7D, 8D, ...
    
    KD, 2S, 8H, 7S, 5H, QD, 10D, 10H, QC, AC, 3S, 9D, 5D, 10C, 8D, 8S, 6C, 6H, 5C, 9H, ...
    
    2C, 3C, 4C, 5C, 6C, 7C, 8C, 9C, 10C, JC, QC, KC, AC, 2D, 3D, 4D, 5D, 6D, 7D, 8D, ...
    
    

1.3

In this problem we will subclass the Hand class.

  1. Define a class named BridgeHand that is a subclass of Hand.
  2. This class will inherit the __init__() method from the Hand class. Is it necessary to override this method? Or will it be suitable for use in the game of Bridge?
  3. Define a method named getName() that returns the value of the label attribute. To test this method, define 4 bridge hands with the labels (names) North, South, East, and West. And print their names. (NOTE: If you wish, you can define a list variable named bridgetable which stores the four hands as values. If you do things this way, you can later use loops to reduce the amount of code you have to write. But this is not necessary.)
  4. Given the four bridge hands (players) you created in the previous exercise, write a code segment (not part of any of your classes) to deal out a complete bridge hand. To do so you will deal one card to each hand in turn -- North, East, South, West -- until all 52 cards have been dealt from the deck. At the end of the deal, print each hand and deck -- the deck should be empty:
    Done dealing, deck =  
    North : 6C, QC, 5D, 8D, 10D, JD, AD, 3H, 4H, 5H, 6H, AH, 5S
    East : 7C, AC, 4D, 7D, QD, 8H, JH, QH, KH, 2S, 3S, 4S, 8S
    South : 2C, 5C, JC, KC, 2D, 3D, 9D, 2H, 9H, 10H, 6S, 7S, 10S
    West : 3C, 4C, 8C, 9C, 10C, 6D, KD, 7H, 9S, JS, QS, KS, AS
    
    

1.4

This exercise will deal with calculate the points value of a bridge hand.

  1. Define a BridgeHand method named countPoints which counts up the point total for that hand. Remeber that only face cards are included in the point totals, with Aces counter as 4 points, Kings as 3, Queens as 2, and Jacks as 1. (HINT: This problem could be made easier by modifying the BridgeCard class to contain a point_values attribute that represents the point totals for each card from 2 through Ace. It should be modeled after rank_names. ByG this approach, the rank of the card can be used to look up its point value in the point_values list.) Print the four hands and the deck.
    Done dealing, deck =  
    North ( 5 ): 3C, 6C, 7D, 8D, 10D, AD, 6H, 8H, 9H, 10H, JH, 3S, 10S
    East ( 12 ): 8C, 9C, KC, 2D, 3H, 5H, 7H, 4S, 5S, 9S, QS, KS, AS
    South ( 8 ): 2C, 5C, 10C, 3D, 4D, 9D, JD, QD, 4H, AH, 6S, 8S, JS
    West ( 15 ): 4C, 7C, JC, QC, AC, 5D, 6D, KD, 2H, QH, KH, 2S, 7S
    
    
  2. Override the __cmp__() method so that you can compare two bridge hands to see who has the higher total. Then add code to your script to display which hand had the highest point total.
    Done dealing, deck =  
    North ( 5 ): 3C, 6C, 7D, 8D, 10D, AD, 6H, 8H, 9H, 10H, JH, 3S, 10S
    East ( 12 ): 8C, 9C, KC, 2D, 3H, 5H, 7H, 4S, 5S, 9S, QS, KS, AS
    South ( 8 ): 2C, 5C, 10C, 3D, 4D, 9D, JD, QD, 4H, AH, 6S, 8S, JS
    West ( 15 ): 4C, 7C, JC, QC, AC, 5D, 6D, KD, 2H, QH, KH, 2S, 7S
    
    The high hand is  West ( 15 ): 4C, 7C, JC, QC, AC, 5D, 6D, KD, 2H, QH, KH, 2S, 7S
    

Documentation

Your bridge.py script should contain a comment block at the beginning with your name, date, file name. It should include a brief description of the program, including the problem statement. Each method in the bridge.py class should include a docstring that describes its purpose of the function.

What to hand in

Submit the following in paper:


* CPSC 115L home page
Valid HTML 4.01!