CIT 590 Assignment 7: Hearts
Spring 2010, David Matuszek

# Purposes

• To give you practice in designing classes
• To give you more practice in using classes and objects

# The Idea

Play a game of Hearts. Give the user the choice of being one of the players, or just watching the game.

As with most card games, there are many variations, so be sure to follow the rules as given here.

# The rules

A deck of cards consists of 52 cards--13 each in four different suits. The four suits are called Spades, Hearts, Diamonds, and Clubs. Each card in a suit has a number or a name. The cards are called, from lowest-ranking to highest-ranking, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `Jack`, `Queen`, `King`, `Ace`.

There are four `Players`, called `North`, `East`, `South` and `West`. One of the players, the `Dealer`, has extra actions ("shuffling" and "dealing") that she may take; otherwise, the dealer is just like the other players. One of the other players may (or may not) be the `Human` user. The dealer is always one of the computer players.

Here's how it goes:

1. The dealer "shuffles" the deck. This randomizes the order of the cards, but there are still 52 different cards--no card ever occurs twice.
2. The dealer "deals" the cards; that is, she gives 13 cards to each player, including herself. The 13 cards held by a player are that player's "hand."
3. Whoever gets the 2 of Clubs will "lead" the first trick; that is, play the first card.

Thirteen "tricks" are played. Each trick proceeds as follows:

1. Some player "leads," that is, plays the first card. "Playing a card" means removing it from her hand and letting the other players know which card it is. (In the real game, the card is placed face up on the table.) It is important to notice what the suit of this card is.
• On the first trick, whoever has the Two of Clubs must lead it.
• Whenever a player takes a trick, that player must lead the next trick.
• In the first part of the game, any card, except a Heart, be led. Sooner or later, someone will be unable to follow suit and will discard a Heart (this is called "breaking Hearts") and after that, Hearts may be led. (In rare cases, a Heart may be led if the player who must lead has no cards of any other suit.)
2. Each of the remaining players, in clockwise order, then plays a card.
3. If a player has any cards of the same suit as the first card played, then she must "follow suit" (play a card of the same suit).
• No one is allowed to play a Heart or the Queen of Spades on the first trick, unless they have no other cards (there is a one in 45 billion chance that this will happen).
• If she has no cards of the same suit, she may play any card, including (after the first trick) a Heart, or the Queen of Spades.
4. The player who played the highest-ranking card of the suit that was led takes the trick, that is, she takes and keeps the cards. These cards will be used for scoring.

After doing the above 13 times, all cards have been played and the hand ends. Then the hands are scored.

Hearts count 1 penalty point each, and the Queen of Spades counts as 13 penaltypoints. The goal of the game is to take as few penalty points as possible, that is, try not to take Hearts, and especially try to avoid the Queen of Spades. However, there is one exception: If a player takes all the Hearts and the Queen of Spades, then that player gets no extra points, and all the other players get 26 points each; this is called "shooting the moon", and is a very good thing to do.

The game ends when some player has scored 100 or more penalty points.

# Partial Example (one trick, not the first)

After the cards are shuffled and dealt:

1. North has the lead, and plays the 5 of Diamonds.
2. East (who has no Diamonds) plays the King of Clubs.
3. South plays the Jack of Diamonds.
4. West (who has no Diamonds) plays the 3 of Spades.

Since the first card played was a Diamond, each of the other three players must play a Diamond, if possible. The highest-ranking Diamond played (in this example, the Jack) will take the trick.

This assignment will be much less structured than the previous assignments. I will specify some classes that you should have, and you may have additional classes. It is up to you to figure out what the responsibilities (methods) of each class will be. Those classes are:

• `Hearts(object)` to represent the entire game. Creating a `Hearts` object should start the game.
• `Card(object)` to represent a single playing card,
• `Player(object)` to represent a single player,
• `Dealer(Player)` to represent the particular Player that shuffles and deals the cards, and
• `Human(Player)` to represent the user of the program. If the user decides to just watch, and not play, then there is no reason to create an object of this type.

I will impose one additional requirement. No object should directly examine or alter the variables of any other object; all access should be done by talking to (calling methods of) the other object.

Figure out what the various classes and objects should be, and what their instance variables and their methods should be. There is no single "correct" design; any reasonable design will do. However, we may take off points for really bad design. The better your design, the easier your programming will be.

Use the objects you define to play a game of Hearts. Your computer players must play cards legally, but they do not need to play well. If you want to make the computer players "smart," do it after you hand in the assignment!

Try hard to separate your methods into those that do input/output (and no computation), and those that do computation (and no input/output). Write unit tests for the latter.

Print out everything that happens during the course of a game: Who leads, who plays which cards, who takes each trick, who wins the game, etc.

Before you turn in this assignment, ask a friend or fellow classmate, not your partner, to try out your game. Watch to see if there is anything they don't understand about what to do. If something isn't obvious to them, it probably won't be obvious to the person who grades your program, and that will cost you points.

# Testing and documentation

Unit test all the computational methods. Don't test the methods that do (only) input/output.

All methods should have a comment telling what the method does, even if you think it's obvious. In addition, each method should include the name of the person who wrote it (which may be both of you).

# Glossary

Ace
The highest ranking card in each suit. From lowest to highest, the cards in a suit are: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.
Deck
A set of 52 cards, 13 of each suit.
Playing a card of some other suit than the suit of the card that was led. A discarded card never takes the trick.
Hand
The cards held by a player and not yet played.